Merge branch 'QA_3_3'
[phpmyadmin-themes.git] / libraries / url_generating.lib.php
blobfddeb7e18d2e7a263dae00eaa09bb66225873a75
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * URL/hidden inputs generating.
6 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * Generates text with hidden inputs.
13 * @see PMA_generate_common_url()
14 * @uses PMA_getHiddenFields
15 * @param string optional database name
16 * (can also be an array of parameters)
17 * @param string optional table name
18 * @param int indenting level
19 * @param string do not generate a hidden field for this parameter
20 * (can be an array of strings)
22 * @return string string with input fields
24 * @global string the current language
25 * @global string the current conversion charset
26 * @global string the current connection collation
27 * @global string the current server
28 * @global array the configuration array
29 * @global boolean whether recoding is allowed or not
31 * @access public
34 function PMA_generate_common_hidden_inputs($db = '', $table = '', $indent = 0, $skip = array())
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)) {
45 $params['db'] = $db;
47 if (strlen($table)) {
48 $params['table'] = $table;
52 if (! empty($GLOBALS['server'])
53 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']) {
54 $params['server'] = $GLOBALS['server'];
56 if (empty($_COOKIE['pma_lang'])
57 && ! empty($GLOBALS['lang'])) {
58 $params['lang'] = $GLOBALS['lang'];
60 if (empty($_COOKIE['pma_charset'])
61 && ! empty($GLOBALS['convcharset'])) {
62 $params['convcharset'] = $GLOBALS['convcharset'];
64 if (empty($_COOKIE['pma_collation_connection'])
65 && ! empty($GLOBALS['collation_connection'])) {
66 $params['collation_connection'] = $GLOBALS['collation_connection'];
69 $params['token'] = $_SESSION[' PMA_token '];
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 unset($params[$skipping]);
83 return PMA_getHiddenFields($params);
86 /**
87 * create hidden form fields from array with name => value
89 * <code>
90 * $values = array(
91 * 'aaa' => aaa,
92 * 'bbb' => array(
93 * 'bbb_0',
94 * 'bbb_1',
95 * ),
96 * 'ccc' => array(
97 * 'a' => 'ccc_a',
98 * 'b' => 'ccc_b',
99 * ),
100 * );
101 * echo PMA_getHiddenFields($values);
103 * // produces:
104 * <input type="hidden" name="aaa" Value="aaa" />
105 * <input type="hidden" name="bbb[0]" Value="bbb_0" />
106 * <input type="hidden" name="bbb[1]" Value="bbb_1" />
107 * <input type="hidden" name="ccc[a]" Value="ccc_a" />
108 * <input type="hidden" name="ccc[b]" Value="ccc_b" />
109 * </code>
111 * @param array $values
112 * @param string $pre
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_generate_common_hidden_inputs() 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 * // OLD derepecated style
143 * // note the ?
144 * echo 'script.php?' . PMA_generate_common_url('mysql', 'rights');
145 * // produces with cookies enabled:
146 * // script.php?db=mysql&amp;table=rights
147 * // with cookies disabled:
148 * // script.php?server=1&amp;lang=en&amp;db=mysql&amp;table=rights
150 * // NEW style
151 * $params['myparam'] = 'myvalue';
152 * $params['db'] = 'mysql';
153 * $params['table'] = 'rights';
154 * // note the missing ?
155 * echo 'script.php' . PMA_generate_common_url($params);
156 * // produces with cookies enabled:
157 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
158 * // with cookies disabled:
159 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql&amp;table=rights
161 * // note the missing ?
162 * echo 'script.php' . PMA_generate_common_url();
163 * // produces with cookies enabled:
164 * // script.php
165 * // with cookies disabled:
166 * // script.php?server=1&amp;lang=en
167 * </code>
169 * @uses $GLOBALS['server']
170 * @uses $GLOBALS['cfg']['ServerDefault']
171 * @uses $_COOKIE['pma_lang']
172 * @uses $GLOBALS['lang']
173 * @uses $_COOKIE['pma_charset']
174 * @uses $GLOBALS['convcharset']
175 * @uses $_COOKIE['pma_collation_connection']
176 * @uses $GLOBALS['collation_connection']
177 * @uses $_SESSION[' PMA_token ']
178 * @uses PMA_get_arg_separator()
179 * @uses is_array()
180 * @uses strlen()
181 * @uses htmlentities()
182 * @uses urlencode()
183 * @uses implode()
184 * @param mixed assoc. array with url params or optional string with database name
185 * if first param is an array there is also an ? prefixed to the url
187 * @param string - if first param is array: 'html' to use htmlspecialchars()
188 * on the resulting URL (for a normal URL displayed in HTML)
189 * or something else to avoid using htmlspecialchars() (for
190 * a URL sent via a header); if not set,'html' is assumed
191 * - if first param is not array: optional table name
193 * @param string - if first param is array: optional character to
194 * use instead of '?'
195 * - if first param is not array: optional character to use
196 * instead of '&amp;' for dividing URL parameters
197 * @return string string with URL parameters
198 * @access public
200 function PMA_generate_common_url()
202 $args = func_get_args();
204 if (isset($args[0]) && is_array($args[0])) {
205 // new style
206 $params = $args[0];
208 if (isset($args[1])) {
209 $encode = $args[1];
210 } else {
211 $encode = 'html';
214 if (isset($args[2])) {
215 $questionmark = $args[2];
216 } else {
217 $questionmark = '?';
219 } else {
220 // old style
222 if (PMA_isValid($args[0])) {
223 $params['db'] = $args[0];
226 if (PMA_isValid($args[1])) {
227 $params['table'] = $args[1];
230 if (isset($args[2]) && $args[2] !== '&amp;') {
231 $encode = 'text';
232 } else {
233 $encode = 'html';
236 $questionmark = '';
239 $separator = PMA_get_arg_separator();
241 if (isset($GLOBALS['server'])
242 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
243 // avoid overwriting when creating navi panel links to servers
244 && ! isset($params['server'])) {
245 $params['server'] = $GLOBALS['server'];
248 if (empty($_COOKIE['pma_lang'])
249 && ! empty($GLOBALS['lang'])) {
250 $params['lang'] = $GLOBALS['lang'];
252 if (empty($_COOKIE['pma_charset'])
253 && ! empty($GLOBALS['convcharset'])) {
254 $params['convcharset'] = $GLOBALS['convcharset'];
256 if (empty($_COOKIE['pma_collation_connection'])
257 && ! empty($GLOBALS['collation_connection'])) {
258 $params['collation_connection'] = $GLOBALS['collation_connection'];
261 if (isset($_SESSION[' PMA_token '])) {
262 $params['token'] = $_SESSION[' PMA_token '];
265 if (empty($params)) {
266 return '';
269 $query = $questionmark . http_build_query($params, null, $separator);
271 if ($encode === 'html') {
272 $query = htmlspecialchars($query);
275 return $query;
279 * Returns url separator
281 * extracted from arg_separator.input as set in php.ini
282 * we do not use arg_separator.output to avoid problems with &amp; and &
284 * @uses ini_get()
285 * @uses strpos()
286 * @uses strlen()
287 * @param string whether to encode separator or not, currently 'none' or 'html'
288 * @return string character used for separating url parts usally ; or &
289 * @access public
291 function PMA_get_arg_separator($encode = 'none')
293 static $separator = null;
295 if (null === $separator) {
296 // use seperators defined by php, but prefer ';'
297 // as recommended by W3C
298 $php_arg_separator_input = ini_get('arg_separator.input');
299 if (strpos($php_arg_separator_input, ';') !== false) {
300 $separator = ';';
301 } elseif (strlen($php_arg_separator_input) > 0) {
302 $separator = $php_arg_separator_input{0};
303 } else {
304 $separator = '&';
308 switch ($encode) {
309 case 'html':
310 return htmlentities($separator);
311 break;
312 case 'text' :
313 case 'none' :
314 default :
315 return $separator;