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
74	public function formatArguments($query = null) {
75		if ($query === null) {
76			$query = $this->query;
77		}
78		if ($query !== null) {
79			$args = explode('&', $query);
80			foreach ($args as $id => $arg) {
81				if (empty($arg)) {
82					continue;
83				}
84
85				if (strpos($arg, '=') !== false) {
86					list($name, $value) = explode('=', $arg);
87					$this->arguments[urldecode($name)] = urldecode($value);
88				}
89				else {
90					$this->arguments[$arg] = '';
91				}
92			}
93		}
94		$this->formatQuery();
95	}
96	/**
97	 * Return relative url.
98	 *
99	 * @return string
100	 */
101	public function getUrl() {
102		$this->formatQuery();
103
104		$url = $this->url;
105		$url .= $this->query ? '?'.$this->query : '';
106		$url .= $this->reference ? '#'.urlencode($this->reference) : '';
107
108		return $url;
109	}
110
111	public function removeArgument($key) {
112		unset($this->arguments[$key]);
113
114		return $this;
115	}
116
117	public function setArgument($key, $value = '') {
118		$this->arguments[$key] = $value;
119
120		return $this;
121	}
122
123	public function toString() {
124		return $this->getUrl();
125	}
126}
127