Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / classes / Url.php
blob0ae483e11fb37105ffb99e064a6aaa84068233fc
1 <?php
2 /**
3 * Static methods for URL/hidden inputs generating
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin;
10 use function htmlentities;
11 use function htmlspecialchars;
12 use function http_build_query;
13 use function ini_get;
14 use function is_array;
15 use function mb_strpos;
16 use function strlen;
18 /**
19 * Static methods for URL/hidden inputs generating
21 class Url
23 /**
24 * Generates text with hidden inputs.
26 * @see Url::getCommon()
28 * @param string|array $db optional database name
29 * (can also be an array of parameters)
30 * @param string $table optional table name
31 * @param int $indent indenting level
32 * @param string|array $skip do not generate a hidden field for this parameter
33 * (can be an array of strings)
35 * @return string string with input fields
37 * @access public
39 public static function getHiddenInputs(
40 $db = '',
41 $table = '',
42 $indent = 0,
43 $skip = []
44 ) {
45 global $config;
47 if (is_array($db)) {
48 $params =& $db;
49 } else {
50 $params = [];
51 if (strlen((string) $db) > 0) {
52 $params['db'] = $db;
55 if (strlen((string) $table) > 0) {
56 $params['table'] = $table;
60 if (
61 ! empty($GLOBALS['server'])
62 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
63 ) {
64 $params['server'] = $GLOBALS['server'];
67 if (empty($config->getCookie('pma_lang')) && ! empty($GLOBALS['lang'])) {
68 $params['lang'] = $GLOBALS['lang'];
71 if (! is_array($skip)) {
72 if (isset($params[$skip])) {
73 unset($params[$skip]);
75 } else {
76 foreach ($skip as $skipping) {
77 if (! isset($params[$skipping])) {
78 continue;
81 unset($params[$skipping]);
85 return self::getHiddenFields($params);
88 /**
89 * create hidden form fields from array with name => value
91 * <code>
92 * $values = array(
93 * 'aaa' => aaa,
94 * 'bbb' => array(
95 * 'bbb_0',
96 * 'bbb_1',
97 * ),
98 * 'ccc' => array(
99 * 'a' => 'ccc_a',
100 * 'b' => 'ccc_b',
101 * ),
102 * );
103 * echo Url::getHiddenFields($values);
105 * // produces:
106 * <input type="hidden" name="aaa" Value="aaa">
107 * <input type="hidden" name="bbb[0]" Value="bbb_0">
108 * <input type="hidden" name="bbb[1]" Value="bbb_1">
109 * <input type="hidden" name="ccc[a]" Value="ccc_a">
110 * <input type="hidden" name="ccc[b]" Value="ccc_b">
111 * </code>
113 * @param array $values hidden values
114 * @param string $pre prefix
115 * @param bool $is_token if token already added in hidden input field
117 * @return string form fields of type hidden
119 public static function getHiddenFields(array $values, $pre = '', $is_token = false)
121 $fields = '';
123 /* Always include token in plain forms */
124 if ($is_token === false) {
125 $values['token'] = $_SESSION[' PMA_token '];
128 foreach ($values as $name => $value) {
129 if (! empty($pre)) {
130 $name = $pre . '[' . $name . ']';
133 if (is_array($value)) {
134 $fields .= self::getHiddenFields($value, $name, true);
135 } else {
136 // do not generate an ending "\n" because
137 // Url::getHiddenInputs() is sometimes called
138 // from a JS document.write()
139 $fields .= '<input type="hidden" name="' . htmlspecialchars((string) $name)
140 . '" value="' . htmlspecialchars((string) $value) . '">';
144 return $fields;
148 * Generates text with URL parameters.
150 * <code>
151 * $params['myparam'] = 'myvalue';
152 * $params['db'] = 'mysql';
153 * $params['table'] = 'rights';
154 * // note the missing ?
155 * echo 'script.php' . Url::getCommon($params);
156 * // produces with cookies enabled:
157 * // script.php?myparam=myvalue&db=mysql&table=rights
158 * // with cookies disabled:
159 * // script.php?server=1&lang=en&myparam=myvalue&db=mysql
160 * // &table=rights
162 * // note the missing ?
163 * echo 'script.php' . Url::getCommon();
164 * // produces with cookies enabled:
165 * // script.php
166 * // with cookies disabled:
167 * // script.php?server=1&lang=en
168 * </code>
170 * @param array<string,int|string|bool> $params optional, Contains an associative array with url params
171 * @param string $divider optional character to use instead of '?'
173 * @return string string with URL parameters
175 * @access public
177 public static function getCommon(array $params = [], $divider = '?')
179 return self::getCommonRaw($params, $divider);
183 * Generates text with URL parameters.
185 * <code>
186 * $params['myparam'] = 'myvalue';
187 * $params['db'] = 'mysql';
188 * $params['table'] = 'rights';
189 * // note the missing ?
190 * echo 'script.php' . Url::getCommon($params);
191 * // produces with cookies enabled:
192 * // script.php?myparam=myvalue&db=mysql&table=rights
193 * // with cookies disabled:
194 * // script.php?server=1&lang=en&myparam=myvalue&db=mysql
195 * // &table=rights
197 * // note the missing ?
198 * echo 'script.php' . Url::getCommon();
199 * // produces with cookies enabled:
200 * // script.php
201 * // with cookies disabled:
202 * // script.php?server=1&lang=en
203 * </code>
205 * @param array<string|int,int|string|bool> $params optional, Contains an associative array with url params
206 * @param string $divider optional character to use instead of '?'
208 * @return string string with URL parameters
210 * @access public
212 public static function getCommonRaw(array $params = [], $divider = '?')
214 global $config;
216 $separator = self::getArgSeparator();
218 // avoid overwriting when creating navigation panel links to servers
219 if (
220 isset($GLOBALS['server'])
221 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
222 && ! isset($params['server'])
223 && ! $config->get('is_setup')
225 $params['server'] = $GLOBALS['server'];
228 // Can be null when the user is missing an extension.
229 // See: Core::checkExtensions()
230 if ($config !== null && empty($config->getCookie('pma_lang')) && ! empty($GLOBALS['lang'])) {
231 $params['lang'] = $GLOBALS['lang'];
234 $query = http_build_query($params, '', $separator);
236 if (($divider !== '?' && $divider !== '&') || strlen($query) > 0) {
237 return $divider . $query;
240 return '';
244 * Returns url separator
246 * extracted from arg_separator.input as set in php.ini
247 * we do not use arg_separator.output to avoid problems with & and &
249 * @param string $encode whether to encode separator or not,
250 * currently 'none' or 'html'
252 * @return string character used for separating url parts usually ; or &
254 * @access public
256 public static function getArgSeparator($encode = 'none')
258 static $separator = null;
259 static $html_separator = null;
261 if ($separator === null) {
262 // use separators defined by php, but prefer ';'
263 // as recommended by W3C
264 // (see https://www.w3.org/TR/1999/REC-html401-19991224/appendix
265 // /notes.html#h-B.2.2)
266 $arg_separator = (string) ini_get('arg_separator.input');
267 if (mb_strpos($arg_separator, ';') !== false) {
268 $separator = ';';
269 } elseif (strlen($arg_separator) > 0) {
270 $separator = $arg_separator[0];
271 } else {
272 $separator = '&';
275 $html_separator = htmlentities($separator);
278 switch ($encode) {
279 case 'html':
280 return $html_separator;
282 case 'text':
283 case 'none':
284 default:
285 return $separator;
290 * @param string $route Route to use
291 * @param array $additionalParameters Additional URL parameters
293 public static function getFromRoute(string $route, array $additionalParameters = []): string
295 return 'index.php?route=' . $route . self::getCommon($additionalParameters, '&');