improved doc for function
[phpmyadmin/crack.git] / libraries / url_generating.lib.php
blob26e7dfa08c053e4c42ec26f02c1ab783bd2ffb48
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * URL/hidden inputs generating.
6 * @version $Id$
7 */
9 /**
10 * Generates text with hidden inputs.
12 * @see PMA_generate_common_url()
13 * @uses PMA_getHiddenFields
14 * @param string optional database name
15 * (can also be an array of parameters)
16 * @param string optional table name
17 * @param int indenting level
18 * @param string do not generate a hidden field for this parameter
19 * (can be an array of strings)
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 * @author nijel
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 $fields .= '<input type="hidden" name="' . htmlspecialchars($name)
128 . '" value="' . htmlspecialchars($value) . '" />' . "\n";
132 return $fields;
136 * Generates text with URL parameters.
138 * <code>
139 * // OLD derepecated style
140 * // note the ?
141 * echo 'script.php?' . PMA_generate_common_url('mysql', 'rights');
142 * // produces with cookies enabled:
143 * // script.php?db=mysql&amp;table=rights
144 * // with cookies disabled:
145 * // script.php?server=1&amp;lang=en-utf-8&amp;db=mysql&amp;table=rights
147 * // NEW style
148 * $params['myparam'] = 'myvalue';
149 * $params['db'] = 'mysql';
150 * $params['table'] = 'rights';
151 * // note the missing ?
152 * echo 'script.php' . PMA_generate_common_url($params);
153 * // produces with cookies enabled:
154 * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
155 * // with cookies disabled:
156 * // script.php?server=1&amp;lang=en-utf-8&amp;myparam=myvalue&amp;db=mysql&amp;table=rights
158 * // note the missing ?
159 * echo 'script.php' . PMA_generate_common_url();
160 * // produces with cookies enabled:
161 * // script.php
162 * // with cookies disabled:
163 * // script.php?server=1&amp;lang=en-utf-8
164 * </code>
166 * @uses $GLOBALS['server']
167 * @uses $GLOBALS['cfg']['ServerDefault']
168 * @uses $_COOKIE['pma_lang']
169 * @uses $GLOBALS['lang']
170 * @uses $_COOKIE['pma_charset']
171 * @uses $GLOBALS['convcharset']
172 * @uses $_COOKIE['pma_collation_connection']
173 * @uses $GLOBALS['collation_connection']
174 * @uses $_SESSION[' PMA_token ']
175 * @uses PMA_get_arg_separator()
176 * @uses is_array()
177 * @uses strlen()
178 * @uses htmlentities()
179 * @uses urlencode()
180 * @uses implode()
181 * @param mixed assoc. array with url params or optional string with database name
182 * if first param is an array there is also an ? prefixed to the url
183 * @param string optional table name only if first param is array
184 * @param string character to use instead of '&amp;' for deviding
185 * multiple URL parameters from each other
186 * @return string string with URL parameters
187 * @access public
188 * @author nijel
190 function PMA_generate_common_url()
192 $args = func_get_args();
194 if (isset($args[0]) && is_array($args[0])) {
195 // new style
196 $params = $args[0];
198 if (isset($args[1])) {
199 $encode = $args[1];
200 } else {
201 $encode = 'html';
204 if (isset($args[2])) {
205 $questionmark = $args[2];
206 } else {
207 $questionmark = '?';
209 } else {
210 // old style
212 if (PMA_isValid($args[0])) {
213 $params['db'] = $args[0];
216 if (PMA_isValid($args[1])) {
217 $params['table'] = $args[1];
220 if (isset($args[2]) && $args[2] !== '&amp;') {
221 $encode = 'text';
222 } else {
223 $encode = 'html';
226 $questionmark = '';
229 $separator = PMA_get_arg_separator();
231 if (isset($GLOBALS['server'])
232 && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']) {
233 $params['server'] = $GLOBALS['server'];
236 if (empty($_COOKIE['pma_lang'])
237 && ! empty($GLOBALS['lang'])) {
238 $params['lang'] = $GLOBALS['lang'];
240 if (empty($_COOKIE['pma_charset'])
241 && ! empty($GLOBALS['convcharset'])) {
242 $params['convcharset'] = $GLOBALS['convcharset'];
244 if (empty($_COOKIE['pma_collation_connection'])
245 && ! empty($GLOBALS['collation_connection'])) {
246 $params['collation_connection'] = $GLOBALS['collation_connection'];
249 if (isset($_SESSION[' PMA_token '])) {
250 $params['token'] = $_SESSION[' PMA_token '];
253 if (empty($params)) {
254 return '';
257 $query = $questionmark . http_build_query($params, null, $separator);
259 if ($encode === 'html') {
260 $query = htmlspecialchars($query);
263 return $query;
267 * Returns url separator
269 * extracted from arg_separator.input as set in php.ini
270 * we do not use arg_separator.output to avoid problems with &amp; and &
272 * @uses ini_get()
273 * @uses strpos()
274 * @uses strlen()
275 * @param string whether to encode separator or not, currently 'none' or 'html'
276 * @return string character used for separating url parts usally ; or &
277 * @access public
278 * @author nijel
280 function PMA_get_arg_separator($encode = 'none')
282 static $separator = null;
284 if (null === $separator) {
285 // use seperators defined by php, but prefer ';'
286 // as recommended by W3C
287 $php_arg_separator_input = ini_get('arg_separator.input');
288 if (strpos($php_arg_separator_input, ';') !== false) {
289 $separator = ';';
290 } elseif (strlen($php_arg_separator_input) > 0) {
291 $separator = $php_arg_separator_input{0};
292 } else {
293 $separator = '&';
297 switch ($encode) {
298 case 'html':
299 return htmlentities($separator);
300 break;
301 case 'text' :
302 case 'none' :
303 default :
304 return $separator;