Source for file global.php

Documentation is available at global.php

  1. <?php
  2.  
  3. /**
  4.  * global.php
  5.  *
  6.  * @copyright 1999-2010 The SquirrelMail Project Team
  7.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8.  * @version $Id: global.php 13896 2010-01-27 23:35:26Z pdontthink $
  9.  * @package squirrelmail
  10.  */
  11.  
  12. /**
  13.  * Set constants
  14.  */
  15. define('SQ_INORDER',0);
  16. define('SQ_GET',1);
  17. define('SQ_POST',2);
  18. define('SQ_SESSION',3);
  19. define('SQ_COOKIE',4);
  20. define('SQ_SERVER',5);
  21. define('SQ_FORM',6);
  22.  
  23. /** First code that should be executed before other files are loaded */
  24.  
  25. /**
  26.  * Must be executed before any other scripts are loaded.
  27.  *
  28.  * If register_globals are on, unregister globals.
  29.  * Second test covers boolean set as string (php_value register_globals off).
  30.  */
  31. if ((bool) ini_get('register_globals'&&
  32.     strtolower(ini_get('register_globals'))!='off'{
  33.     /**
  34.      * Remove all globals that are not reserved by PHP
  35.      * 'value' and 'key' are used by foreach. Don't unset them inside foreach.
  36.      */
  37.     foreach ($GLOBALS as $key => $value{
  38.         switch($key{
  39.         case 'HTTP_POST_VARS':
  40.         case '_POST':
  41.         case 'HTTP_GET_VARS':
  42.         case '_GET':
  43.         case 'HTTP_COOKIE_VARS':
  44.         case '_COOKIE':
  45.         case 'HTTP_SERVER_VARS':
  46.         case '_SERVER':
  47.         case 'HTTP_ENV_VARS':
  48.         case '_ENV':
  49.         case 'HTTP_POST_FILES':
  50.         case '_FILES':
  51.         case '_REQUEST':
  52.         case 'HTTP_SESSION_VARS':
  53.         case '_SESSION':
  54.         case 'GLOBALS':
  55.         case 'key':
  56.         case 'value':
  57.             break;
  58.         default:
  59.             unset($GLOBALS[$key]);
  60.         }
  61.     }
  62.     // Unset variables used in foreach
  63.     unset($GLOBALS['key']);
  64.     unset($GLOBALS['value']);
  65. }
  66.  
  67. /**
  68.  * There are some PHP settings that SquirrelMail is incompatible with
  69.  * and cannot be changed by software at run-time; refuse to run if such
  70.  * settings are being used...
  71.  */
  72. $php_session_auto_start ini_get('session.auto_start');
  73. if ((bool)$php_session_auto_start && $php_session_auto_start != 'off'{
  74.     die('SquirrelMail 1.4.x is not compatible with PHP\'s session.auto_start setting.  Please disable it at least for the location where SquirrelMail is installed.');
  75. }
  76.  
  77. /**
  78.  * Strip any tags added to the url from PHP_SELF.
  79.  * This fixes hand crafted url XXS expoits for any
  80.  * page that uses PHP_SELF as the FORM action.
  81.  * Must be executed before strings.php is loaded (php_self() call in strings.php).
  82.  * Update: strip_tags() won't catch something like
  83.  * src/right_main.php?sort=0&startMessage=1&mailbox=INBOX&xxx="><script>window.open("http://example.com")</script>
  84.  * or
  85.  * contrib/decrypt_headers.php/%22%20onmouseover=%22alert(%27hello%20world%27)%22%3E
  86.  * because it doesn't bother with broken tags.
  87.  * htmlspecialchars() is the preferred method.
  88.  */
  89. if (isset($_SERVER['PHP_SELF'])) {
  90.     $_SERVER['PHP_SELF'htmlspecialchars($_SERVER['PHP_SELF']);
  91. }
  92. /*
  93.  * same needed for QUERY_STRING because SquirrelMail
  94.  * uses it along with PHP_SELF when using location
  95.  * strings
  96.  */
  97. if (isset($_SERVER['QUERY_STRING'])) {
  98.     $_SERVER['QUERY_STRING'htmlspecialchars($_SERVER['QUERY_STRING']);
  99. }
  100. /*
  101.  * same needed for REQUEST_URI because it's used in php_self()
  102.  */
  103. if (isset($_SERVER['REQUEST_URI'])) {
  104.     $_SERVER['REQUEST_URI'htmlspecialchars($_SERVER['REQUEST_URI']);
  105. }
  106.  
  107. /**
  108.  * Bring in the config file
  109.  * We need $session_name
  110.  * config.php $version depends on strings.php.
  111.  * strings.php sets $PHP_SELF.
  112.  */
  113. require_once(SM_PATH 'functions/strings.php');
  114. require_once(SM_PATH 'config/config.php');
  115.  
  116. /**
  117.  * Detect SSL connections
  118.  */
  119. $is_secure_connection is_ssl_secured_connection();
  120.  
  121. /** set the name of the session cookie */
  122. if(isset($session_name&& $session_name{
  123.     ini_set('session.name' $session_name);
  124. else {
  125.     ini_set('session.name' 'SQMSESSID');
  126. }
  127.  
  128. /**
  129.  * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
  130.  * Force magic_quotes_runtime off.
  131.  * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
  132.  * If there's a better place, please let me know.
  133.  */
  134. ini_set('magic_quotes_runtime','0');
  135.  
  136. /**
  137.  * [#1518885] session.use_cookies = off breaks SquirrelMail
  138.  *
  139.  * When session cookies are not used, all http redirects, meta refreshes,
  140.  * src/download.php and javascript URLs are broken. Setting must be set
  141.  * before session is started.
  142.  */
  143. if (!(bool)ini_get('session.use_cookies'||
  144.     ini_get('session.use_cookies'== 'off'{
  145.     ini_set('session.use_cookies','1');
  146. }
  147.  
  148. /**
  149.  * Make sure to have $base_uri always initialized to avoid having session
  150.  * cookie set separately for each $base_uri subdirectory that receives direct
  151.  * requests from user's browser (typically $base_uri and $base_uri/src).
  152.  */
  153. $base_uri sqm_baseuri();
  154.  
  155.  
  156. /* if running with magic_quotes_gpc then strip the slashes
  157.    from POST and GET global arrays */
  158.  
  159. if (function_exists('get_magic_quotes_gpc'&& @get_magic_quotes_gpc()) {
  160.     sqstripslashes($_GET);
  161.     sqstripslashes($_POST);
  162. }
  163.  
  164. /**
  165.  * returns true if current php version is at mimimum a.b.c
  166.  *
  167.  * Called: check_php_version(4,1)
  168.  * @param int a major version number
  169.  * @param int b minor version number
  170.  * @param int c release number
  171.  * @return bool 
  172.  */
  173. function check_php_version ($a '0'$b '0'$c '0')
  174. {
  175.     global $SQ_PHP_VERSION;
  176.  
  177.     if(!isset($SQ_PHP_VERSION))
  178.         $SQ_PHP_VERSION substrstr_padpreg_replace('/\D/',''PHP_VERSION)3'0')03);
  179.  
  180.     return $SQ_PHP_VERSION >= ($a.$b.$c);
  181. }
  182.  
  183. /**
  184.  * returns true if the current internal SM version is at minimum a.b.c
  185.  * These are plain integer comparisons, as our internal version is
  186.  * constructed by us, as an array of 3 ints.
  187.  *
  188.  * Called: check_sm_version(1,3,3)
  189.  * @param int a major version number
  190.  * @param int b minor version number
  191.  * @param int c release number
  192.  * @return bool 
  193.  */
  194. function check_sm_version($a 0$b 0$c 0)
  195. {
  196.     global $SQM_INTERNAL_VERSION;
  197.     if !isset($SQM_INTERNAL_VERSION||
  198.          $SQM_INTERNAL_VERSION[0$a ||
  199.          $SQM_INTERNAL_VERSION[0== $a &&
  200.            $SQM_INTERNAL_VERSION[1$b||
  201.          $SQM_INTERNAL_VERSION[0== $a &&
  202.            $SQM_INTERNAL_VERSION[1== $b &&
  203.            $SQM_INTERNAL_VERSION[2$c ) ) {
  204.         return FALSE;
  205.     }
  206.     return TRUE;
  207. }
  208.  
  209.  
  210. /**
  211.  * Recursively strip slashes from the values of an array.
  212.  * @param array array the array to strip, passed by reference
  213.  * @return void 
  214.  */
  215. function sqstripslashes(&$array{
  216.     if(count($array0{
  217.         foreach ($array as $index=>$value{
  218.             if (is_array($array[$index])) {
  219.                 sqstripslashes($array[$index]);
  220.             }
  221.             else {
  222.                 $array[$indexstripslashes($value);
  223.             }
  224.         }
  225.     }
  226. }
  227.  
  228. /**
  229.  * Squelch error output to screen (only) for the given function.
  230.  *
  231.  * This provides an alternative to the @ error-suppression
  232.  * operator where errors will not be shown in the interface
  233.  * but will show up in the server log file (assuming the
  234.  * administrator has configured PHP logging).
  235.  * 
  236.  * @since 1.4.12 and 1.5.2
  237.  * 
  238.  * @param string $function The function to be executed
  239.  * @param array  $args     The arguments to be passed to the function
  240.  *                          (OPTIONAL; default no arguments)
  241.  *                          NOTE: The caller must take extra action if
  242.  *                                the function being called is supposed
  243.  *                                to use any of the parameters by
  244.  *                                reference.  In the following example,
  245.  *                                $x is passed by reference and $y is
  246.  *                                passed by value to the "my_func"
  247.  *                                function.
  248.  *  sq_call_function_suppress_errors('my_func', array(&$x, $y));
  249.  * 
  250.  * @return mixed The return value, if any, of the function being
  251.  *                executed will be returned.
  252.  * 
  253.  */ 
  254. function sq_call_function_suppress_errors($function$args=NULL{
  255.    $display_errors ini_get('display_errors');
  256.    ini_set('display_errors''0');
  257.    $ret call_user_func_array($function$args);
  258.    ini_set('display_errors'$display_errors);
  259.    return $ret;
  260. }
  261.  
  262. /**
  263.  * Add a variable to the session.
  264.  * @param mixed $var the variable to register
  265.  * @param string $name the name to refer to this variable
  266.  * @return void 
  267.  */
  268. function sqsession_register ($var$name{
  269.  
  270.  
  271.     $_SESSION[$name$var;
  272. }
  273.  
  274. /**
  275.  * Delete a variable from the session.
  276.  * @param string $name the name of the var to delete
  277.  * @return void 
  278.  */
  279. function sqsession_unregister ($name{
  280.  
  281.  
  282.     unset($_SESSION[$name]);
  283.  
  284.     // starts throwing warnings in PHP 5.3.0 and is
  285.     // removed in PHP 6 and is redundant anyway
  286.     //session_unregister($name);
  287. }
  288.  
  289. /**
  290.  * Checks to see if a variable has already been registered
  291.  * in the session.
  292.  * @param string $name the name of the var to check
  293.  * @return bool whether the var has been registered
  294.  */
  295. function sqsession_is_registered ($name{
  296.     $test_name &$name;
  297.     return isset($_SESSION[$test_name]);
  298. }
  299.  
  300. /**
  301.  * Search for the var $name in $_SESSION, $_POST, $_GET,
  302.  * $_COOKIE, or $_SERVER and set it in provided var.
  303.  *
  304.  * If $search is not provided,  or == SQ_INORDER, it will search
  305.  * $_SESSION, then $_POST, then $_GET. Otherwise,
  306.  * use one of the defined constants to look for
  307.  * a var in one place specifically.
  308.  *
  309.  * Note: $search is an int value equal to one of the
  310.  * constants defined above.
  311.  *
  312.  * example:
  313.  *    sqgetGlobalVar('username',$username,SQ_SESSION);
  314.  *  -- no quotes around last param!
  315.  *
  316.  * @param string name the name of the var to search
  317.  * @param mixed value the variable to return
  318.  * @param int search constant defining where to look
  319.  * @return bool whether variable is found.
  320.  */
  321. function sqgetGlobalVar($name&$value$search SQ_INORDER{
  322.  
  323.     /* NOTE: DO NOT enclose the constants in the switch
  324.        statement with quotes. They are constant values,
  325.        enclosing them in quotes will cause them to evaluate
  326.        as strings. */
  327.     switch ($search{
  328.         /* we want the default case to be first here,
  329.            so that if a valid value isn't specified,
  330.            all three arrays will be searched. */
  331.       default:
  332.       case SQ_INORDER// check session, post, get
  333.       case SQ_SESSION:
  334.         ifisset($_SESSION[$name]) ) {
  335.             $value $_SESSION[$name];
  336.             return TRUE;
  337.         elseif $search == SQ_SESSION {
  338.             break;
  339.         }
  340.       case SQ_FORM:   // check post, get
  341.       case SQ_POST:
  342.         ifisset($_POST[$name]) ) {
  343.             $value $_POST[$name];
  344.             return TRUE;
  345.         elseif $search == SQ_POST {
  346.           break;
  347.         }
  348.       case SQ_GET:
  349.         if isset($_GET[$name]) ) {
  350.             $value $_GET[$name];
  351.             return TRUE;
  352.         }
  353.         /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  354.         break;
  355.       case SQ_COOKIE:
  356.         if isset($_COOKIE[$name]) ) {
  357.             $value $_COOKIE[$name];
  358.             return TRUE;
  359.         }
  360.         break;
  361.       case SQ_SERVER:
  362.         if isset($_SERVER[$name]) ) {
  363.             $value $_SERVER[$name];
  364.             return TRUE;
  365.         }
  366.         break;
  367.     }
  368.     /* if not found, return false */
  369.     return FALSE;
  370. }
  371.  
  372. /**
  373.  * Deletes an existing session, more advanced than the standard PHP
  374.  * session_destroy(), it explicitly deletes the cookies and global vars.
  375.  */
  376. function sqsession_destroy({
  377.  
  378.     /*
  379.      * php.net says we can kill the cookie by setting just the name:
  380.      * http://www.php.net/manual/en/function.setcookie.php
  381.      * maybe this will help fix the session merging again.
  382.      *
  383.      * Changed the theory on this to kill the cookies first starting
  384.      * a new session will provide a new session for all instances of
  385.      * the browser, we don't want that, as that is what is causing the
  386.      * merging of sessions.
  387.      */
  388.  
  389.     global $base_uri;
  390.  
  391.     if (isset($_COOKIE[session_name()])) {
  392.         sqsetcookie(session_name()$_COOKIE[session_name()]1$base_uri);
  393.  
  394.         /*
  395.          * Make sure to kill /src and /src/ cookies, just in case there are
  396.          * some left-over or malicious ones set in user's browser.
  397.          * NB: Note that an attacker could try to plant a cookie for one
  398.          *     of the /plugins/* directories.  Such cookies can block
  399.          *     access to certain plugin pages, but they do not influence
  400.          *     or fixate the $base_uri cookie, so we don't worry about
  401.          *     trying to delete all of them here.
  402.          */
  403.         sqsetcookie(session_name()$_COOKIE[session_name()]1$base_uri 'src');
  404.         sqsetcookie(session_name()$_COOKIE[session_name()]1$base_uri 'src/');
  405.     }
  406.  
  407.     if (isset($_COOKIE['key'])) sqsetcookie('key''SQMTRASH'1$base_uri);
  408.  
  409.     /* Make sure new session id is generated on subsequent session_start() */
  410.     unset($_COOKIE[session_name()]);
  411.     unset($_GET[session_name()]);
  412.     unset($_POST[session_name()]);
  413.  
  414.     $sessid session_id();
  415.     if (!empty$sessid )) {
  416.         $_SESSION array();
  417.         @session_destroy();
  418.     }
  419.  
  420. }
  421.  
  422. /**
  423.  * Function to verify a session has been started.  If it hasn't
  424.  * start a session up.  php.net doesn't tell you that $_SESSION
  425.  * (even though autoglobal), is not created unless a session is
  426.  * started, unlike $_POST, $_GET and such
  427.  */
  428.  
  429. function sqsession_is_active({
  430.     sqsession_start();
  431. }
  432.  
  433. /**
  434.  * Function to start the session and store the cookie with the session_id as
  435.  * HttpOnly cookie which means that the cookie isn't accessible by javascript
  436.  * (IE6 only)
  437.  * Note that as sqsession_is_active() no longer discriminates as to when
  438.  * it calls this function, session_start() has to have E_NOTICE suppression
  439.  * (thus the @ sign).
  440.  *
  441.  * @return void 
  442.  *
  443.  * @since 1.4.16
  444.  *
  445.  */
  446. function sqsession_start({
  447.     global $base_uri;
  448.  
  449.     session_set_cookie_params (0$base_uri);
  450.     @session_start();
  451.     // could be: sq_call_function_suppress_errors('session_start');
  452.     $session_id session_id();
  453.  
  454.     // session_starts sets the sessionid cookie but without the httponly var
  455.     // setting the cookie again sets the httponly cookie attribute
  456.     //
  457.     // need to check if headers have been sent, since sqsession_is_active()
  458.     // has become just a passthru to this function, so the sqsetcookie()
  459.     // below is called every time, even after headers have already been sent
  460.     //
  461.     if (!headers_sent())
  462.        sqsetcookie(session_name(),$session_id,false,$base_uri);
  463. }
  464.  
  465. /**
  466.  * Set a cookie
  467.  *
  468.  * @param string  $sName     The name of the cookie.
  469.  * @param string  $sValue    The value of the cookie.
  470.  * @param int     $iExpire   The time the cookie expires. This is a Unix
  471.  *                            timestamp so is in number of seconds since
  472.  *                            the epoch.
  473.  * @param string  $sPath     The path on the server in which the cookie
  474.  *                            will be available on.
  475.  * @param string  $sDomain   The domain that the cookie is available.
  476.  * @param boolean $bSecure   Indicates that the cookie should only be
  477.  *                            transmitted over a secure HTTPS connection.
  478.  * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6/FF2)
  479.  * @param boolean $bReplace  Replace previous cookies with same name?
  480.  *
  481.  * @return void 
  482.  *
  483.  * @since 1.4.16 and 1.5.1
  484.  *
  485.  */
  486. function sqsetcookie($sName$sValue='deleted'$iExpire=0$sPath=""$sDomain="",
  487.                      $bSecure=false$bHttpOnly=true$bReplace=false{
  488.  
  489.     // if we have a secure connection then limit the cookies to https only.
  490.     global $is_secure_connection;
  491.     if ($sName && $is_secure_connection)
  492.         $bSecure true;
  493.  
  494.     // admin config can override the restriction of secure-only cookies
  495.     //
  496.     // (we have to check if the value is set and default it to true if
  497.     // not because when upgrading without re-running conf.pl, it will
  498.     // not be found in config/config.php and thusly evaluate to false,
  499.     // but we want to default people who upgrade to true due to security
  500.     // implications of setting this to false)
  501.     //
  502.     global $only_secure_cookies;
  503.     if (!isset($only_secure_cookies)) $only_secure_cookies true;
  504.     if (!$only_secure_cookies)
  505.         $bSecure false;
  506.  
  507.     if (false && check_php_version(5,2)) {
  508.        // php 5 supports the httponly attribute in setcookie, but because setcookie seems a bit
  509.        // broken we use the header function for php 5.2 as well. We might change that later.
  510.        //setcookie($sName,$sValue,(int) $iExpire,$sPath,$sDomain,$bSecure,$bHttpOnly);
  511.     else {
  512.         if (!empty($sDomain)) {
  513.             // Fix the domain to accept domains with and without 'www.'.
  514.             if (strtolower(substr($sDomain04)) == 'www.')  $sDomain substr($sDomain4);
  515.             $sDomain '.' $sDomain;
  516.  
  517.             // Remove port information.
  518.             $Port strpos($sDomain':');
  519.             if ($Port !== false)  $sDomain substr($sDomain0$Port);
  520.         }
  521.         if (!$sValue$sValue 'deleted';
  522.         header('Set-Cookie: ' rawurlencode($sName'=' rawurlencode($sValue)
  523.                             . (empty($iExpire'' '; expires=' gmdate('D, d-M-Y H:i:s'$iExpire' GMT')
  524.                             . (empty($sPath'' '; path=' $sPath)
  525.                             . (empty($sDomain'' '; domain=' $sDomain)
  526.                             . (!$bSecure '' '; secure')
  527.                             . (!$bHttpOnly '' '; HttpOnly')$bReplace);
  528.     }
  529. }
  530.  
  531. /**
  532.  * Detect whether or not we have a SSL secured (HTTPS)
  533.  * connection to the browser
  534.  *
  535.  * It is thought to be so if you have 'SSLOptions +StdEnvVars'
  536.  * in your Apache configuration,
  537.  *     OR if you have HTTPS set to a non-empty value (except "off")
  538.  *        in your HTTP_SERVER_VARS,
  539.  *     OR if you have HTTP_X_FORWARDED_PROTO=https in your HTTP_SERVER_VARS,
  540.  *     OR if you are on port 443.
  541.  *
  542.  * Note: HTTP_X_FORWARDED_PROTO could be sent from the client and
  543.  *       therefore possibly spoofed/hackable - for now, the
  544.  *       administrator can tell SM to ignore this value by setting
  545.  *       $sq_ignore_http_x_forwarded_headers to boolean TRUE in
  546.  *       config/config_local.php, but in the future we may
  547.  *       want to default this to TRUE and make administrators
  548.  *       who use proxy systems turn it off (see 1.5.2+).
  549.  *
  550.  * Note: It is possible to run SSL on a port other than 443, and
  551.  *       if that is the case, the administrator should set
  552.  *       $sq_https_port to the applicable port number in
  553.  *       config/config_local.php
  554.  *
  555.  * @return boolean TRUE if the current connection is SSL-encrypted;
  556.  *                  FALSE otherwise.
  557.  *
  558.  * @since 1.4.17 and 1.5.2
  559.  *
  560.  */
  561.     global $sq_ignore_http_x_forwarded_headers$sq_https_port;
  562.     $https_env_var getenv('HTTPS');
  563.     if ($sq_ignore_http_x_forwarded_headers
  564.      || !sqgetGlobalVar('HTTP_X_FORWARDED_PROTO'$forwarded_protoSQ_SERVER))
  565.         $forwarded_proto '';
  566.     if (empty($sq_https_port)) // won't work with port 0 (zero)
  567.        $sq_https_port 443;
  568.     if ((isset($https_env_var&& strcasecmp($https_env_var'on'=== 0)
  569.      || (sqgetGlobalVar('HTTPS'$httpsSQ_SERVER&& !empty($https)
  570.       && strcasecmp($https'off'!== 0)
  571.      || (strcasecmp($forwarded_proto'https'=== 0)
  572.      || (sqgetGlobalVar('SERVER_PORT'$server_portSQ_SERVER)
  573.       && $server_port == $sq_https_port))
  574.         return TRUE;
  575.     return FALSE;
  576. }
  577.  
  578. /**
  579.  * Determine if there are lines in a file longer than a given length
  580.  *
  581.  * @param string $filename   The full file path of the file to inspect
  582.  * @param int    $max_length If any lines in the file are GREATER THAN
  583.  *                            this number, this function returns TRUE.
  584.  *
  585.  * @return boolean TRUE as explained above, otherwise, (no long lines
  586.  *                  found) FALSE is returned.
  587.  *
  588.  */
  589. function file_has_long_lines($filename$max_length{
  590.  
  591.     $FILE @fopen($filename'rb');
  592.  
  593.     if ($FILE{
  594.         while (!feof($FILE)) {
  595.             $buffer fgets($FILE4096);
  596.             if (strlen($buffer$max_length{
  597.                 fclose($FILE);
  598.                 return TRUE;
  599.             }
  600.         }
  601.         fclose($FILE);
  602.     }
  603.  
  604.     return FALSE;
  605. }

Documentation generated on Thu, 29 Jul 2010 04:19:11 +0200 by phpDocumentor 1.4.3