global.php 7.7 KB

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