Bug: Live query chart always zero
[phpmyadmin/tyronm.git] / libraries / url_generating.lib.php
blob388412520a000c08e2a5a0545f9d440c765b1c67
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * URL/hidden inputs generating.
6 * @package phpMyAdmin
7 */
9 /**
10 * Generates text with hidden inputs.
12 * @param string $db optional database name
13 * (can also be an array of parameters)
14 * @param string $table optional table name
15 * @param int $indent indenting level
16 * @param string $skip do not generate a hidden field for this parameter
17 * (can be an array of strings)
19 * @see PMA_generate_common_url()
21 * @return string string with input fields
23 * @global string the current language
24 * @global string the current conversion charset
25 * @global string the current connection collation
26 * @global string the current server
27 * @global array the configuration array
28 * @global boolean whether recoding is allowed or not
30 * @access public
32 function PMA_generate_common_hidden_inputs($db = '', $table = '', $indent = 0, $skip = array())
34 if (is_array($db)) {
35 $params =& $db;
36 $_indent = empty($table) ? $indent : $table;
37 $_skip = empty($indent) ? $skip : $indent;
38 $indent =& $_indent;
39 $skip =& $_skip;
40 } else {
41 $params = array();
42 if (strlen($db)) {
43 $params['db'] = $db;
45 if (strlen($table)) {
46 $params['table'] = $table;
50 if (! empty($GLOBALS['server'])
51 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
52 ) {
53 $params['server'] = $GLOBALS['server'];
55 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
56 $params['lang'] = $GLOBALS['lang'];
58 if (empty($_COOKIE['pma_collation_connection'])
59 && ! empty($GLOBALS['collation_connection'])
60 ) {
61 $params['collation_connection'] = $GLOBALS['collation_connection'];
64 $params['token'] = $_SESSION[' PMA_token '];
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 PMA_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 PMA_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 function PMA_getHiddenFields($values, $pre = '')
113 $fields = '';
115 foreach ($values as $name => $value) {
116 if (! empty($pre)) {
117 $name = $pre. '[' . $name . ']';
120 if (is_array($value)) {
121 $fields .= PMA_getHiddenFields($value, $name);
122 } else {
123 // do not generate an ending "\n" because
124 // PMA_generate_common_hidden_inputs() is sometimes called
125 // from a JS document.write()
126 $fields .= '<input type="hidden" name="' . htmlspecialchars($name)
127 . '" value="' . htmlspecialchars($value) . '" />';
131 return $fields;
135 * Generates text with URL parameters.
137 * <code>
138 * // OLD derepecated style
139 * // note the ?
140 * echo 'script.php?' . PMA_generate_common_url('mysql', 'rights');
141 * // produces with cookies enabled:
142 * // script.php?db=mysql&amp;table=rights
143 * // with cookies disabled:
144 * // script.php?server=1&amp;lang=en&amp;db=mysql&amp;table=rights
146 * // NEW style
147 * $params['myparam'] = 'myvalue';
148 * $params['db'] = 'mysql';
149 * $params['table'] = 'rights';
150 * // note the missing ?
151 * echo 'script.php' . PMA_generate_common_url($params);
152 * // produces with cookies enabled:
153 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
154 * // with cookies disabled:
155 * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql&amp;table=rights
157 * // note the missing ?
158 * echo 'script.php' . PMA_generate_common_url();
159 * // produces with cookies enabled:
160 * // script.php
161 * // with cookies disabled:
162 * // script.php?server=1&amp;lang=en
163 * </code>
165 * @param mixed assoc. array with url params or optional string with database name
166 * if first param is an array there is also an ? prefixed to the url
168 * @param string - if first param is array: 'html' to use htmlspecialchars()
169 * on the resulting URL (for a normal URL displayed in HTML)
170 * or something else to avoid using htmlspecialchars() (for
171 * a URL sent via a header); if not set,'html' is assumed
172 * - if first param is not array: optional table name
174 * @param string - if first param is array: optional character to
175 * use instead of '?'
176 * - if first param is not array: optional character to use
177 * instead of '&amp;' for dividing URL parameters
179 * @return string string with URL parameters
180 * @access public
182 function PMA_generate_common_url()
184 $args = func_get_args();
186 if (isset($args[0]) && is_array($args[0])) {
187 // new style
188 $params = $args[0];
190 if (isset($args[1])) {
191 $encode = $args[1];
192 } else {
193 $encode = 'html';
196 if (isset($args[2])) {
197 $questionmark = $args[2];
198 } else {
199 $questionmark = '?';
201 } else {
202 // old style
204 if (PMA_isValid($args[0])) {
205 $params['db'] = $args[0];
208 if (PMA_isValid($args[1])) {
209 $params['table'] = $args[1];
212 if (isset($args[2]) && $args[2] !== '&amp;') {
213 $encode = 'text';
214 } else {
215 $encode = 'html';
218 $questionmark = '';
221 $separator = PMA_get_arg_separator();
223 if (isset($GLOBALS['server'])
224 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
225 // avoid overwriting when creating navi panel links to servers
226 && ! isset($params['server'])
228 $params['server'] = $GLOBALS['server'];
231 if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
232 $params['lang'] = $GLOBALS['lang'];
234 if (empty($_COOKIE['pma_collation_connection'])
235 && ! empty($GLOBALS['collation_connection'])
237 $params['collation_connection'] = $GLOBALS['collation_connection'];
240 if (isset($_SESSION[' PMA_token '])) {
241 $params['token'] = $_SESSION[' PMA_token '];
244 if (empty($params)) {
245 return '';
248 $query = $questionmark . http_build_query($params, null, $separator);
250 if ($encode === 'html') {
251 $query = htmlspecialchars($query);
254 return $query;
258 * Returns url separator
260 * extracted from arg_separator.input as set in php.ini
261 * we do not use arg_separator.output to avoid problems with &amp; and &
263 * @param string $encode whether to encode separator or not,
264 * currently 'none' or 'html'
266 * @return string character used for separating url parts usally ; or &
267 * @access public
269 function PMA_get_arg_separator($encode = 'none')
271 static $separator = null;
273 if (null === $separator) {
274 // use seperators defined by php, but prefer ';'
275 // as recommended by W3C
276 $php_arg_separator_input = ini_get('arg_separator.input');
277 if (strpos($php_arg_separator_input, ';') !== false) {
278 $separator = ';';
279 } elseif (strlen($php_arg_separator_input) > 0) {
280 $separator = $php_arg_separator_input{0};
281 } else {
282 $separator = '&';
286 switch ($encode) {
287 case 'html':
288 return htmlentities($separator);
289 break;
290 case 'text' :
291 case 'none' :
292 default :
293 return $separator;