global.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * global.php
  4. *
  5. * This includes code to update < 4.1.0 globals to the newer format
  6. * It also has some session register functions that work across various
  7. * php versions.
  8. *
  9. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package squirrelmail
  13. */
  14. /**
  15. */
  16. define('SQ_INORDER',0);
  17. define('SQ_GET',1);
  18. define('SQ_POST',2);
  19. define('SQ_SESSION',3);
  20. define('SQ_COOKIE',4);
  21. define('SQ_SERVER',5);
  22. define('SQ_FORM',6);
  23. /**
  24. * returns true if current php version is at mimimum a.b.c
  25. *
  26. * Called: check_php_version(4,1)
  27. * @param int a major version number
  28. * @param int b minor version number
  29. * @param int c release number
  30. * @return bool
  31. */
  32. function check_php_version ($a = '0', $b = '0', $c = '0')
  33. {
  34. return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' );
  35. }
  36. /**
  37. * returns true if the current internal SM version is at minimum a.b.c
  38. * These are plain integer comparisons, as our internal version is
  39. * constructed by us, as an array of 3 ints.
  40. *
  41. * Called: check_sm_version(1,3,3)
  42. * @param int a major version number
  43. * @param int b minor version number
  44. * @param int c release number
  45. * @return bool
  46. */
  47. function check_sm_version($a = 0, $b = 0, $c = 0)
  48. {
  49. global $SQM_INTERNAL_VERSION;
  50. if ( !isset($SQM_INTERNAL_VERSION) ||
  51. $SQM_INTERNAL_VERSION[0] < $a ||
  52. ( $SQM_INTERNAL_VERSION[0] == $a &&
  53. $SQM_INTERNAL_VERSION[1] < $b) ||
  54. ( $SQM_INTERNAL_VERSION[0] == $a &&
  55. $SQM_INTERNAL_VERSION[1] == $b &&
  56. $SQM_INTERNAL_VERSION[2] < $c ) ) {
  57. return FALSE;
  58. }
  59. return TRUE;
  60. }
  61. /**
  62. * Recursively strip slashes from the values of an array.
  63. * @param array array the array to strip, passed by reference
  64. * @return void
  65. */
  66. function sqstripslashes(&$array) {
  67. if(count($array) > 0) {
  68. foreach ($array as $index=>$value) {
  69. if (is_array($array[$index])) {
  70. sqstripslashes($array[$index]);
  71. }
  72. else {
  73. $array[$index] = stripslashes($value);
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Add a variable to the session.
  80. * @param mixed $var the variable to register
  81. * @param string $name the name to refer to this variable
  82. * @return void
  83. */
  84. function sqsession_register ($var, $name) {
  85. sqsession_is_active();
  86. $_SESSION["$name"] = $var;
  87. session_register("$name");
  88. }
  89. /**
  90. * Delete a variable from the session.
  91. * @param string $name the name of the var to delete
  92. * @return void
  93. */
  94. function sqsession_unregister ($name) {
  95. sqsession_is_active();
  96. unset($_SESSION[$name]);
  97. session_unregister("$name");
  98. }
  99. /**
  100. * Checks to see if a variable has already been registered
  101. * in the session.
  102. * @param string $name the name of the var to check
  103. * @return bool whether the var has been registered
  104. */
  105. function sqsession_is_registered ($name) {
  106. $test_name = &$name;
  107. $result = false;
  108. if (isset($_SESSION[$test_name])) {
  109. $result = true;
  110. }
  111. return $result;
  112. }
  113. /**
  114. * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
  115. * and set it in provided var.
  116. *
  117. * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
  118. * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
  119. * $_GET. Otherwise, use one of the defined constants to look for a var in one
  120. * place specifically.
  121. *
  122. * Note: $search is an int value equal to one of the constants defined above.
  123. *
  124. * Example:
  125. * sqgetGlobalVar('username',$username,SQ_SESSION);
  126. * // No quotes around last param, it's a constant - not a string!
  127. *
  128. * @param string name the name of the var to search
  129. * @param mixed value the variable to return
  130. * @param int search constant defining where to look
  131. * @param int typecast force variable to be cast to given type (please
  132. * use SQ_TYPE_XXX constants or set to FALSE (default)
  133. * to leave variable type unmolested)
  134. * @return bool whether variable is found.
  135. */
  136. function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
  137. $result = false;
  138. switch ($search) {
  139. /* we want the default case to be first here,
  140. so that if a valid value isn't specified,
  141. all three arrays will be searched. */
  142. default:
  143. case SQ_INORDER: // check session, post, get
  144. case SQ_SESSION:
  145. if( isset($_SESSION[$name]) ) {
  146. $value = $_SESSION[$name];
  147. $result = TRUE;
  148. break;
  149. } elseif ( $search == SQ_SESSION ) {
  150. break;
  151. }
  152. case SQ_FORM: // check post, get
  153. case SQ_POST:
  154. if( isset($_POST[$name]) ) {
  155. $value = $_POST[$name];
  156. $result = TRUE;
  157. break;
  158. } elseif ( $search == SQ_POST ) {
  159. break;
  160. }
  161. case SQ_GET:
  162. if ( isset($_GET[$name]) ) {
  163. $value = $_GET[$name];
  164. $result = TRUE;
  165. break;
  166. }
  167. /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  168. break;
  169. case SQ_COOKIE:
  170. if ( isset($_COOKIE[$name]) ) {
  171. $value = $_COOKIE[$name];
  172. $result = TRUE;
  173. break;
  174. }
  175. break;
  176. case SQ_SERVER:
  177. if ( isset($_SERVER[$name]) ) {
  178. $value = $_SERVER[$name];
  179. $result = TRUE;
  180. break;
  181. }
  182. break;
  183. }
  184. if ($result && $typecast) {
  185. switch ($typecast) {
  186. case SQ_TYPE_INT: $value = (int) $value; break;
  187. case SQ_TYPE_STRING: $value = (string) $value; break;
  188. case SQ_TYPE_BOOL: $value = (bool) $value; break;
  189. default: break;
  190. }
  191. } else if (!$result && !is_null($default)) {
  192. $value = $default;
  193. }
  194. return $result;
  195. }
  196. /**
  197. * Deletes an existing session, more advanced than the standard PHP
  198. * session_destroy(), it explicitly deletes the cookies and global vars.
  199. *
  200. * WARNING: Older PHP versions have some issues with session management.
  201. * See http://bugs.php.net/11643 (warning, spammed bug tracker) and
  202. * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
  203. * 4.2.3 and maybe other versions. If you restart session after session
  204. * is destroyed, affected PHP versions produce PHP notice. Bug should
  205. * be fixed only in 4.3.0
  206. */
  207. function sqsession_destroy() {
  208. /*
  209. * php.net says we can kill the cookie by setting just the name:
  210. * http://www.php.net/manual/en/function.setcookie.php
  211. * maybe this will help fix the session merging again.
  212. *
  213. * Changed the theory on this to kill the cookies first starting
  214. * a new session will provide a new session for all instances of
  215. * the browser, we don't want that, as that is what is causing the
  216. * merging of sessions.
  217. */
  218. global $base_uri;
  219. if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
  220. if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
  221. if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
  222. $sessid = session_id();
  223. if (!empty( $sessid )) {
  224. $_SESSION = array();
  225. @session_destroy();
  226. }
  227. }
  228. /**
  229. * Function to verify a session has been started. If it hasn't
  230. * start a session up. php.net doesn't tell you that $_SESSION
  231. * (even though autoglobal), is not created unless a session is
  232. * started, unlike $_POST, $_GET and such
  233. */
  234. function sqsession_is_active() {
  235. $sessid = session_id();
  236. if ( empty( $sessid ) ) {
  237. sqsession_start();
  238. }
  239. }
  240. /**
  241. * Function to start the session and store the cookie with the session_id as
  242. * HttpOnly cookie which means that the cookie isn't accessible by javascript
  243. * (IE6 only)
  244. */
  245. function sqsession_start() {
  246. global $base_uri;
  247. session_start();
  248. $session_id = session_id();
  249. // session_starts sets the sessionid cookie buth without the httponly var
  250. // setting the cookie again sets the httponly cookie attribute
  251. // disable, @see sqsetcookie and php 5.1.2
  252. // sqsetcookie(session_name(),session_id(),false,$base_uri);
  253. }
  254. /**
  255. * Set a cookie
  256. * @param string $sName The name of the cookie.
  257. * @param string $sValue The value of the cookie.
  258. * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
  259. * @param string $sPath The path on the server in which the cookie will be available on.
  260. * @param string $sDomain The domain that the cookie is available.
  261. * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  262. * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
  263. * @return void
  264. */
  265. function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) {
  266. static $sCookieCache;
  267. if (!isset($sCache)) {
  268. $sCache = '';
  269. }
  270. /**
  271. * We have to send all cookies with one header call otherwise we loose cookies.
  272. * In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true.
  273. * If that happens we send the cookie header.
  274. */
  275. if ($bFlush) {
  276. // header($sCookieCache);
  277. return;
  278. }
  279. if (!$sName) return;
  280. // php 5.1.2 and 4.4.2 do not allow to send multiple headers at once.
  281. // Because that's the only way to get this thing working we fallback to
  282. // setcookie until we solved this
  283. if ($iExpire===false) $iExpire = 0;
  284. setcookie($sName, $sValue, $iExpire, $sPath);
  285. return;
  286. $sHeader = "Set-Cookie: $sName=$sValue";
  287. if ($sPath) {
  288. $sHeader .= "; path=$sPath";
  289. }
  290. if ($iExpire !== false) {
  291. $sHeader .= "; Max-Age=$iExpire";
  292. // php uses Expire header, also add the expire header
  293. $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire);
  294. }
  295. if ($sDomain) {
  296. $sHeader .= "; Domain=$sDomain";
  297. }
  298. // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
  299. if ($bSecure) {
  300. $sHeader .= "; Secure";
  301. }
  302. if ($bHttpOnly) {
  303. $sHeader .= "; HttpOnly";
  304. }
  305. // $sHeader .= "; Version=1";
  306. $sCookieCache .= $sHeader ."\r\n";
  307. //header($sHeader."\r\n");
  308. }
  309. /**
  310. * Send the cookie header
  311. *
  312. * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
  313. * @return void
  314. */
  315. function sqsetcookieflush() {
  316. sqsetcookie('','','','','','','',true);
  317. }
  318. /**
  319. * session_regenerate_id replacement for PHP < 4.3.2
  320. *
  321. * This code is borrowed from Gallery, session.php version 1.53.2.1
  322. */
  323. if (!function_exists('session_regenerate_id')) {
  324. function make_seed() {
  325. list($usec, $sec) = explode(' ', microtime());
  326. return (float)$sec + ((float)$usec * 100000);
  327. }
  328. function php_combined_lcg() {
  329. mt_srand(make_seed());
  330. $tv = gettimeofday();
  331. $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
  332. $lcg['s2'] = mt_rand();
  333. $q = (int) ($lcg['s1'] / 53668);
  334. $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
  335. if ($lcg['s1'] < 0) {
  336. $lcg['s1'] += 2147483563;
  337. }
  338. $q = (int) ($lcg['s2'] / 52774);
  339. $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
  340. if ($lcg['s2'] < 0) {
  341. $lcg['s2'] += 2147483399;
  342. }
  343. $z = (int) ($lcg['s1'] - $lcg['s2']);
  344. if ($z < 1) {
  345. $z += 2147483562;
  346. }
  347. return $z * 4.656613e-10;
  348. }
  349. function session_regenerate_id() {
  350. global $base_uri;
  351. $tv = gettimeofday();
  352. sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
  353. $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
  354. session_id(md5($buf));
  355. if (ini_get('session.use_cookies')) {
  356. // at a later stage we use sqsetcookie. At this point just do
  357. // what session_regenerate_id would do
  358. setcookie(session_name(), session_id(), NULL, $base_uri);
  359. }
  360. return TRUE;
  361. }
  362. }
  363. /**
  364. * php_self
  365. *
  366. * Creates an URL for the page calling this function, using either the PHP global
  367. * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
  368. * function was stored in function/strings.php.
  369. *
  370. * @return string the complete url for this page
  371. * @since 1.2.3
  372. */
  373. function php_self () {
  374. if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
  375. return $req_uri;
  376. }
  377. if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
  378. // need to add query string to end of PHP_SELF to match REQUEST_URI
  379. //
  380. if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
  381. $php_self .= '?' . $query_string;
  382. }
  383. return $php_self;
  384. }
  385. return '';
  386. }