3 * Static methods for URL/hidden inputs generating
6 declare(strict_types
=1);
10 use function htmlentities
;
11 use function htmlspecialchars
;
12 use function http_build_query
;
14 use function is_array
;
15 use function mb_strpos
;
19 * Static methods for URL/hidden inputs generating
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
39 public static function getHiddenInputs(
51 if (strlen((string) $db) > 0) {
55 if (strlen((string) $table) > 0) {
56 $params['table'] = $table;
61 ! empty($GLOBALS['server'])
62 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
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]);
76 foreach ($skip as $skipping) {
77 if (! isset($params[$skipping])) {
81 unset($params[$skipping]);
85 return self
::getHiddenFields($params);
89 * create hidden form fields from array with name => value
103 * echo Url::getHiddenFields($values);
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">
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)
123 /* Always include token in plain forms */
124 if ($is_token === false) {
125 $values['token'] = $_SESSION[' PMA_token '];
128 foreach ($values as $name => $value) {
130 $name = $pre . '[' . $name . ']';
133 if (is_array($value)) {
134 $fields .= self
::getHiddenFields($value, $name, true);
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) . '">';
148 * Generates text with URL parameters.
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
162 * // note the missing ?
163 * echo 'script.php' . Url::getCommon();
164 * // produces with cookies enabled:
166 * // with cookies disabled:
167 * // script.php?server=1&lang=en
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
177 public static function getCommon(array $params = [], $divider = '?')
179 return self
::getCommonRaw($params, $divider);
183 * Generates text with URL parameters.
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
197 * // note the missing ?
198 * echo 'script.php' . Url::getCommon();
199 * // produces with cookies enabled:
201 * // with cookies disabled:
202 * // script.php?server=1&lang=en
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
212 public static function getCommonRaw(array $params = [], $divider = '?')
216 $separator = self
::getArgSeparator();
218 // avoid overwriting when creating navigation panel links to servers
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;
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 &
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) {
269 } elseif (strlen($arg_separator) > 0) {
270 $separator = $arg_separator[0];
275 $html_separator = htmlentities($separator);
280 return $html_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, '&');