global.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * globals.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This includes code to update < 4.1.0 globals to the newer format
  9. * It also has some session register functions that work across various
  10. * php versions.
  11. *
  12. * $Id$
  13. */
  14. /* convert old-style superglobals to current method
  15. * this is executed if you are running PHP 4.0.x.
  16. * it is run via a require_once directive in validate.php
  17. * and redirect.php. Patch submitted by Ray Black.
  18. */
  19. if ( !check_php_version(4,1) ) {
  20. global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
  21. global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
  22. $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
  23. $_COOKIE =& $HTTP_COOKIE_VARS;
  24. $_ENV =& $HTTP_ENV_VARS;
  25. $_FILES =& $HTTP_POST_FILES;
  26. $_GET =& $HTTP_GET_VARS;
  27. $_POST =& $HTTP_POST_VARS;
  28. $_SERVER =& $HTTP_SERVER_VARS;
  29. $_SESSION =& $HTTP_SESSION_VARS;
  30. }
  31. /* if running with magic_quotes_gpc then strip the slashes
  32. from POST and GET global arrays */
  33. if (get_magic_quotes_gpc()) {
  34. sqstripslashes($_GET);
  35. sqstripslashes($_POST);
  36. }
  37. /* strip any tags added to the url from PHP_SELF.
  38. This fixes hand crafted url XXS expoits for any
  39. page that uses PHP_SELF as the FORM action */
  40. strip_tags($_SERVER['PHP_SELF']);
  41. /* returns true if current php version is at mimimum a.b.c */
  42. function check_php_version ($a = '0', $b = '0', $c = '0')
  43. {
  44. global $SQ_PHP_VERSION;
  45. if(!isset($SQ_PHP_VERSION))
  46. $SQ_PHP_VERSION = str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0');
  47. return $SQ_PHP_VERSION >= ($a.$b.$c);
  48. }
  49. function sqstripslashes(&$array) {
  50. foreach ($array as $index=>$value) {
  51. if (is_array($array[$index])) {
  52. sqstripslashes($array[$index]);
  53. }
  54. else {
  55. $array[$index] = stripslashes($value);
  56. }
  57. }
  58. }
  59. function sqsession_register ($var, $name) {
  60. if ( !check_php_version(4,1) ) {
  61. global $HTTP_SESSION_VARS;
  62. $HTTP_SESSION_VARS[$name] = $var;
  63. }
  64. else {
  65. $_SESSION["$name"] = $var;
  66. }
  67. }
  68. function sqsession_unregister ($name) {
  69. if ( !check_php_version(4,1) ) {
  70. global $HTTP_SESSION_VARS;
  71. unset($HTTP_SESSION_VARS[$name]);
  72. }
  73. else {
  74. unset($_SESSION[$name]);
  75. }
  76. }
  77. function sqsession_is_registered ($name) {
  78. $test_name = &$name;
  79. $result = false;
  80. if ( !check_php_version(4,1) ) {
  81. global $HTTP_SESSION_VARS;
  82. if (isset($HTTP_SESSION_VARS[$test_name])) {
  83. $result = true;
  84. }
  85. }
  86. else {
  87. if (isset($_SESSION[$test_name])) {
  88. $result = true;
  89. }
  90. }
  91. return $result;
  92. }
  93. /**
  94. * Search for the var $name in $_SESSION, $_POST, $_GET
  95. * (in that order) and register it as a global var.
  96. */
  97. function sqextractGlobalVar ($name) {
  98. if ( !check_php_version(4,1) ) {
  99. global $_SESSION, $_GET, $_POST;
  100. }
  101. global $$name;
  102. if( isset($_SESSION[$name]) ) {
  103. $$name = $_SESSION[$name];
  104. }
  105. if( isset($_POST[$name]) ) {
  106. $$name = $_POST[$name];
  107. }
  108. else if ( isset($_GET[$name]) ) {
  109. $$name = $_GET[$name];
  110. }
  111. }
  112. function sqsession_destroy() {
  113. global $base_uri;
  114. /* start session to be able to destroy it later */
  115. session_start();
  116. if ( !check_php_version(4,1) ) {
  117. global $HTTP_SESSION_VARS;
  118. $HTTP_SESSION_VARS = array();
  119. }
  120. else {
  121. $_SESSION = array();
  122. }
  123. /*
  124. * now reset cookies to 5 seconds ago to delete from browser
  125. */
  126. @session_destroy();
  127. $cookie_params = session_get_cookie_params();
  128. setcookie(session_name(), '', time() - 5, $cookie_params['path'],
  129. $cookie_params['domain']);
  130. setcookie('username', '', time() - 5, $base_uri);
  131. setcookie('key', '', time() - 5 , $base_uri);
  132. }
  133. ?>