Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / URL.php
blob14c714e455d5a94ea3e2e170501a8c4760de1700
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Static methods for URL/hidden inputs generating
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 /**
11 * Static methods for URL/hidden inputs generating
13 * @package PhpMyAdmin
15 class URL
17 /**
18 * Generates text with hidden inputs.
20 * @param string|array $db optional database name
21 * (can also be an array of parameters)
22 * @param string $table optional table name
23 * @param int $indent indenting level
24 * @param string|array $skip do not generate a hidden field for this parameter
25 * (can be an array of strings)
27 * @see URL::getCommon()
29 * @return string string with input fields
31 * @access public
33 public static function getHiddenInputs($db = '', $table = '',
34 $indent = 0, $skip = array()
35 ) {
36 if (is_array($db)) {
37 $params =& $db;
38 $_indent = empty($table) ? $indent : $table;
39 $_skip = empty($indent) ? $skip : $indent;
40 $indent =& $_indent;
41 $skip =& $_skip;
42 } else {
43 $params = array();
44 if (strlen($db) > 0) {
45 $params['db'] = $db;
47 if (strlen($table) > 0) {
48 $params['table'] = $table;
52 if (! empty($GLOBALS['server'])
53 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
54 ) {
55 $params['server'] = $GLOBALS['server'];
57 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
58 $params['lang'] = $GLOBALS['lang'];
60 if (empty($_COOKIE['pma_collation_connection'])
61 && ! empty($GLOBALS['collation_connection'])
62 ) {
63 $params['collation_connection'] = $GLOBALS['collation_connection'];
66 $params['token'] = $_SESSION[' PMA_token '];
68 if (! is_array($skip)) {
69 if (isset($params[$skip])) {
70 unset($params[$skip]);
72 } else {
73 foreach ($skip as $skipping) {
74 if (isset($params[$skipping])) {
75 unset($params[$skipping]);
80 return URL::getHiddenFields($params);
83 /**
84 * create hidden form fields from array with name => value
86 * <code>
87 * $values = array(
88 * 'aaa' => aaa,
89 * 'bbb' => array(
90 * 'bbb_0',
91 * 'bbb_1',
92 * ),
93 * 'ccc' => array(
94 * 'a' => 'ccc_a',
95 * 'b' => 'ccc_b',
96 * ),
97 * );
98 * echo URL::getHiddenFields($values);
100 * // produces:
101 * <input type="hidden" name="aaa" Value="aaa" />
102 * <input type="hidden" name="bbb[0]" Value="bbb_0" />
103 * <input type="hidden" name="bbb[1]" Value="bbb_1" />
104 * <input type="hidden" name="ccc[a]" Value="ccc_a" />
105 * <input type="hidden" name="ccc[b]" Value="ccc_b" />
106 * </code>
108 * @param array $values hidden values
109 * @param string $pre prefix
111 * @return string form fields of type hidden
113 public static function getHiddenFields($values, $pre = '')
115 $fields = '';
117 foreach ($values as $name => $value) {
118 if (! empty($pre)) {
119 $name = $pre . '[' . $name . ']';
122 if (is_array($value)) {
123 $fields .= URL::getHiddenFields($value, $name);
124 } else {
125 // do not generate an ending "\n" because
126 // URL::getHiddenInputs() is sometimes called
127 // from a JS document.write()
128 $fields .= '<input type="hidden" name="' . htmlspecialchars($name)
129 . '" value="' . htmlspecialchars($value) . '" />';
133 return $fields;
137 * Generates text with URL parameters.
139 * <code>
140 * $params['myparam'] = 'myvalue';
141 * $params['db'] = 'mysql';
142 * $params['table'] = 'rights';
143 * // note the missing ?
144 * echo 'script.php' . URL::getCommon($params);
145 * // produces with cookies enabled:
146 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
147 * // with cookies disabled:
148 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql
149 * // &amp;table=rights
151 * // note the missing ?
152 * echo 'script.php' . URL::getCommon();
153 * // produces with cookies enabled:
154 * // script.php
155 * // with cookies disabled:
156 * // script.php?server=1&amp;lang=en
157 * </code>
159 * @param mixed $params optional, Contains an associative array with url params
160 * @param string $divider optional character to use instead of '?'
162 * @return string string with URL parameters
163 * @access public
165 public static function getCommon($params = array(), $divider = '?')
167 return htmlspecialchars(
168 URL::getCommonRaw($params, $divider)
173 * Generates text with URL parameters.
175 * <code>
176 * $params['myparam'] = 'myvalue';
177 * $params['db'] = 'mysql';
178 * $params['table'] = 'rights';
179 * // note the missing ?
180 * echo 'script.php' . URL::getCommon($params);
181 * // produces with cookies enabled:
182 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
183 * // with cookies disabled:
184 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql
185 * // &amp;table=rights
187 * // note the missing ?
188 * echo 'script.php' . URL::getCommon();
189 * // produces with cookies enabled:
190 * // script.php
191 * // with cookies disabled:
192 * // script.php?server=1&amp;lang=en
193 * </code>
195 * @param mixed $params optional, Contains an associative array with url params
196 * @param string $divider optional character to use instead of '?'
198 * @return string string with URL parameters
199 * @access public
201 public static function getCommonRaw($params = array(), $divider = '?')
203 $separator = URL::getArgSeparator();
205 // avoid overwriting when creating navi panel links to servers
206 if (isset($GLOBALS['server'])
207 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
208 && ! isset($params['server'])
209 && ! defined('PMA_SETUP')
211 $params['server'] = $GLOBALS['server'];
214 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
215 $params['lang'] = $GLOBALS['lang'];
217 if (empty($_COOKIE['pma_collation_connection'])
218 && ! empty($GLOBALS['collation_connection'])
220 $params['collation_connection'] = $GLOBALS['collation_connection'];
223 return $divider . http_build_query($params, null, $separator);
227 * Returns url separator
229 * extracted from arg_separator.input as set in php.ini
230 * we do not use arg_separator.output to avoid problems with &amp; and &
232 * @param string $encode whether to encode separator or not,
233 * currently 'none' or 'html'
235 * @return string character used for separating url parts usually ; or &
236 * @access public
238 public static function getArgSeparator($encode = 'none')
240 static $separator = null;
241 static $html_separator = null;
243 if (null === $separator) {
244 // use separators defined by php, but prefer ';'
245 // as recommended by W3C
246 // (see https://www.w3.org/TR/1999/REC-html401-19991224/appendix
247 // /notes.html#h-B.2.2)
248 $arg_separator = ini_get('arg_separator.input');
249 if (mb_strpos($arg_separator, ';') !== false) {
250 $separator = ';';
251 } elseif (strlen($arg_separator) > 0) {
252 $separator = $arg_separator{0};
253 } else {
254 $separator = '&';
256 $html_separator = htmlentities($separator);
259 switch ($encode) {
260 case 'html':
261 return $html_separator;
262 case 'text' :
263 case 'none' :
264 default :
265 return $separator;