Source for file global.php
Documentation is available at global.php
* This includes code to update < 4.1.0 globals to the newer format
* It also has some session register functions that work across various
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: global.php,v 1.78 2006/10/02 11:50:49 pdontthink Exp $
* returns true if current php version is at mimimum a.b.c
* Called: check_php_version(4,1)
* @param int a major version number
* @param int b minor version number
* @param int c release number
* returns true if the current internal SM version is at minimum a.b.c
* These are plain integer comparisons, as our internal version is
* constructed by us, as an array of 3 ints.
* Called: check_sm_version(1,3,3)
* @param int a major version number
* @param int b minor version number
* @param int c release number
global $SQM_INTERNAL_VERSION;
if ( !isset
($SQM_INTERNAL_VERSION) ||
$SQM_INTERNAL_VERSION[0] <
$a ||
( $SQM_INTERNAL_VERSION[0] ==
$a &&
$SQM_INTERNAL_VERSION[1] <
$b) ||
( $SQM_INTERNAL_VERSION[0] ==
$a &&
$SQM_INTERNAL_VERSION[1] ==
$b &&
$SQM_INTERNAL_VERSION[2] <
$c ) ) {
* Recursively strip slashes from the values of an array.
* @param array array the array to strip, passed by reference
foreach ($array as $index=>
$value) {
* Add a variable to the session.
* @param mixed $var the variable to register
* @param string $name the name to refer to this variable
$_SESSION["$name"] =
$var;
* Delete a variable from the session.
* @param string $name the name of the var to delete
* Checks to see if a variable has already been registered
* @param string $name the name of the var to check
* @return bool whether the var has been registered
if (isset
($_SESSION[$test_name])) {
* Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER
* and set it in provided var.
* If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION,
* then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and
* $_GET. Otherwise, use one of the defined constants to look for a var in one
* Note: $search is an int value equal to one of the constants defined above.
* sqgetGlobalVar('username',$username,SQ_SESSION);
* // No quotes around last param, it's a constant - not a string!
* @param string name the name of the var to search
* @param mixed value the variable to return
* @param int search constant defining where to look
* @param int typecast force variable to be cast to given type (please
* use SQ_TYPE_XXX constants or set to FALSE (default)
* to leave variable type unmolested)
* @return bool whether variable is found.
function sqgetGlobalVar($name, &$value, $search =
SQ_INORDER, $default =
NULL, $typecast =
false) {
/* we want the default case to be first here,
so that if a valid value isn't specified,
all three arrays will be searched. */
if( isset
($_SESSION[$name]) ) {
$value =
$_SESSION[$name];
if( isset
($_POST[$name]) ) {
if ( isset
($_GET[$name]) ) {
/* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
if ( isset
($_COOKIE[$name]) ) {
$value =
$_COOKIE[$name];
if ( isset
($_SERVER[$name]) ) {
$value =
$_SERVER[$name];
if ($result &&
$typecast) {
} else if (!$result &&
!is_null($default)) {
* Deletes an existing session, more advanced than the standard PHP
* session_destroy(), it explicitly deletes the cookies and global vars.
* WARNING: Older PHP versions have some issues with session management.
* See http://bugs.php.net/11643 (warning, spammed bug tracker) and
* http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2,
* 4.2.3 and maybe other versions. If you restart session after session
* is destroyed, affected PHP versions produce PHP notice. Bug should
* php.net says we can kill the cookie by setting just the name:
* http://www.php.net/manual/en/function.setcookie.php
* maybe this will help fix the session merging again.
* Changed the theory on this to kill the cookies first starting
* a new session will provide a new session for all instances of
* the browser, we don't want that, as that is what is causing the
if (isset
($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri);
if (isset
($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri);
* Function to verify a session has been started. If it hasn't
* start a session up. php.net doesn't tell you that $_SESSION
* (even though autoglobal), is not created unless a session is
* started, unlike $_POST, $_GET and such
if ( empty( $sessid ) ) {
* Function to start the session and store the cookie with the session_id as
* HttpOnly cookie which means that the cookie isn't accessible by javascript
// session_starts sets the sessionid cookie buth without the httponly var
// setting the cookie again sets the httponly cookie attribute
// disable, @see sqsetcookie and php 5.1.2
// sqsetcookie(session_name(),session_id(),false,$base_uri);
* @param string $sName The name of the cookie.
* @param string $sValue The value of the cookie.
* @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
* @param string $sPath The path on the server in which the cookie will be available on.
* @param string $sDomain The domain that the cookie is available.
* @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection.
* @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
function sqsetcookie($sName,$sValue,$iExpire=
false,$sPath=
"",$sDomain=
"",$bSecure=
false,$bHttpOnly=
true,$bFlush=
false) {
* We have to send all cookies with one header call otherwise we loose cookies.
* In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true.
* If that happens we send the cookie header.
// header($sCookieCache);
// php 5.1.2 and 4.4.2 do not allow to send multiple headers at once.
// Because that's the only way to get this thing working we fallback to
// setcookie until we solved this
if ($iExpire===
false) $iExpire =
0;
setcookie($sName, $sValue, $iExpire, $sPath);
$sHeader =
"Set-Cookie: $sName=$sValue";
$sHeader .=
"; path=$sPath";
if ($iExpire !==
false) {
$sHeader .=
"; Max-Age=$iExpire";
// php uses Expire header, also add the expire header
$sHeader .=
"; expires=".
gmdate('D, d-M-Y H:i:s T',$iExpire);
$sHeader .=
"; Domain=$sDomain";
// TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
$sHeader .=
"; HttpOnly";
// $sHeader .= "; Version=1";
$sCookieCache .=
$sHeader .
"\r\n";
//header($sHeader."\r\n");
* Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
* session_regenerate_id replacement for PHP < 4.3.2
* This code is borrowed from Gallery, session.php version 1.53.2.1
return (float)
$sec +
((float)
$usec *
100000);
$lcg['s1'] =
$tv['sec'] ^
(~
$tv['usec']);
$q = (int)
($lcg['s1'] /
53668);
$lcg['s1'] = (int)
(40014 *
($lcg['s1'] -
53668 *
$q) -
12211 *
$q);
$lcg['s1'] +=
2147483563;
$q = (int)
($lcg['s2'] /
52774);
$lcg['s2'] = (int)
(40692 *
($lcg['s2'] -
52774 *
$q) -
3791 *
$q);
$lcg['s2'] +=
2147483399;
$z = (int)
($lcg['s1'] -
$lcg['s2']);
return $z *
4.656613e-10;
if (ini_get('session.use_cookies')) {
// at a later stage we use sqsetcookie. At this point just do
// what session_regenerate_id would do
* Creates an URL for the page calling this function, using either the PHP global
* REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
* function was stored in function/strings.php.
* @return string the complete url for this page
// need to add query string to end of PHP_SELF to match REQUEST_URI
$php_self .=
'?' .
$query_string;
* Find files and/or directories in a given directory optionally
* limited to only those with the given file extension. If the
* directory is not found or cannot be opened, no error is generated;
* only an empty file list is returned.
FIXME: do we WANT to throw an error or a notice or... or return FALSE?
* @param string $directory_path The path (relative or absolute)
* to the desired directory.
* @param string $extension The file extension filter (optional;
* default is to return all files (dirs).
* @param boolean $return_filenames_only When TRUE, only file/dir names
* are returned, otherwise the
* $directory_path string is
* prepended to each file/dir in
* the returned list (optional;
* default is filename/dirname only)
* @param boolean $include_directories When TRUE, directories are
* included (optional; default
* DO include directories).
* @param boolean $directories_only When TRUE, ONLY directories
* are included (optional; default
* is to include files too).
* @param boolean $separate_files_and_directories When TRUE, files and
* directories are returned
* formatted as a two-element
* array with the two keys
* "FILES" and "DIRECTORIES",
* where corresponding values
* are lists of either all
* files or all directories
* (optional; default do not
* split up return array).
* @return array The requested file/directory list(s).
function list_files($directory_path, $extension=
'', $return_filenames_only=
TRUE,
$include_directories=
TRUE, $directories_only=
FALSE,
$separate_files_and_directories=
FALSE) {
//FIXME: do we want to place security restrictions here like only allowing
// directories under SM_PATH?
// validate given directory
if (empty($directory_path)
||
!($DIR =
opendir($directory_path))) {
if (!empty($extension)) $extension =
'.' .
trim($extension, '.');
$directory_path =
rtrim($directory_path, '/');
// parse through the files
while (($file =
readdir($DIR)) !==
false) {
if ($file ==
'.' ||
$file ==
'..') continue;
// only use is_dir() if we really need to (be as efficient as possible)
if (!$include_directories ||
$directories_only
||
$separate_files_and_directories) {
if (is_dir($directory_path .
'/' .
$file)) {
if (!$include_directories) continue;
$directories[] =
($return_filenames_only
:
$directory_path .
'/' .
$file);
if ($directories_only) continue;
if (!$separate_files_and_directories
||
($separate_files_and_directories &&
!$is_dir)) {
$files[] =
($return_filenames_only
:
$directory_path .
'/' .
$file);
if ($directories_only) return $directories;
if ($separate_files_and_directories) return array('FILES' =>
$files,
'DIRECTORIES' =>
$directories);
* sm_print_r($some_variable, [$some_other_variable [, ...]]);
* Debugging function - does the same as print_r, but makes sure special
* characters are converted to htmlentities first. This will allow
* The output is wrapped in <<pre>> and <</pre>> tags.
* Since 1.4.2 accepts unlimited number of arguments.
// php has get_class_methods function that can print class methods
// get class methods if $var is object
// make sure that $aMethods is array and array is not empty
if (is_array($aMethods) &&
$aMethods!=
array()) {
echo
"Object methods:\n";
foreach($aMethods as $method) {
echo
'* ' .
$method .
"\n";
ob_end_clean(); // Silently discard the output & stop buffering
print
'<div align="left"><pre>';
Documentation generated on Sat, 07 Oct 2006 16:11:28 +0300 by phpDocumentor 1.3.0RC6