define('SQ_SERVER',5);
define('SQ_FORM',6);
+
/**
* returns true if current php version is at mimimum a.b.c
*
* @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) {
+function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) {
+
+ $result = false;
- /* NOTE: DO NOT enclose the constants in the switch
- statement with quotes. They are constant values,
- enclosing them in quotes will cause them to evaluate
- as strings. */
switch ($search) {
/* we want the default case to be first here,
so that if a valid value isn't specified,
case SQ_SESSION:
if( isset($_SESSION[$name]) ) {
$value = $_SESSION[$name];
- return TRUE;
+ $result = TRUE;
+ break;
} elseif ( $search == SQ_SESSION ) {
break;
}
case SQ_POST:
if( isset($_POST[$name]) ) {
$value = $_POST[$name];
- return TRUE;
+ $result = TRUE;
+ break;
} elseif ( $search == SQ_POST ) {
break;
}
case SQ_GET:
if ( isset($_GET[$name]) ) {
$value = $_GET[$name];
- return TRUE;
+ $result = TRUE;
+ break;
}
/* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */
break;
case SQ_COOKIE:
if ( isset($_COOKIE[$name]) ) {
$value = $_COOKIE[$name];
- return TRUE;
+ $result = TRUE;
+ break;
}
break;
case SQ_SERVER:
if ( isset($_SERVER[$name]) ) {
$value = $_SERVER[$name];
- return TRUE;
+ $result = TRUE;
+ break;
}
break;
}
- /* Nothing found, return FALSE */
- return FALSE;
+ if ($result && $typecast) {
+ switch ($typecast) {
+ case SQ_TYPE_INT: $value = (int) $value; break;
+ case SQ_TYPE_STRING: $value = (string) $value; break;
+ case SQ_TYPE_BOOL: $value = (bool) $value; break;
+ default: break;
+ }
+ } else if (!$result && !is_null($default)) {
+ $value = $default;
+ }
+ return $result;
}
/**
* 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
+ * be fixed only in 4.3.0
*/
function sqsession_destroy() {
$_SESSION = array();
@session_destroy();
}
-
}
/**
* (IE6 only)
*/
function sqsession_start() {
- global $PHP_SELF;
-
- $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
- $repl = array('', '', '');
- $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
-
+ global $base_uri;
session_start();
- $sessid = session_id();
+ $session_id = session_id();
+
// session_starts sets the sessionid cookie buth without the httponly var
// setting the cookie again sets the httponly cookie attribute
- sqsetcookie(session_name(),$sessid,false,$base_uri);
+
+ // disable, @see sqsetcookie and php 5.1.2
+ // sqsetcookie(session_name(),session_id(),false,$base_uri);
}
* @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
* @return void
*/
-function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true) {
+function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) {
+ static $sCookieCache;
+ if (!isset($sCache)) {
+ $sCache = '';
+ }
+ /**
+ * 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.
+ */
+ if ($bFlush) {
+ // header($sCookieCache);
+ return;
+ }
+ if (!$sName) return;
+
+ // 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);
+ return;
+
$sHeader = "Set-Cookie: $sName=$sValue";
if ($sPath) {
$sHeader .= "; path=$sPath";
if ($sDomain) {
$sHeader .= "; Domain=$sDomain";
}
+ // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain
if ($bSecure) {
$sHeader .= "; Secure";
}
$sHeader .= "; HttpOnly";
}
// $sHeader .= "; Version=1";
+ $sCookieCache .= $sHeader ."\r\n";
+ //header($sHeader."\r\n");
+}
+
+/**
+ * Send the cookie header
+ *
+ * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call.
+ * @return void
+ */
+function sqsetcookieflush() {
+ sqsetcookie('','','','','','','',true);
+}
+
+/**
+ * session_regenerate_id replacement for PHP < 4.3.2
+ *
+ * This code is borrowed from Gallery, session.php version 1.53.2.1
+ */
+if (!function_exists('session_regenerate_id')) {
+ function make_seed() {
+ list($usec, $sec) = explode(' ', microtime());
+ return (float)$sec + ((float)$usec * 100000);
+ }
+
+ function php_combined_lcg() {
+ mt_srand(make_seed());
+ $tv = gettimeofday();
+ $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
+ $lcg['s2'] = mt_rand();
+ $q = (int) ($lcg['s1'] / 53668);
+ $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
+ if ($lcg['s1'] < 0) {
+ $lcg['s1'] += 2147483563;
+ }
+ $q = (int) ($lcg['s2'] / 52774);
+ $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
+ if ($lcg['s2'] < 0) {
+ $lcg['s2'] += 2147483399;
+ }
+ $z = (int) ($lcg['s1'] - $lcg['s2']);
+ if ($z < 1) {
+ $z += 2147483562;
+ }
+ return $z * 4.656613e-10;
+ }
- header($sHeader);
+ function session_regenerate_id() {
+ global $base_uri;
+ $tv = gettimeofday();
+ sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
+ $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
+ session_id(md5($buf));
+ if (ini_get('session.use_cookies')) {
+ // at a later stage we use sqsetcookie. At this point just do
+ // what session_regenerate_id would do
+ setcookie(session_name(), session_id(), NULL, $base_uri);
+ }
+ return TRUE;
+ }
}
+
/**
* php_self
*
return '';
}
-/** set the name of the session cookie */
-if(isset($session_name) && $session_name) {
- ini_set('session.name' , $session_name);
-} else {
- ini_set('session.name' , 'SQMSESSID');
-}
/**
- * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
- * Force magic_quotes_runtime off.
- * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
- * If there's a better place, please let me know.
- */
-ini_set('magic_quotes_runtime','0');
+ * Find files 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.
+ * @param boolean $return_filenames_only When TRUE, only file names
+ * are returned, otherwise the
+ * $directory_path string is
+ * prepended to each file in
+ * the returned list (optional;
+ * default is filename only)
+ *
+ * @return array The requested file list.
+ *
+ * @since 1.5.2
+ *
+ */
+function list_files($directory_path, $extension='', $return_filenames_only=TRUE) {
+
+ $files = array();
+
+//FIXME: do we want to place security restrictions here like only allowing
+// directories under SM_PATH?
+ // validate given directory
+ //
+ if (empty($directory_path)
+ || !is_dir($directory_path)
+ || !is_readable($directory_path)
+ || !($DIR = opendir($directory_path))) {
+ return $files;
+ }
-/* Since we decided all IMAP servers must implement the UID command as defined in
- * the IMAP RFC, we force $uid_support to be on.
- */
-global $uid_support;
-$uid_support = true;
+ // parse through the files
+ //
+ $extension = '.' . trim($extension, '.');
+ while (($file = readdir($DIR)) !== false) {
-/* if running with magic_quotes_gpc then strip the slashes
- from POST and GET global arrays */
-if (get_magic_quotes_gpc()) {
- sqstripslashes($_GET);
- sqstripslashes($_POST);
-}
+ if ($file == '.' || $file == '..') continue;
+
+ if (empty($extension)
+ || strrpos($file, $extension) === (strlen($file) - strlen($extension))) {
+ $files[] = ($return_filenames_only
+ ? $file
+ : $directory_path . '/' . $file);
+ }
-/**
- * If register_globals are on, unregister globals.
- * Code requires PHP 4.1.0 or newer.
- */
-if ((bool) @ini_get('register_globals')) {
- /**
- * Remove all globals from $_GET, $_POST, and $_COOKIE.
- */
- foreach ($_REQUEST as $key => $value) {
- unset($GLOBALS[$key]);
- }
- /**
- * Remove globalized $_FILES variables
- * Before 4.3.0 $_FILES are included in $_REQUEST.
- * Unglobalize them in separate call in order to remove dependency
- * on PHP version.
- */
- foreach ($_FILES as $key => $value) {
- unset($GLOBALS[$key]);
- // there are three undocumented $_FILES globals.
- unset($GLOBALS[$key.'_type']);
- unset($GLOBALS[$key.'_name']);
- unset($GLOBALS[$key.'_size']);
- }
- /**
- * Remove globalized environment variables.
- */
- foreach ($_ENV as $key => $value) {
- unset($GLOBALS[$key]);
- }
- /**
- * Remove globalized server variables.
- */
- foreach ($_SERVER as $key => $value) {
- unset($GLOBALS[$key]);
}
-}
+ closedir($DIR);
-/* strip any tags added to the url from PHP_SELF.
- This fixes hand crafted url XXS expoits for any
- page that uses PHP_SELF as the FORM action */
-$_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
-$PHP_SELF = php_self();
+ return $files;
-sqsession_is_active();
+}
-/**
- * Remove globalized session data in rg=on setups
+
+/**
+ * Print variable
+ *
+ * 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
+ * values like <some@email.address> to be displayed.
+ * The output is wrapped in <<pre>> and <</pre>> tags.
+ * Since 1.4.2 accepts unlimited number of arguments.
+ * @since 1.4.1
+ * @return void
*/
-if ((bool) @ini_get('register_globals')) {
- foreach ($_SESSION as $key => $value) {
- unset($GLOBALS[$key]);
+function sm_print_r() {
+ ob_start(); // Buffer output
+ foreach(func_get_args() as $var) {
+ print_r($var);
+ echo "\n";
+ // php has get_class_methods function that can print class methods
+ if (is_object($var)) {
+ // get class methods if $var is object
+ $aMethods=get_class_methods(get_class($var));
+ // 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";
+ }
+ }
+ echo "\n";
+ }
}
+ $buffer = ob_get_contents(); // Grab the print_r output
+ ob_end_clean(); // Silently discard the output & stop buffering
+ print '<div align="left"><pre>';
+ print htmlentities($buffer);
+ print '</pre></div>';
}
-?>
+
+