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		'disc_prototypes.php' => [
36			'remove' => ['itemid'],
37			'add' => ['hostid', 'parent_discoveryid']
38		],
39		'graphs.php' => [
40			'remove' => ['graphid'],
41			'add' => ['hostid', 'parent_discoveryid']
42		],
43		'host_discovery.php' => [
44			'remove' => ['itemid'],
45			'add' => ['hostid']
46		],
47		'host_prototypes.php' => [
48			'remove' => ['hostid'],
49			'add' => ['parent_discoveryid']
50		],
51		'hostgroups.php' => [
52			'remove' => ['groupid']
53		],
54		'hosts.php' => [
55			'remove' => ['hostid']
56		],
57		'httpconf.php' => [
58			'remove' => ['httptestid']
59		],
60		'items.php' => [
61			'remove' => ['itemid']
62		],
63		'maintenance.php' => [
64			'remove' => ['maintenanceid']
65		],
66		'sysmaps.php' => [
67			'remove' => ['sysmapid']
68		],
69		'templates.php' => [
70			'remove' => ['templateid']
71		],
72		'trigger_prototypes.php' => [
73			'remove' =>  ['triggerid'],
74			'add' => ['parent_discoveryid', 'hostid']
75		],
76		'triggers.php' => [
77			'remove' => ['triggerid'],
78			'add' => ['hostid']
79		],
80		'__default' => [
81			'remove' => ['cancel', 'form', 'delete']
82		]
83	];
84
85	/**
86	 * Creates new CUrl object based on giver URL (or $_REQUEST if null is given),
87	 * and adds/removes parameters based on current page context.
88	 *
89	 * @param string $sourceUrl
90	 *
91	 * @return CUrl
92	 */
93	public static function getContextUrl($sourceUrl = null) {
94		$config = self::resolveConfig();
95
96		$url = new CUrl($sourceUrl);
97
98		if (isset($config['remove'])) {
99			foreach ($config['remove'] as $key) {
100				$url->removeArgument($key);
101			}
102		}
103
104		if (isset($config['add'])) {
105			foreach ($config['add'] as $key) {
106				$url->setArgument($key, getRequest($key));
107			}
108		}
109
110		return $url;
111	}
112
113	/**
114	 * Resolves context configuration for current file (based on $page['file'] global variable)
115	 *
116	 * @return array
117	 */
118	protected static function resolveConfig() {
119		global $page;
120
121		if (isset($page['file']) && isset(self::$contextConfigs[$page['file']])) {
122			return array_merge_recursive(self::$contextConfigs['__default'], self::$contextConfigs[$page['file']]);
123		}
124
125		return self::$contextConfigs['__default'];
126	}
127}
128