Url.php 8.0 KB

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