global.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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) {
  134. /* NOTE: DO NOT enclose the constants in the switch
  135. statement with quotes. They are constant values,
  136. enclosing them in quotes will cause them to evaluate
  137. as strings. */
  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. return TRUE;
  148. } elseif ( $search == SQ_SESSION ) {
  149. break;
  150. }
  151. case SQ_FORM: // check post, get
  152. case SQ_POST:
  153. if( isset($_POST[$name]) ) {
  154. $value = $_POST[$name];
  155. return TRUE;
  156. } elseif ( $search == SQ_POST ) {
  157. break;
  158. }
  159. case SQ_GET:
  160. if ( isset($_GET[$name]) ) {
  161. $value = $_GET[$name];
  162. return TRUE;
  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. return TRUE;
  170. }
  171. break;
  172. case SQ_SERVER:
  173. if ( isset($_SERVER[$name]) ) {
  174. $value = $_SERVER[$name];
  175. return TRUE;
  176. }
  177. break;
  178. }
  179. /* Nothing found, return FALSE */
  180. return FALSE;
  181. }
  182. /**
  183. * Deletes an existing session, more advanced than the standard PHP
  184. * session_destroy(), it explicitly deletes the cookies and global vars.
  185. */
  186. function sqsession_destroy() {
  187. /*
  188. * php.net says we can kill the cookie by setting just the name:
  189. * http://www.php.net/manual/en/function.setcookie.php
  190. * maybe this will help fix the session merging again.
  191. *
  192. * Changed the theory on this to kill the cookies first starting
  193. * a new session will provide a new session for all instances of
  194. * the browser, we don't want that, as that is what is causing the
  195. * merging of sessions.
  196. */
  197. global $base_uri;
  198. if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
  199. if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
  200. if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
  201. $sessid = session_id();
  202. if (!empty( $sessid )) {
  203. $_SESSION = array();
  204. @session_destroy();
  205. }
  206. }
  207. /**
  208. * Function to verify a session has been started. If it hasn't
  209. * start a session up. php.net doesn't tell you that $_SESSION
  210. * (even though autoglobal), is not created unless a session is
  211. * started, unlike $_POST, $_GET and such
  212. */
  213. function sqsession_is_active() {
  214. $sessid = session_id();
  215. if ( empty( $sessid ) ) {
  216. sqsession_start();
  217. }
  218. }
  219. /**
  220. * Function to start the session and store the cookie with the session_id as
  221. * HttpOnly cookie which means that the cookie isn't accessible by javascript
  222. * (IE6 only)
  223. */
  224. function sqsession_start() {
  225. global $PHP_SELF;
  226. $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
  227. $repl = array('', '', '');
  228. $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
  229. session_start();
  230. $sessid = session_id();
  231. // session_starts sets the sessionid cookie buth without the httponly var
  232. // setting the cookie again sets the httponly cookie attribute
  233. sqsetcookie(session_name(),$sessid,false,$base_uri);
  234. }
  235. /**
  236. * Set a cookie
  237. * @param string $sName The name of the cookie.
  238. * @param string $sValue The value of the cookie.
  239. * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
  240. * @param string $sPath The path on the server in which the cookie will be available on.
  241. * @param string $sDomain The domain that the cookie is available.
  242. * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  243. * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
  244. * @return void
  245. */
  246. function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true) {
  247. $sHeader = "Set-Cookie: $sName=$sValue";
  248. if ($sPath) {
  249. $sHeader .= "; path=$sPath";
  250. }
  251. if ($iExpire !== false) {
  252. $sHeader .= "; Max-Age=$iExpire";
  253. // php uses Expire header, also add the expire header
  254. $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire);
  255. }
  256. if ($sDomain) {
  257. $sHeader .= "; Domain=$sDomain";
  258. }
  259. if ($bSecure) {
  260. $sHeader .= "; Secure";
  261. }
  262. if ($bHttpOnly) {
  263. $sHeader .= "; HttpOnly";
  264. }
  265. // $sHeader .= "; Version=1";
  266. header($sHeader);
  267. }
  268. /**
  269. * php_self
  270. *
  271. * Creates an URL for the page calling this function, using either the PHP global
  272. * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
  273. * function was stored in function/strings.php.
  274. *
  275. * @return string the complete url for this page
  276. * @since 1.2.3
  277. */
  278. function php_self () {
  279. if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
  280. return $req_uri;
  281. }
  282. if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
  283. // need to add query string to end of PHP_SELF to match REQUEST_URI
  284. //
  285. if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
  286. $php_self .= '?' . $query_string;
  287. }
  288. return $php_self;
  289. }
  290. return '';
  291. }
  292. /** set the name of the session cookie */
  293. if(isset($session_name) && $session_name) {
  294. ini_set('session.name' , $session_name);
  295. } else {
  296. ini_set('session.name' , 'SQMSESSID');
  297. }
  298. /**
  299. * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
  300. * Force magic_quotes_runtime off.
  301. * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
  302. * If there's a better place, please let me know.
  303. */
  304. ini_set('magic_quotes_runtime','0');
  305. /* Since we decided all IMAP servers must implement the UID command as defined in
  306. * the IMAP RFC, we force $uid_support to be on.
  307. */
  308. global $uid_support;
  309. $uid_support = true;
  310. /* if running with magic_quotes_gpc then strip the slashes
  311. from POST and GET global arrays */
  312. if (get_magic_quotes_gpc()) {
  313. sqstripslashes($_GET);
  314. sqstripslashes($_POST);
  315. }
  316. /**
  317. * If register_globals are on, unregister globals.
  318. * Code requires PHP 4.1.0 or newer.
  319. */
  320. if ((bool) @ini_get('register_globals')) {
  321. /**
  322. * Remove all globals from $_GET, $_POST, and $_COOKIE.
  323. */
  324. foreach ($_REQUEST as $key => $value) {
  325. unset($GLOBALS[$key]);
  326. }
  327. /**
  328. * Remove globalized $_FILES variables
  329. * Before 4.3.0 $_FILES are included in $_REQUEST.
  330. * Unglobalize them in separate call in order to remove dependency
  331. * on PHP version.
  332. */
  333. foreach ($_FILES as $key => $value) {
  334. unset($GLOBALS[$key]);
  335. // there are three undocumented $_FILES globals.
  336. unset($GLOBALS[$key.'_type']);
  337. unset($GLOBALS[$key.'_name']);
  338. unset($GLOBALS[$key.'_size']);
  339. }
  340. /**
  341. * Remove globalized environment variables.
  342. */
  343. foreach ($_ENV as $key => $value) {
  344. unset($GLOBALS[$key]);
  345. }
  346. /**
  347. * Remove globalized server variables.
  348. */
  349. foreach ($_SERVER as $key => $value) {
  350. unset($GLOBALS[$key]);
  351. }
  352. }
  353. /* strip any tags added to the url from PHP_SELF.
  354. This fixes hand crafted url XXS expoits for any
  355. page that uses PHP_SELF as the FORM action */
  356. $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
  357. $PHP_SELF = php_self();
  358. sqsession_is_active();
  359. /**
  360. * Remove globalized session data in rg=on setups
  361. */
  362. if ((bool) @ini_get('register_globals')) {
  363. foreach ($_SESSION as $key => $value) {
  364. unset($GLOBALS[$key]);
  365. }
  366. }
  367. ?>