url_generating.lib.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * URL/hidden inputs generating.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Generates text with hidden inputs.
  13. *
  14. * @param string|array $db optional database name
  15. * (can also be an array of parameters)
  16. * @param string $table optional table name
  17. * @param int $indent indenting level
  18. * @param string|array $skip do not generate a hidden field for this parameter
  19. * (can be an array of strings)
  20. *
  21. * @see PMA_URL_getCommon()
  22. *
  23. * @return string string with input fields
  24. *
  25. * @global string the current language
  26. * @global string the current conversion charset
  27. * @global string the current connection collation
  28. * @global string the current server
  29. * @global array the configuration array
  30. * @global boolean whether recoding is allowed or not
  31. *
  32. * @access public
  33. */
  34. function PMA_URL_getHiddenInputs($db = '', $table = '',
  35. $indent = 0, $skip = array()
  36. ) {
  37. if (is_array($db)) {
  38. $params =& $db;
  39. $_indent = empty($table) ? $indent : $table;
  40. $_skip = empty($indent) ? $skip : $indent;
  41. $indent =& $_indent;
  42. $skip =& $_skip;
  43. } else {
  44. $params = array();
  45. if (strlen($db)) {
  46. $params['db'] = $db;
  47. }
  48. if (strlen($table)) {
  49. $params['table'] = $table;
  50. }
  51. }
  52. if (! empty($GLOBALS['server'])
  53. && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
  54. ) {
  55. $params['server'] = $GLOBALS['server'];
  56. }
  57. if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
  58. $params['lang'] = $GLOBALS['lang'];
  59. }
  60. if (empty($_COOKIE['pma_collation_connection'])
  61. && ! empty($GLOBALS['collation_connection'])
  62. ) {
  63. $params['collation_connection'] = $GLOBALS['collation_connection'];
  64. }
  65. $params['token'] = $_SESSION[' PMA_token '];
  66. if (! is_array($skip)) {
  67. if (isset($params[$skip])) {
  68. unset($params[$skip]);
  69. }
  70. } else {
  71. foreach ($skip as $skipping) {
  72. if (isset($params[$skipping])) {
  73. unset($params[$skipping]);
  74. }
  75. }
  76. }
  77. return PMA_getHiddenFields($params);
  78. }
  79. /**
  80. * create hidden form fields from array with name => value
  81. *
  82. * <code>
  83. * $values = array(
  84. * 'aaa' => aaa,
  85. * 'bbb' => array(
  86. * 'bbb_0',
  87. * 'bbb_1',
  88. * ),
  89. * 'ccc' => array(
  90. * 'a' => 'ccc_a',
  91. * 'b' => 'ccc_b',
  92. * ),
  93. * );
  94. * echo PMA_getHiddenFields($values);
  95. *
  96. * // produces:
  97. * <input type="hidden" name="aaa" Value="aaa" />
  98. * <input type="hidden" name="bbb[0]" Value="bbb_0" />
  99. * <input type="hidden" name="bbb[1]" Value="bbb_1" />
  100. * <input type="hidden" name="ccc[a]" Value="ccc_a" />
  101. * <input type="hidden" name="ccc[b]" Value="ccc_b" />
  102. * </code>
  103. *
  104. * @param array $values hidden values
  105. * @param string $pre prefix
  106. *
  107. * @return string form fields of type hidden
  108. */
  109. function PMA_getHiddenFields($values, $pre = '')
  110. {
  111. $fields = '';
  112. foreach ($values as $name => $value) {
  113. if (! empty($pre)) {
  114. $name = $pre . '[' . $name . ']';
  115. }
  116. if (is_array($value)) {
  117. $fields .= PMA_getHiddenFields($value, $name);
  118. } else {
  119. // do not generate an ending "\n" because
  120. // PMA_URL_getHiddenInputs() is sometimes called
  121. // from a JS document.write()
  122. $fields .= '<input type="hidden" name="' . htmlspecialchars($name)
  123. . '" value="' . htmlspecialchars($value) . '" />';
  124. }
  125. }
  126. return $fields;
  127. }
  128. /**
  129. * Generates text with URL parameters.
  130. *
  131. * <code>
  132. * // OLD (deprecated) style
  133. * // note the ?
  134. * echo 'script.php?' . PMA_URL_getCommon('mysql', 'rights');
  135. * // produces with cookies enabled:
  136. * // script.php?db=mysql&amp;table=rights
  137. * // with cookies disabled:
  138. * // script.php?server=1&amp;lang=en&amp;db=mysql&amp;table=rights
  139. *
  140. * // NEW style
  141. * $params['myparam'] = 'myvalue';
  142. * $params['db'] = 'mysql';
  143. * $params['table'] = 'rights';
  144. * // note the missing ?
  145. * echo 'script.php' . PMA_URL_getCommon($params);
  146. * // produces with cookies enabled:
  147. * // script.php?myparam=myvalue&amp;db=mysql&amp;table=rights
  148. * // with cookies disabled:
  149. * // script.php?server=1&amp;lang=en&amp;myparam=myvalue&amp;db=mysql
  150. * // &amp;table=rights
  151. *
  152. * // note the missing ?
  153. * echo 'script.php' . PMA_URL_getCommon();
  154. * // produces with cookies enabled:
  155. * // script.php
  156. * // with cookies disabled:
  157. * // script.php?server=1&amp;lang=en
  158. * </code>
  159. *
  160. * @param mixed assoc. array with url params or optional string with database name
  161. * if first param is an array there is also an ? prefixed to the url
  162. *
  163. * @param string - if first param is array: 'html' to use htmlspecialchars()
  164. * on the resulting URL (for a normal URL displayed in HTML)
  165. * or something else to avoid using htmlspecialchars() (for
  166. * a URL sent via a header); if not set,'html' is assumed
  167. * - if first param is not array: optional table name
  168. *
  169. * @param string - if first param is array: optional character to
  170. * use instead of '?'
  171. * - if first param is not array: optional character to use
  172. * instead of '&amp;' for dividing URL parameters
  173. *
  174. * @return string string with URL parameters
  175. * @access public
  176. */
  177. function PMA_URL_getCommon()
  178. {
  179. $args = func_get_args();
  180. if (isset($args[0]) && is_array($args[0])) {
  181. // new style
  182. $params = $args[0];
  183. if (isset($args[1])) {
  184. $encode = $args[1];
  185. } else {
  186. $encode = 'html';
  187. }
  188. if (isset($args[2])) {
  189. $questionmark = $args[2];
  190. } else {
  191. $questionmark = '?';
  192. }
  193. } else {
  194. // old style
  195. $params = array();
  196. if (PMA_isValid($args[0])) {
  197. $params['db'] = $args[0];
  198. }
  199. if (PMA_isValid($args[1])) {
  200. $params['table'] = $args[1];
  201. }
  202. if (isset($args[2]) && $args[2] !== '&amp;') {
  203. $encode = 'text';
  204. } else {
  205. $encode = 'html';
  206. }
  207. $questionmark = '';
  208. }
  209. $separator = PMA_URL_getArgSeparator();
  210. // avoid overwriting when creating navi panel links to servers
  211. if (isset($GLOBALS['server'])
  212. && $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
  213. && ! isset($params['server'])
  214. ) {
  215. $params['server'] = $GLOBALS['server'];
  216. }
  217. if (empty($_COOKIE['pma_lang']) && ! empty($GLOBALS['lang'])) {
  218. $params['lang'] = $GLOBALS['lang'];
  219. }
  220. if (empty($_COOKIE['pma_collation_connection'])
  221. && ! empty($GLOBALS['collation_connection'])
  222. ) {
  223. $params['collation_connection'] = $GLOBALS['collation_connection'];
  224. }
  225. if (isset($_SESSION[' PMA_token '])) {
  226. $params['token'] = $_SESSION[' PMA_token '];
  227. }
  228. if (empty($params)) {
  229. return '';
  230. }
  231. $query = $questionmark . http_build_query($params, null, $separator);
  232. if ($encode === 'html') {
  233. $query = htmlspecialchars($query);
  234. }
  235. return $query;
  236. }
  237. /**
  238. * Returns url separator
  239. *
  240. * extracted from arg_separator.input as set in php.ini
  241. * we do not use arg_separator.output to avoid problems with &amp; and &
  242. *
  243. * @param string $encode whether to encode separator or not,
  244. * currently 'none' or 'html'
  245. *
  246. * @return string character used for separating url parts usually ; or &
  247. * @access public
  248. */
  249. function PMA_URL_getArgSeparator($encode = 'none')
  250. {
  251. static $separator = null;
  252. static $html_separator = null;
  253. if (null === $separator) {
  254. // use separators defined by php, but prefer ';'
  255. // as recommended by W3C
  256. // (see http://www.w3.org/TR/1999/REC-html401-19991224/appendix
  257. // /notes.html#h-B.2.2)
  258. $arg_separator = ini_get('arg_separator.input');
  259. if (strpos($arg_separator, ';') !== false) {
  260. $separator = ';';
  261. } elseif (strlen($arg_separator) > 0) {
  262. $separator = $arg_separator{0};
  263. } else {
  264. $separator = '&';
  265. }
  266. $html_separator = htmlentities($separator);
  267. }
  268. switch ($encode) {
  269. case 'html':
  270. return $html_separator;
  271. break;
  272. case 'text' :
  273. case 'none' :
  274. default :
  275. return $separator;
  276. }
  277. }
  278. ?>