global.php 7.8 KB

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