global.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. * Merges two variables into a single array
  80. *
  81. * Similar to PHP array_merge function, but provides same
  82. * functionality as array_merge without losing array values
  83. * with same key names. If the values under identical array
  84. * keys are both strings and $concat_strings is TRUE, those
  85. * values are concatenated together, otherwise they are placed
  86. * in a sub-array and are merged (recursively) in the same manner.
  87. *
  88. * If either of the elements being merged is not an array,
  89. * it will simply be added to the returned array.
  90. *
  91. * If both values are strings and $concat_strings is TRUE,
  92. * a concatenated string is returned instead of an array.
  93. *
  94. * @param mixed $a First element to be merged
  95. * @param mixed $b Second element to be merged
  96. * @param boolean $concat_strings Whether or not string values
  97. * should be concatenated instead
  98. * of added to different array
  99. * keys (default TRUE)
  100. *
  101. * @return array The merged $a and $b in one array
  102. *
  103. */
  104. function sq_array_merge($a, $b, $concat_strings=true) {
  105. $ret = array();
  106. if (is_array($a)) {
  107. $ret = $a;
  108. } else {
  109. if (is_string($a) && is_string($b) && $concat_strings) {
  110. return $a . $b;
  111. }
  112. $ret[] = $a;
  113. }
  114. if (is_array($b)) {
  115. foreach ($b as $key => $value) {
  116. if (isset($ret[$key])) {
  117. $ret[$key] = sq_array_merge($ret[$key], $value, $concat_strings);
  118. } else {
  119. $ret[$key] = $value;
  120. }
  121. }
  122. } else {
  123. $ret[] = $b;
  124. }
  125. return $ret;
  126. }
  127. /**
  128. * Add a variable to the session.
  129. * @param mixed $var the variable to register
  130. * @param string $name the name to refer to this variable
  131. * @return void
  132. */
  133. function sqsession_register ($var, $name) {
  134. sqsession_is_active();
  135. $_SESSION["$name"] = $var;
  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. unset($_SESSION[$name]);
  146. session_unregister("$name");
  147. }
  148. /**
  149. * Checks to see if a variable has already been registered
  150. * in the session.
  151. * @param string $name the name of the var to check
  152. * @return bool whether the var has been registered
  153. */
  154. function sqsession_is_registered ($name) {
  155. $test_name = &$name;
  156. $result = false;
  157. if (isset($_SESSION[$test_name])) {
  158. $result = true;
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
  164. * and set it in provided var.
  165. *
  166. * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
  167. * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
  168. * $_GET. Otherwise, use one of the defined constants to look for a var in one
  169. * place specifically.
  170. *
  171. * Note: $search is an int value equal to one of the constants defined above.
  172. *
  173. * Example:
  174. * sqgetGlobalVar('username',$username,SQ_SESSION);
  175. * // No quotes around last param, it's a constant - not a string!
  176. *
  177. * @param string name the name of the var to search
  178. * @param mixed value the variable to return
  179. * @param int search constant defining where to look
  180. * @param int typecast force variable to be cast to given type (please
  181. * use SQ_TYPE_XXX constants or set to FALSE (default)
  182. * to leave variable type unmolested)
  183. * @return bool whether variable is found.
  184. */
  185. function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
  186. $result = false;
  187. switch ($search) {
  188. /* we want the default case to be first here,
  189. so that if a valid value isn't specified,
  190. all three arrays will be searched. */
  191. default:
  192. case SQ_INORDER: // check session, post, get
  193. case SQ_SESSION:
  194. if( isset($_SESSION[$name]) ) {
  195. $value = $_SESSION[$name];
  196. $result = TRUE;
  197. break;
  198. } elseif ( $search == SQ_SESSION ) {
  199. break;
  200. }
  201. case SQ_FORM: // check post, get
  202. case SQ_POST:
  203. if( isset($_POST[$name]) ) {
  204. $value = $_POST[$name];
  205. $result = TRUE;
  206. break;
  207. } elseif ( $search == SQ_POST ) {
  208. break;
  209. }
  210. case SQ_GET:
  211. if ( isset($_GET[$name]) ) {
  212. $value = $_GET[$name];
  213. $result = TRUE;
  214. break;
  215. }
  216. /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  217. break;
  218. case SQ_COOKIE:
  219. if ( isset($_COOKIE[$name]) ) {
  220. $value = $_COOKIE[$name];
  221. $result = TRUE;
  222. break;
  223. }
  224. break;
  225. case SQ_SERVER:
  226. if ( isset($_SERVER[$name]) ) {
  227. $value = $_SERVER[$name];
  228. $result = TRUE;
  229. break;
  230. }
  231. break;
  232. }
  233. if ($result && $typecast) {
  234. switch ($typecast) {
  235. case SQ_TYPE_INT: $value = (int) $value; break;
  236. case SQ_TYPE_STRING: $value = (string) $value; break;
  237. case SQ_TYPE_BOOL: $value = (bool) $value; break;
  238. default: break;
  239. }
  240. } else if (!$result && !is_null($default)) {
  241. $value = $default;
  242. }
  243. return $result;
  244. }
  245. /**
  246. * Deletes an existing session, more advanced than the standard PHP
  247. * session_destroy(), it explicitly deletes the cookies and global vars.
  248. *
  249. * WARNING: Older PHP versions have some issues with session management.
  250. * See http://bugs.php.net/11643 (warning, spammed bug tracker) and
  251. * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
  252. * 4.2.3 and maybe other versions. If you restart session after session
  253. * is destroyed, affected PHP versions produce PHP notice. Bug should
  254. * be fixed only in 4.3.0
  255. */
  256. function sqsession_destroy() {
  257. /*
  258. * php.net says we can kill the cookie by setting just the name:
  259. * http://www.php.net/manual/en/function.setcookie.php
  260. * maybe this will help fix the session merging again.
  261. *
  262. * Changed the theory on this to kill the cookies first starting
  263. * a new session will provide a new session for all instances of
  264. * the browser, we don't want that, as that is what is causing the
  265. * merging of sessions.
  266. */
  267. global $base_uri;
  268. if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
  269. if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
  270. if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
  271. $sessid = session_id();
  272. if (!empty( $sessid )) {
  273. $_SESSION = array();
  274. @session_destroy();
  275. }
  276. }
  277. /**
  278. * Function to verify a session has been started. If it hasn't
  279. * start a session up. php.net doesn't tell you that $_SESSION
  280. * (even though autoglobal), is not created unless a session is
  281. * started, unlike $_POST, $_GET and such
  282. */
  283. function sqsession_is_active() {
  284. $sessid = session_id();
  285. if ( empty( $sessid ) ) {
  286. sqsession_start();
  287. }
  288. }
  289. /**
  290. * Function to start the session and store the cookie with the session_id as
  291. * HttpOnly cookie which means that the cookie isn't accessible by javascript
  292. * (IE6 only)
  293. */
  294. function sqsession_start() {
  295. global $base_uri;
  296. session_start();
  297. $session_id = session_id();
  298. // session_starts sets the sessionid cookie buth without the httponly var
  299. // setting the cookie again sets the httponly cookie attribute
  300. // disable, @see sqsetcookie and php 5.1.2
  301. // sqsetcookie(session_name(),session_id(),false,$base_uri);
  302. }
  303. /**
  304. * Set a cookie
  305. * @param string $sName The name of the cookie.
  306. * @param string $sValue The value of the cookie.
  307. * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
  308. * @param string $sPath The path on the server in which the cookie will be available on.
  309. * @param string $sDomain The domain that the cookie is available.
  310. * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  311. * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
  312. * @return void
  313. */
  314. function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) {
  315. static $sCookieCache;
  316. if (!isset($sCache)) {
  317. $sCache = '';
  318. }
  319. /**
  320. * We have to send all cookies with one header call otherwise we loose cookies.
  321. * In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true.
  322. * If that happens we send the cookie header.
  323. */
  324. if ($bFlush) {
  325. // header($sCookieCache);
  326. return;
  327. }
  328. if (!$sName) return;
  329. // php 5.1.2 and 4.4.2 do not allow to send multiple headers at once.
  330. // Because that's the only way to get this thing working we fallback to
  331. // setcookie until we solved this
  332. if ($iExpire===false) $iExpire = 0;
  333. setcookie($sName, $sValue, $iExpire, $sPath);
  334. return;
  335. $sHeader = "Set-Cookie: $sName=$sValue";
  336. if ($sPath) {
  337. $sHeader .= "; path=$sPath";
  338. }
  339. if ($iExpire !== false) {
  340. $sHeader .= "; Max-Age=$iExpire";
  341. // php uses Expire header, also add the expire header
  342. $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire);
  343. }
  344. if ($sDomain) {
  345. $sHeader .= "; Domain=$sDomain";
  346. }
  347. // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
  348. if ($bSecure) {
  349. $sHeader .= "; Secure";
  350. }
  351. if ($bHttpOnly) {
  352. $sHeader .= "; HttpOnly";
  353. }
  354. // $sHeader .= "; Version=1";
  355. $sCookieCache .= $sHeader ."\r\n";
  356. //header($sHeader."\r\n");
  357. }
  358. /**
  359. * Send the cookie header
  360. *
  361. * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
  362. * @return void
  363. */
  364. function sqsetcookieflush() {
  365. sqsetcookie('','','','','','','',true);
  366. }
  367. /**
  368. * session_regenerate_id replacement for PHP < 4.3.2
  369. *
  370. * This code is borrowed from Gallery, session.php version 1.53.2.1
  371. */
  372. if (!function_exists('session_regenerate_id')) {
  373. function make_seed() {
  374. list($usec, $sec) = explode(' ', microtime());
  375. return (float)$sec + ((float)$usec * 100000);
  376. }
  377. function php_combined_lcg() {
  378. mt_srand(make_seed());
  379. $tv = gettimeofday();
  380. $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
  381. $lcg['s2'] = mt_rand();
  382. $q = (int) ($lcg['s1'] / 53668);
  383. $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
  384. if ($lcg['s1'] < 0) {
  385. $lcg['s1'] += 2147483563;
  386. }
  387. $q = (int) ($lcg['s2'] / 52774);
  388. $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
  389. if ($lcg['s2'] < 0) {
  390. $lcg['s2'] += 2147483399;
  391. }
  392. $z = (int) ($lcg['s1'] - $lcg['s2']);
  393. if ($z < 1) {
  394. $z += 2147483562;
  395. }
  396. return $z * 4.656613e-10;
  397. }
  398. function session_regenerate_id() {
  399. global $base_uri;
  400. $tv = gettimeofday();
  401. sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
  402. $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
  403. session_id(md5($buf));
  404. if (ini_get('session.use_cookies')) {
  405. // at a later stage we use sqsetcookie. At this point just do
  406. // what session_regenerate_id would do
  407. setcookie(session_name(), session_id(), NULL, $base_uri);
  408. }
  409. return TRUE;
  410. }
  411. }
  412. /**
  413. * php_self
  414. *
  415. * Creates an URL for the page calling this function, using either the PHP global
  416. * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
  417. * function was stored in function/strings.php.
  418. *
  419. * @return string the complete url for this page
  420. * @since 1.2.3
  421. */
  422. function php_self () {
  423. if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
  424. return $req_uri;
  425. }
  426. if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
  427. // need to add query string to end of PHP_SELF to match REQUEST_URI
  428. //
  429. if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
  430. $php_self .= '?' . $query_string;
  431. }
  432. return $php_self;
  433. }
  434. return '';
  435. }