Translated using Weblate (Ukrainian)
[phpmyadmin.git] / libraries / URL.php
blob2d21d6821d423d7e8fe860839f0451e5e0f7d143
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 if (! is_array($skip)) {
67 if (isset($params[$skip])) {
68 unset($params[$skip]);
70 } else {
71 foreach ($skip as $skipping) {
72 if (isset($params[$skipping])) {
73 unset($params[$skipping]);
78 return URL::getHiddenFields($params);
81 /**
82 * create hidden form fields from array with name => value
84 * <code>
85 * $values = array(
86 * 'aaa' => aaa,
87 * 'bbb' => array(
88 * 'bbb_0',
89 * 'bbb_1',
90 * ),
91 * 'ccc' => array(
92 * 'a' => 'ccc_a',
93 * 'b' => 'ccc_b',
94 * ),
95 * );
96 * echo URL::getHiddenFields($values);
98 * // produces:
99 * <input type="hidden" name="aaa" Value="aaa" />
100 * <input type="hidden" name="bbb[0]" Value="bbb_0" />
101 * <input type="hidden" name="bbb[1]" Value="bbb_1" />
102 * <input type="hidden" name="ccc[a]" Value="ccc_a" />
103 * <input type="hidden" name="ccc[b]" Value="ccc_b" />
104 * </code>
106 * @param array $values hidden values
107 * @param string $pre prefix
109 * @return string form fields of type hidden
111 public static function getHiddenFields($values, $pre = '')
113 $fields = '';
115 /* Always include token in plain forms */
116 if ($pre === '') {
117 $values['token'] = $_SESSION[' PMA_token '];
120 foreach ($values as $name => $value) {
121 if (! empty($pre)) {
122 $name = $pre . '[' . $name . ']';
125 if (is_array($value)) {
126 $fields .= URL::getHiddenFields($value, $name);
127 } else {
128 // do not generate an ending "\n" because
129 // URL::getHiddenInputs() is sometimes called
130 // from a JS document.write()
131 $fields .= '<input type="hidden" name="' . htmlspecialchars($name)
132 . '" value="' . htmlspecialchars($value) . '" />';
136 return $fields;
140 * Generates text with URL parameters.
142 * <code>
143 * $params['myparam'] = 'myvalue';
144 * $params['db'] = 'mysql';
145 * $params['table'] = 'rights';
146 * // note the missing ?
147 * echo 'script.php' . URL::getCommon($params);
148 * // produces with cookies enabled:
149 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
150 * // with cookies disabled:
151 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql
152 * // &amp;table=rights
154 * // note the missing ?
155 * echo 'script.php' . URL::getCommon();
156 * // produces with cookies enabled:
157 * // script.php
158 * // with cookies disabled:
159 * // script.php?server=1&amp;lang=en
160 * </code>
162 * @param mixed $params optional, Contains an associative array with url params
163 * @param string $divider optional character to use instead of '?'
165 * @return string string with URL parameters
166 * @access public
168 public static function getCommon($params = array(), $divider = '?')
170 return htmlspecialchars(
171 URL::getCommonRaw($params, $divider)
176 * Generates text with URL parameters.
178 * <code>
179 * $params['myparam'] = 'myvalue';
180 * $params['db'] = 'mysql';
181 * $params['table'] = 'rights';
182 * // note the missing ?
183 * echo 'script.php' . URL::getCommon($params);
184 * // produces with cookies enabled:
185 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
186 * // with cookies disabled:
187 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql
188 * // &amp;table=rights
190 * // note the missing ?
191 * echo 'script.php' . URL::getCommon();
192 * // produces with cookies enabled:
193 * // script.php
194 * // with cookies disabled:
195 * // script.php?server=1&amp;lang=en
196 * </code>
198 * @param mixed $params optional, Contains an associative array with url params
199 * @param string $divider optional character to use instead of '?'
201 * @return string string with URL parameters
202 * @access public
204 public static function getCommonRaw($params = array(), $divider = '?')
206 $separator = URL::getArgSeparator();
208 // avoid overwriting when creating navi panel links to servers
209 if (isset($GLOBALS['server'])
210 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
211 && ! isset($params['server'])
212 && ! defined('PMA_SETUP')
214 $params['server'] = $GLOBALS['server'];
217 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
218 $params['lang'] = $GLOBALS['lang'];
220 if (empty($_COOKIE['pma_collation_connection'])
221 && ! empty($GLOBALS['collation_connection'])
223 $params['collation_connection'] = $GLOBALS['collation_connection'];
226 if (isset($_SESSION[' PMA_token '])) {
227 $params['token'] = $_SESSION[' PMA_token '];
230 $query = http_build_query($params, null, $separator);
232 if ($divider != '?' || strlen($query) > 0) {
233 return $divider . $query;
236 return '';
240 * Returns url separator
242 * extracted from arg_separator.input as set in php.ini
243 * we do not use arg_separator.output to avoid problems with &amp; and &
245 * @param string $encode whether to encode separator or not,
246 * currently 'none' or 'html'
248 * @return string character used for separating url parts usually ; or &
249 * @access public
251 public static function getArgSeparator($encode = 'none')
253 static $separator = null;
254 static $html_separator = null;
256 if (null === $separator) {
257 // use separators defined by php, but prefer ';'
258 // as recommended by W3C
259 // (see https://www.w3.org/TR/1999/REC-html401-19991224/appendix
260 // /notes.html#h-B.2.2)
261 $arg_separator = ini_get('arg_separator.input');
262 if (mb_strpos($arg_separator, ';') !== false) {
263 $separator = ';';
264 } elseif (strlen($arg_separator) > 0) {
265 $separator = $arg_separator{0};
266 } else {
267 $separator = '&';
269 $html_separator = htmlentities($separator);
272 switch ($encode) {
273 case 'html':
274 return $html_separator;
275 case 'text' :
276 case 'none' :
277 default :
278 return $separator;