global.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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-2007 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. * Squelch error output to screen (only) for the given function.
  80. * If the SquirrelMail debug mode SM_DEBUG_MODE_ADVANCED is not
  81. * enabled, error output will not go to the log, either.
  82. *
  83. * This provides an alternative to the @ error-suppression
  84. * operator where errors will not be shown in the interface
  85. * but will show up in the server log file (assuming the
  86. * administrator has configured PHP logging).
  87. *
  88. * @since 1.4.12 and 1.5.2
  89. *
  90. * @param string $function The function to be executed
  91. * @param array $args The arguments to be passed to the function
  92. * (OPTIONAL; default no arguments)
  93. * NOTE: The caller must take extra action if
  94. * the function being called is supposed
  95. * to use any of the parameters by
  96. * reference. In the following example,
  97. * $x is passed by reference and $y is
  98. * passed by value to the "my_func"
  99. * function.
  100. * sq_call_function_suppress_errors('my_func', array(&$x, $y));
  101. *
  102. * @return mixed The return value, if any, of the function being
  103. * executed will be returned.
  104. *
  105. */
  106. function sq_call_function_suppress_errors($function, $args=NULL) {
  107. global $sm_debug_mode;
  108. $display_errors = ini_get('display_errors');
  109. ini_set('display_errors', '0');
  110. // if advanced debug mode isn't enabled, don't log the error, either
  111. //
  112. if (!($sm_debug_mode & SM_DEBUG_MODE_ADVANCED))
  113. $error_reporting = error_reporting(0);
  114. $ret = call_user_func_array($function, $args);
  115. if (!($sm_debug_mode & SM_DEBUG_MODE_ADVANCED))
  116. error_reporting($error_reporting);
  117. ini_set('display_errors', $display_errors);
  118. return $ret;
  119. }
  120. /**
  121. * Add a variable to the session.
  122. * @param mixed $var the variable to register
  123. * @param string $name the name to refer to this variable
  124. * @return void
  125. */
  126. function sqsession_register ($var, $name) {
  127. sqsession_is_active();
  128. $_SESSION[$name] = $var;
  129. }
  130. /**
  131. * Delete a variable from the session.
  132. * @param string $name the name of the var to delete
  133. * @return void
  134. */
  135. function sqsession_unregister ($name) {
  136. sqsession_is_active();
  137. unset($_SESSION[$name]);
  138. session_unregister("$name");
  139. }
  140. /**
  141. * Checks to see if a variable has already been registered
  142. * in the session.
  143. * @param string $name the name of the var to check
  144. * @return bool whether the var has been registered
  145. */
  146. function sqsession_is_registered ($name) {
  147. $test_name = &$name;
  148. $result = false;
  149. if (isset($_SESSION[$test_name])) {
  150. $result = true;
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Retrieves a form variable, from a set of possible similarly named
  156. * form variables, based on finding a different, single field. This
  157. * is intended to allow more than one same-named inputs in a single
  158. * <form>, where the submit button that is clicked tells us which
  159. * input we should retrieve. An example is if we have:
  160. * <select name="startMessage_1">
  161. * <select name="startMessage_2">
  162. * <input type="submit" name="form_submit_1" />
  163. * <input type="submit" name="form_submit_2" />
  164. * and we want to know which one of the select inputs should be
  165. * returned as $startMessage (without the suffix!), this function
  166. * decides by looking for either "form_submit_1" or "form_submit_2"
  167. * (both should not appear). In this example, $name should be
  168. * "startMessage" and $indicator_field should be "form_submit".
  169. *
  170. * NOTE that form widgets must be named with the suffix "_1", "_2", "_3"
  171. * and so on, or this function will not work.
  172. *
  173. * If more than one of the indicator fields is found, the first one
  174. * (numerically) will win.
  175. *
  176. * If an indicator field is found without a matching input ($name)
  177. * field, FALSE is returned.
  178. *
  179. * If no indicator fields are found, a field of $name *without* any
  180. * suffix is searched for (but only if $fallback_no_suffix is TRUE),
  181. * and if not found, FALSE is ultimately returned.
  182. *
  183. * It should also be possible to use the same string for both
  184. * $name and $indicator_field to look for the first possible
  185. * widget with a suffix that can be found (and possibly fallback
  186. * to a widget without a suffix).
  187. *
  188. * @param string name the name of the var to search
  189. * @param mixed value the variable to return
  190. * @param string indicator_field the name of the field upon which to base
  191. * our decision upon (see above)
  192. * @param int search constant defining where to look
  193. * @param bool fallback_no_suffix whether or not to look for $name with
  194. * no suffix when nothing else is found
  195. * @param mixed default the value to assign to $value when nothing is found
  196. * @param int typecast force variable to be cast to given type (please
  197. * use SQ_TYPE_XXX constants or set to FALSE (default)
  198. * to leave variable type unmolested)
  199. *
  200. * @return bool whether variable is found.
  201. */
  202. function sqGetGlobalVarMultiple($name, &$value, $indicator_field,
  203. $search = SQ_INORDER,
  204. $fallback_no_suffix=TRUE, $default=NULL,
  205. $typecast=FALSE) {
  206. // Set arbitrary max limit -- should be much lower except on the
  207. // search results page, if there are many (50 or more?) mailboxes
  208. // shown, this may not be high enough. Is there some way we should
  209. // automate this value?
  210. //
  211. $max_form_search = 100;
  212. for ($i = 1; $i <= $max_form_search; $i++) {
  213. if (sqGetGlobalVar($indicator_field . '_' . $i, $temp, $search)) {
  214. return sqGetGlobalVar($name . '_' . $i, $value, $search, $default, $typecast);
  215. }
  216. }
  217. // no indicator field found; just try without suffix if allowed
  218. //
  219. if ($fallback_no_suffix) {
  220. return sqGetGlobalVar($name, $value, $search, $default, $typecast);
  221. }
  222. // no dice, set default and return FALSE
  223. //
  224. if (!is_null($default)) {
  225. $value = $default;
  226. }
  227. return FALSE;
  228. }
  229. /**
  230. * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
  231. * and set it in provided var.
  232. *
  233. * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
  234. * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
  235. * $_GET. Otherwise, use one of the defined constants to look for a var in one
  236. * place specifically.
  237. *
  238. * Note: $search is an int value equal to one of the constants defined above.
  239. *
  240. * Example:
  241. * sqgetGlobalVar('username',$username,SQ_SESSION);
  242. * // No quotes around last param, it's a constant - not a string!
  243. *
  244. * @param string name the name of the var to search
  245. * @param mixed value the variable to return
  246. * @param int search constant defining where to look
  247. * @param mixed default the value to assign to $value when nothing is found
  248. * @param int typecast force variable to be cast to given type (please
  249. * use SQ_TYPE_XXX constants or set to FALSE (default)
  250. * to leave variable type unmolested)
  251. *
  252. * @return bool whether variable is found.
  253. */
  254. function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
  255. $result = false;
  256. switch ($search) {
  257. /* we want the default case to be first here,
  258. so that if a valid value isn't specified,
  259. all three arrays will be searched. */
  260. default:
  261. case SQ_INORDER: // check session, post, get
  262. case SQ_SESSION:
  263. if( isset($_SESSION[$name]) ) {
  264. $value = $_SESSION[$name];
  265. $result = TRUE;
  266. break;
  267. } elseif ( $search == SQ_SESSION ) {
  268. break;
  269. }
  270. case SQ_FORM: // check post, get
  271. case SQ_POST:
  272. if( isset($_POST[$name]) ) {
  273. $value = $_POST[$name];
  274. $result = TRUE;
  275. break;
  276. } elseif ( $search == SQ_POST ) {
  277. break;
  278. }
  279. case SQ_GET:
  280. if ( isset($_GET[$name]) ) {
  281. $value = $_GET[$name];
  282. $result = TRUE;
  283. break;
  284. }
  285. /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
  286. break;
  287. case SQ_COOKIE:
  288. if ( isset($_COOKIE[$name]) ) {
  289. $value = $_COOKIE[$name];
  290. $result = TRUE;
  291. break;
  292. }
  293. break;
  294. case SQ_SERVER:
  295. if ( isset($_SERVER[$name]) ) {
  296. $value = $_SERVER[$name];
  297. $result = TRUE;
  298. break;
  299. }
  300. break;
  301. }
  302. if ($result && $typecast) {
  303. switch ($typecast) {
  304. case SQ_TYPE_INT: $value = (int) $value; break;
  305. case SQ_TYPE_STRING: $value = (string) $value; break;
  306. case SQ_TYPE_BOOL: $value = (bool) $value; break;
  307. default: break;
  308. }
  309. } else if (!$result && !is_null($default)) {
  310. $value = $default;
  311. }
  312. return $result;
  313. }
  314. /**
  315. * Get an immutable copy of a configuration variable if SquirrelMail
  316. * is in "secured configuration" mode. This guarantees the caller
  317. * gets a copy of the requested value as it is set in the main
  318. * application configuration (including config_local overrides), and
  319. * not what it might be after possibly having been modified by some
  320. * other code (usually a plugin overriding configuration values for
  321. * one reason or another).
  322. *
  323. * WARNING: Please use this function as little as possible, because
  324. * every time it is called, it forcibly reloads the main configuration
  325. * file(s).
  326. *
  327. * Caller beware that this function will do nothing if SquirrelMail
  328. * is not in "secured configuration" mode per the $secured_config
  329. * setting.
  330. *
  331. * @param string $var_name The name of the desired variable
  332. *
  333. * @return mixed The desired value
  334. *
  335. * @since 1.5.2
  336. *
  337. */
  338. function get_secured_config_value($var_name) {
  339. static $return_values = array();
  340. // if we can avoid it, return values that have
  341. // already been retrieved (so we don't have to
  342. // include the config file yet again)
  343. //
  344. if (isset($return_values[$var_name])) {
  345. return $return_values[$var_name];
  346. }
  347. // load site configuration
  348. //
  349. require(SM_PATH . 'config/config.php');
  350. // load local configuration overrides
  351. //
  352. if (file_exists(SM_PATH . 'config/config_local.php')) {
  353. require(SM_PATH . 'config/config_local.php');
  354. }
  355. // if SM isn't in "secured configuration" mode,
  356. // just return the desired value from the global scope
  357. //
  358. if (!$secured_config) {
  359. global $$var_name;
  360. $return_values[$var_name] = $$var_name;
  361. return $$var_name;
  362. }
  363. // else we return what we got from the config file
  364. //
  365. $return_values[$var_name] = $$var_name;
  366. return $$var_name;
  367. }
  368. /**
  369. * Deletes an existing session, more advanced than the standard PHP
  370. * session_destroy(), it explicitly deletes the cookies and global vars.
  371. *
  372. * WARNING: Older PHP versions have some issues with session management.
  373. * See http://bugs.php.net/11643 (warning, spammed bug tracker) and
  374. * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
  375. * 4.2.3 and maybe other versions. If you restart session after session
  376. * is destroyed, affected PHP versions produce PHP notice. Bug should
  377. * be fixed only in 4.3.0
  378. */
  379. function sqsession_destroy() {
  380. /*
  381. * php.net says we can kill the cookie by setting just the name:
  382. * http://www.php.net/manual/en/function.setcookie.php
  383. * maybe this will help fix the session merging again.
  384. *
  385. * Changed the theory on this to kill the cookies first starting
  386. * a new session will provide a new session for all instances of
  387. * the browser, we don't want that, as that is what is causing the
  388. * merging of sessions.
  389. */
  390. global $base_uri, $_COOKIE, $_SESSION;
  391. if (isset($_COOKIE[session_name()]) && session_name()) sqsetcookie(session_name(), $_COOKIE[session_name()], 1, $base_uri);
  392. if (isset($_COOKIE['key']) && $_COOKIE['key']) sqsetcookie('key','SQMTRASH',1,$base_uri);
  393. $sessid = session_id();
  394. if (!empty( $sessid )) {
  395. $_SESSION = array();
  396. @session_destroy();
  397. }
  398. }
  399. /**
  400. * Function to verify a session has been started. If it hasn't
  401. * start a session up. php.net doesn't tell you that $_SESSION
  402. * (even though autoglobal), is not created unless a session is
  403. * started, unlike $_POST, $_GET and such
  404. * Update: (see #1685031) the session ID is left over after the
  405. * session is closed in some PHP setups; this function just becomes
  406. * a passthru to sqsession_start(), but leaving old code in for
  407. * edification.
  408. */
  409. function sqsession_is_active() {
  410. //$sessid = session_id();
  411. //if ( empty( $sessid ) ) {
  412. sqsession_start();
  413. //}
  414. }
  415. /**
  416. * Function to start the session and store the cookie with the session_id as
  417. * HttpOnly cookie which means that the cookie isn't accessible by javascript
  418. * (IE6 only)
  419. * Note that as sqsession_is_active() no longer discriminates as to when
  420. * it calls this function, session_start() has to have E_NOTICE suppression
  421. * (thus the @ sign).
  422. */
  423. function sqsession_start() {
  424. global $base_uri;
  425. sq_call_function_suppress_errors('session_start');
  426. // was: @session_start();
  427. $session_id = session_id();
  428. // session_starts sets the sessionid cookie but without the httponly var
  429. // setting the cookie again sets the httponly cookie attribute
  430. //
  431. // need to check if headers have been sent, since sqsession_is_active()
  432. // has become just a passthru to this function, so the sqsetcookie()
  433. // below is called every time, even after headers have already been sent
  434. //
  435. if (!headers_sent())
  436. sqsetcookie(session_name(),$session_id,false,$base_uri);
  437. }
  438. /**
  439. * Set a cookie
  440. *
  441. * @param string $sName The name of the cookie.
  442. * @param string $sValue The value of the cookie.
  443. * @param int $iExpire The time the cookie expires. This is a Unix
  444. * timestamp so is in number of seconds since
  445. * the epoch.
  446. * @param string $sPath The path on the server in which the cookie
  447. * will be available on.
  448. * @param string $sDomain The domain that the cookie is available.
  449. * @param boolean $bSecure Indicates that the cookie should only be
  450. * transmitted over a secure HTTPS connection.
  451. * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
  452. * @param boolean $bReplace Replace previous cookies with same name?
  453. *
  454. * @return void
  455. *
  456. * @since 1.4.16 and 1.5.1
  457. *
  458. */
  459. function sqsetcookie($sName, $sValue='deleted', $iExpire=0, $sPath="", $sDomain="",
  460. $bSecure=false, $bHttpOnly=true, $bReplace=false) {
  461. // if we have a secure connection then limit the cookies to https only.
  462. global $is_secure_connection;
  463. if ($sName && $is_secure_connection)
  464. $bSecure = true;
  465. // admin config can override the restriction of secure-only cookies
  466. global $only_secure_cookies;
  467. if (!$only_secure_cookies)
  468. $bSecure = false;
  469. if (false && check_php_version(5,2)) {
  470. // php 5 supports the httponly attribute in setcookie, but because setcookie seems a bit
  471. // broken we use the header function for php 5.2 as well. We might change that later.
  472. //setcookie($sName,$sValue,(int) $iExpire,$sPath,$sDomain,$bSecure,$bHttpOnly);
  473. } else {
  474. if (!empty($sDomain)) {
  475. // Fix the domain to accept domains with and without 'www.'.
  476. if (strtolower(substr($sDomain, 0, 4)) == 'www.') $sDomain = substr($sDomain, 4);
  477. $sDomain = '.' . $sDomain;
  478. // Remove port information.
  479. $Port = strpos($sDomain, ':');
  480. if ($Port !== false) $sDomain = substr($sDomain, 0, $Port);
  481. }
  482. if (!$sValue) $sValue = 'deleted';
  483. header('Set-Cookie: ' . rawurlencode($sName) . '=' . rawurlencode($sValue)
  484. . (empty($iExpire) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', $iExpire) . ' GMT')
  485. . (empty($sPath) ? '' : '; path=' . $sPath)
  486. . (empty($sDomain) ? '' : '; domain=' . $sDomain)
  487. . (!$bSecure ? '' : '; secure')
  488. . (!$bHttpOnly ? '' : '; HttpOnly'), $bReplace);
  489. }
  490. }
  491. /**
  492. * session_regenerate_id replacement for PHP < 4.3.2
  493. *
  494. * This code is borrowed from Gallery, session.php version 1.53.2.1
  495. */
  496. if (!function_exists('session_regenerate_id')) {
  497. function php_combined_lcg() {
  498. $tv = gettimeofday();
  499. $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
  500. $lcg['s2'] = mt_rand();
  501. $q = (int) ($lcg['s1'] / 53668);
  502. $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
  503. if ($lcg['s1'] < 0) {
  504. $lcg['s1'] += 2147483563;
  505. }
  506. $q = (int) ($lcg['s2'] / 52774);
  507. $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
  508. if ($lcg['s2'] < 0) {
  509. $lcg['s2'] += 2147483399;
  510. }
  511. $z = (int) ($lcg['s1'] - $lcg['s2']);
  512. if ($z < 1) {
  513. $z += 2147483562;
  514. }
  515. return $z * 4.656613e-10;
  516. }
  517. function session_regenerate_id() {
  518. global $base_uri;
  519. $tv = gettimeofday();
  520. sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
  521. $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
  522. session_id(md5($buf));
  523. if (ini_get('session.use_cookies')) {
  524. sqsetcookie(session_name(), session_id(), 0, $base_uri);
  525. }
  526. return TRUE;
  527. }
  528. }
  529. /**
  530. * php_self
  531. *
  532. * Creates an URL for the page calling this function, using either the PHP global
  533. * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
  534. * function was stored in function/strings.php.
  535. *
  536. * @return string the complete url for this page
  537. * @since 1.2.3
  538. */
  539. function php_self () {
  540. if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
  541. return $req_uri;
  542. }
  543. if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
  544. // need to add query string to end of PHP_SELF to match REQUEST_URI
  545. //
  546. if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
  547. $php_self .= '?' . $query_string;
  548. }
  549. return $php_self;
  550. }
  551. return '';
  552. }
  553. /**
  554. * Print variable
  555. *
  556. * sm_print_r($some_variable, [$some_other_variable [, ...]]);
  557. *
  558. * Debugging function - does the same as print_r, but makes sure special
  559. * characters are converted to htmlentities first. This will allow
  560. * values like <some@email.address> to be displayed.
  561. * The output is wrapped in <<pre>> and <</pre>> tags.
  562. * Since 1.4.2 accepts unlimited number of arguments.
  563. * @since 1.4.1
  564. * @return void
  565. */
  566. function sm_print_r() {
  567. ob_start(); // Buffer output
  568. foreach(func_get_args() as $var) {
  569. print_r($var);
  570. echo "\n";
  571. // php has get_class_methods function that can print class methods
  572. if (is_object($var)) {
  573. // get class methods if $var is object
  574. $aMethods=get_class_methods(get_class($var));
  575. // make sure that $aMethods is array and array is not empty
  576. if (is_array($aMethods) && $aMethods!=array()) {
  577. echo "Object methods:\n";
  578. foreach($aMethods as $method) {
  579. echo '* ' . $method . "\n";
  580. }
  581. }
  582. echo "\n";
  583. }
  584. }
  585. $buffer = ob_get_contents(); // Grab the print_r output
  586. ob_end_clean(); // Silently discard the output & stop buffering
  587. print '<div align="left"><pre>';
  588. print htmlentities($buffer);
  589. print '</pre></div>';
  590. }
  591. /**
  592. * Sanitize a value using htmlspecialchars() or similar, but also
  593. * recursively run htmlspecialchars() (or similar) on array keys
  594. * and values.
  595. *
  596. * If $value is not a string or an array with strings in it,
  597. * the value is returned as is.
  598. *
  599. * @param mixed $value The value to be sanitized.
  600. * @param mixed $quote_style Either boolean or an integer. If it
  601. * is an integer, it must be the PHP
  602. * constant indicating if/how to escape
  603. * quotes: ENT_QUOTES, ENT_COMPAT, or
  604. * ENT_NOQUOTES. If it is a boolean value,
  605. * it must be TRUE and thus indicates
  606. * that the only sanitizing to be done
  607. * herein is to replace single and double
  608. * quotes with &#039; and &quot;, no other
  609. * changes are made to $value. If it is
  610. * boolean and FALSE, behavior reverts
  611. * to same as if the value was ENT_QUOTES
  612. * (OPTIONAL; default is ENT_QUOTES).
  613. *
  614. * @return mixed The sanitized value.
  615. *
  616. * @since 1.5.2
  617. *
  618. **/
  619. function sq_htmlspecialchars($value, $quote_style=ENT_QUOTES) {
  620. if ($quote_style === FALSE) $quote_style = ENT_QUOTES;
  621. // array? go recursive...
  622. //
  623. if (is_array($value)) {
  624. $return_array = array();
  625. foreach ($value as $key => $val) {
  626. $return_array[sq_htmlspecialchars($key, $quote_style)]
  627. = sq_htmlspecialchars($val, $quote_style);
  628. }
  629. return $return_array;
  630. // sanitize strings only
  631. //
  632. } else if (is_string($value)) {
  633. if ($quote_style === TRUE)
  634. return str_replace(array('\'', '"'), array('&#039;', '&quot;'), $value);
  635. else
  636. return htmlspecialchars($value, $quote_style);
  637. }
  638. // anything else gets returned with no changes
  639. //
  640. return $value;
  641. }
  642. /**
  643. * Detect whether or not we have a SSL secured (HTTPS) connection
  644. * connection to the browser
  645. *
  646. * It is thought to be so if you have 'SSLOptions +StdEnvVars'
  647. * in your Apache configuration,
  648. * OR if you have HTTPS set to a non-empty value (except "off")
  649. * in your HTTP_SERVER_VARS,
  650. * OR if you have HTTP_X_FORWARDED_PROTO=https in your HTTP_SERVER_VARS,
  651. * OR if you are on port 443.
  652. *
  653. * Note: HTTP_X_FORWARDED_PROTO could be sent from the client and
  654. * therefore possibly spoofed/hackable. Thus, SquirrelMail
  655. * ignores such headers by default. The administrator
  656. * can tell SM to use such header values by setting
  657. * $sq_ignore_http_x_forwarded_headers to boolean FALSE
  658. * in config/config.php or by using config/conf.pl.
  659. *
  660. * Note: It is possible to run SSL on a port other than 443, and
  661. * if that is the case, the administrator should set
  662. * $sq_https_port in config/config.php or by using config/conf.pl.
  663. *
  664. * @return boolean TRUE if the current connection is SSL-encrypted;
  665. * FALSE otherwise.
  666. *
  667. * @since 1.4.17 and 1.5.2
  668. *
  669. */
  670. function is_ssl_secured_connection()
  671. {
  672. global $sq_ignore_http_x_forwarded_headers, $sq_https_port;
  673. $https_env_var = getenv('HTTPS');
  674. if ($sq_ignore_http_x_forwarded_headers
  675. || !sqgetGlobalVar('HTTP_X_FORWARDED_PROTO', $forwarded_proto, SQ_SERVER))
  676. $forwarded_proto = '';
  677. if (empty($sq_https_port)) // won't work with port 0 (zero)
  678. $sq_https_port = 443;
  679. if ((isset($https_env_var) && strcasecmp($https_env_var, 'on') === 0)
  680. || (sqgetGlobalVar('HTTPS', $https, SQ_SERVER) && !empty($https)
  681. && strcasecmp($https, 'off') !== 0)
  682. || (strcasecmp($forwarded_proto, 'https') === 0)
  683. || (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)
  684. && $server_port == $sq_https_port))
  685. return TRUE;
  686. return FALSE;
  687. }