UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / url_generating.lib.php
blobbc078ad9f12bf765691f46384c872fd082640771
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * URL/hidden inputs generating.
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Generates text with hidden inputs.
15 * @param string|array $db optional database name
16 * (can also be an array of parameters)
17 * @param string $table optional table name
18 * @param int $indent indenting level
19 * @param string|array $skip do not generate a hidden field for this parameter
20 * (can be an array of strings)
22 * @see PMA_URL_getCommon()
24 * @return string string with input fields
26 * @global string the current language
27 * @global string the current conversion charset
28 * @global string the current connection collation
29 * @global string the current server
30 * @global array the configuration array
31 * @global boolean whether recoding is allowed or not
33 * @access public
35 function PMA_URL_getHiddenInputs($db = '', $table = '',
36 $indent = 0, $skip = array()
37 ) {
38 if (is_array($db)) {
39 $params =& $db;
40 $_indent = empty($table) ? $indent : $table;
41 $_skip = empty($indent) ? $skip : $indent;
42 $indent =& $_indent;
43 $skip =& $_skip;
44 } else {
45 $params = array();
46 if (/*overload*/mb_strlen($db)) {
47 $params['db'] = $db;
49 if (/*overload*/mb_strlen($table)) {
50 $params['table'] = $table;
54 if (! empty($GLOBALS['server'])
55 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
56 ) {
57 $params['server'] = $GLOBALS['server'];
59 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
60 $params['lang'] = $GLOBALS['lang'];
62 if (empty($_COOKIE['pma_collation_connection'])
63 && ! empty($GLOBALS['collation_connection'])
64 ) {
65 $params['collation_connection'] = $GLOBALS['collation_connection'];
68 $params['token'] = $_SESSION[' PMA_token '];
70 if (! is_array($skip)) {
71 if (isset($params[$skip])) {
72 unset($params[$skip]);
74 } else {
75 foreach ($skip as $skipping) {
76 if (isset($params[$skipping])) {
77 unset($params[$skipping]);
82 return PMA_getHiddenFields($params);
85 /**
86 * create hidden form fields from array with name => value
88 * <code>
89 * $values = array(
90 * 'aaa' => aaa,
91 * 'bbb' => array(
92 * 'bbb_0',
93 * 'bbb_1',
94 * ),
95 * 'ccc' => array(
96 * 'a' => 'ccc_a',
97 * 'b' => 'ccc_b',
98 * ),
99 * );
100 * echo PMA_getHiddenFields($values);
102 * // produces:
103 * <input type="hidden" name="aaa" Value="aaa" />
104 * <input type="hidden" name="bbb[0]" Value="bbb_0" />
105 * <input type="hidden" name="bbb[1]" Value="bbb_1" />
106 * <input type="hidden" name="ccc[a]" Value="ccc_a" />
107 * <input type="hidden" name="ccc[b]" Value="ccc_b" />
108 * </code>
110 * @param array $values hidden values
111 * @param string $pre prefix
113 * @return string form fields of type hidden
115 function PMA_getHiddenFields($values, $pre = '')
117 $fields = '';
119 foreach ($values as $name => $value) {
120 if (! empty($pre)) {
121 $name = $pre . '[' . $name . ']';
124 if (is_array($value)) {
125 $fields .= PMA_getHiddenFields($value, $name);
126 } else {
127 // do not generate an ending "\n" because
128 // PMA_URL_getHiddenInputs() is sometimes called
129 // from a JS document.write()
130 $fields .= '<input type="hidden" name="' . htmlspecialchars($name)
131 . '" value="' . htmlspecialchars($value) . '" />';
135 return $fields;
139 * Generates text with URL parameters.
141 * <code>
142 * $params['myparam'] = 'myvalue';
143 * $params['db'] = 'mysql';
144 * $params['table'] = 'rights';
145 * // note the missing ?
146 * echo 'script.php' . PMA_URL_getCommon($params);
147 * // produces with cookies enabled:
148 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
149 * // with cookies disabled:
150 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql
151 * // &amp;table=rights
153 * // note the missing ?
154 * echo 'script.php' . PMA_URL_getCommon();
155 * // produces with cookies enabled:
156 * // script.php
157 * // with cookies disabled:
158 * // script.php?server=1&amp;lang=en
159 * </code>
161 * @param mixed $params optional, Contains an associative array with url params
163 * @param string $encode 'html' to use htmlspecialchars() on the resulting
164 * URL (for a normal URL displayed in HTML) or
165 * something else to avoid using htmlspecialchars()
166 * (for a URL sent via a header);
167 * if not set,'html' is assumed
169 * @param string $divider optional character to use instead of '?'
171 * @return string string with URL parameters
172 * @access public
174 function PMA_URL_getCommon($params = array(), $encode = 'html', $divider = '?')
176 $separator = PMA_URL_getArgSeparator();
178 // avoid overwriting when creating navi panel links to servers
179 if (isset($GLOBALS['server'])
180 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
181 && ! isset($params['server'])
183 $params['server'] = $GLOBALS['server'];
186 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
187 $params['lang'] = $GLOBALS['lang'];
189 if (empty($_COOKIE['pma_collation_connection'])
190 && ! empty($GLOBALS['collation_connection'])
192 $params['collation_connection'] = $GLOBALS['collation_connection'];
195 if (isset($_SESSION[' PMA_token '])) {
196 $params['token'] = $_SESSION[' PMA_token '];
199 if (empty($params)) {
200 return '';
203 $query = $divider . http_build_query($params, null, $separator);
205 if ($encode === 'html') {
206 $query = htmlspecialchars($query);
209 return $query;
213 * Returns url separator
215 * extracted from arg_separator.input as set in php.ini
216 * we do not use arg_separator.output to avoid problems with &amp; and &
218 * @param string $encode whether to encode separator or not,
219 * currently 'none' or 'html'
221 * @return string character used for separating url parts usually ; or &
222 * @access public
224 function PMA_URL_getArgSeparator($encode = 'none')
226 static $separator = null;
227 static $html_separator = null;
229 if (null === $separator) {
230 // use separators defined by php, but prefer ';'
231 // as recommended by W3C
232 // (see http://www.w3.org/TR/1999/REC-html401-19991224/appendix
233 // /notes.html#h-B.2.2)
234 $arg_separator = ini_get('arg_separator.input');
235 if (/*overload*/mb_strpos($arg_separator, ';') !== false) {
236 $separator = ';';
237 } elseif (/*overload*/mb_strlen($arg_separator) > 0) {
238 $separator = $arg_separator{0};
239 } else {
240 $separator = '&';
242 $html_separator = htmlentities($separator);
245 switch ($encode) {
246 case 'html':
247 return $html_separator;
248 case 'text' :
249 case 'none' :
250 default :
251 return $separator;