1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22class CUrl { 23 24 private $url; 25 protected $reference; 26 protected $query; 27 protected $arguments = []; 28 29 /** 30 * WARNING: the class doesn't support parsing query strings with multi-dimentional arrays. 31 * 32 * @param string|null $url 33 */ 34 public function __construct($url = null) { 35 if (empty($url)) { 36 $this->formatGetArguments(); 37 38 $this->url = basename($_SERVER['SCRIPT_NAME']); 39 } 40 else { 41 $this->url = $url; 42 43 // parse reference 44 $pos = strpos($url, '#'); 45 if ($pos !== false) { 46 $this->reference = substr($url, $pos + 1); 47 $this->url = substr($url, 0, $pos); 48 } 49 50 // parse query 51 $pos = strpos($url, '?'); 52 if ($pos !== false) { 53 $this->query = substr($url, $pos + 1); 54 $this->url = substr($url, 0, $pos); 55 } 56 57 $this->formatArguments(); 58 } 59 } 60 61 /** 62 * Creates a HTTP query string from the arguments set in self::$arguments and saves it in self::$query. 63 */ 64 public function formatQuery() { 65 $this->query = http_build_query($this->arguments); 66 } 67 68 public function formatGetArguments() { 69 $this->arguments = $_GET; 70 71 $this->formatQuery(); 72 73 return $this; 74 } 75 76 public function formatArguments($query = null) { 77 if ($query === null) { 78 $query = $this->query; 79 } 80 if ($query !== null) { 81 $args = explode('&', $query); 82 foreach ($args as $id => $arg) { 83 if (empty($arg)) { 84 continue; 85 } 86 87 if (strpos($arg, '=') !== false) { 88 list($name, $value) = explode('=', $arg); 89 $this->arguments[urldecode($name)] = urldecode($value); 90 } 91 else { 92 $this->arguments[$arg] = ''; 93 } 94 } 95 } 96 $this->formatQuery(); 97 } 98 99 /** 100 * Return relative url. 101 * 102 * @return string 103 */ 104 public function getUrl() { 105 $this->formatQuery(); 106 107 $url = $this->url; 108 $url .= $this->query ? '?'.$this->query : ''; 109 $url .= $this->reference ? '#'.urlencode($this->reference) : ''; 110 111 return $url; 112 } 113 114 public function removeArgument($key) { 115 unset($this->arguments[$key]); 116 117 return $this; 118 } 119 120 public function setArgument($key, $value = '') { 121 $this->arguments[$key] = $value; 122 123 return $this; 124 } 125 126 public function setArgumentSID() { 127 $this->arguments['sid'] = substr($_COOKIE[ZBX_SESSION_NAME], 16, 16); 128 129 return $this; 130 } 131 132 public function toString() { 133 return $this->getUrl(); 134 } 135} 136