global.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. * @return bool whether variable is found.
  132. */
  133. function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
  134. $result = false;
  135. switch ($search) {
  136. /* we want the default case to be first here,
  137. so that if a valid value isn't specified,
  138. all three arrays will be searched. */
  139. default:
  140. case SQ_INORDER: // check session, post, get
  141. case SQ_SESSION:
  142. if( isset($_SESSION[$name]) ) {
  143. $value = $_SESSION[$name];
  144. $result = TRUE;
  145. break;
  146. } elseif ( $search == SQ_SESSION ) {
  147. break;
  148. }
  149. case SQ_FORM: // check post, get
  150. case SQ_POST:
  151. if( isset($_POST[$name]) ) {
  152. $value = $_POST[$name];
  153. $result = TRUE;
  154. break;
  155. } elseif ( $search == SQ_POST ) {
  156. break;
  157. }
  158. case SQ_GET:
  159. if ( isset($_GET[$name]) ) {
  160. $value = $_GET[$name];
  161. $result = TRUE;
  162. break;
  163. }
  164. /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  165. break;
  166. case SQ_COOKIE:
  167. if ( isset($_COOKIE[$name]) ) {
  168. $value = $_COOKIE[$name];
  169. $result = TRUE;
  170. break;
  171. }
  172. break;
  173. case SQ_SERVER:
  174. if ( isset($_SERVER[$name]) ) {
  175. $value = $_SERVER[$name];
  176. $result = TRUE;
  177. break;
  178. }
  179. break;
  180. }
  181. if ($result && $typecast) {
  182. switch ($typecast) {
  183. case 'int': $value = (int) $value; break;
  184. case 'bool': $value = (bool) $value; break;
  185. default: break;
  186. }
  187. } else if (!is_null($default)) {
  188. $value = $default;
  189. }
  190. return $result;
  191. }
  192. /**
  193. * Deletes an existing session, more advanced than the standard PHP
  194. * session_destroy(), it explicitly deletes the cookies and global vars.
  195. */
  196. function sqsession_destroy() {
  197. /*
  198. * php.net says we can kill the cookie by setting just the name:
  199. * http://www.php.net/manual/en/function.setcookie.php
  200. * maybe this will help fix the session merging again.
  201. *
  202. * Changed the theory on this to kill the cookies first starting
  203. * a new session will provide a new session for all instances of
  204. * the browser, we don't want that, as that is what is causing the
  205. * merging of sessions.
  206. */
  207. global $base_uri;
  208. if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
  209. if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
  210. if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
  211. $sessid = session_id();
  212. if (!empty( $sessid )) {
  213. $_SESSION = array();
  214. @session_destroy();
  215. }
  216. }
  217. /**
  218. * Function to verify a session has been started. If it hasn't
  219. * start a session up. php.net doesn't tell you that $_SESSION
  220. * (even though autoglobal), is not created unless a session is
  221. * started, unlike $_POST, $_GET and such
  222. */
  223. function sqsession_is_active() {
  224. $sessid = session_id();
  225. if ( empty( $sessid ) ) {
  226. sqsession_start();
  227. }
  228. }
  229. /**
  230. * Function to start the session and store the cookie with the session_id as
  231. * HttpOnly cookie which means that the cookie isn't accessible by javascript
  232. * (IE6 only)
  233. */
  234. function sqsession_start() {
  235. global $base_uri;
  236. session_start();
  237. $session_id = session_id();
  238. // session_starts sets the sessionid cookie buth without the httponly var
  239. // setting the cookie again sets the httponly cookie attribute
  240. // disable, @see sqsetcookie and php 5.1.2
  241. // sqsetcookie(session_name(),session_id(),false,$base_uri);
  242. }
  243. /**
  244. * Set a cookie
  245. * @param string $sName The name of the cookie.
  246. * @param string $sValue The value of the cookie.
  247. * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
  248. * @param string $sPath The path on the server in which the cookie will be available on.
  249. * @param string $sDomain The domain that the cookie is available.
  250. * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  251. * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
  252. * @return void
  253. */
  254. function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) {
  255. static $sCookieCache;
  256. if (!isset($sCache)) {
  257. $sCache = '';
  258. }
  259. /**
  260. * We have to send all cookies with one header call otherwise we loose cookies.
  261. * In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true.
  262. * If that happens we send the cookie header.
  263. */
  264. if ($bFlush) {
  265. // header($sCookieCache);
  266. return;
  267. }
  268. if (!$sName) return;
  269. // php 5.1.2 and 4.4.2 do not allow to send multiple headers at once.
  270. // Because that's the only way to get this thing working we fallback to
  271. // setcookie until we solved this
  272. if ($iExpire===false) $iExpire = 0;
  273. setcookie($sName, $sValue, $iExpire, $sPath);
  274. return;
  275. $sHeader = "Set-Cookie: $sName=$sValue";
  276. if ($sPath) {
  277. $sHeader .= "; path=$sPath";
  278. }
  279. if ($iExpire !== false) {
  280. $sHeader .= "; Max-Age=$iExpire";
  281. // php uses Expire header, also add the expire header
  282. $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire);
  283. }
  284. if ($sDomain) {
  285. $sHeader .= "; Domain=$sDomain";
  286. }
  287. // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
  288. if ($bSecure) {
  289. $sHeader .= "; Secure";
  290. }
  291. if ($bHttpOnly) {
  292. $sHeader .= "; HttpOnly";
  293. }
  294. // $sHeader .= "; Version=1";
  295. $sCookieCache .= $sHeader ."\r\n";
  296. //header($sHeader."\r\n");
  297. }
  298. /**
  299. * Send the cookie header
  300. *
  301. * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
  302. * @return void
  303. */
  304. function sqsetcookieflush() {
  305. sqsetcookie('','','','','','','',true);
  306. }
  307. /**
  308. * session_regenerate_id replacement for PHP < 4.3.2
  309. *
  310. * This code is borrowed from Gallery, session.php version 1.53.2.1
  311. */
  312. if (!function_exists('session_regenerate_id')) {
  313. function make_seed() {
  314. list($usec, $sec) = explode(' ', microtime());
  315. return (float)$sec + ((float)$usec * 100000);
  316. }
  317. function php_combined_lcg() {
  318. mt_srand(make_seed());
  319. $tv = gettimeofday();
  320. $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
  321. $lcg['s2'] = mt_rand();
  322. $q = (int) ($lcg['s1'] / 53668);
  323. $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
  324. if ($lcg['s1'] < 0) {
  325. $lcg['s1'] += 2147483563;
  326. }
  327. $q = (int) ($lcg['s2'] / 52774);
  328. $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
  329. if ($lcg['s2'] < 0) {
  330. $lcg['s2'] += 2147483399;
  331. }
  332. $z = (int) ($lcg['s1'] - $lcg['s2']);
  333. if ($z < 1) {
  334. $z += 2147483562;
  335. }
  336. return $z * 4.656613e-10;
  337. }
  338. function session_regenerate_id() {
  339. global $base_uri;
  340. $tv = gettimeofday();
  341. sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
  342. $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
  343. session_id(md5($buf));
  344. if (ini_get('session.use_cookies')) {
  345. // at a later stage we use sqsetcookie. At this point just do
  346. // what session_regenerate_id would do
  347. setcookie(session_name(), session_id(), NULL, $base_uri);
  348. }
  349. return TRUE;
  350. }
  351. }
  352. /**
  353. * php_self
  354. *
  355. * Creates an URL for the page calling this function, using either the PHP global
  356. * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
  357. * function was stored in function/strings.php.
  358. *
  359. * @return string the complete url for this page
  360. * @since 1.2.3
  361. */
  362. function php_self () {
  363. if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
  364. return $req_uri;
  365. }
  366. if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
  367. // need to add query string to end of PHP_SELF to match REQUEST_URI
  368. //
  369. if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
  370. $php_self .= '?' . $query_string;
  371. }
  372. return $php_self;
  373. }
  374. return '';
  375. }