Source for file global.php

Documentation is available at global.php

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

Documentation generated on 2020年1月13日 04:24:40 +0100 by phpDocumentor 1.4.3

AltStyle によって変換されたページ (->オリジナル) /