Url.php 8.3 KB

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