global.php 9.4 KB

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