PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/sitemanager/phpmyadmin/libraries/grab_globals.lib.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 118 lines | 73 code | 13 blank | 32 comment | 15 complexity | 8b936beaf281f4718afdf5213c22e941 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * This library grabs the names and values of the variables sent or posted to a
  5. * script in $_GET, $_POST and $_FILES superglobals and sets simple globals
  6. * variables from them. It does the same work for $HTTP_ACCEPT_LANGUAGE and
  7. * $HTTP_AUTHORIZATION.
  8. *
  9. * @package PhpMyAdmin
  10. */
  11. if (! defined('PHPMYADMIN')) {
  12. exit;
  13. }
  14. /**
  15. * copy values from one array to another, usually from a superglobal into $GLOBALS
  16. *
  17. * @param array $array values from
  18. * @param array &$target values to
  19. * @param bool $sanitize prevent importing key names in $_import_blacklist
  20. * @return bool
  21. */
  22. function PMA_recursive_extract($array, &$target, $sanitize = true)
  23. {
  24. if (! is_array($array)) {
  25. return false;
  26. }
  27. if ($sanitize) {
  28. $valid_variables = preg_replace($GLOBALS['_import_blacklist'], '',
  29. array_keys($array));
  30. $valid_variables = array_unique($valid_variables);
  31. } else {
  32. $valid_variables = array_keys($array);
  33. }
  34. foreach ($valid_variables as $key) {
  35. if (strlen($key) === 0) {
  36. continue;
  37. }
  38. if (is_array($array[$key])) {
  39. // there could be a variable coming from a cookie of
  40. // another application, with the same name as this array
  41. unset($target[$key]);
  42. PMA_recursive_extract($array[$key], $target[$key], false);
  43. } else {
  44. $target[$key] = $array[$key];
  45. }
  46. }
  47. return true;
  48. }
  49. /**
  50. * @var array $_import_blacklist variable names that should NEVER be imported
  51. * from superglobals
  52. */
  53. $_import_blacklist = array(
  54. '/^cfg$/i', // PMA configuration
  55. '/^server$/i', // selected server
  56. '/^db$/i', // page to display
  57. '/^table$/i', // page to display
  58. '/^goto$/i', // page to display
  59. '/^back$/i', // the page go back
  60. '/^lang$/i', // selected language
  61. '/^collation_connection$/i', //
  62. '/^set_theme$/i', //
  63. '/^sql_query$/i', // the query to be executed
  64. '/^GLOBALS$/i', // the global scope
  65. '/^str.*$/i', // PMA localized strings
  66. '/^error_handler.*$/i', // the error handler
  67. '/^_.*$/i', // PMA does not use variables starting with _ from extern
  68. '/^.*\s+.*$/i', // no whitespaces anywhere
  69. '/^[0-9]+.*$/i', // numeric variable names
  70. //'/^PMA_.*$/i', // other PMA variables
  71. );
  72. if (! empty($_GET)) {
  73. PMA_recursive_extract($_GET, $GLOBALS);
  74. }
  75. if (! empty($_POST)) {
  76. PMA_recursive_extract($_POST, $GLOBALS);
  77. }
  78. if (! empty($_FILES)) {
  79. $_valid_variables = preg_replace($GLOBALS['_import_blacklist'], '', array_keys($_FILES));
  80. foreach ($_valid_variables as $name) {
  81. if (strlen($name) != 0) {
  82. $$name = $_FILES[$name]['tmp_name'];
  83. ${$name . '_name'} = $_FILES[$name]['name'];
  84. }
  85. }
  86. unset($name, $value);
  87. }
  88. /**
  89. * globalize some environment variables
  90. */
  91. $server_vars = array('HTTP_ACCEPT_LANGUAGE', 'HTTP_AUTHORIZATION');
  92. foreach ($server_vars as $current) {
  93. // it's not important HOW we detect html tags
  94. // it's more important to prevent XSS
  95. // so it's not important if we result in an invalid string,
  96. // it's even better than a XSS capable string
  97. if (PMA_getenv($current) && false === strpos(PMA_getenv($current), '<')) {
  98. $$current = PMA_getenv($current);
  99. // already imported by register_globals?
  100. } elseif (! isset($$current) || false !== strpos($$current, '<')) {
  101. $$current = '';
  102. }
  103. }
  104. unset($server_vars, $current, $_import_blacklist);
  105. ?>