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