global.php 8.3 KB

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