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
21class CUrlFactory {
22
23	/**
24	 * Configuration of context configurations. Top-level key is file name from $page['file'], below goes:
25	 * 'remove' - to remove arguments
26	 * 'add' - name of $_REQUEST keys to be kept as arguments
27	 * 'callable' - callable to apply to argument list
28	 *
29	 * @var array
30	 */
31	protected static $contextConfigs = [
32		'actionconf.php' => [
33			'remove' => ['actionid']
34		],
35		'applications.php' => [
36			'remove' => ['applicationid']
37		],
38		'disc_prototypes.php' => [
39			'remove' => ['itemid'],
40			'add' => ['hostid', 'parent_discoveryid']
41		],
42		'discoveryconf.php' => [
43			'remove' => ['druleid']
44		],
45		'graphs.php' => [
46			'remove' => ['graphid'],
47			'add' => ['hostid', 'parent_discoveryid']
48		],
49		'host_discovery.php' => [
50			'remove' => ['itemid'],
51			'add' => ['hostid']
52		],
53		'host_prototypes.php' => [
54			'remove' => ['hostid'],
55			'add' => ['parent_discoveryid']
56		],
57		'hostgroups.php' => [
58			'remove' => ['groupid']
59		],
60		'hosts.php' => [
61			'remove' => ['hostid']
62		],
63		'httpconf.php' => [
64			'remove' => ['httptestid']
65		],
66		'items.php' => [
67			'remove' => ['itemid']
68		],
69		'maintenance.php' => [
70			'remove' => ['maintenanceid']
71		],
72		'screenconf.php' => [
73			'remove' => ['screenid'],
74			'add' => ['templateid']
75		],
76		'slideconf.php' => [
77			'remove' => ['slideshowid']
78		],
79		'sysmaps.php' => [
80			'remove' => ['sysmapid']
81		],
82		'templates.php' => [
83			'remove' => ['templateid']
84		],
85		'trigger_prototypes.php' => [
86			'remove' =>  ['triggerid'],
87			'add' => ['parent_discoveryid', 'hostid']
88		],
89		'triggers.php' => [
90			'remove' => ['triggerid'],
91			'add' => ['hostid']
92		],
93		'usergrps.php' => [
94			'remove' => ['usrgrpid']
95		],
96		'users.php' => [
97			'remove' => ['userid']
98		],
99		'__default' => [
100			'remove' => ['cancel', 'form', 'delete']
101		]
102	];
103
104	/**
105	 * Creates new CUrl object based on giver URL (or $_REQUEST if null is given),
106	 * and adds/removes parameters based on current page context.
107	 *
108	 * @param string $sourceUrl
109	 *
110	 * @return CUrl
111	 */
112	public static function getContextUrl($sourceUrl = null) {
113		$config = self::resolveConfig();
114
115		$url = new CUrl($sourceUrl);
116
117		if (isset($config['remove'])) {
118			foreach ($config['remove'] as $key) {
119				$url->removeArgument($key);
120			}
121		}
122
123		if (isset($config['add'])) {
124			foreach ($config['add'] as $key) {
125				$url->setArgument($key, getRequest($key));
126			}
127		}
128
129		return $url;
130	}
131
132	/**
133	 * Resolves context configuration for current file (based on $page['file'] global variable)
134	 *
135	 * @return array
136	 */
137	protected static function resolveConfig() {
138		global $page;
139
140		if (isset($page['file']) && isset(self::$contextConfigs[$page['file']])) {
141			return array_merge_recursive(self::$contextConfigs['__default'], self::$contextConfigs[$page['file']]);
142		}
143
144		return self::$contextConfigs['__default'];
145	}
146}
147