Source for file global.php

Documentation is available at global.php

  1. <?php
  2.  
  3. /**
  4.  * global.php
  5.  *
  6.  * This includes code to update < 4.1.0 globals to the newer format
  7.  * It also has some session register functions that work across various
  8.  * php versions.
  9.  *
  10.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  11.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12.  * @version $Id: global.php,v 1.27.2.19 2006/07/29 08:57:52 tokul Exp $
  13.  * @package squirrelmail
  14.  */
  15.  
  16. /**
  17.  * Set constants
  18.  */
  19. define('SQ_INORDER',0);
  20. define('SQ_GET',1);
  21. define('SQ_POST',2);
  22. define('SQ_SESSION',3);
  23. define('SQ_COOKIE',4);
  24. define('SQ_SERVER',5);
  25. define('SQ_FORM',6);
  26.  
  27. /** First code that should be executed before other files are loaded */
  28.  
  29. /**
  30.  * Must be executed before any other scripts are loaded.
  31.  *
  32.  * If register_globals are on, unregister globals.
  33.  * Second test covers boolean set as string (php_value register_globals off).
  34.  */
  35. if ((bool) ini_get('register_globals'&&
  36.     strtolower(ini_get('register_globals'))!='off'{
  37.     /**
  38.      * Remove all globals that are not reserved by PHP
  39.      * 'value' and 'key' are used by foreach. Don't unset them inside foreach.
  40.      */
  41.     foreach ($GLOBALS as $key => $value{
  42.         switch($key{
  43.         case 'HTTP_POST_VARS':
  44.         case '_POST':
  45.         case 'HTTP_GET_VARS':
  46.         case '_GET':
  47.         case 'HTTP_COOKIE_VARS':
  48.         case '_COOKIE':
  49.         case 'HTTP_SERVER_VARS':
  50.         case '_SERVER':
  51.         case 'HTTP_ENV_VARS':
  52.         case '_ENV':
  53.         case 'HTTP_POST_FILES':
  54.         case '_FILES':
  55.         case '_REQUEST':
  56.         case 'HTTP_SESSION_VARS':
  57.         case '_SESSION':
  58.         case 'GLOBALS':
  59.         case 'key':
  60.         case 'value':
  61.             break;
  62.         default:
  63.             unset($GLOBALS[$key]);
  64.         }
  65.     }
  66.     // Unset variables used in foreach
  67.         unset($GLOBALS['key']);
  68.     unset($GLOBALS['value']);
  69. }
  70.  
  71. /*
  72.  * strip any tags added to the url from PHP_SELF.
  73.  * This fixes hand crafted url XXS expoits for any
  74.  * page that uses PHP_SELF as the FORM action
  75.  * Must be executed before strings.php is loaded (php_self() call in strings.php).
  76.  */
  77. if (isset($_SERVER['PHP_SELF'])) {
  78.     // php 4.1+
  79.         $_SERVER['PHP_SELF'strip_tags($_SERVER['PHP_SELF']);
  80. elseif (isset($HTTP_SERVER_VARS['PHP_SELF'])) {
  81.     // php 4.0.6
  82.         $HTTP_SERVER_VARS['PHP_SELF'strip_tags($HTTP_SERVER_VARS['PHP_SELF']);
  83. }
  84.  
  85. /**
  86.  * Bring in the config file
  87.  * We need $session_name
  88.  * config.php $version depends on strings.php.
  89.  * strings.php sets $PHP_SELF.
  90.  */
  91. require_once(SM_PATH 'functions/strings.php');
  92. require_once(SM_PATH 'config/config.php');
  93.  
  94. /** set the name of the session cookie */
  95. if(isset($session_name&& $session_name{
  96.     ini_set('session.name' $session_name);
  97. else {
  98.     ini_set('session.name' 'SQMSESSID');
  99. }
  100.  
  101. /**
  102.  * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
  103.  * Force magic_quotes_runtime off.
  104.  * [email protected] - I put it here in the hopes that all SM code includes this.
  105.  * If there's a better place, please let me know.
  106.  */
  107. ini_set('magic_quotes_runtime','0');
  108.  
  109. /**
  110.  * [#1518885] session.use_cookies = off breaks SquirrelMail
  111.  *
  112.  * When session cookies are not used, all http redirects, meta refreshes,
  113.  * src/download.php and javascript URLs are broken. Setting must be set
  114.  * before session is started.
  115.  */
  116. if (!(bool)ini_get('session.use_cookies'||
  117.     ini_get('session.use_cookies'== 'off'{
  118.     ini_set('session.use_cookies','1');
  119. }
  120.  
  121. /* convert old-style superglobals to current method
  122.  * this is executed if you are running PHP 4.0.x.
  123.  * it is run via a require_once directive in validate.php
  124.  * and redirect.php. Patch submitted by Ray Black.
  125.  */
  126. if !check_php_version(4,1) ) {
  127.   global $_COOKIE$_ENV$_FILES$_GET$_POST$_SERVER$_SESSION;
  128.   global $HTTP_COOKIE_VARS$HTTP_ENV_VARS$HTTP_POST_FILES$HTTP_GET_VARS,
  129.          $HTTP_POST_VARS$HTTP_SERVER_VARS$HTTP_SESSION_VARS$PHP_SELF;
  130.   $_COOKIE  =$HTTP_COOKIE_VARS;
  131.   $_ENV     =$HTTP_ENV_VARS;
  132.   $_FILES   =$HTTP_POST_FILES;
  133.   $_GET     =$HTTP_GET_VARS;
  134.   $_POST    =$HTTP_POST_VARS;
  135.   $_SERVER  =$HTTP_SERVER_VARS;
  136.   $_SESSION =$HTTP_SESSION_VARS;
  137.   if (!isset($PHP_SELF|| empty($PHP_SELF)) {
  138.      $PHP_SELF =  $HTTP_SERVER_VARS['PHP_SELF'];
  139.   }
  140. }
  141.  
  142. /* if running with magic_quotes_gpc then strip the slashes
  143.    from POST and GET global arrays */
  144.  
  145.     sqstripslashes($_GET);
  146.     sqstripslashes($_POST);
  147. }
  148.  
  149. /**
  150.  * returns true if current php version is at mimimum a.b.c
  151.  *
  152.  * Called: check_php_version(4,1)
  153.  * @param int a major version number
  154.  * @param int b minor version number
  155.  * @param int c release number
  156.  * @return bool 
  157.  */
  158. function check_php_version ($a '0'$b '0'$c '0')
  159. {
  160.     global $SQ_PHP_VERSION;
  161.  
  162.     if(!isset($SQ_PHP_VERSION))
  163.         $SQ_PHP_VERSION substrstr_padpreg_replace('/\D/',''PHP_VERSION)3'0')03);
  164.  
  165.     return $SQ_PHP_VERSION >= ($a.$b.$c);
  166. }
  167.  
  168. /**
  169.  * returns true if the current internal SM version is at minimum a.b.c
  170.  * These are plain integer comparisons, as our internal version is
  171.  * constructed by us, as an array of 3 ints.
  172.  *
  173.  * Called: check_sm_version(1,3,3)
  174.  * @param int a major version number
  175.  * @param int b minor version number
  176.  * @param int c release number
  177.  * @return bool 
  178.  */
  179. function check_sm_version($a 0$b 0$c 0)
  180. {
  181.     global $SQM_INTERNAL_VERSION;
  182.     if !isset($SQM_INTERNAL_VERSION||
  183.          $SQM_INTERNAL_VERSION[0$a ||
  184.          $SQM_INTERNAL_VERSION[0== $a &&
  185.            $SQM_INTERNAL_VERSION[1$b||
  186.          $SQM_INTERNAL_VERSION[0== $a &&
  187.            $SQM_INTERNAL_VERSION[1== $b &&
  188.            $SQM_INTERNAL_VERSION[2$c ) ) {
  189.         return FALSE;
  190.     }
  191.     return TRUE;
  192. }
  193.  
  194.  
  195. /**
  196.  * Recursively strip slashes from the values of an array.
  197.  * @param array array the array to strip, passed by reference
  198.  * @return void 
  199.  */
  200. function sqstripslashes(&$array{
  201.     if(count($array0{
  202.         foreach ($array as $index=>$value{
  203.             if (is_array($array[$index])) {
  204.                 sqstripslashes($array[$index]);
  205.             }
  206.             else {
  207.                 $array[$indexstripslashes($value);
  208.             }
  209.         }
  210.     }
  211. }
  212.  
  213. /**
  214.  * Add a variable to the session.
  215.  * @param mixed $var the variable to register
  216.  * @param string $name the name to refer to this variable
  217.  * @return void 
  218.  */
  219. function sqsession_register ($var$name{
  220.  
  221.  
  222.     if !check_php_version(4,1) ) {
  223.         global $HTTP_SESSION_VARS;
  224.         $HTTP_SESSION_VARS[$name$var;
  225.     else {
  226.         $_SESSION["$name"$var;
  227.     }
  228.     session_register("$name");
  229. }
  230.  
  231. /**
  232.  * Delete a variable from the session.
  233.  * @param string $name the name of the var to delete
  234.  * @return void 
  235.  */
  236. function sqsession_unregister ($name{
  237.  
  238.  
  239.     if !check_php_version(4,1) ) {
  240.         global $HTTP_SESSION_VARS;
  241.         unset($HTTP_SESSION_VARS[$name]);
  242.     else {
  243.         unset($_SESSION[$name]);
  244.     }
  245.     session_unregister("$name");
  246. }
  247.  
  248. /**
  249.  * Checks to see if a variable has already been registered
  250.  * in the session.
  251.  * @param string $name the name of the var to check
  252.  * @return bool whether the var has been registered
  253.  */
  254. function sqsession_is_registered ($name{
  255.     $test_name &$name;
  256.     $result false;
  257.     if !check_php_version(4,1) ) {
  258.         global $HTTP_SESSION_VARS;
  259.         if (isset($HTTP_SESSION_VARS[$test_name])) {
  260.             $result true;
  261.         }
  262.     else {
  263.         if (isset($_SESSION[$test_name])) {
  264.             $result true;
  265.         }
  266.     }
  267.  
  268.     return $result;
  269. }
  270.  
  271. /**
  272.  * Search for the var $name in $_SESSION, $_POST, $_GET,
  273.  * $_COOKIE, or $_SERVER and set it in provided var.
  274.  *
  275.  * If $search is not provided,  or == SQ_INORDER, it will search
  276.  * $_SESSION, then $_POST, then $_GET. Otherwise,
  277.  * use one of the defined constants to look for
  278.  * a var in one place specifically.
  279.  *
  280.  * Note: $search is an int value equal to one of the
  281.  * constants defined above.
  282.  *
  283.  * example:
  284.  *    sqgetGlobalVar('username',$username,SQ_SESSION);
  285.  *  -- no quotes around last param!
  286.  *
  287.  * @param string name the name of the var to search
  288.  * @param mixed value the variable to return
  289.  * @param int search constant defining where to look
  290.  * @return bool whether variable is found.
  291.  */
  292. function sqgetGlobalVar($name&$value$search SQ_INORDER{
  293.  
  294.     if !check_php_version(4,1) ) {
  295.         global $HTTP_COOKIE_VARS$HTTP_GET_VARS$HTTP_POST_VARS,
  296.                $HTTP_SERVER_VARS$HTTP_SESSION_VARS;
  297.  
  298.         $_COOKIE  =$HTTP_COOKIE_VARS;
  299.         $_GET     =$HTTP_GET_VARS;
  300.         $_POST    =$HTTP_POST_VARS;
  301.         $_SERVER  =$HTTP_SERVER_VARS;
  302.         $_SESSION =$HTTP_SESSION_VARS;
  303.     }
  304.  
  305.     /* NOTE: DO NOT enclose the constants in the switch
  306.        statement with quotes. They are constant values,
  307.        enclosing them in quotes will cause them to evaluate
  308.        as strings. */
  309.     switch ($search{
  310.         /* we want the default case to be first here,
  311.            so that if a valid value isn't specified,
  312.            all three arrays will be searched. */
  313.       default:
  314.       case SQ_INORDER// check session, post, get
  315.       case SQ_SESSION:
  316.         ifisset($_SESSION[$name]) ) {
  317.             $value $_SESSION[$name];
  318.             return TRUE;
  319.         elseif $search == SQ_SESSION {
  320.             break;
  321.         }
  322.       case SQ_FORM:   // check post, get
  323.       case SQ_POST:
  324.         ifisset($_POST[$name]) ) {
  325.             $value $_POST[$name];
  326.             return TRUE;
  327.         elseif $search == SQ_POST {
  328.           break;
  329.         }
  330.       case SQ_GET:
  331.         if isset($_GET[$name]) ) {
  332.             $value $_GET[$name];
  333.             return TRUE;
  334.         }
  335.         /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  336.         break;
  337.       case SQ_COOKIE:
  338.         if isset($_COOKIE[$name]) ) {
  339.             $value $_COOKIE[$name];
  340.             return TRUE;
  341.         }
  342.         break;
  343.       case SQ_SERVER:
  344.         if isset($_SERVER[$name]) ) {
  345.             $value $_SERVER[$name];
  346.             return TRUE;
  347.         }
  348.         break;
  349.     }
  350.     /* if not found, return false */
  351.     return FALSE;
  352. }
  353.  
  354. /**
  355.  * Deletes an existing session, more advanced than the standard PHP
  356.  * session_destroy(), it explicitly deletes the cookies and global vars.
  357.  */
  358. function sqsession_destroy({
  359.  
  360.     /*
  361.      * php.net says we can kill the cookie by setting just the name:
  362.      * http://www.php.net/manual/en/function.setcookie.php
  363.      * maybe this will help fix the session merging again.
  364.      *
  365.      * Changed the theory on this to kill the cookies first starting
  366.      * a new session will provide a new session for all instances of
  367.      * the browser, we don't want that, as that is what is causing the
  368.      * merging of sessions.
  369.      */
  370.  
  371.     global $base_uri;
  372.  
  373.     if (isset($_COOKIE[session_name()])) setcookie(session_name()''0$base_uri);
  374.     if (isset($_COOKIE['username'])) setcookie('username'''0$base_uri);
  375.     if (isset($_COOKIE['key'])) setcookie('key'''0$base_uri);
  376.  
  377.     $sessid session_id();
  378.     if (!empty$sessid )) {
  379.         if !check_php_version(4,1) ) {
  380.             global $HTTP_SESSION_VARS;
  381.             $HTTP_SESSION_VARS array();
  382.         else {
  383.             $_SESSION array();
  384.         }
  385.         @session_destroy();
  386.     }
  387.  
  388. }
  389.  
  390. /**
  391.  * Function to verify a session has been started.  If it hasn't
  392.  * start a session up.  php.net doesn't tell you that $_SESSION
  393.  * (even though autoglobal), is not created unless a session is
  394.  * started, unlike $_POST, $_GET and such
  395.  */
  396.  
  397. function sqsession_is_active({
  398.  
  399.     $sessid session_id();
  400.     if empty$sessid ) ) {
  401.         session_start();
  402.     }
  403. }
  404.  
  405. // vim: et ts=4
  406. ?>

Documentation generated on Sat, 07 Oct 2006 16:31:26 +0300 by phpDocumentor 1.3.0RC6