92a3705ecb343388958431594b2d30a3b895fe34
[squirrelmail.git] / functions / 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-2009 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
14 */
15
16 /**
17 */
18 define('SQ_INORDER',0);
19 define('SQ_GET',1);
20 define('SQ_POST',2);
21 define('SQ_SESSION',3);
22 define('SQ_COOKIE',4);
23 define('SQ_SERVER',5);
24 define('SQ_FORM',6);
25
26
27 /**
28 * returns true if current php version is at mimimum a.b.c
29 *
30 * Called: check_php_version(4,1)
31 * @param int a major version number
32 * @param int b minor version number
33 * @param int c release number
34 * @return bool
35 */
36 function check_php_version ($a = '0', $b = '0', $c = '0')
37 {
38 return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' );
39 }
40
41 /**
42 * returns true if the current internal SM version is at minimum a.b.c
43 * These are plain integer comparisons, as our internal version is
44 * constructed by us, as an array of 3 ints.
45 *
46 * Called: check_sm_version(1,3,3)
47 * @param int a major version number
48 * @param int b minor version number
49 * @param int c release number
50 * @return bool
51 */
52 function check_sm_version($a = 0, $b = 0, $c = 0)
53 {
54 global $SQM_INTERNAL_VERSION;
55 if ( !isset($SQM_INTERNAL_VERSION) ||
56 $SQM_INTERNAL_VERSION[0] < $a ||
57 ( $SQM_INTERNAL_VERSION[0] == $a &&
58 $SQM_INTERNAL_VERSION[1] < $b) ||
59 ( $SQM_INTERNAL_VERSION[0] == $a &&
60 $SQM_INTERNAL_VERSION[1] == $b &&
61 $SQM_INTERNAL_VERSION[2] < $c ) ) {
62 return FALSE;
63 }
64 return TRUE;
65 }
66
67
68 /**
69 * Recursively strip slashes from the values of an array.
70 * @param array array the array to strip, passed by reference
71 * @return void
72 */
73 function sqstripslashes(&$array) {
74 if(count($array) > 0) {
75 foreach ($array as $index=>$value) {
76 if (is_array($array[$index])) {
77 sqstripslashes($array[$index]);
78 }
79 else {
80 $array[$index] = stripslashes($value);
81 }
82 }
83 }
84 }
85
86 /**
87 * Squelch error output to screen (only) for the given function.
88 * If the SquirrelMail debug mode SM_DEBUG_MODE_ADVANCED is not
89 * enabled, error output will not go to the log, either.
90 *
91 * This provides an alternative to the @ error-suppression
92 * operator where errors will not be shown in the interface
93 * but will show up in the server log file (assuming the
94 * administrator has configured PHP logging).
95 *
96 * @since 1.4.12 and 1.5.2
97 *
98 * @param string $function The function to be executed
99 * @param array $args The arguments to be passed to the function
100 * (OPTIONAL; default no arguments)
101 * NOTE: The caller must take extra action if
102 * the function being called is supposed
103 * to use any of the parameters by
104 * reference. In the following example,
105 * $x is passed by reference and $y is
106 * passed by value to the "my_func"
107 * function.
108 * sq_call_function_suppress_errors('my_func', array(&$x, $y));
109 *
110 * @return mixed The return value, if any, of the function being
111 * executed will be returned.
112 *
113 */
114 function sq_call_function_suppress_errors($function, $args=NULL) {
115 global $sm_debug_mode;
116
117 $display_errors = ini_get('display_errors');
118 ini_set('display_errors', '0');
119
120 // if advanced debug mode isn't enabled, don't log the error, either
121 //
122 if (!($sm_debug_mode & SM_DEBUG_MODE_ADVANCED))
123 $error_reporting = error_reporting(0);
124
125 $ret = call_user_func_array($function, $args);
126
127 if (!($sm_debug_mode & SM_DEBUG_MODE_ADVANCED))
128 error_reporting($error_reporting);
129
130 ini_set('display_errors', $display_errors);
131 return $ret;
132 }
133
134 /**
135 * Add a variable to the session.
136 * @param mixed $var the variable to register
137 * @param string $name the name to refer to this variable
138 * @return void
139 */
140 function sqsession_register ($var, $name) {
141
142 sqsession_is_active();
143
144 $_SESSION[$name] = $var;
145 }
146
147 /**
148 * Delete a variable from the session.
149 * @param string $name the name of the var to delete
150 * @return void
151 */
152 function sqsession_unregister ($name) {
153
154 sqsession_is_active();
155
156 unset($_SESSION[$name]);
157
158 // starts throwing warnings in PHP 5.3.0 and is
159 // removed in PHP 6 and is redundant anyway
160 //session_unregister("$name");
161 }
162
163 /**
164 * Checks to see if a variable has already been registered
165 * in the session.
166 * @param string $name the name of the var to check
167 * @return bool whether the var has been registered
168 */
169 function sqsession_is_registered ($name) {
170 $test_name = &$name;
171 $result = false;
172
173 if (isset($_SESSION[$test_name])) {
174 $result = true;
175 }
176
177 return $result;
178 }
179
180
181 /**
182 * Retrieves a form variable, from a set of possible similarly named
183 * form variables, based on finding a different, single field. This
184 * is intended to allow more than one same-named inputs in a single
185 * <form>, where the submit button that is clicked tells us which
186 * input we should retrieve. An example is if we have:
187 * <select name="startMessage_1">
188 * <select name="startMessage_2">
189 * <input type="submit" name="form_submit_1" />
190 * <input type="submit" name="form_submit_2" />
191 * and we want to know which one of the select inputs should be
192 * returned as $startMessage (without the suffix!), this function
193 * decides by looking for either "form_submit_1" or "form_submit_2"
194 * (both should not appear). In this example, $name should be
195 * "startMessage" and $indicator_field should be "form_submit".
196 *
197 * NOTE that form widgets must be named with the suffix "_1", "_2", "_3"
198 * and so on, or this function will not work.
199 *
200 * If more than one of the indicator fields is found, the first one
201 * (numerically) will win.
202 *
203 * If an indicator field is found without a matching input ($name)
204 * field, FALSE is returned.
205 *
206 * If no indicator fields are found, a field of $name *without* any
207 * suffix is searched for (but only if $fallback_no_suffix is TRUE),
208 * and if not found, FALSE is ultimately returned.
209 *
210 * It should also be possible to use the same string for both
211 * $name and $indicator_field to look for the first possible
212 * widget with a suffix that can be found (and possibly fallback
213 * to a widget without a suffix).
214 *
215 * @param string name the name of the var to search
216 * @param mixed value the variable to return
217 * @param string indicator_field the name of the field upon which to base
218 * our decision upon (see above)
219 * @param int search constant defining where to look
220 * @param bool fallback_no_suffix whether or not to look for $name with
221 * no suffix when nothing else is found
222 * @param mixed default the value to assign to $value when nothing is found
223 * @param int typecast force variable to be cast to given type (please
224 * use SQ_TYPE_XXX constants or set to FALSE (default)
225 * to leave variable type unmolested)
226 *
227 * @return bool whether variable is found.
228 */
229 function sqGetGlobalVarMultiple($name, &$value, $indicator_field,
230 $search = SQ_INORDER,
231 $fallback_no_suffix=TRUE, $default=NULL,
232 $typecast=FALSE) {
233
234 // Set arbitrary max limit -- should be much lower except on the
235 // search results page, if there are many (50 or more?) mailboxes
236 // shown, this may not be high enough. Is there some way we should
237 // automate this value?
238 //
239 $max_form_search = 100;
240
241 for ($i = 1; $i <= $max_form_search; $i++) {
242 if (sqGetGlobalVar($indicator_field . '_' . $i, $temp, $search)) {
243 return sqGetGlobalVar($name . '_' . $i, $value, $search, $default, $typecast);
244 }
245 }
246
247
248 // no indicator field found; just try without suffix if allowed
249 //
250 if ($fallback_no_suffix) {
251 return sqGetGlobalVar($name, $value, $search, $default, $typecast);
252 }
253
254
255 // no dice, set default and return FALSE
256 //
257 if (!is_null($default)) {
258 $value = $default;
259 }
260 return FALSE;
261
262 }
263
264
265 /**
266 * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
267 * and set it in provided var.
268 *
269 * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
270 * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
271 * $_GET. Otherwise, use one of the defined constants to look for a var in one
272 * place specifically.
273 *
274 * Note: $search is an int value equal to one of the constants defined above.
275 *
276 * Example:
277 * sqgetGlobalVar('username',$username,SQ_SESSION);
278 * // No quotes around last param, it's a constant - not a string!
279 *
280 * @param string name the name of the var to search
281 * @param mixed value the variable to return
282 * @param int search constant defining where to look
283 * @param mixed default the value to assign to $value when nothing is found
284 * @param int typecast force variable to be cast to given type (please
285 * use SQ_TYPE_XXX constants or set to FALSE (default)
286 * to leave variable type unmolested)
287 *
288 * @return bool whether variable is found.
289 */
290 function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
291
292 $result = false;
293
294 switch ($search) {
295 /* we want the default case to be first here,
296 so that if a valid value isn't specified,
297 all three arrays will be searched. */
298 default:
299 case SQ_INORDER: // check session, post, get
300 case SQ_SESSION:
301 if( isset($_SESSION[$name]) ) {
302 $value = $_SESSION[$name];
303 $result = TRUE;
304 break;
305 } elseif ( $search == SQ_SESSION ) {
306 break;
307 }
308 case SQ_FORM: // check post, get
309 case SQ_POST:
310 if( isset($_POST[$name]) ) {
311 $value = $_POST[$name];
312 $result = TRUE;
313 break;
314 } elseif ( $search == SQ_POST ) {
315 break;
316 }
317 case SQ_GET:
318 if ( isset($_GET[$name]) ) {
319 $value = $_GET[$name];
320 $result = TRUE;
321 break;
322 }
323 /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
324 break;
325 case SQ_COOKIE:
326 if ( isset($_COOKIE[$name]) ) {
327 $value = $_COOKIE[$name];
328 $result = TRUE;
329 break;
330 }
331 break;
332 case SQ_SERVER:
333 if ( isset($_SERVER[$name]) ) {
334 $value = $_SERVER[$name];
335 $result = TRUE;
336 break;
337 }
338 break;
339 }
340 if ($result && $typecast) {
341 switch ($typecast) {
342 case SQ_TYPE_INT: $value = (int) $value; break;
343 case SQ_TYPE_STRING: $value = (string) $value; break;
344 case SQ_TYPE_BOOL: $value = (bool) $value; break;
345 case SQ_TYPE_BIGINT:
346 $value = (preg_match('/^[0-9]+$/', $value) ? $value : '0');
347 break;
348 default: break;
349 }
350 } else if (!$result && !is_null($default)) {
351 $value = $default;
352 }
353 return $result;
354 }
355
356 /**
357 * Get an immutable copy of a configuration variable if SquirrelMail
358 * is in "secured configuration" mode. This guarantees the caller
359 * gets a copy of the requested value as it is set in the main
360 * application configuration (including config_local overrides), and
361 * not what it might be after possibly having been modified by some
362 * other code (usually a plugin overriding configuration values for
363 * one reason or another).
364 *
365 * WARNING: Please use this function as little as possible, because
366 * every time it is called, it forcibly reloads the main configuration
367 * file(s).
368 *
369 * Caller beware that this function will do nothing if SquirrelMail
370 * is not in "secured configuration" mode per the $secured_config
371 * setting.
372 *
373 * @param string $var_name The name of the desired variable
374 *
375 * @return mixed The desired value
376 *
377 * @since 1.5.2
378 *
379 */
380 function get_secured_config_value($var_name) {
381
382 static $return_values = array();
383
384 // if we can avoid it, return values that have
385 // already been retrieved (so we don't have to
386 // include the config file yet again)
387 //
388 if (isset($return_values[$var_name])) {
389 return $return_values[$var_name];
390 }
391
392
393 // load site configuration
394 //
395 require(SM_PATH . 'config/config.php');
396
397 // load local configuration overrides
398 //
399 if (file_exists(SM_PATH . 'config/config_local.php')) {
400 require(SM_PATH . 'config/config_local.php');
401 }
402
403 // if SM isn't in "secured configuration" mode,
404 // just return the desired value from the global scope
405 //
406 if (!$secured_config) {
407 global $$var_name;
408 $return_values[$var_name] = $$var_name;
409 return $$var_name;
410 }
411
412 // else we return what we got from the config file
413 //
414 $return_values[$var_name] = $$var_name;
415 return $$var_name;
416
417 }
418
419 /**
420 * Deletes an existing session, more advanced than the standard PHP
421 * session_destroy(), it explicitly deletes the cookies and global vars.
422 *
423 * WARNING: Older PHP versions have some issues with session management.
424 * See http://bugs.php.net/11643 (warning, spammed bug tracker) and
425 * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
426 * 4.2.3 and maybe other versions. If you restart session after session
427 * is destroyed, affected PHP versions produce PHP notice. Bug should
428 * be fixed only in 4.3.0
429 */
430 function sqsession_destroy() {
431
432 /*
433 * php.net says we can kill the cookie by setting just the name:
434 * http://www.php.net/manual/en/function.setcookie.php
435 * maybe this will help fix the session merging again.
436 *
437 * Changed the theory on this to kill the cookies first starting
438 * a new session will provide a new session for all instances of
439 * the browser, we don't want that, as that is what is causing the
440 * merging of sessions.
441 */
442
443 global $base_uri, $_COOKIE, $_SESSION;
444
445 if (isset($_COOKIE[session_name()]) && session_name()) {
446 sqsetcookie(session_name(), $_COOKIE[session_name()], 1, $base_uri);
447
448 /*
449 * Make sure to kill /src and /src/ cookies, just in case there are
450 * some left-over or malicious ones set in user's browser.
451 * NB: Note that an attacker could try to plant a cookie for one
452 * of the /plugins/* directories. Such cookies can block
453 * access to certain plugin pages, but they do not influence
454 * or fixate the $base_uri cookie, so we don't worry about
455 * trying to delete all of them here.
456 */
457 sqsetcookie(session_name(), $_COOKIE[session_name()], 1, $base_uri . 'src');
458 sqsetcookie(session_name(), $_COOKIE[session_name()], 1, $base_uri . 'src/');
459 }
460
461 if (isset($_COOKIE['key']) && $_COOKIE['key']) sqsetcookie('key','SQMTRASH',1,$base_uri);
462
463 /* Make sure new session id is generated on subsequent session_start() */
464 unset($_COOKIE[session_name()]);
465 unset($_GET[session_name()]);
466 unset($_POST[session_name()]);
467
468 $sessid = session_id();
469 if (!empty( $sessid )) {
470 $_SESSION = array();
471 @session_destroy();
472 }
473 }
474
475 /**
476 * Function to verify a session has been started. If it hasn't
477 * start a session up. php.net doesn't tell you that $_SESSION
478 * (even though autoglobal), is not created unless a session is
479 * started, unlike $_POST, $_GET and such
480 * Update: (see #1685031) the session ID is left over after the
481 * session is closed in some PHP setups; this function just becomes
482 * a passthru to sqsession_start(), but leaving old code in for
483 * edification.
484 */
485 function sqsession_is_active() {
486 //$sessid = session_id();
487 //if ( empty( $sessid ) ) {
488 sqsession_start();
489 //}
490 }
491
492 /**
493 * Function to start the session and store the cookie with the session_id as
494 * HttpOnly cookie which means that the cookie isn't accessible by javascript
495 * (IE6 only)
496 * Note that as sqsession_is_active() no longer discriminates as to when
497 * it calls this function, session_start() has to have E_NOTICE suppression
498 * (thus the @ sign).
499 */
500 function sqsession_start() {
501 global $base_uri;
502
503 sq_call_function_suppress_errors('session_start');
504 // was: @session_start();
505 $session_id = session_id();
506
507 // session_starts sets the sessionid cookie but without the httponly var
508 // setting the cookie again sets the httponly cookie attribute
509 //
510 // need to check if headers have been sent, since sqsession_is_active()
511 // has become just a passthru to this function, so the sqsetcookie()
512 // below is called every time, even after headers have already been sent
513 //
514 if (!headers_sent())
515 sqsetcookie(session_name(),$session_id,false,$base_uri);
516 }
517
518
519
520 /**
521 * Set a cookie
522 *
523 * @param string $sName The name of the cookie.
524 * @param string $sValue The value of the cookie.
525 * @param int $iExpire The time the cookie expires. This is a Unix
526 * timestamp so is in number of seconds since
527 * the epoch.
528 * @param string $sPath The path on the server in which the cookie
529 * will be available on.
530 * @param string $sDomain The domain that the cookie is available.
531 * @param boolean $bSecure Indicates that the cookie should only be
532 * transmitted over a secure HTTPS connection.
533 * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
534 * @param boolean $bReplace Replace previous cookies with same name?
535 *
536 * @return void
537 *
538 * @since 1.4.16 and 1.5.1
539 *
540 */
541 function sqsetcookie($sName, $sValue='deleted', $iExpire=0, $sPath="", $sDomain="",
542 $bSecure=false, $bHttpOnly=true, $bReplace=false) {
543
544 // if we have a secure connection then limit the cookies to https only.
545 global $is_secure_connection;
546 if ($sName && $is_secure_connection)
547 $bSecure = true;
548
549 // admin config can override the restriction of secure-only cookies
550 global $only_secure_cookies;
551 if (!$only_secure_cookies)
552 $bSecure = false;
553
554 if (false && check_php_version(5,2)) {
555 // php 5 supports the httponly attribute in setcookie, but because setcookie seems a bit
556 // broken we use the header function for php 5.2 as well. We might change that later.
557 //setcookie($sName,$sValue,(int) $iExpire,$sPath,$sDomain,$bSecure,$bHttpOnly);
558 } else {
559 if (!empty($sDomain)) {
560 // Fix the domain to accept domains with and without 'www.'.
561 if (strtolower(substr($sDomain, 0, 4)) == 'www.') $sDomain = substr($sDomain, 4);
562 $sDomain = '.' . $sDomain;
563
564 // Remove port information.
565 $Port = strpos($sDomain, ':');
566 if ($Port !== false) $sDomain = substr($sDomain, 0, $Port);
567 }
568 if (!$sValue) $sValue = 'deleted';
569 header('Set-Cookie: ' . rawurlencode($sName) . '=' . rawurlencode($sValue)
570 . (empty($iExpire) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', $iExpire) . ' GMT')
571 . (empty($sPath) ? '' : '; path=' . $sPath)
572 . (empty($sDomain) ? '' : '; domain=' . $sDomain)
573 . (!$bSecure ? '' : '; secure')
574 . (!$bHttpOnly ? '' : '; HttpOnly'), $bReplace);
575 }
576 }
577
578
579 /**
580 * session_regenerate_id replacement for PHP < 4.3.2
581 *
582 * This code is borrowed from Gallery, session.php version 1.53.2.1
583 FIXME: I saw this code on php.net (in the manual); that's where it comes from originally, but I don't think we need it - it's just redundant to all the hard work we already did seeding the random number generator IMO. I think we can just call to GenerateRandomString() and dump the rest.
584 */
585 if (!function_exists('session_regenerate_id')) {
586
587 function php_combined_lcg() {
588 $tv = gettimeofday();
589 $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
590 $lcg['s2'] = mt_rand();
591 $q = (int) ($lcg['s1'] / 53668);
592 $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
593 if ($lcg['s1'] < 0) {
594 $lcg['s1'] += 2147483563;
595 }
596 $q = (int) ($lcg['s2'] / 52774);
597 $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
598 if ($lcg['s2'] < 0) {
599 $lcg['s2'] += 2147483399;
600 }
601 $z = (int) ($lcg['s1'] - $lcg['s2']);
602 if ($z < 1) {
603 $z += 2147483562;
604 }
605 return $z * 4.656613e-10;
606 }
607
608 function session_regenerate_id() {
609 global $base_uri;
610 $tv = gettimeofday();
611 sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
612 $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
613 session_id(md5($buf));
614 if (ini_get('session.use_cookies')) {
615 sqsetcookie(session_name(), session_id(), 0, $base_uri);
616 }
617 return TRUE;
618 }
619 }
620
621
622 /**
623 * php_self
624 *
625 * Creates an URL for the page calling this function, using either the PHP global
626 * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
627 * function was stored in function/strings.php.
628 *
629 * @return string the complete url for this page
630 * @since 1.2.3
631 */
632 function php_self () {
633 // PHP 4.4.4 apparently gives the wrong value here - missing the query string
634 // this code is commented out in the 1.4.x code, so we'll do the same here
635 //if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
636 // return $req_uri;
637 //}
638
639 if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
640
641 // need to add query string to end of PHP_SELF to match REQUEST_URI
642 //
643 if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
644 $php_self .= '?' . $query_string;
645 }
646
647 return $php_self;
648 }
649
650 return '';
651 }
652
653
654 /**
655 * Print variable
656 *
657 * sm_print_r($some_variable, [$some_other_variable [, ...]]);
658 *
659 * Debugging function - does the same as print_r, but makes sure special
660 * characters are converted to htmlentities first. This will allow
661 * values like <some@email.address> to be displayed.
662 * The output is wrapped in <<pre>> and <</pre>> tags.
663 * Since 1.4.2 accepts unlimited number of arguments.
664 * @since 1.4.1
665 * @return void
666 */
667 function sm_print_r() {
668 ob_start(); // Buffer output
669 foreach(func_get_args() as $var) {
670 print_r($var);
671 echo "\n";
672 // php has get_class_methods function that can print class methods
673 if (is_object($var)) {
674 // get class methods if $var is object
675 $aMethods=get_class_methods(get_class($var));
676 // make sure that $aMethods is array and array is not empty
677 if (is_array($aMethods) && $aMethods!=array()) {
678 echo "Object methods:\n";
679 foreach($aMethods as $method) {
680 echo '* ' . $method . "\n";
681 }
682 }
683 echo "\n";
684 }
685 }
686 $buffer = ob_get_contents(); // Grab the print_r output
687 ob_end_clean(); // Silently discard the output & stop buffering
688 print '<div align="left"><pre>';
689 print htmlentities($buffer);
690 print '</pre></div>';
691 }
692
693
694 /**
695 * Sanitize a value using htmlspecialchars() or similar, but also
696 * recursively run htmlspecialchars() (or similar) on array keys
697 * and values.
698 *
699 * If $value is not a string or an array with strings in it,
700 * the value is returned as is.
701 *
702 * @param mixed $value The value to be sanitized.
703 * @param mixed $quote_style Either boolean or an integer. If it
704 * is an integer, it must be the PHP
705 * constant indicating if/how to escape
706 * quotes: ENT_QUOTES, ENT_COMPAT, or
707 * ENT_NOQUOTES. If it is a boolean value,
708 * it must be TRUE and thus indicates
709 * that the only sanitizing to be done
710 * herein is to replace single and double
711 * quotes with &#039; and &quot;, no other
712 * changes are made to $value. If it is
713 * boolean and FALSE, behavior reverts
714 * to same as if the value was ENT_QUOTES
715 * (OPTIONAL; default is ENT_QUOTES).
716 *
717 * @return mixed The sanitized value.
718 *
719 * @since 1.5.2
720 *
721 **/
722 function sq_htmlspecialchars($value, $quote_style=ENT_QUOTES) {
723
724 if ($quote_style === FALSE) $quote_style = ENT_QUOTES;
725
726 // array? go recursive...
727 //
728 if (is_array($value)) {
729 $return_array = array();
730 foreach ($value as $key => $val) {
731 $return_array[sq_htmlspecialchars($key, $quote_style)]
732 = sq_htmlspecialchars($val, $quote_style);
733 }
734 return $return_array;
735
736 // sanitize strings only
737 //
738 } else if (is_string($value)) {
739 if ($quote_style === TRUE)
740 return str_replace(array('\'', '"'), array('&#039;', '&quot;'), $value);
741 else
742 return htmlspecialchars($value, $quote_style);
743 }
744
745 // anything else gets returned with no changes
746 //
747 return $value;
748
749 }
750
751
752 /**
753 * Detect whether or not we have a SSL secured (HTTPS) connection
754 * connection to the browser
755 *
756 * It is thought to be so if you have 'SSLOptions +StdEnvVars'
757 * in your Apache configuration,
758 * OR if you have HTTPS set to a non-empty value (except "off")
759 * in your HTTP_SERVER_VARS,
760 * OR if you have HTTP_X_FORWARDED_PROTO=https in your HTTP_SERVER_VARS,
761 * OR if you are on port 443.
762 *
763 * Note: HTTP_X_FORWARDED_PROTO could be sent from the client and
764 * therefore possibly spoofed/hackable. Thus, SquirrelMail
765 * ignores such headers by default. The administrator
766 * can tell SM to use such header values by setting
767 * $sq_ignore_http_x_forwarded_headers to boolean FALSE
768 * in config/config.php or by using config/conf.pl.
769 *
770 * Note: It is possible to run SSL on a port other than 443, and
771 * if that is the case, the administrator should set
772 * $sq_https_port in config/config.php or by using config/conf.pl.
773 *
774 * @return boolean TRUE if the current connection is SSL-encrypted;
775 * FALSE otherwise.
776 *
777 * @since 1.4.17 and 1.5.2
778 *
779 */
780 function is_ssl_secured_connection()
781 {
782 global $sq_ignore_http_x_forwarded_headers, $sq_https_port;
783 $https_env_var = getenv('HTTPS');
784 if ($sq_ignore_http_x_forwarded_headers
785 || !sqgetGlobalVar('HTTP_X_FORWARDED_PROTO', $forwarded_proto, SQ_SERVER))
786 $forwarded_proto = '';
787 if (empty($sq_https_port)) // won't work with port 0 (zero)
788 $sq_https_port = 443;
789 if ((isset($https_env_var) && strcasecmp($https_env_var, 'on') === 0)
790 || (sqgetGlobalVar('HTTPS', $https, SQ_SERVER) && !empty($https)
791 && strcasecmp($https, 'off') !== 0)
792 || (strcasecmp($forwarded_proto, 'https') === 0)
793 || (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)
794 && $server_port == $sq_https_port))
795 return TRUE;
796 return FALSE;
797 }
798
799
800 /**
801 * Endeavor to detect what user and group PHP is currently
802 * running as. Probably only works in non-Windows environments.
803 *
804 * @return mixed Boolean FALSE is returned if something went wrong,
805 * otherwise an array is returned with the following
806 * elements:
807 * uid The current process' UID (integer)
808 * euid The current process' effective UID (integer)
809 * gid The current process' GID (integer)
810 * egid The current process' effective GID (integer)
811 * name The current process' name/handle (string)
812 * ename The current process' effective name/handle (string)
813 * group The current process' group name (string)
814 * egroup The current process' effective group name (string)
815 * Note that some of these elements may have empty
816 * values, especially if they could not be determined.
817 *
818 * @since 1.5.2
819 *
820 */
821 function get_process_owner_info()
822 {
823 if (!function_exists('posix_getuid'))
824 return FALSE;
825
826 $process_info['uid'] = posix_getuid();
827 $process_info['euid'] = posix_geteuid();
828 $process_info['gid'] = posix_getgid();
829 $process_info['egid'] = posix_getegid();
830
831 $user_info = posix_getpwuid($process_info['uid']);
832 $euser_info = posix_getpwuid($process_info['euid']);
833 $group_info = posix_getgrgid($process_info['gid']);
834 $egroup_info = posix_getgrgid($process_info['egid']);
835
836 $process_info['name'] = $user_info['name'];
837 $process_info['ename'] = $euser_info['name'];
838 $process_info['group'] = $user_info['name'];
839 $process_info['egroup'] = $euser_info['name'];
840
841 return $process_info;
842 }
843
844