global.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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-2005 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,
  115. * $_COOKIE, or $_SERVER and set it in provided var.
  116. *
  117. * If $search is not provided, or == SQ_INORDER, it will search
  118. * $_SESSION, then $_POST, then $_GET. Otherwise,
  119. * use one of the defined constants to look for
  120. * a var in one place specifically.
  121. *
  122. * Note: $search is an int value equal to one of the
  123. * constants defined above.
  124. *
  125. * example:
  126. * sqgetGlobalVar('username',$username,SQ_SESSION);
  127. * -- no quotes around last param!
  128. *
  129. * @param string name the name of the var to search
  130. * @param mixed value the variable to return
  131. * @param int search constant defining where to look
  132. * @return bool whether variable is found.
  133. */
  134. function sqgetGlobalVar($name, &$value, $search = SQ_INORDER) {
  135. /* NOTE: DO NOT enclose the constants in the switch
  136. statement with quotes. They are constant values,
  137. enclosing them in quotes will cause them to evaluate
  138. as strings. */
  139. switch ($search) {
  140. /* we want the default case to be first here,
  141. so that if a valid value isn't specified,
  142. all three arrays will be searched. */
  143. default:
  144. case SQ_INORDER: // check session, post, get
  145. case SQ_SESSION:
  146. if( isset($_SESSION[$name]) ) {
  147. $value = $_SESSION[$name];
  148. return TRUE;
  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. return TRUE;
  157. } elseif ( $search == SQ_POST ) {
  158. break;
  159. }
  160. case SQ_GET:
  161. if ( isset($_GET[$name]) ) {
  162. $value = $_GET[$name];
  163. return TRUE;
  164. }
  165. /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  166. break;
  167. case SQ_COOKIE:
  168. if ( isset($_COOKIE[$name]) ) {
  169. $value = $_COOKIE[$name];
  170. return TRUE;
  171. }
  172. break;
  173. case SQ_SERVER:
  174. if ( isset($_SERVER[$name]) ) {
  175. $value = $_SERVER[$name];
  176. return TRUE;
  177. }
  178. break;
  179. }
  180. /* Nothing found, return FALSE */
  181. return FALSE;
  182. }
  183. /**
  184. * Deletes an existing session, more advanced than the standard PHP
  185. * session_destroy(), it explicitly deletes the cookies and global vars.
  186. */
  187. function sqsession_destroy() {
  188. /*
  189. * php.net says we can kill the cookie by setting just the name:
  190. * http://www.php.net/manual/en/function.setcookie.php
  191. * maybe this will help fix the session merging again.
  192. *
  193. * Changed the theory on this to kill the cookies first starting
  194. * a new session will provide a new session for all instances of
  195. * the browser, we don't want that, as that is what is causing the
  196. * merging of sessions.
  197. */
  198. global $base_uri;
  199. if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri);
  200. if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
  201. if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
  202. $sessid = session_id();
  203. if (!empty( $sessid )) {
  204. $_SESSION = array();
  205. @session_destroy();
  206. }
  207. }
  208. /**
  209. * Function to verify a session has been started. If it hasn't
  210. * start a session up. php.net doesn't tell you that $_SESSION
  211. * (even though autoglobal), is not created unless a session is
  212. * started, unlike $_POST, $_GET and such
  213. */
  214. function sqsession_is_active() {
  215. $sessid = session_id();
  216. if ( empty( $sessid ) ) {
  217. sqsession_start();
  218. }
  219. }
  220. /**
  221. * Function to start the session and store the cookie with the session_id as
  222. * HttpOnly cookie which means that the cookie isn't accessible by javascript
  223. * (IE6 only)
  224. */
  225. function sqsession_start() {
  226. global $PHP_SELF;
  227. $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
  228. $repl = array('', '', '');
  229. $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
  230. session_start();
  231. $sessid = session_id();
  232. // session_starts sets the sessionid cookie buth without the httponly var
  233. // setting the cookie again sets the httponly cookie attribute
  234. sqsetcookie(session_name(),$sessid,false,$base_uri);
  235. }
  236. /**
  237. * Set a cookie
  238. * @param string $sName The name of the cookie.
  239. * @param string $sValue The value of the cookie.
  240. * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
  241. * @param string $sPath The path on the server in which the cookie will be available on.
  242. * @param string $sDomain The domain that the cookie is available.
  243. * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
  244. * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
  245. * @return void
  246. */
  247. function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true) {
  248. $sHeader = "Set-Cookie: $sName=$sValue";
  249. if ($sPath) {
  250. $sHeader .= "; Path=\"$sPath\"";
  251. }
  252. if ($iExpire !==false) {
  253. $sHeader .= "; Max-Age=$iExpire";
  254. }
  255. if ($sPath) {
  256. $sHeader .= "; Path=$sPath";
  257. }
  258. if ($sDomain) {
  259. $sHeader .= "; Domain=$sDomain";
  260. }
  261. if ($bSecure) {
  262. $sHeader .= "; Secure";
  263. }
  264. if ($bHttpOnly) {
  265. $sHeader .= "; HttpOnly";
  266. }
  267. $sHeader .= "; Version=1";
  268. header($sHeader);
  269. }
  270. /**
  271. * php_self
  272. *
  273. * Creates an URL for the page calling this function, using either the PHP global
  274. * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
  275. * function was stored in function/strings.php.
  276. *
  277. * @return string the complete url for this page
  278. * @since 1.2.3
  279. */
  280. function php_self () {
  281. if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
  282. return $req_uri;
  283. }
  284. if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
  285. // need to add query string to end of PHP_SELF to match REQUEST_URI
  286. //
  287. if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
  288. $php_self .= '?' . $query_string;
  289. }
  290. return $php_self;
  291. }
  292. return '';
  293. }
  294. /** set the name of the session cookie */
  295. if(isset($session_name) && $session_name) {
  296. ini_set('session.name' , $session_name);
  297. } else {
  298. ini_set('session.name' , 'SQMSESSID');
  299. }
  300. /**
  301. * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
  302. * Force magic_quotes_runtime off.
  303. * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
  304. * If there's a better place, please let me know.
  305. */
  306. ini_set('magic_quotes_runtime','0');
  307. /* Since we decided all IMAP servers must implement the UID command as defined in
  308. * the IMAP RFC, we force $uid_support to be on.
  309. */
  310. global $uid_support;
  311. $uid_support = true;
  312. /* if running with magic_quotes_gpc then strip the slashes
  313. from POST and GET global arrays */
  314. if (get_magic_quotes_gpc()) {
  315. sqstripslashes($_GET);
  316. sqstripslashes($_POST);
  317. }
  318. /* strip any tags added to the url from PHP_SELF.
  319. This fixes hand crafted url XXS expoits for any
  320. page that uses PHP_SELF as the FORM action */
  321. $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
  322. $PHP_SELF = php_self();
  323. sqsession_is_active();
  324. // vim: et ts=4
  325. ?>