global.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * globals.php
  4. *
  5. * Copyright (c) 1999-2003 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. require_once(SM_PATH . 'config/config.php');
  15. /* set the name of the session cookie */
  16. if(isset($session_name) && $session_name) {
  17. ini_set('session.name' , $session_name);
  18. } else {
  19. ini_set('session.name' , 'SQMSESSID');
  20. }
  21. /* If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
  22. * Force magic_quotes_runtime off.
  23. * chilts@birdbrained.org - I put it here in the hopes that all SM code includes this.
  24. * If there's a better place, please let me know.
  25. */
  26. ini_set('magic_quotes_runtime','0');
  27. sqsession_is_active();
  28. /* convert old-style superglobals to current method
  29. * this is executed if you are running PHP 4.0.x.
  30. * it is run via a require_once directive in validate.php
  31. * and redirect.php. Patch submitted by Ray Black.
  32. */
  33. if ( !check_php_version(4,1) ) {
  34. global $_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_SERVER, $_SESSION;
  35. global $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_GET_VARS,
  36. $HTTP_POST_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS, $PHP_SELF;
  37. $_COOKIE =& $HTTP_COOKIE_VARS;
  38. $_ENV =& $HTTP_ENV_VARS;
  39. $_FILES =& $HTTP_POST_FILES;
  40. $_GET =& $HTTP_GET_VARS;
  41. $_POST =& $HTTP_POST_VARS;
  42. $_SERVER =& $HTTP_SERVER_VARS;
  43. $_SESSION =& $HTTP_SESSION_VARS;
  44. if (!isset($PHP_SELF) || empty($PHP_SELF)) {
  45. $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
  46. }
  47. }
  48. /* if running with magic_quotes_gpc then strip the slashes
  49. from POST and GET global arrays */
  50. if (get_magic_quotes_gpc()) {
  51. sqstripslashes($_GET);
  52. sqstripslashes($_POST);
  53. }
  54. /* strip any tags added to the url from PHP_SELF.
  55. This fixes hand crafted url XXS expoits for any
  56. page that uses PHP_SELF as the FORM action */
  57. $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
  58. /**
  59. * returns true if current php version is at mimimum a.b.c
  60. *
  61. * Called: check_php_version(4,1)
  62. */
  63. function check_php_version ($a = '0', $b = '0', $c = '0')
  64. {
  65. global $SQ_PHP_VERSION;
  66. if(!isset($SQ_PHP_VERSION))
  67. $SQ_PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
  68. return $SQ_PHP_VERSION >= ($a.$b.$c);
  69. }
  70. /**
  71. * returns true if the current internal SM version is at minimum a.b.c
  72. * These are plain integer comparisons, as our internal version is
  73. * constructed by us, as an array of 3 ints.
  74. *
  75. * Called: check_sm_version(1,3,3)
  76. */
  77. function check_sm_version($a = 0, $b = 0, $c = 0)
  78. {
  79. global $SQM_INTERNAL_VERSION;
  80. if ( !isset($SQM_INTERNAL_VERSION) ||
  81. $SQM_INTERNAL_VERSION[0] < $a ||
  82. $SQM_INTERNAL_VERSION[1] < $b ||
  83. ( $SQM_INTERNAL_VERSION[1] == $b &&
  84. $SQM_INTERNAL_VERSION[2] < $c ) ) {
  85. return FALSE;
  86. }
  87. return TRUE;
  88. }
  89. /* recursively strip slashes from the values of an array */
  90. function sqstripslashes(&$array) {
  91. if(count($array) > 0) {
  92. foreach ($array as $index=>$value) {
  93. if (is_array($array[$index])) {
  94. sqstripslashes($array[$index]);
  95. }
  96. else {
  97. $array[$index] = stripslashes($value);
  98. }
  99. }
  100. }
  101. }
  102. function sqsession_register ($var, $name) {
  103. sqsession_is_active();
  104. if ( !check_php_version(4,1) ) {
  105. global $HTTP_SESSION_VARS;
  106. $HTTP_SESSION_VARS[$name] = $var;
  107. }
  108. else {
  109. $_SESSION["$name"] = $var;
  110. }
  111. session_register("$name");
  112. }
  113. function sqsession_unregister ($name) {
  114. sqsession_is_active();
  115. if ( !check_php_version(4,1) ) {
  116. global $HTTP_SESSION_VARS;
  117. unset($HTTP_SESSION_VARS[$name]);
  118. }
  119. else {
  120. unset($_SESSION[$name]);
  121. }
  122. session_unregister("$name");
  123. }
  124. function sqsession_is_registered ($name) {
  125. $test_name = &$name;
  126. $result = false;
  127. if ( !check_php_version(4,1) ) {
  128. global $HTTP_SESSION_VARS;
  129. if (isset($HTTP_SESSION_VARS[$test_name])) {
  130. $result = true;
  131. }
  132. }
  133. else {
  134. if (isset($_SESSION[$test_name])) {
  135. $result = true;
  136. }
  137. }
  138. return $result;
  139. }
  140. define('SQ_INORDER',0);
  141. define('SQ_GET',1);
  142. define('SQ_POST',2);
  143. define('SQ_SESSION',3);
  144. define('SQ_COOKIE',4);
  145. define('SQ_SERVER',5);
  146. define('SQ_FORM',6);
  147. /**
  148. * Search for the var $name in $_SESSION, $_POST, $_GET,
  149. * $_COOKIE, or $_SERVER and set it in provided var.
  150. *
  151. * If $search is not provided, or == SQ_INORDER, it will search
  152. * $_SESSION, then $_POST, then $_GET. Otherwise,
  153. * use one of the defined constants to look for
  154. * a var in one place specifically.
  155. *
  156. * Note: $search is an int value equal to one of the
  157. * constants defined above.
  158. *
  159. * example:
  160. * sqgetGlobalVar('username',$username,SQ_SESSION);
  161. * -- no quotes around last param!
  162. *
  163. * Returns FALSE if variable is not found.
  164. * Returns TRUE if it is.
  165. */
  166. function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
  167. if ( !check_php_version(4,1) ) {
  168. global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS,
  169. $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
  170. $_COOKIE =& $HTTP_COOKIE_VARS;
  171. $_GET =& $HTTP_GET_VARS;
  172. $_POST =& $HTTP_POST_VARS;
  173. $_SERVER =& $HTTP_SERVER_VARS;
  174. $_SESSION =& $HTTP_SESSION_VARS;
  175. }
  176. /* NOTE: DO NOT enclose the constants in the switch
  177. statement with quotes. They are constant values,
  178. enclosing them in quotes will cause them to evaluate
  179. as strings. */
  180. switch ($search) {
  181. /* we want the default case to be first here,
  182. so that if a valid value isn't specified,
  183. all three arrays will be searched. */
  184. default:
  185. case SQ_INORDER: // check session, post, get
  186. case SQ_SESSION:
  187. if( isset($_SESSION[$name]) ) {
  188. $value = $_SESSION[$name];
  189. return TRUE;
  190. } elseif ( $search == SQ_SESSION ) {
  191. break;
  192. }
  193. case SQ_FORM: // check post, get
  194. case SQ_POST:
  195. if( isset($_POST[$name]) ) {
  196. $value = $_POST[$name];
  197. return TRUE;
  198. } elseif ( $search == SQ_POST ) {
  199. break;
  200. }
  201. case SQ_GET:
  202. if ( isset($_GET[$name]) ) {
  203. $value = $_GET[$name];
  204. return TRUE;
  205. }
  206. /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  207. break;
  208. case SQ_COOKIE:
  209. if ( isset($_COOKIE[$name]) ) {
  210. $value = $_COOKIE[$name];
  211. return TRUE;
  212. }
  213. break;
  214. case SQ_SERVER:
  215. if ( isset($_SERVER[$name]) ) {
  216. $value = $_SERVER[$name];
  217. return TRUE;
  218. }
  219. break;
  220. }
  221. return FALSE;
  222. }
  223. function sqsession_destroy() {
  224. /*
  225. * php.net says we can kill the cookie by setting just the name:
  226. * http://www.php.net/manual/en/function.setcookie.php
  227. * maybe this will help fix the session merging again.
  228. *
  229. * Changed the theory on this to kill the cookies first starting
  230. * a new session will provide a new session for all instances of
  231. * the browser, we don't want that, as that is what is causing the
  232. * merging of sessions.
  233. */
  234. global $base_uri;
  235. if (isset($_COOKIE[session_name()])) setcookie(session_name(), '', time() - 5, $base_uri);
  236. if (isset($_COOKIE['username'])) setcookie('username','',time() - 5,$base_uri);
  237. if (isset($_COOKIE['key'])) setcookie('key','',time() - 5,$base_uri);
  238. $sessid = session_id();
  239. if (!empty( $sessid )) {
  240. if ( !check_php_version(4,1) ) {
  241. global $HTTP_SESSION_VARS;
  242. $HTTP_SESSION_VARS = array();
  243. } else {
  244. $_SESSION = array();
  245. }
  246. @session_destroy();
  247. }
  248. }
  249. /*
  250. * Function to verify a session has been started. If it hasn't
  251. * start a session up. php.net doesn't tell you that $_SESSION
  252. * (even though autoglobal), is not created unless a session is
  253. * started, unlike $_POST, $_GET and such
  254. */
  255. function sqsession_is_active() {
  256. $sessid = session_id();
  257. if ( empty( $sessid ) ) {
  258. session_start();
  259. }
  260. }
  261. ?>