Fixed bug #15383: [Unit tests] Add tests for t3lib_div::validEmail
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_div.php
index 27648c1..d90994b 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -58,8 +58,8 @@
  *  499:     function fixed_lgd($string,$origChars,$preStr='...')
  *  524:     function fixed_lgd_pre($string,$chars)
  *  538:     function fixed_lgd_cs($string,$chars)
- *  555:     function breakTextForEmail($str,$implChar="\n",$charWidth=76)
- *  574:     function breakLinesForEmail($str,$implChar="\n",$charWidth=76)
+ *  555:     function breakTextForEmail($str,$implChar=LF,$charWidth=76)
+ *  574:     function breakLinesForEmail($str,$implChar=LF,$charWidth=76)
  *  610:     function cmpIP($baseIP, $list)
  *  626:     function cmpIPv4($baseIP, $list)
  *  668:     function cmpIPv6($baseIP, $list)
  *
  */
 
-
-
-
-
-
-
-
-
-
-
+       // a tabulator
+define('TAB', chr(9));
+       // a linefeed
+define('LF', chr(10));
+       // a carriage return
+define('CR', chr(13));
+       // a CR-LF combination
+define('CRLF', CR . LF);
 
 /**
  * The legendary "t3lib_div" class - Miscellaneous functions for general purpose.
@@ -238,53 +236,6 @@ final class t3lib_div {
        const SYSLOG_SEVERITY_ERROR = 3;
        const SYSLOG_SEVERITY_FATAL = 4;
 
-               // HTTP Headers, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for Details
-       const HTTP_STATUS_100 = 'HTTP/1.1 100 Continue';
-       const HTTP_STATUS_101 = 'HTTP/1.1 101 Switching Protocols';
-
-       const HTTP_STATUS_200 = 'HTTP/1.1 200 OK';
-       const HTTP_STATUS_201 = 'HTTP/1.1 201 Created';
-       const HTTP_STATUS_202 = 'HTTP/1.1 202 Accepted';
-       const HTTP_STATUS_203 = 'HTTP/1.1 203 Non-Authoritative Information';
-       const HTTP_STATUS_204 = 'HTTP/1.1 204 No Content';
-       const HTTP_STATUS_205 = 'HTTP/1.1 205 Reset Content';
-       const HTTP_STATUS_206 = 'HTTP/1.1 206 Partial Content';
-
-       const HTTP_STATUS_300 = 'HTTP/1.1 300 Multiple Choices';
-       const HTTP_STATUS_301 = 'HTTP/1.1 301 Moved Permanently';
-       const HTTP_STATUS_302 = 'HTTP/1.1 302 Found';
-       const HTTP_STATUS_303 = 'HTTP/1.1 303 See Other';
-       const HTTP_STATUS_304 = 'HTTP/1.1 304 Not Modified';
-       const HTTP_STATUS_305 = 'HTTP/1.1 305 Use Proxy';
-       const HTTP_STATUS_307 = 'HTTP/1.1 307 Temporary Redirect';
-
-       const HTTP_STATUS_400 = 'HTTP/1.1 400 Bad Request';
-       const HTTP_STATUS_401 = 'HTTP/1.1 401 Unauthorized';
-       const HTTP_STATUS_402 = 'HTTP/1.1 402 Payment Required';
-       const HTTP_STATUS_403 = 'HTTP/1.1 403 Forbidden';
-       const HTTP_STATUS_404 = 'HTTP/1.1 404 Not Found';
-       const HTTP_STATUS_405 = 'HTTP/1.1 405 Method Not Allowed';
-       const HTTP_STATUS_406 = 'HTTP/1.1 406 Not Acceptable';
-       const HTTP_STATUS_407 = 'HTTP/1.1 407 Proxy Authentication Required';
-       const HTTP_STATUS_408 = 'HTTP/1.1 408 Request Timeout';
-       const HTTP_STATUS_409 = 'HTTP/1.1 409 Conflict';
-       const HTTP_STATUS_410 = 'HTTP/1.1 410 Gone';
-       const HTTP_STATUS_411 = 'HTTP/1.1 411 Length Required';
-       const HTTP_STATUS_412 = 'HTTP/1.1 412 Precondition Failed';
-       const HTTP_STATUS_413 = 'HTTP/1.1 413 Request Entity Too Large';
-       const HTTP_STATUS_414 = 'HTTP/1.1 414 Request-URI Too Long';
-       const HTTP_STATUS_415 = 'HTTP/1.1 415 Unsupported Media Type';
-       const HTTP_STATUS_416 = 'HTTP/1.1 416 Requested Range Not Satisfiable';
-       const HTTP_STATUS_417 = 'HTTP/1.1 417 Expectation Failed';
-
-       const HTTP_STATUS_500 = 'HTTP/1.1 500 Internal Server Error';
-       const HTTP_STATUS_501 = 'HTTP/1.1 501 Not Implemented';
-       const HTTP_STATUS_502 = 'HTTP/1.1 502 Bad Gateway';
-       const HTTP_STATUS_503 = 'HTTP/1.1 503 Service Unavailable';
-       const HTTP_STATUS_504 = 'HTTP/1.1 504 Gateway Timeout';
-       const HTTP_STATUS_505 = 'HTTP/1.1 505 Version Not Supported';
-
-
 
        /*************************
         *
@@ -308,18 +259,33 @@ final class t3lib_div {
         *
         * @param       string          GET/POST var to return
         * @return      mixed           POST var named $var and if not set, the GET var of the same name.
-        * @see GPvar()
         */
        public static function _GP($var)        {
                if(empty($var)) return;
                $value = isset($_POST[$var]) ? $_POST[$var] : $_GET[$var];
                if (isset($value))      {
-                       if (is_array($value))   { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
+                       if (is_array($value))   { self::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
                }
                return $value;
        }
 
        /**
+        * Returns the global arrays $_GET and $_POST merged with $_POST taking precedence.
+        *
+        * @param       string          Key (variable name) from GET or POST vars
+        * @return      array           Returns the GET vars merged recursively onto the POST vars.
+        */
+       public static function _GPmerged($parameter) {
+               $postParameter = (isset($_POST[$parameter]) && is_array($_POST[$parameter])) ? $_POST[$parameter] : array();
+               $getParameter  = (isset($_GET[$parameter]) && is_array($_GET[$parameter])) ? $_GET[$parameter] : array();
+
+               $mergedParameters = self::array_merge_recursive_overrule($getParameter, $postParameter);
+               self::stripSlashesOnArray($mergedParameters);
+
+               return $mergedParameters;
+       }
+
+       /**
         * Returns the global $_GET array (or value from) normalized to contain un-escaped values.
         * ALWAYS use this API function to acquire the GET variables!
         * Usage: 27
@@ -331,7 +297,7 @@ final class t3lib_div {
        public static function _GET($var=NULL)  {
                $value = ($var === NULL) ? $_GET : (empty($var) ? NULL : $_GET[$var]);
                if (isset($value))      {       // Removes slashes since TYPO3 has added them regardless of magic_quotes setting.
-                       if (is_array($value))   { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
+                       if (is_array($value))   { self::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
                }
                return $value;
        }
@@ -348,31 +314,60 @@ final class t3lib_div {
        public static function _POST($var=NULL) {
                $value = ($var === NULL) ? $_POST : (empty($var) ? NULL : $_POST[$var]);
                if (isset($value))      {       // Removes slashes since TYPO3 has added them regardless of magic_quotes setting.
-                       if (is_array($value))   { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
+                       if (is_array($value))   { self::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
                }
                return $value;
        }
 
        /**
-        * Writes input value to $_GET
+        * Writes input value to $_GET.
         * Usage: 2
         *
-        * @param       mixed           Array to write to $_GET. Values should NOT be escaped at input time (but will be escaped before writing according to TYPO3 standards).
-        * @param       string          Alternative key; If set, this will not set the WHOLE GET array, but only the key in it specified by this value!
+        * @param mixed $inputGet
+        *        array or single value to write to $_GET. Values should NOT be
+        *        escaped at input time (but will be escaped before writing
+        *        according to TYPO3 standards).
+        * @param string $key
+        *        alternative key; If set, this will not set the WHOLE GET array,
+        *        but only the key in it specified by this value!
+        *        You can specify to replace keys on deeper array levels by
+        *        separating the keys with a pipe.
+        *        Example: 'parentKey|childKey' will result in
+        *        array('parentKey' => array('childKey' => $inputGet))
+        *
         * @return      void
         */
-       public static function _GETset($inputGet,$key='')       {
-                       // ADDS slashes since TYPO3 standard currently is that slashes MUST be applied (regardless of magic_quotes setting).
-               if (strcmp($key,''))    {
-                       if (is_array($inputGet)) {
-                               t3lib_div::addSlashesOnArray($inputGet);
+       public static function _GETset($inputGet, $key = '') {
+                       // adds slashes since TYPO3 standard currently is that slashes
+                       // must be applied (regardless of magic_quotes setting)
+               if (is_array($inputGet)) {
+                       self::addSlashesOnArray($inputGet);
+               } else {
+                       $inputGet = addslashes($inputGet);
+               }
+
+               if ($key != '') {
+                       if (strpos($key, '|') !== FALSE) {
+                               $pieces = explode('|', $key);
+                               $newGet = array();
+                               $pointer =& $newGet;
+                               foreach ($pieces as $piece) {
+                                       $pointer =& $pointer[$piece];
+                               }
+                               $pointer = $inputGet;
+                               $mergedGet = self::array_merge_recursive_overrule(
+                                       $_GET, $newGet
+                               );
+
+                               $_GET = $mergedGet;
+                               $GLOBALS['HTTP_GET_VARS'] = $mergedGet;
                        } else {
-                               $inputGet = addslashes($inputGet);
+                               $_GET[$key] = $inputGet;
+                               $GLOBALS['HTTP_GET_VARS'][$key] = $inputGet;
                        }
-                       $GLOBALS['HTTP_GET_VARS'][$key] = $_GET[$key] = $inputGet;
                } elseif (is_array($inputGet)) {
-                       t3lib_div::addSlashesOnArray($inputGet);
-                       $GLOBALS['HTTP_GET_VARS'] = $_GET = $inputGet;
+                       $_GET = $inputGet;
+                       $GLOBALS['HTTP_GET_VARS'] = $inputGet;
                }
        }
 
@@ -393,7 +388,7 @@ final class t3lib_div {
                if(empty($var)) return;
                $value = isset($_POST[$var]) ? $_POST[$var] : $_GET[$var];
                if (isset($value) && is_string($value)) { $value = stripslashes($value); }      // Originally check '&& get_magic_quotes_gpc() ' but the values of $_GET are always slashed regardless of get_magic_quotes_gpc() because HTTP_POST/GET_VARS are run through addSlashesOnArray in the very beginning of index_ts.php eg.
-               if ($strip && isset($value) && is_array($value)) { t3lib_div::stripSlashesOnArray($value); }
+               if ($strip && isset($value) && is_array($value)) { self::stripSlashesOnArray($value); }
                return $value;
        }
 
@@ -403,13 +398,13 @@ final class t3lib_div {
         *
         * @param       string          Key (variable name) from GET or POST vars
         * @return      array           Returns the GET vars merged recursively onto the POST vars.
+        * @deprecated since TYPO3 3.7 - Use t3lib_div::_GPmerged instead
+        * @see _GP()
         */
        public static function GParrayMerged($var)      {
-               $postA = is_array($_POST[$var]) ? $_POST[$var] : array();
-               $getA = is_array($_GET[$var]) ? $_GET[$var] : array();
-               $mergedA = t3lib_div::array_merge_recursive_overrule($getA,$postA);
-               t3lib_div::stripSlashesOnArray($mergedA);
-               return $mergedA;
+               self::logDeprecatedFunction();
+
+               return self::_GPmerged($var);
        }
 
        /**
@@ -423,7 +418,7 @@ final class t3lib_div {
         */
        public static function removeXSS($string)       {
                require_once(PATH_typo3.'contrib/RemoveXSS/RemoveXSS.php');
-               $string = RemoveXSS::RemoveXSS($string);
+               $string = RemoveXSS::process($string);
                return $string;
        }
 
@@ -467,7 +462,7 @@ final class t3lib_div {
                $returnCode='';
                if ($gfxConf['gif_compress'] && strtolower(substr($theFile,-4,4))=='.gif')      {       // GIF...
                        if (($type=='IM' || !$type) && $gfxConf['im'] && $gfxConf['im_path_lzw'])       {       // IM
-                               $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$theFile.'"', $gfxConf['im_path_lzw']);
+                               $cmd = self::imageMagickCommand('convert', '"'.$theFile.'" "'.$theFile.'"', $gfxConf['im_path_lzw']);
                                exec($cmd);
 
                                $returnCode='IM';
@@ -496,7 +491,7 @@ final class t3lib_div {
                        && strtolower(substr($theFile,-4,4))=='.png'
                        && @is_file($theFile))  {       // IM
                                $newFile = substr($theFile,0,-4).'.gif';
-                               $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']);
+                               $cmd = self::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']);
                                exec($cmd);
                                $theFile = $newFile;
                                        // unlink old file?? May be bad idea bacause TYPO3 would then recreate the file every time as TYPO3 thinks the file is not generated because it's missing!! So do not unlink $theFile here!!
@@ -523,7 +518,7 @@ final class t3lib_div {
                                return $theFile;
                        } else {
                                $newFile = PATH_site.'typo3temp/readPG_'.md5($theFile.'|'.filemtime($theFile)).($output_png?'.png':'.gif');
-                               $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']);
+                               $cmd = self::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']);
                                exec($cmd);
                                if (@is_file($newFile)) return $newFile;
                        }
@@ -593,7 +588,7 @@ final class t3lib_div {
        public static function fixed_lgd_pre($string,$chars)    {
                self::logDeprecatedFunction();
 
-               return strrev(t3lib_div::fixed_lgd(strrev($string),$chars));
+               return strrev(self::fixed_lgd(strrev($string),$chars));
        }
 
        /**
@@ -603,16 +598,17 @@ final class t3lib_div {
         * @param       string          string to truncate
         * @param       integer         must be an integer with an absolute value of at least 4. if negative the string is cropped from the right end.
         * @param       string          appendix to the truncated string
-        * @return      string          New string
+        * @return      string          cropped string
         */
        public static function fixed_lgd_cs($string, $chars, $appendString='...') {
                if (is_object($GLOBALS['LANG'])) {
                        return $GLOBALS['LANG']->csConvObj->crop($GLOBALS['LANG']->charSet, $string, $chars, $appendString);
                } elseif (is_object($GLOBALS['TSFE'])) {
-                       return $GLOBALS['TSFE']->csConvObj->crop($GLOBALS['TSFE']->charSet, $string, $chars, $appendString);
+                       $charSet = ($GLOBALS['TSFE']->renderCharset != '' ? $GLOBALS['TSFE']->renderCharset : $GLOBALS['TSFE']->defaultCharSet);
+                       return $GLOBALS['TSFE']->csConvObj->crop($charSet, $string, $chars, $appendString);
                } else {
                                // this case should not happen
-                       $csConvObj = t3lib_div::makeInstance('t3lib_cs');
+                       $csConvObj = self::makeInstance('t3lib_cs');
                        return $csConvObj->crop('iso-8859-1', $string, $chars, $appendString);
                }
        }
@@ -627,15 +623,15 @@ final class t3lib_div {
         * @deprecated since TYPO3 4.1 - Use PHP function wordwrap()
         * @return      string
         */
-       public static function breakTextForEmail($str,$implChar="\n",$charWidth=76)     {
+       public static function breakTextForEmail($str,$implChar=LF,$charWidth=76)       {
                self::logDeprecatedFunction();
 
-               $lines = explode(chr(10),$str);
+               $lines = explode(LF,$str);
                $outArr=array();
                foreach ($lines as $lStr) {
-                       $outArr[] = t3lib_div::breakLinesForEmail($lStr,$implChar,$charWidth);
+                       $outArr[] = self::breakLinesForEmail($lStr,$implChar,$charWidth);
                }
-               return implode(chr(10),$outArr);
+               return implode(LF,$outArr);
        }
 
        /**
@@ -648,7 +644,7 @@ final class t3lib_div {
         * @return      string
         * @see breakTextForEmail()
         */
-       public static function breakLinesForEmail($str,$implChar="\n",$charWidth=76)    {
+       public static function breakLinesForEmail($str,$implChar=LF,$charWidth=76)      {
                $lines=array();
                $l=$charWidth;
                $p=0;
@@ -691,10 +687,10 @@ final class t3lib_div {
                } elseif ($list === '*')        {
                        return true;
                }
-               if (strpos($baseIP, ':') !== false && t3lib_div::validIPv6($baseIP))    {
-                       return t3lib_div::cmpIPv6($baseIP, $list);
+               if (strpos($baseIP, ':') !== false && self::validIPv6($baseIP)) {
+                       return self::cmpIPv6($baseIP, $list);
                } else {
-                       return t3lib_div::cmpIPv4($baseIP, $list);
+                       return self::cmpIPv4($baseIP, $list);
                }
        }
 
@@ -708,7 +704,7 @@ final class t3lib_div {
        public static function cmpIPv4($baseIP, $list)  {
                $IPpartsReq = explode('.',$baseIP);
                if (count($IPpartsReq)==4)      {
-                       $values = t3lib_div::trimExplode(',',$list,1);
+                       $values = self::trimExplode(',',$list,1);
 
                        foreach($values as $test)       {
                                list($test,$mask) = explode('/',$test);
@@ -748,32 +744,32 @@ final class t3lib_div {
         */
        public static function cmpIPv6($baseIP, $list)  {
                $success = false;       // Policy default: Deny connection
-               $baseIP = t3lib_div::normalizeIPv6($baseIP);
+               $baseIP = self::normalizeIPv6($baseIP);
 
-               $values = t3lib_div::trimExplode(',',$list,1);
+               $values = self::trimExplode(',',$list,1);
                foreach ($values as $test)      {
                        list($test,$mask) = explode('/',$test);
-                       if (t3lib_div::validIPv6($test))        {
-                               $test = t3lib_div::normalizeIPv6($test);
+                       if (self::validIPv6($test))     {
+                               $test = self::normalizeIPv6($test);
                                if (intval($mask))      {
                                        switch ($mask) {        // test on /48 /64
                                                case '48':
-                                                       $testBin = substr(t3lib_div::IPv6Hex2Bin($test), 0, 48);
-                                                       $baseIPBin = substr(t3lib_div::IPv6Hex2Bin($baseIP), 0, 48);
+                                                       $testBin = substr(self::IPv6Hex2Bin($test), 0, 48);
+                                                       $baseIPBin = substr(self::IPv6Hex2Bin($baseIP), 0, 48);
                                                        $success = strcmp($testBin, $baseIPBin)==0 ? true : false;
                                                break;
                                                case '64':
-                                                       $testBin = substr(t3lib_div::IPv6Hex2Bin($test), 0, 64);
-                                                       $baseIPBin = substr(t3lib_div::IPv6Hex2Bin($baseIP), 0, 64);
+                                                       $testBin = substr(self::IPv6Hex2Bin($test), 0, 64);
+                                                       $baseIPBin = substr(self::IPv6Hex2Bin($baseIP), 0, 64);
                                                        $success = strcmp($testBin, $baseIPBin)==0 ? true : false;
                                                break;
                                                default:
                                                        $success = false;
                                        }
                                } else {
-                                       if (t3lib_div::validIPv6($test))        {       // test on full ip address 128 bits
-                                               $testBin = t3lib_div::IPv6Hex2Bin($test);
-                                               $baseIPBin = t3lib_div::IPv6Hex2Bin($baseIP);
+                                       if (self::validIPv6($test))     {       // test on full ip address 128 bits
+                                               $testBin = self::IPv6Hex2Bin($test);
+                                               $baseIPBin = self::IPv6Hex2Bin($baseIP);
                                                $success = strcmp($testBin, $baseIPBin)==0 ? true : false;
                                        }
                                }
@@ -898,7 +894,7 @@ final class t3lib_div {
        public static function cmpFQDN($baseIP, $list)        {
                if (count(explode('.',$baseIP))==4)     {
                        $resolvedHostName = explode('.', gethostbyaddr($baseIP));
-                       $values = t3lib_div::trimExplode(',',$list,1);
+                       $values = self::trimExplode(',',$list,1);
 
                        foreach($values as $test)       {
                                $hostNameParts = explode('.',$test);
@@ -917,6 +913,17 @@ final class t3lib_div {
        }
 
        /**
+        * Checks if a given URL matches the host that currently handles this HTTP request.
+        * Scheme, hostname and (optional) port of the given URL are compared.
+        *
+        * @param       string          $url: URL to compare with the TYPO3 request host
+        * @return      boolean         Whether the URL matches the TYPO3 request host
+        */
+       public static function isOnCurrentHost($url) {
+               return (stripos($url . '/', self::getIndpEnv('TYPO3_REQUEST_HOST') . '/') === 0);
+       }
+
+       /**
         * Check for item in list
         * Check if an item exists in a comma-separated list of items.
         * Usage: 163
@@ -1031,7 +1038,7 @@ final class t3lib_div {
                global $TYPO3_CONF_VARS;
                $currVersionStr = $TYPO3_CONF_VARS['SYS']['compat_version'] ? $TYPO3_CONF_VARS['SYS']['compat_version'] : TYPO3_branch;
 
-               if (t3lib_div::int_from_ver($currVersionStr) < t3lib_div::int_from_ver($verNumberStr))  {
+               if (self::int_from_ver($currVersionStr) < self::int_from_ver($verNumberStr))    {
                        return FALSE;
                } else {
                        return TRUE;
@@ -1063,6 +1070,36 @@ final class t3lib_div {
        }
 
        /**
+        * Returns a proper HMAC on a given input string and secret TYPO3 encryption key.
+        *
+        * @param       string          Input string to create HMAC from
+        * @return      string          resulting (hexadecimal) HMAC currently with a length of 40 (HMAC-SHA-1)
+        */
+       public static function hmac($input) {
+               $hashAlgorithm = 'sha1';
+               $hashBlocksize = 64;
+               $hmac = '';
+
+               if (extension_loaded('hash') && function_exists('hash_hmac') && function_exists('hash_algos') && in_array($hashAlgorithm, hash_algos())) {
+                       $hmac = hash_hmac($hashAlgorithm, $input, $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
+               } else {
+                               // outer padding
+                       $opad = str_repeat(chr(0x5C), $hashBlocksize);
+                               // innner padding
+                       $ipad = str_repeat(chr(0x36), $hashBlocksize);
+                       if (strlen($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']) > $hashBlocksize) {
+                                       // keys longer than blocksize are shorten
+                               $key = str_pad(pack('H*', call_user_func($hashAlgorithm, $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])), $hashBlocksize, chr(0x00));
+                       } else {
+                                       // keys shorter than blocksize are zero-padded
+                               $key = str_pad($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], $hashBlocksize, chr(0x00));
+                       }
+                       $hmac = call_user_func($hashAlgorithm, ($key^$opad) . pack('H*', call_user_func($hashAlgorithm, ($key^$ipad) . $input)));
+               }
+               return $hmac;
+       }
+
+       /**
         * Takes comma-separated lists and arrays and removes all duplicates
         * If a value in the list is trim(empty), the value is ignored.
         * Usage: 16
@@ -1072,10 +1109,20 @@ final class t3lib_div {
         * @return      string          Returns the list without any duplicates of values, space around values are trimmed
         */
        public static function uniqueList($in_list, $secondParameter=NULL)      {
-               if (is_array($in_list)) die('t3lib_div::uniqueList() does NOT support array arguments anymore! Only string comma lists!');
-               if (isset($secondParameter))    die('t3lib_div::uniqueList() does NOT support more than a single argument value anymore. You have specified more than one.');
+               if (is_array($in_list)) {
+                       throw new InvalidArgumentException(
+                               'TYPO3 Fatal Error: t3lib_div::uniqueList() does NOT support array arguments anymore! Only string comma lists!',
+                               1270853885
+                       );
+               }
+               if (isset($secondParameter)) {
+                       throw new InvalidArgumentException(
+                               'TYPO3 Fatal Error: t3lib_div::uniqueList() does NOT support more than a single argument value anymore. You have specified more than one!',
+                               1270853886
+                       );
+               }
 
-               return implode(',',array_unique(t3lib_div::trimExplode(',',$in_list,1)));
+               return implode(',',array_unique(self::trimExplode(',',$in_list,1)));
        }
 
        /**
@@ -1087,15 +1134,16 @@ final class t3lib_div {
         */
        public static function split_fileref($fileref)  {
                $reg = array();
-               if (    ereg('(.*/)(.*)$',$fileref,$reg)        )       {
+               if (preg_match('/(.*\/)(.*)$/', $fileref, $reg)) {
                        $info['path'] = $reg[1];
                        $info['file'] = $reg[2];
                } else {
                        $info['path'] = '';
                        $info['file'] = $fileref;
                }
-               $reg='';
-               if (    ereg('(.*)\.([^\.]*$)',$info['file'],$reg)      )       {
+
+               $reg = '';
+               if (!is_dir($fileref) && preg_match('/(.*)\.([^\.]*$)/', $info['file'], $reg)) {
                        $info['filebody'] = $reg[1];
                        $info['fileext'] = strtolower($reg[2]);
                        $info['realFileext'] = $reg[2];
@@ -1124,7 +1172,7 @@ final class t3lib_div {
         * @return      string          Processed input value. See function description.
         */
        public static function dirname($path)   {
-               $p = t3lib_div::revExplode('/',$path,2);
+               $p = self::revExplode('/',$path,2);
                return count($p)==2 ? $p[0] : '';
        }
 
@@ -1141,9 +1189,9 @@ final class t3lib_div {
         */
        public static function modifyHTMLColor($color,$R,$G,$B) {
                // This takes a hex-color (# included!) and adds $R, $G and $B to the HTML-color (format: #xxxxxx) and returns the new color
-               $nR = t3lib_div::intInRange(hexdec(substr($color,1,2))+$R,0,255);
-               $nG = t3lib_div::intInRange(hexdec(substr($color,3,2))+$G,0,255);
-               $nB = t3lib_div::intInRange(hexdec(substr($color,5,2))+$B,0,255);
+               $nR = self::intInRange(hexdec(substr($color,1,2))+$R,0,255);
+               $nG = self::intInRange(hexdec(substr($color,3,2))+$G,0,255);
+               $nB = self::intInRange(hexdec(substr($color,5,2))+$B,0,255);
                return '#'.
                        substr('0'.dechex($nR),-2).
                        substr('0'.dechex($nG),-2).
@@ -1160,7 +1208,7 @@ final class t3lib_div {
         * @see modifyHTMLColor()
         */
        public static function modifyHTMLColorAll($color,$all)  {
-               return t3lib_div::modifyHTMLColor($color,$all,$all,$all);
+               return self::modifyHTMLColor($color,$all,$all,$all);
        }
 
        /**
@@ -1171,7 +1219,7 @@ final class t3lib_div {
         * @return      string
         */
        public static function rm_endcomma($string)     {
-               return ereg_replace(',$','',$string);
+               return rtrim($string, ',');
        }
 
        /**
@@ -1187,7 +1235,23 @@ final class t3lib_div {
                self::logDeprecatedFunction();
 
                $value = strtoupper($string);
-               return strtr($value, 'áéúíâêûôîæøåäöü', 'ÁÉÚÍÄËÜÖÏÆØÅÄÖÜ');
+               return strtr($value, array(
+                       chr(225) => chr(193),
+                       chr(233) => chr(201),
+                       chr(250) => chr(218),
+                       chr(237) => chr(205),
+                       chr(226) => chr(196),
+                       chr(234) => chr(203),
+                       chr(251) => chr(220),
+                       chr(244) => chr(214),
+                       chr(238) => chr(207),
+                       chr(230) => chr(198),
+                       chr(248) => chr(216),
+                       chr(229) => chr(197),
+                       chr(228) => chr(196),
+                       chr(246) => chr(214),
+                       chr(252) => chr(220),
+               ));
        }
 
        /**
@@ -1203,9 +1267,9 @@ final class t3lib_div {
        public static function convUmlauts($str)        {
                self::logDeprecatedFunction();
 
-               $pat  = array ( '/ä/',  '/Ä/',  '/ö/',  '/Ö/',  '/ü/',  '/Ü/',  '/ß/',  '/å/',  '/Å/',  '/ø/',  '/Ø/',  '/æ/',  '/Æ/'   );
-               $repl = array ( 'ae',   'Ae',   'oe',   'Oe',   'ue',   'Ue',   'ss',   'aa',   'AA',   'oe',   'OE',   'ae',   'AE'    );
-               return preg_replace($pat,$repl,$str);
+               $pattern  = array (chr(228), chr(196), chr(246), chr(214), chr(252), chr(220), chr(223), chr(229), chr(197), chr(248), chr(216), chr(230), chr(198));
+               $replace = array ('ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss', 'aa', 'AA', 'oe', 'OE', 'ae', 'AE');
+               return str_replace($pattern, $replace, $str);
        }
 
        /**
@@ -1315,7 +1379,7 @@ final class t3lib_div {
         * @see calcParenthesis()
         */
        public static function calcPriority($string)    {
-               $string=ereg_replace('[[:space:]]*','',$string);        // removing all whitespace
+               $string=preg_replace('/[[:space:]]*/','',$string);      // removing all whitespace
                $string='+'.$string;    // Ensuring an operator for the first entrance
                $qm='\*\/\+-^%';
                $regex = '(['.$qm.'])(['.$qm.']?[0-9\.]*)';
@@ -1362,11 +1426,11 @@ final class t3lib_div {
                        $valueLenO=strcspn($string,'(');
                        $valueLenC=strcspn($string,')');
                        if ($valueLenC==strlen($string) || $valueLenC < $valueLenO)     {
-                               $value = t3lib_div::calcPriority(substr($string,0,$valueLenC));
+                               $value = self::calcPriority(substr($string,0,$valueLenC));
                                $string = $value.substr($string,$valueLenC+1);
                                return $string;
                        } else {
-                               $string = substr($string,0,$valueLenO).t3lib_div::calcParenthesis(substr($string,$valueLenO+1));
+                               $string = substr($string,0,$valueLenO).self::calcParenthesis(substr($string,$valueLenO+1));
                        }
                                // Security:
                        $securC--;
@@ -1398,7 +1462,7 @@ final class t3lib_div {
         * @return      string          Converted result.
         */
        public static function deHSCentities($str)      {
-               return ereg_replace('&amp;([#[:alnum:]]*;)','&\1',$str);
+               return preg_replace('/&amp;([#[:alnum:]]*;)/','&\1',$str);
        }
 
        /**
@@ -1491,7 +1555,7 @@ final class t3lib_div {
         * @return      string          Formatted for <textarea>-tags
         */
        public static function formatForTextarea($content)      {
-               return chr(10).htmlspecialchars($content);
+               return LF.htmlspecialchars($content);
        }
 
        /**
@@ -1535,22 +1599,111 @@ final class t3lib_div {
                if (TYPO3_OS != 'WIN' && ($fh = @fopen('/dev/urandom', 'rb'))) {
                        $output = fread($fh, $count);
                        fclose($fh);
+               } elseif (TYPO3_OS == 'WIN') {
+                       if (class_exists('COM')) {
+                               try {
+                                       $com = new COM('CAPICOM.Utilities.1');
+                                       $output = base64_decode($com->GetRandom($count, 0));
+                               } catch(Exception $e) {
+                                       // CAPICOM not installed
+                               }
+                       }
+                       if ($output === '' && version_compare(PHP_VERSION, '5.3.0', '>=')) {
+                               if (function_exists('mcrypt_create_iv')) {
+                                       $output = mcrypt_create_iv($count, MCRYPT_DEV_URANDOM);
+                               } elseif (function_exists('openssl_random_pseudo_bytes')) {
+                                       $isStrong = null;
+                                       $output = openssl_random_pseudo_bytes($count, $isStrong);
+                                               // skip ssl since it wasn't using the strong algo
+                                       if ($isStrong !== TRUE) {
+                                               $output = '';
+                                       }
+                               }
+                       }
                }
 
-                       // fallback if /dev/urandom is not available
+                       // fallback if other random byte generation failed until now
                if (!isset($output{$count - 1})) {
                                // We initialize with the somewhat random.
                        $randomState = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
-                                                       . microtime() . getmypid();
+                                                       . base_convert(memory_get_usage() % pow(10, 6), 10, 2)
+                                                       . microtime() . uniqid('') . getmypid();
                        while (!isset($output{$count - 1})) {
-                               $randomState = md5(microtime() . mt_rand() . $randomState);
-                               $output .= md5(mt_rand() . $randomState, true);
+                               $randomState = sha1(microtime() . mt_rand() . $randomState);
+                               $output .= sha1(mt_rand() . $randomState, true);
                        }
                        $output = substr($output, strlen($output) - $count, $count);
                }
                return $output;
        }
 
+       /**
+        * Returns a hex representation of a random byte string.
+        *
+        * @param               integer  Number of hex characters to return
+        * @return              string   Random Bytes
+        */
+       public static function getRandomHexString($count) {
+               return substr(bin2hex(self::generateRandomBytes(intval(($count + 1) / 2))), 0, $count);
+       }
+
+       /**
+        * Returns a given string with underscores as UpperCamelCase.
+        * Example: Converts blog_example to BlogExample
+        *
+        * @param       string          $string: String to be converted to camel case
+        * @return      string          UpperCamelCasedWord
+        */
+       public static function underscoredToUpperCamelCase($string) {
+               $upperCamelCase = str_replace(' ', '', ucwords(str_replace('_', ' ', self::strtolower($string))));
+               return $upperCamelCase;
+       }
+
+       /**
+        * Returns a given string with underscores as lowerCamelCase.
+        * Example: Converts minimal_value to minimalValue
+        *
+        * @param       string          $string: String to be converted to camel case
+        * @return      string          lowerCamelCasedWord
+        */
+       public static function underscoredToLowerCamelCase($string) {
+               $upperCamelCase = str_replace(' ', '', ucwords(str_replace('_', ' ', self::strtolower($string))));
+               $lowerCamelCase = self::lcfirst($upperCamelCase);
+               return $lowerCamelCase;
+       }
+
+       /**
+        * Returns a given CamelCasedString as an lowercase string with underscores.
+        * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value
+        *
+        * @param       string          $string: String to be converted to lowercase underscore
+        * @return      string          lowercase_and_underscored_string
+        */
+       public static function camelCaseToLowerCaseUnderscored($string) {
+               return self::strtolower(preg_replace('/(?<=\w)([A-Z])/', '_\\1', $string));
+       }
+
+       /**
+        * Converts the first char of a string to lowercase if it is a latin character (A-Z).
+        * Example: Converts "Hello World" to "hello World"
+        *
+        * @param       string          $string: The string to be used to lowercase the first character
+        * @return      string          The string with the first character as lowercase
+        */
+       public static function lcfirst($string) {
+               return self::strtolower(substr($string, 0, 1)) . substr($string, 1);
+       }
+
+       /**
+        * Checks if a given string is a Uniform Resource Locator (URL).
+        *
+        * @param       string          $url: The URL to be validated
+        * @return      boolean         Whether the given URL is valid
+        */
+       public static function isValidUrl($url) {
+               return (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) !== false);
+       }
+
 
 
 
@@ -1611,13 +1764,9 @@ final class t3lib_div {
         *                                              if zero (default), the result is not limited at all
         * @return      array           Exploded values, all converted to integers
         */
-       public static function intExplode($delim, $string, $onlyNonEmptyValues = false, $limit = 0)     {
-               $temp = self::trimExplode($delim, $string, $onlyNonEmptyValues, $limit);
-               foreach ($temp as &$val) {
-                       $val = intval($val);
-               }
-               reset($temp);
-               return $temp;
+       public static function intExplode($delimiter, $string, $onlyNonEmptyValues = FALSE, $limit = 0) {
+               $explodedValues = self::trimExplode($delimiter, $string, $onlyNonEmptyValues, $limit);
+               return array_map('intval', $explodedValues);
        }
 
        /**
@@ -1630,14 +1779,10 @@ final class t3lib_div {
         * @param       integer         Number of array entries
         * @return      array           Exploded values
         */
-       public static function revExplode($delim, $string, $count=0)    {
-               $temp = explode($delim,strrev($string),$count);
-               foreach ($temp as &$val) {
-                       $val = strrev($val);
-               }
-               $temp = array_reverse($temp);
-               reset($temp);
-               return $temp;
+       public static function revExplode($delimiter, $string, $count=0) {
+               $explodedValues = explode($delimiter, strrev($string), $count);
+               $explodedValues = array_map('strrev', $explodedValues);
+               return array_reverse($explodedValues);
        }
 
        /**
@@ -1672,7 +1817,13 @@ final class t3lib_div {
                }
 
                if ($limit != 0) {
-                       $result = array_slice($result, 0, $limit);
+                       if ($limit < 0) {
+                               $result = array_slice($result, 0, $limit);
+                       } elseif (count($result) > $limit) {
+                               $lastElements = array_slice($result, $limit - 1);
+                               $result = array_slice($result, 0, $limit - 1);
+                               $result[] = implode($delim, $lastElements);
+                       }
                }
 
                return $result;
@@ -1704,12 +1855,11 @@ final class t3lib_div {
        public static function removeArrayEntryByValue(array $array, $cmpValue) {
                foreach ($array as $k => $v) {
                        if (is_array($v)) {
-                               $array[$k] = t3lib_div::removeArrayEntryByValue($v, $cmpValue);
+                               $array[$k] = self::removeArrayEntryByValue($v, $cmpValue);
                        } elseif (!strcmp($v, $cmpValue)) {
                                unset($array[$k]);
                        }
                }
-               reset($array);
                return $array;
        }
 
@@ -1740,7 +1890,7 @@ final class t3lib_div {
                if ($array) {
                                // Convert strings to arrays:
                        if (is_string($keepItems)) {
-                               $keepItems = t3lib_div::trimExplode(',', $keepItems);
+                               $keepItems = self::trimExplode(',', $keepItems);
                        }
                                // create_function() returns a string:
                        if (!is_string($getValueFunc)) {
@@ -1776,7 +1926,7 @@ final class t3lib_div {
                foreach($theArray as $Akey => $AVal)    {
                        $thisKeyName = $name ? $name.'['.$Akey.']' : $Akey;
                        if (is_array($AVal))    {
-                               $str = t3lib_div::implodeArrayForUrl($thisKeyName,$AVal,$str,$skipBlank,$rawurlencodeParamName);
+                               $str = self::implodeArrayForUrl($thisKeyName,$AVal,$str,$skipBlank,$rawurlencodeParamName);
                        } else {
                                if (!$skipBlank || strcmp($AVal,''))    {
                                        $str.='&'.($rawurlencodeParamName ? rawurlencode($thisKeyName) : $thisKeyName).
@@ -1822,13 +1972,13 @@ final class t3lib_div {
         * @return      array           Output array with selected variables.
         */
        public static function compileSelectedGetVarsFromArray($varList,array $getArray,$GPvarAlt=1)    {
-               $keys = t3lib_div::trimExplode(',',$varList,1);
+               $keys = self::trimExplode(',',$varList,1);
                $outArr = array();
                foreach($keys as $v)    {
                        if (isset($getArray[$v]))       {
                                $outArr[$v] = $getArray[$v];
                        } elseif ($GPvarAlt) {
-                               $outArr[$v] = t3lib_div::_GP($v);
+                               $outArr[$v] = self::_GP($v);
                        }
                }
                return $outArr;
@@ -1847,10 +1997,11 @@ final class t3lib_div {
        public static function addSlashesOnArray(array &$theArray)      {
                foreach ($theArray as &$value) {
                        if (is_array($value)) {
-                               t3lib_div::addSlashesOnArray($value);
+                               self::addSlashesOnArray($value);
                        } else {
                                $value = addslashes($value);
                        }
+                       unset($value);
                }
                reset($theArray);
        }
@@ -1868,10 +2019,11 @@ final class t3lib_div {
        public static function stripSlashesOnArray(array &$theArray)    {
                foreach ($theArray as &$value) {
                        if (is_array($value)) {
-                               t3lib_div::stripSlashesOnArray($value);
+                               self::stripSlashesOnArray($value);
                        } else {
                                $value = stripslashes($value);
                        }
+                       unset($value);
                }
                reset($theArray);
        }
@@ -1885,12 +2037,29 @@ final class t3lib_div {
         * @return      array
         */
        public static function slashArray(array $arr,$cmd)      {
-               if ($cmd=='strip')      t3lib_div::stripSlashesOnArray($arr);
-               if ($cmd=='add')        t3lib_div::addSlashesOnArray($arr);
+               if ($cmd=='strip')      self::stripSlashesOnArray($arr);
+               if ($cmd=='add')        self::addSlashesOnArray($arr);
                return $arr;
        }
 
        /**
+       * Rename Array keys with a given mapping table
+       * @param        array   Array by reference which should be remapped
+       * @param        array   Array with remap information, array/$oldKey => $newKey)
+       */
+       function remapArrayKeys(&$array, $mappingTable) {
+               if (is_array($mappingTable)) {
+                       foreach ($mappingTable as $old => $new) {
+                               if ($new && isset($array[$old])) {
+                                       $array[$new] = $array[$old];
+                                       unset ($array[$old]);
+                               }
+                       }
+               }
+       }
+
+
+       /**
         * Merges two arrays recursively and "binary safe" (integer keys are
         * overridden as well), overruling similar values in the first array
         * ($arr0) with the values of the second array ($arr1)
@@ -1907,7 +2076,7 @@ final class t3lib_div {
                foreach ($arr1 as $key => $val) {
                        if(is_array($arr0[$key])) {
                                if (is_array($arr1[$key]))      {
-                                       $arr0[$key] = t3lib_div::array_merge_recursive_overrule($arr0[$key],$arr1[$key],$notAddKeys,$includeEmtpyValues);
+                                       $arr0[$key] = self::array_merge_recursive_overrule($arr0[$key],$arr1[$key],$notAddKeys,$includeEmtpyValues);
                                }
                        } else {
                                if ($notAddKeys) {
@@ -1940,6 +2109,29 @@ final class t3lib_div {
        }
 
        /**
+        * Filters keys off from first array that also exist in second array. Comparision is done by keys.
+        * This method is a recursive version of php array_diff_assoc()
+        *
+        * @param       array           Source array
+        * @param       array           Reduce source array by this array
+        * @return      array           Source array reduced by keys also present in second array
+        */
+       public static function arrayDiffAssocRecursive(array $array1, array $array2) {
+               $differenceArray = array();
+               foreach ($array1 as $key => $value) {
+                       if (!array_key_exists($key, $array2)) {
+                               $differenceArray[$key] = $value;
+                       } elseif (is_array($value)) {
+                               if (is_array($array2[$key])) {
+                                       $differenceArray[$key] = self::arrayDiffAssocRecursive($value, $array2[$key]);
+                               }
+                       }
+               }
+
+               return $differenceArray;
+       }
+
+       /**
         * Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars.
         * Usage: 5
         *
@@ -1984,7 +2176,7 @@ final class t3lib_div {
                foreach ($ts as $key => $value) {
                        if (is_array($value)) {
                                $key = rtrim($key, '.');
-                               $out[$key] = t3lib_div::removeDotsFromTS($value);
+                               $out[$key] = self::removeDotsFromTS($value);
                        } else {
                                $out[$key] = $value;
                        }
@@ -2023,7 +2215,7 @@ final class t3lib_div {
         * @return      array           Array with the attribute values.
         */
        public static function get_tag_attributes($tag) {
-               $components = t3lib_div::split_tag_attributes($tag);
+               $components = self::split_tag_attributes($tag);
                $name = '';      // attribute name is stored here
                $valuemode = false;
                $attributes = array();
@@ -2035,7 +2227,7 @@ final class t3lib_div {
                                                $name = '';
                                        }
                                } else {
-                                       if ($key = strtolower(ereg_replace('[^a-zA-Z0-9]','',$val)))    {
+                                       if ($key = strtolower(preg_replace('/[^a-zA-Z0-9]/','',$val)))  {
                                                $attributes[$key] = '';
                                                $name = $key;
                                        }
@@ -2057,9 +2249,9 @@ final class t3lib_div {
         * @return      array           Array with the attribute values.
         */
        public static function split_tag_attributes($tag)       {
-               $tag_tmp = trim(eregi_replace ('^<[^[:space:]]*','',trim($tag)));
+               $tag_tmp = trim(preg_replace('/^<[^[:space:]]*/','',trim($tag)));
                        // Removes any > in the end of the string
-               $tag_tmp = trim(eregi_replace ('>$','',$tag_tmp));
+               $tag_tmp = trim(rtrim($tag_tmp, '>'));
 
                $value = array();
                while (strcmp($tag_tmp,''))     {       // Compared with empty string instead , 030102
@@ -2119,7 +2311,7 @@ final class t3lib_div {
        public static function implodeParams(array $arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE)   {
                self::logDeprecatedFunction();
 
-               return t3lib_div::implodeAttributes($arr,$xhtmlSafe,$dontOmitBlankAttribs);
+               return self::implodeAttributes($arr,$xhtmlSafe,$dontOmitBlankAttribs);
        }
 
        /**
@@ -2136,14 +2328,14 @@ final class t3lib_div {
        public static function wrapJS($string, $linebreak=TRUE) {
                if(trim($string)) {
                                // <script wrapped in nl?
-                       $cr = $linebreak? "\n" : '';
+                       $cr = $linebreak? LF : '';
 
                                // remove nl from the beginning
                        $string = preg_replace ('/^\n+/', '', $string);
                                // re-ident to one tab using the first line as reference
                        $match = array();
                        if(preg_match('/^(\t+)/',$string,$match)) {
-                               $string = str_replace($match[1],"\t", $string);
+                               $string = str_replace($match[1],TAB, $string);
                        }
                        $string = $cr.'<script type="text/javascript">
 /*<![CDATA[*/
@@ -2212,8 +2404,8 @@ final class t3lib_div {
                                                        $startPoint+1,
                                                        $key-$startPoint-1
                                                );
-                                               #$oldtagi=array('XMLvalue'=>t3lib_div::xmlRecompileFromStructValArray($partArray));
-                                               $oldtagi['XMLvalue']=t3lib_div::xmlRecompileFromStructValArray($partArray);
+                                               #$oldtagi=array('XMLvalue'=>self::xmlRecompileFromStructValArray($partArray));
+                                               $oldtagi['XMLvalue']=self::xmlRecompileFromStructValArray($partArray);
                                        } else {
                                                $oldtagi['XMLvalue']=$oldtagi['values'][0];
                                        }
@@ -2254,8 +2446,8 @@ final class t3lib_div {
                }
 
                        // Return XML:
-               return '<?xml version="1.0" encoding="'.htmlspecialchars($charset).'" standalone="yes" ?>'.chr(10).
-                               t3lib_div::array2xml($array,'',0,$docTag,0, $options);
+               return '<?xml version="1.0" encoding="'.htmlspecialchars($charset).'" standalone="yes" ?>'.LF.
+                               self::array2xml($array,'',0,$docTag,0, $options);
        }
 
        /**
@@ -2275,7 +2467,7 @@ final class t3lib_div {
         * @param       string          tag-prefix, eg. a namespace prefix like "T3:"
         * @param       integer         Current recursion level. Don't change, stay at zero!
         * @param       string          Alternative document tag. Default is "phparray".
-        * @param       integer         If greater than zero, then the number of spaces corresponding to this number is used for indenting, if less than zero - no indentation, if zero - a single chr(9) (TAB) is used
+        * @param       integer         If greater than zero, then the number of spaces corresponding to this number is used for indenting, if less than zero - no indentation, if zero - a single TAB is used
         * @param       array           Options for the compilation. Key "useNindex" => 0/1 (boolean: whether to use "n0, n1, n2" for num. indexes); Key "useIndexTagForNum" => "[tag for numerical indexes]"; Key "useIndexTagForAssoc" => "[tag for associative indexes"; Key "parentTagMap" => array('parentTag' => 'thisLevelTag')
         * @param       string          Stack data. Don't touch.
         * @return      string          An XML string made from the input content in the array.
@@ -2288,9 +2480,9 @@ final class t3lib_div {
                                                chr(20).chr(21).chr(22).chr(23).chr(24).chr(25).chr(26).chr(27).chr(28).chr(29).
                                                chr(30).chr(31);
                        // Set indenting mode:
-               $indentChar = $spaceInd ? ' ' : chr(9);
+               $indentChar = $spaceInd ? ' ' : TAB;
                $indentN = $spaceInd>0 ? $spaceInd : 1;
-               $nl = ($spaceInd >= 0 ? chr(10) : '');
+               $nl = ($spaceInd >= 0 ? LF : '');
 
                        // Init output variable:
                $output='';
@@ -2304,7 +2496,7 @@ final class t3lib_div {
                        if(isset($options['grandParentTagMap'][$stackData['grandParentTagName'].'/'.$stackData['parentTagName']])) {            // Use tag based on grand-parent + parent tag name
                                $attr.=' index="'.htmlspecialchars($tagName).'"';
                                $tagName = (string)$options['grandParentTagMap'][$stackData['grandParentTagName'].'/'.$stackData['parentTagName']];
-                       }elseif(isset($options['parentTagMap'][$stackData['parentTagName'].':_IS_NUM']) && t3lib_div::testInt($tagName)) {              // Use tag based on parent tag name + if current tag is numeric
+                       }elseif(isset($options['parentTagMap'][$stackData['parentTagName'].':_IS_NUM']) && self::testInt($tagName)) {           // Use tag based on parent tag name + if current tag is numeric
                                $attr.=' index="'.htmlspecialchars($tagName).'"';
                                $tagName = (string)$options['parentTagMap'][$stackData['parentTagName'].':_IS_NUM'];
                        }elseif(isset($options['parentTagMap'][$stackData['parentTagName'].':'.$tagName])) {            // Use tag based on parent tag name + current tag
@@ -2326,7 +2518,7 @@ final class t3lib_div {
                        }
 
                                // The tag name is cleaned up so only alphanumeric chars (plus - and _) are in there and not longer than 100 chars either.
-                       $tagName = substr(ereg_replace('[^[:alnum:]_-]','',$tagName),0,100);
+                       $tagName = substr(preg_replace('/[^[:alnum:]_-]/','',$tagName),0,100);
 
                                // If the value is an array then we will call this function recursively:
                        if (is_array($v))       {
@@ -2341,7 +2533,7 @@ final class t3lib_div {
                                }
 
                                $content = $nl .
-                                                       t3lib_div::array2xml(
+                                                       self::array2xml(
                                                                $v,
                                                                $NSprefix,
                                                                $level+1,
@@ -2398,15 +2590,51 @@ final class t3lib_div {
        /**
         * Converts an XML string to a PHP array.
         * This is the reverse function of array2xml()
+        * This is a wrapper for xml2arrayProcess that adds a two-level cache
         * Usage: 17
         *
         * @param       string          XML content to convert into an array
         * @param       string          The tag-prefix resolve, eg. a namespace like "T3:"
         * @param       boolean         If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
         * @return      mixed           If the parsing had errors, a string with the error message is returned. Otherwise an array with the content.
-        * @see array2xml()
+        * @see array2xml(),xml2arrayProcess()
+        * @author      Fabrizio Branca <typo3@fabrizio-branca.de> (added caching)
         */
        public static function xml2array($string,$NSprefix='',$reportDocTag=FALSE) {
+               static $firstLevelCache = array();
+
+               $identifier = md5($string . $NSprefix . ($reportDocTag ? '1' : '0'));
+
+                       // look up in first level cache
+               if (!empty($firstLevelCache[$identifier])) {
+                       $array = $firstLevelCache[$identifier];
+               } else {
+                               // look up in second level cache
+                       $cacheContent = t3lib_pageSelect::getHash($identifier, 0);
+                       $array = unserialize($cacheContent);
+
+                       if ($array === false) {
+                               $array = self::xml2arrayProcess($string, $NSprefix, $reportDocTag);
+                               t3lib_pageSelect::storeHash($identifier, serialize($array), 'ident_xml2array');
+                       }
+                               // store content in first level cache
+                       $firstLevelCache[$identifier] = $array;
+               }
+               return $array;
+       }
+
+       /**
+        * Converts an XML string to a PHP array.
+        * This is the reverse function of array2xml()
+        * Usage: 1
+        *
+        * @param       string          XML content to convert into an array
+        * @param       string          The tag-prefix resolve, eg. a namespace like "T3:"
+        * @param       boolean         If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
+        * @return      mixed           If the parsing had errors, a string with the error message is returned. Otherwise an array with the content.
+        * @see array2xml()
+        */
+       protected function xml2arrayProcess($string,$NSprefix='',$reportDocTag=FALSE) {
                global $TYPO3_CONF_VARS;
 
                        // Create parser:
@@ -2558,7 +2786,7 @@ final class t3lib_div {
        public static function xmlGetHeaderAttribs($xmlData)    {
                $match = array();
                if (preg_match('/^\s*<\?xml([^>]*)\?\>/', $xmlData, $match))    {
-                       return t3lib_div::get_tag_attributes($match[1]);
+                       return self::get_tag_attributes($match[1]);
                }
        }
 
@@ -2573,11 +2801,11 @@ final class t3lib_div {
                require_once(PATH_typo3 . 'contrib/jsmin/jsmin.php');
                try {
                        $error = '';
-                       $script = trim(JSMin::minify(str_replace(chr(13), '', $script)));
+                       $script = trim(JSMin::minify(str_replace(CR, '', $script)));
                }
                catch(JSMinException $e) {
                        $error = 'Error while minifying JavaScript: ' . $e->getMessage();
-                       t3lib_div::devLog($error, 't3lib_div', 2,
+                       self::devLog($error, 't3lib_div', 2,
                                array('JavaScript' => $script, 'Stack trace' => $e->getTrace()));
                }
                return $script;
@@ -2637,6 +2865,7 @@ final class t3lib_div {
                        curl_setopt($ch, CURLOPT_HTTPGET, $includeHeader == 2 ? 'HEAD' : 'GET');
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
+                       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, max(0, intval($GLOBALS['TYPO3_CONF_VARS']['SYS']['curlTimeout'])));
 
                                // may fail (PHP 5.2.0+ and 5.1.5+) when open_basedir or safe_mode are enabled
                        $followLocation = @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
@@ -2711,12 +2940,12 @@ final class t3lib_div {
                        $method = ($includeHeader == 2) ? 'HEAD' : 'GET';
                        $msg = $method . ' ' . $parsedURL['path'] .
                                        ($parsedURL['query'] ? '?' . $parsedURL['query'] : '') .
-                                       ' HTTP/1.0' . "\r\n" . 'Host: ' .
+                                       ' HTTP/1.0' . CRLF . 'Host: ' .
                                        $parsedURL['host'] . "\r\nConnection: close\r\n";
                        if (is_array($requestHeaders))  {
-                               $msg .= implode("\r\n", $requestHeaders) . "\r\n";
+                               $msg .= implode(CRLF, $requestHeaders) . CRLF;
                        }
-                       $msg .= "\r\n";
+                       $msg .= CRLF;
 
                        fputs($fp, $msg);
                        while (!feof($fp))      {
@@ -2753,7 +2982,7 @@ final class t3lib_div {
                        }
                        $ctx = stream_context_create(array(
                                                'http' => array(
-                                                       'header' => implode("\r\n", $requestHeaders)
+                                                       'header' => implode(CRLF, $requestHeaders)
                                                )
                                        )
                                );
@@ -2801,7 +3030,7 @@ final class t3lib_div {
                        if ($res===false)       return false;
 
                        if ($changePermissions) {       // Change the permissions only if the file has just been created
-                               t3lib_div::fixPermissions($file);
+                               self::fixPermissions($file);
                        }
 
                        return true;
@@ -2811,18 +3040,55 @@ final class t3lib_div {
        }
 
        /**
-        * Setting file system mode & group ownership of file
+        * Sets the file system mode and group ownership of a file or a folder.
         *
-        * @param       string          Filepath of newly created file
-        * @return      void
+        * @param   string   Absolute filepath of file or folder, must not be escaped.
+        * @param   boolean  If set, also fixes permissions of files and folders in the folder (if $path is a folder)
+        * @return  mixed    TRUE on success, FALSE on error, always TRUE on Windows OS
         */
-       public static function fixPermissions($file)    {
-               if (@is_file($file) && TYPO3_OS!='WIN') {
-                       @chmod($file, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask']));             // "@" is there because file is not necessarily OWNED by the user
-                       if($GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'])    {       // skip this if createGroup is empty
-                               @chgrp($file, $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']);                // "@" is there because file is not necessarily OWNED by the user
+       public static function fixPermissions($path, $recursive = FALSE) {
+               if (TYPO3_OS != 'WIN') {
+                       $result = FALSE;
+                       if (self::isAllowedAbsPath($path)) {
+                               if (@is_file($path)) {
+                                               // "@" is there because file is not necessarily OWNED by the user
+                                       $result = @chmod($path, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask']));
+                               } elseif (@is_dir($path)) {
+                                       $path = preg_replace('|/$|', '', $path);
+                                               // "@" is there because file is not necessarily OWNED by the user
+                                       $result = @chmod($path, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
+                               }
+
+                                       // Set createGroup if not empty
+                               if($GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']) {
+                                               // "@" is there because file is not necessarily OWNED by the user
+                                       $changeGroupResult = @chgrp($path, $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']);
+                                       $result = $changeGroupResult ? $result : FALSE;
+                               }
+
+                                       // Call recursive if recursive flag if set and $path is directory
+                               if ($recursive && @is_dir($path)) {
+                                       $handle = opendir($path);
+                                       while (($file = readdir($handle)) !== FALSE) {
+                                               unset($recursionResult);
+                                               if ($file !== '.' && $file !== '..') {
+                                                       if (@is_file($path . '/' . $file)) {
+                                                               $recursionResult = self::fixPermissions($path . '/' . $file);
+                                                       } elseif (@is_dir($path . '/' . $file)) {
+                                                               $recursionResult = self::fixPermissions($path . '/' . $file, TRUE);
+                                                       }
+                                                       if (isset($recursionResult) && !$recursionResult) {
+                                                               $result = FALSE;
+                                                       }
+                                               }
+                                       }
+                                       closedir($handle);
+                               }
                        }
+               } else {
+                       $result = TRUE;
                }
+               return $result;
        }
 
        /**
@@ -2840,26 +3106,26 @@ final class t3lib_div {
                $fI['dirname'].= '/';
 
                        // Check parts:
-               if (t3lib_div::validPathStr($filepath) && $fI['basename'] && strlen($fI['basename'])<60)        {
+               if (self::validPathStr($filepath) && $fI['basename'] && strlen($fI['basename'])<60)     {
                        if (defined('PATH_site'))       {
                                $dirName = PATH_site.'typo3temp/';      // Setting main temporary directory name (standard)
                                if (@is_dir($dirName))  {
-                                       if (t3lib_div::isFirstPartOfStr($fI['dirname'],$dirName))       {
+                                       if (self::isFirstPartOfStr($fI['dirname'],$dirName))    {
 
                                                        // Checking if the "subdir" is found:
                                                $subdir = substr($fI['dirname'],strlen($dirName));
                                                if ($subdir)    {
-                                                       if (ereg('^[[:alnum:]_]+\/$',$subdir) || ereg('^[[:alnum:]_]+\/[[:alnum:]_]+\/$',$subdir))      {
+                                                       if (preg_match('/^[[:alnum:]_]+\/$/',$subdir) || preg_match('/^[[:alnum:]_]+\/[[:alnum:]_]+\/$/',$subdir))      {
                                                                $dirName.= $subdir;
                                                                if (!@is_dir($dirName)) {
-                                                                       t3lib_div::mkdir_deep(PATH_site.'typo3temp/', $subdir);
+                                                                       self::mkdir_deep(PATH_site.'typo3temp/', $subdir);
                                                                }
                                                        } else return 'Subdir, "'.$subdir.'", was NOT on the form "[[:alnum:]_]/" or  "[[:alnum:]_]/[[:alnum:]_]/"';
                                                }
                                                        // Checking dir-name again (sub-dir might have been created):
                                                if (@is_dir($dirName))  {
                                                        if ($filepath == $dirName.$fI['basename'])      {
-                                                               t3lib_div::writeFile($filepath, $content);
+                                                               self::writeFile($filepath, $content);
                                                                if (!@is_file($filepath))       return 'File not written to disk! Write permission error in filesystem?';
                                                        } else return 'Calculated filelocation didn\'t match input $filepath!';
                                                } else return '"'.$dirName.'" is not a directory!';
@@ -2870,24 +3136,20 @@ final class t3lib_div {
        }
 
        /**
-        * Wrapper function for mkdir, setting folder permissions according to $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] and group ownership according to $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']
-        * Usage: 6
+        * Wrapper function for mkdir.
+        * Sets folder permissions according to $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']
+        * and group ownership according to $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']
         *
         * @param       string          Absolute path to folder, see PHP mkdir() function. Removes trailing slash internally.
         * @return      boolean         TRUE if @mkdir went well!
         */
-       public static function mkdir($theNewFolder)     {
-               $theNewFolder = preg_replace('|/$|','',$theNewFolder);
-               if (@mkdir($theNewFolder, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']))){
-                       chmod($theNewFolder, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'])); //added this line, because the mode at 'mkdir' has a strange behaviour sometimes
-
-                       if($GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'])    {       // skip this if createGroup is empty
-                               @chgrp($theNewFolder, $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']);
-                       }
-                       return true;
-               } else {
-                       return false;
+       public static function mkdir($newFolder) {
+               $newFolder = preg_replace('|/$|', '', $newFolder);
+               $result = @mkdir($newFolder, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
+               if ($result) {
+                       self::fixPermissions($newFolder);
                }
+               return $result;
        }
 
        /**
@@ -2898,12 +3160,12 @@ final class t3lib_div {
         * @return      string          If error, returns error string.
         */
        public static function mkdir_deep($destination,$deepDir)        {
-               $allParts = t3lib_div::trimExplode('/',$deepDir,1);
+               $allParts = self::trimExplode('/',$deepDir,1);
                $root = '';
                foreach($allParts as $part)     {
                        $root.= $part.'/';
                        if (!is_dir($destination.$root))        {
-                               t3lib_div::mkdir($destination.$root);
+                               self::mkdir($destination.$root);
                                if (!@is_dir($destination.$root))       {
                                        return 'Error: The directory "'.$destination.$root.'" could not be created...';
                                }
@@ -2929,7 +3191,7 @@ final class t3lib_div {
                                if ($removeNonEmpty==true && $handle = opendir($path))  {
                                        while ($OK && false !== ($file = readdir($handle)))     {
                                                if ($file=='.' || $file=='..') continue;
-                                               $OK = t3lib_div::rmdir($path.'/'.$file,$removeNonEmpty);
+                                               $OK = self::rmdir($path.'/'.$file,$removeNonEmpty);
                                        }
                                        closedir($handle);
                                }
@@ -2954,18 +3216,20 @@ final class t3lib_div {
         * @return      array           Returns an array with the directory entries as values. If no path, the return value is nothing.
         */
        public static function get_dirs($path)  {
-               if ($path)      {
-                       $d = @dir($path);
-                       if (is_object($d))      {
-                               while(false !== ($entry=$d->read())) {
-                                       if (@is_dir($path.'/'.$entry) && $entry!= '..' && $entry!= '.') {
-                                               $filearray[]=$entry;
+               if ($path) {
+                       if (is_dir($path)) {
+                               $dir = scandir($path);
+                               $dirs = array();
+                               foreach ($dir as $entry) {
+                                       if (is_dir($path . '/' . $entry) && $entry != '..' && $entry != '.') {
+                                               $dirs[] = $entry;
                                        }
                                }
-                               $d->close();
-                       } else return 'error';
-                       return $filearray;
+                       } else {
+                               $dirs = 'error';
+                       }
                }
+               return $dirs;
        }
 
        /**
@@ -2984,7 +3248,7 @@ final class t3lib_div {
                        // Initialize variabels:
                $filearray = array();
                $sortarray = array();
-               $path = ereg_replace('\/$','',$path);
+               $path = rtrim($path, '/');
 
                        // Find files+directories:
                if (@is_dir($path))     {
@@ -2995,7 +3259,7 @@ final class t3lib_div {
                                        if (@is_file($path.'/'.$entry)) {
                                                $fI = pathinfo($entry);
                                                $key = md5($path.'/'.$entry);   // Don't change this ever - extensions may depend on the fact that the hash is an md5 of the path! (import/export extension)
-                                               if ((!strlen($extensionList) || t3lib_div::inList($extensionList,strtolower($fI['extension']))) && (!strlen($excludePattern) || !preg_match('/^'.$excludePattern.'$/',$entry))) {
+                                               if ((!strlen($extensionList) || self::inList($extensionList,strtolower($fI['extension']))) && (!strlen($excludePattern) || !preg_match('/^'.$excludePattern.'$/',$entry)))      {
                                                        $filearray[$key]=($prependPath?$path.'/':'').$entry;
                                                                if ($order=='mtime') {$sortarray[$key]=filemtime($path.'/'.$entry);}
                                                                elseif ($order) {$sortarray[$key]=$entry;}
@@ -3035,13 +3299,13 @@ final class t3lib_div {
         */
        public static function getAllFilesAndFoldersInPath(array $fileArr,$path,$extList='',$regDirs=0,$recursivityLevels=99,$excludePattern='')        {
                if ($regDirs)   $fileArr[] = $path;
-               $fileArr = array_merge($fileArr, t3lib_div::getFilesInDir($path,$extList,1,1,$excludePattern));
+               $fileArr = array_merge($fileArr, self::getFilesInDir($path,$extList,1,1,$excludePattern));
 
-               $dirs = t3lib_div::get_dirs($path);
+               $dirs = self::get_dirs($path);
                if (is_array($dirs) && $recursivityLevels>0)    {
                        foreach ($dirs as $subdirs)     {
                                if ((string)$subdirs!='' && (!strlen($excludePattern) || !preg_match('/^'.$excludePattern.'$/',$subdirs)))      {
-                                       $fileArr = t3lib_div::getAllFilesAndFoldersInPath($fileArr,$path.$subdirs.'/',$extList,$regDirs,$recursivityLevels-1,$excludePattern);
+                                       $fileArr = self::getAllFilesAndFoldersInPath($fileArr,$path.$subdirs.'/',$extList,$regDirs,$recursivityLevels-1,$excludePattern);
                                }
                        }
                }
@@ -3058,7 +3322,7 @@ final class t3lib_div {
         */
        public static function removePrefixPathFromList(array $fileArr,$prefixToRemove) {
                foreach ($fileArr as $k => &$absFileRef) {
-                       if (t3lib_div::isFirstPartOfStr($absFileRef, $prefixToRemove)) {
+                       if (self::isFirstPartOfStr($absFileRef, $prefixToRemove)) {
                                $absFileRef = substr($absFileRef, strlen($prefixToRemove));
                        } else {
                                return 'ERROR: One or more of the files was NOT prefixed with the prefix-path!';
@@ -3117,16 +3381,16 @@ final class t3lib_div {
        public static function locationHeaderUrl($path) {
                $uI = parse_url($path);
                if (substr($path,0,1)=='/')     { // relative to HOST
-                       $path = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').$path;
+                       $path = self::getIndpEnv('TYPO3_REQUEST_HOST').$path;
                } elseif (!$uI['scheme'])       { // No scheme either
-                       $path = t3lib_div::getIndpEnv('TYPO3_REQUEST_DIR').$path;
+                       $path = self::getIndpEnv('TYPO3_REQUEST_DIR').$path;
                }
                return $path;
        }
 
        /**
         * Returns the maximum upload size for a file that is allowed. Measured in KB.
-        * This might be handy to find out the real upload limit that is possible for this 
+        * This might be handy to find out the real upload limit that is possible for this
         * TYPO3 installation. The first parameter can be used to set something that overrides
         * the maxFileSize, usually for the TCA values.
         *
@@ -3171,9 +3435,95 @@ final class t3lib_div {
                return $bytes;
        }
 
+       /**
+        * Retrieves the maximum path length that is valid in the current environment.
+        *
+        * @return integer The maximum available path length
+        * @author Ingo Renner <ingo@typo3.org>
+        */
+       public static function getMaximumPathLength() {
+               $maximumPathLength = 0;
 
+               if (version_compare(PHP_VERSION, '5.3.0', '<')) {
+                               // rough assumptions
+                       if (TYPO3_OS == 'WIN') {
+                                       // WIN is usually 255, Vista 260, although NTFS can hold about 2k
+                               $maximumPathLength = 255;
+                       } else {
+                               $maximumPathLength = 2048;
+                       }
+               } else {
+                               // precise information is available since PHP 5.3
+                       $maximumPathLength = PHP_MAXPATHLEN;
+               }
+
+               return $maximumPathLength;
+       }
 
 
+       /**
+        * Function for static version numbers on files, based on the filemtime
+        *
+        * This will make the filename automatically change when a file is
+        * changed, and by that re-cached by the browser. If the file does not
+        * exist physically the original file passed to the function is
+        * returned without the timestamp.
+        *
+        * Behaviour is influenced by the setting
+        * TYPO3_CONF_VARS[TYPO3_MODE][versionNumberInFilename]
+        * = true (BE) / "embed" (FE) : modify filename
+        * = false (BE) / "querystring" (FE) : add timestamp as parameter
+        *
+        * @param string $file Relative path to file including all potential query parameters (not htmlspecialchared yet)
+        * @param boolean $forceQueryString If settings would suggest to embed in filename, this parameter allows us to force the versioning to occur in the query string. This is needed for scriptaculous.js which cannot have a different filename in order to load its modules (?load=...)
+        * @return Relative path with version filename including the timestamp
+        * @author Lars Houmark <lars@houmark.com>
+        */
+       public static function createVersionNumberedFilename($file, $forceQueryString = FALSE) {
+               $lookupFile = explode('?', $file);
+               $path = self::resolveBackPath(self::dirname(PATH_thisScript) .'/'. $lookupFile[0]);
+
+               if (TYPO3_MODE == 'FE') {
+                       $mode = strtolower($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['versionNumberInFilename']);
+                       if ($mode === 'embed') {
+                               $mode = TRUE;
+                       } else if ($mode === 'querystring') {
+                               $mode = FALSE;
+                       } else {
+                               $doNothing = TRUE;
+                       }
+               } else {
+                       $mode = $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['versionNumberInFilename'];
+               }
+
+               if (! file_exists($path) || $doNothing) {
+                               // File not found, return filename unaltered
+                       $fullName = $file;
+
+               } else if (! $mode || $forceQueryString) {
+                               // If use of .htaccess rule is not configured,
+                               // we use the default query-string method
+                       if ($lookupFile[1]) {
+                               $separator = '&';
+                       } else {
+                               $separator = '?';
+                       }
+                       $fullName = $file . $separator . filemtime($path);
+
+               } else {
+                               // Change the filename
+                       $name = explode('.', $lookupFile[0]);
+                       $extension = array_pop($name);
+
+                       array_push($name, filemtime($path), $extension);
+                       $fullName = implode('.', $name);
+                               // append potential query string
+                       $fullName .= $lookupFile[1] ? '?' . $lookupFile[1] : '';
+               }
+
+               return $fullName;
+       }
+
 
 
 
@@ -3218,14 +3568,14 @@ final class t3lib_div {
                        $result='
                        <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
                        if (count($array_in) == 0)      {
-                               $result.= '<tr><td><font face="Verdana,Arial" size="1"><b>EMPTY!</b></font></td></tr>';
+                               $result.= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
                        } else  {
                                foreach ($array_in as $key => $val)     {
                                        $result.= '<tr>
                                                <td valign="top"><font face="Verdana,Arial" size="1">'.htmlspecialchars((string)$key).'</font></td>
                                                <td>';
                                        if (is_array($val))     {
-                                               $result.=t3lib_div::view_array($val);
+                                               $result.=self::view_array($val);
                                        } elseif (is_object($val))      {
                                                $string = get_class($val);
                                                if (method_exists($val, '__toString'))  {
@@ -3245,7 +3595,7 @@ final class t3lib_div {
                                }
                        }
                        $result.= '</table>';
-               } else  {
+               } else {
                        $result  = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
                                <tr>
                                        <td><font face="Verdana,Arial" size="1" color="red">'.nl2br(htmlspecialchars((string)$array_in)).'<br /></font></td>
@@ -3264,7 +3614,7 @@ final class t3lib_div {
         * @see view_array()
         */
        public static function print_array($array_in)   {
-               echo t3lib_div::view_array($array_in);
+               echo self::view_array($array_in);
        }
 
        /**
@@ -3275,37 +3625,106 @@ final class t3lib_div {
         * Usage: 8
         *
         * @param       mixed           Variable to print
-        * @param       mixed           If the parameter is a string it will be used as header. Otherwise number of break tags to apply after (positive integer) or before (negative integer) the output.
+        * @param       string          The header.
+        * @param       string          Group for the debug console
         * @return      void
         */
-       public static function debug($var='',$brOrHeader=0)     {
+       public static function debug($var = '', $header = '', $group = 'Debug') {
                        // buffer the output of debug if no buffering started before
                if (ob_get_level()==0) {
                        ob_start();
                }
+               $debug = '';
 
-               if ($brOrHeader && !t3lib_div::testInt($brOrHeader))    {
-                       echo '<table class="typo3-debug" border="0" cellpadding="0" cellspacing="0" bgcolor="white" style="border:0px; margin-top:3px; margin-bottom:3px;"><tr><td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">'.htmlspecialchars((string)$brOrHeader).'</td></tr><tr><td>';
-               } elseif ($brOrHeader<0)        {
-                       for($a=0;$a<abs(intval($brOrHeader));$a++){echo '<br />';}
+               if ($header) {
+                       $debug .= '
+                       <table class="typo3-debug" border="0" cellpadding="0" cellspacing="0" bgcolor="white" style="border:0px; margin-top:3px; margin-bottom:3px;">
+                               <tr>
+                                       <td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">' .
+                                               htmlspecialchars((string) $header) .
+                                       '</td>
+                               </tr>
+                               <tr>
+                                       <td>';
                }
 
                if (is_array($var))     {
-                       t3lib_div::print_array($var);
-               } elseif (is_object($var))      {
-                       echo '<b>|Object:<pre>';
-                       print_r($var);
-                       echo '</pre>|</b>';
-               } elseif ((string)$var!='')     {
-                       echo '<b>|'.htmlspecialchars((string)$var).'|</b>';
+                       $debug .= self::view_array($var);
+               } elseif (is_object($var)) {
+                       $debug .=  '<strong>|Object:<pre>';
+                       $debug .= print_r($var, TRUE);
+                       $debug .=  '</pre>|</strong>';
+               } elseif ((string) $var !== '') {
+                       $debug .= '<strong>|' . htmlspecialchars((string)$var) . '|</strong>';
                } else {
-                       echo '<b>| debug |</b>';
+                       $debug .= '<strong>| debug |</strong>';
                }
 
-               if ($brOrHeader && !t3lib_div::testInt($brOrHeader))    {
-                       echo '</td></tr></table>';
-               } elseif ($brOrHeader>0)        {
-                       for($a=0;$a<intval($brOrHeader);$a++){echo '<br />';}
+               if ($header) {
+                       $debug .=  '
+                                       </td>
+                               </tr>
+                       </table>';
+               }
+
+               if (TYPO3_MODE === 'BE') {
+                       $group = htmlspecialchars($group);
+
+                       if ($header !== '') {
+                               $tabHeader = htmlspecialchars($header);
+                       } else {
+                               $tabHeader = 'Debug';
+                       }
+
+                       if (is_object($var)) {
+                               $debug = str_replace(
+                                       array('"', '/', '<', "\n", "\r"),
+                                       array('\"', '\/', '\<', '<br />', ''),
+                                       $debug
+                               );
+                       } else {
+                               $debug = str_replace(
+                                       array('"', '/', '<', "\n", "\r"),
+                                       array('\"', '\/', '\<', '', ''),
+                                       $debug
+                               );
+                       }
+
+                       $script = '
+                               (function debug() {
+                                       var debugMessage = "' . $debug . '";
+                                       var header = "' . $tabHeader . '";
+                                       var group = "' . $group . '";
+
+                                       if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
+                                               document.write(debugMessage);
+                                               return;
+                                       }
+
+                                       if (top && typeof Ext !== "object") {
+                                               Ext = top.Ext;
+                                       }
+
+                                       Ext.onReady(function() {
+                                               var TYPO3ViewportInstance = null;
+
+                                               if (top && top.TYPO3 && typeof top.TYPO3.Backend === "object") {
+                                                       TYPO3ViewportInstance = top.TYPO3.Backend;
+                                               } else if (typeof TYPO3 === "object" && typeof TYPO3.Backend === "object") {
+                                                       TYPO3ViewportInstance = TYPO3.Backend;
+                                               }
+
+                                               if (TYPO3ViewportInstance !== null) {
+                                                       TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
+                                               } else {
+                                                       document.write(debugMessage);
+                                               }
+                                       });
+                               })();
+                       ';
+                       echo self::wrapJS($script);
+               } else {
+                       echo $debug;
                }
        }
 
@@ -3359,7 +3778,7 @@ final class t3lib_div {
                                        $tCells = array();
                                        foreach($headerColumns as $key) {
                                                $tCells[] = '
-                                                       <td><font face="Verdana,Arial" size="1">'.(is_array($singleRow[$key]) ? t3lib_div::debugRows($singleRow[$key],'',TRUE) : htmlspecialchars($singleRow[$key])).'</font></td>';
+                                                       <td><font face="Verdana,Arial" size="1">'.(is_array($singleRow[$key]) ? self::debugRows($singleRow[$key],'',TRUE) : htmlspecialchars($singleRow[$key])).'</font></td>';
                                        }
                                        $tRows[] = '
                                                <tr>'.implode('',$tCells).'
@@ -3416,8 +3835,8 @@ final class t3lib_div {
         * @return      string
         */
        public static function getThisUrl()     {
-               $p=parse_url(t3lib_div::getIndpEnv('TYPO3_REQUEST_SCRIPT'));            // Url of this script
-               $dir=t3lib_div::dirname($p['path']).'/';        // Strip file
+               $p=parse_url(self::getIndpEnv('TYPO3_REQUEST_SCRIPT'));         // Url of this script
+               $dir=self::dirname($p['path']).'/';     // Strip file
                $url = str_replace('//','/',$p['host'].($p['port']?':'.$p['port']:'').$dir);
                return $url;
        }
@@ -3432,8 +3851,8 @@ final class t3lib_div {
         * @return      string
         */
        public static function linkThisScript(array $getParams = array()) {
-               $parts = t3lib_div::getIndpEnv('SCRIPT_NAME');
-               $params = t3lib_div::_GET();
+               $parts = self::getIndpEnv('SCRIPT_NAME');
+               $params = self::_GET();
 
                foreach ($getParams as $key => $value) {
                        if ($value !== '') {
@@ -3443,9 +3862,9 @@ final class t3lib_div {
                        }
                }
 
-               $pString = t3lib_div::implodeArrayForUrl('', $params);
+               $pString = self::implodeArrayForUrl('', $params);
 
-               return $pString ? $parts . '?' . ereg_replace('^&', '', $pString) : $parts;
+               return $pString ? $parts . '?' . preg_replace('/^&/', '', $pString) : $parts;
        }
 
        /**
@@ -3463,32 +3882,16 @@ final class t3lib_div {
                if ($parts['query'])    {
                        parse_str($parts['query'],$getP);
                }
-               $getP = t3lib_div::array_merge_recursive_overrule($getP,$getParams);
+               $getP = self::array_merge_recursive_overrule($getP,$getParams);
                $uP = explode('?',$url);
 
-               $params = t3lib_div::implodeArrayForUrl('',$getP);
+               $params = self::implodeArrayForUrl('',$getP);
                $outurl = $uP[0].($params ? '?'.substr($params, 1) : '');
 
                return $outurl;
        }
 
        /**
-        * Sends a redirect header response and exits. Additionaly the URL is
-        * checked and if needed corrected to match the format required for a
-        * Location redirect header. By default the HTTP status code sent is
-        * a 'HTTP/1.1 303 See Other'.
-        *
-        * @param       string  The target URL to redirect to
-        * @param       string  An optional HTTP status header. Default is 'HTTP/1.1 303 See Other'
-        */
-       public static function redirect($url, $httpStatus = t3lib_div::HTTP_STATUS_303) {
-               header($httpStatus);
-               header('Location: ' . t3lib_div::locationHeaderUrl($url));
-
-               exit;
-       }
-
-       /**
         * Abstraction method which returns System Environment Variables regardless of server OS, CGI/MODULE version etc. Basically this is SERVER variables for most of them.
         * This should be used instead of getEnv() and $_SERVER/ENV_VARS to get reliable values for all situations.
         * Usage: 221
@@ -3544,6 +3947,7 @@ final class t3lib_div {
                                TYPO3_REQUEST_SCRIPT =          [scheme]://[host][:[port]][path_script]
                                TYPO3_REQUEST_DIR =             [scheme]://[host][:[port]][path_dir]
                                TYPO3_SITE_URL =                [scheme]://[host][:[port]][path_dir] of the TYPO3 website frontend
+                               TYPO3_SITE_PATH =               [path_dir] of the TYPO3 website frontend
                                TYPO3_SITE_SCRIPT =             [script / Speaking URL] of the TYPO3 website
                                TYPO3_DOCUMENT_ROOT =           Absolute path of root of documents: TYPO3_DOCUMENT_ROOT.SCRIPT_NAME = SCRIPT_FILENAME (typically)
                                TYPO3_SSL =                     Returns TRUE if this session uses SSL/TLS (https)
@@ -3565,10 +3969,10 @@ final class t3lib_div {
 
                switch ((string)$getEnvName)    {
                        case 'SCRIPT_NAME':
-                               $retVal = (php_sapi_name()=='cgi'||php_sapi_name()=='cgi-fcgi')&&($_SERVER['ORIG_PATH_INFO']?$_SERVER['ORIG_PATH_INFO']:$_SERVER['PATH_INFO']) ? ($_SERVER['ORIG_PATH_INFO']?$_SERVER['ORIG_PATH_INFO']:$_SERVER['PATH_INFO']) : ($_SERVER['ORIG_SCRIPT_NAME']?$_SERVER['ORIG_SCRIPT_NAME']:$_SERVER['SCRIPT_NAME']);
+                               $retVal = (PHP_SAPI=='cgi'||PHP_SAPI=='cgi-fcgi')&&($_SERVER['ORIG_PATH_INFO']?$_SERVER['ORIG_PATH_INFO']:$_SERVER['PATH_INFO']) ? ($_SERVER['ORIG_PATH_INFO']?$_SERVER['ORIG_PATH_INFO']:$_SERVER['PATH_INFO']) : ($_SERVER['ORIG_SCRIPT_NAME']?$_SERVER['ORIG_SCRIPT_NAME']:$_SERVER['SCRIPT_NAME']);
                                        // add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
-                               if (t3lib_div::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
-                                       if (t3lib_div::getIndpEnv('TYPO3_SSL') && $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL']) {
+                               if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
+                                       if (self::getIndpEnv('TYPO3_SSL') && $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL']) {
                                                $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL'].$retVal;
                                        } elseif ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix']) {
                                                $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix'].$retVal;
@@ -3576,7 +3980,7 @@ final class t3lib_div {
                                }
                        break;
                        case 'SCRIPT_FILENAME':
-                               $retVal = str_replace('//','/', str_replace('\\','/', (php_sapi_name()=='cgi'||php_sapi_name()=='isapi' ||php_sapi_name()=='cgi-fcgi')&&($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED'])? ($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED']):($_SERVER['ORIG_SCRIPT_FILENAME']?$_SERVER['ORIG_SCRIPT_FILENAME']:$_SERVER['SCRIPT_FILENAME'])));
+                               $retVal = str_replace('//','/', str_replace('\\','/', (PHP_SAPI=='cgi'||PHP_SAPI=='isapi' ||PHP_SAPI=='cgi-fcgi')&&($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED'])? ($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED']):($_SERVER['ORIG_SCRIPT_FILENAME']?$_SERVER['ORIG_SCRIPT_FILENAME']:$_SERVER['SCRIPT_FILENAME'])));
                        break;
                        case 'REQUEST_URI':
                                        // Typical application of REQUEST_URI is return urls, forms submitting to itself etc. Example: returnUrl='.rawurlencode(t3lib_div::getIndpEnv('REQUEST_URI'))
@@ -3584,14 +3988,14 @@ final class t3lib_div {
                                        list($v,$n) = explode('|',$GLOBALS['TYPO3_CONF_VARS']['SYS']['requestURIvar']);
                                        $retVal = $GLOBALS[$v][$n];
                                } elseif (!$_SERVER['REQUEST_URI'])     {       // This is for ISS/CGI which does not have the REQUEST_URI available.
-                                       $retVal = '/'.ereg_replace('^/','',t3lib_div::getIndpEnv('SCRIPT_NAME')).
+                                       $retVal = '/'.ltrim(self::getIndpEnv('SCRIPT_NAME'), '/').
                                                ($_SERVER['QUERY_STRING']?'?'.$_SERVER['QUERY_STRING']:'');
                                } else {
                                        $retVal = $_SERVER['REQUEST_URI'];
                                }
                                        // add a prefix if TYPO3 is behind a proxy: ext-domain.com => int-server.com/prefix
-                               if (t3lib_div::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
-                                       if (t3lib_div::getIndpEnv('TYPO3_SSL') && $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL']) {
+                               if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
+                                       if (self::getIndpEnv('TYPO3_SSL') && $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL']) {
                                                $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefixSSL'].$retVal;
                                        } elseif ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix']) {
                                                $retVal = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix'].$retVal;
@@ -3601,19 +4005,19 @@ final class t3lib_div {
                        case 'PATH_INFO':
                                        // $_SERVER['PATH_INFO']!=$_SERVER['SCRIPT_NAME'] is necessary because some servers (Windows/CGI) are seen to set PATH_INFO equal to script_name
                                        // Further, there must be at least one '/' in the path - else the PATH_INFO value does not make sense.
-                                       // IF 'PATH_INFO' never works for our purpose in TYPO3 with CGI-servers, then 'php_sapi_name()=='cgi'' might be a better check. Right now strcmp($_SERVER['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) will always return false for CGI-versions, but that is only as long as SCRIPT_NAME is set equal to PATH_INFO because of php_sapi_name()=='cgi' (see above)
-//                             if (strcmp($_SERVER['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) && count(explode('/',$_SERVER['PATH_INFO']))>1)  {
-                               if (php_sapi_name()!='cgi' && php_sapi_name()!='cgi-fcgi')      {
+                                       // IF 'PATH_INFO' never works for our purpose in TYPO3 with CGI-servers, then 'PHP_SAPI=='cgi'' might be a better check. Right now strcmp($_SERVER['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) will always return false for CGI-versions, but that is only as long as SCRIPT_NAME is set equal to PATH_INFO because of PHP_SAPI=='cgi' (see above)
+//                             if (strcmp($_SERVER['PATH_INFO'],self::getIndpEnv('SCRIPT_NAME')) && count(explode('/',$_SERVER['PATH_INFO']))>1)       {
+                               if (PHP_SAPI!='cgi' && PHP_SAPI!='cgi-fcgi')    {
                                        $retVal = $_SERVER['PATH_INFO'];
                                }
                        break;
                        case 'TYPO3_REV_PROXY':
-                               $retVal = t3lib_div::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
+                               $retVal = self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP']);
                        break;
                        case 'REMOTE_ADDR':
                                $retVal = $_SERVER['REMOTE_ADDR'];
-                               if (t3lib_div::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
-                                       $ip = t3lib_div::trimExplode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
+                               if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
+                                       $ip = self::trimExplode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                                                // choose which IP in list to use
                                        if (count($ip)) {
                                                switch ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyHeaderMultiValue']) {
@@ -3629,15 +4033,15 @@ final class t3lib_div {
                                                        break;
                                                }
                                        }
-                                       if (t3lib_div::validIP($ip)) {
+                                       if (self::validIP($ip)) {
                                                $retVal = $ip;
                                        }
                                }
                        break;
                        case 'HTTP_HOST':
                                $retVal = $_SERVER['HTTP_HOST'];
-                               if (t3lib_div::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
-                                       $host = t3lib_div::trimExplode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
+                               if (self::cmpIP($_SERVER['REMOTE_ADDR'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'])) {
+                                       $host = self::trimExplode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
                                                // choose which host in list to use
                                        if (count($host)) {
                                                switch ($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyHeaderMultiValue']) {
@@ -3670,8 +4074,8 @@ final class t3lib_div {
                        case 'TYPO3_DOCUMENT_ROOT':
                                // Some CGI-versions (LA13CGI) and mod-rewrite rules on MODULE versions will deliver a 'wrong' DOCUMENT_ROOT (according to our description). Further various aliases/mod_rewrite rules can disturb this as well.
                                // Therefore the DOCUMENT_ROOT is now always calculated as the SCRIPT_FILENAME minus the end part shared with SCRIPT_NAME.
-                               $SFN = t3lib_div::getIndpEnv('SCRIPT_FILENAME');
-                               $SN_A = explode('/',strrev(t3lib_div::getIndpEnv('SCRIPT_NAME')));
+                               $SFN = self::getIndpEnv('SCRIPT_FILENAME');
+                               $SN_A = explode('/',strrev(self::getIndpEnv('SCRIPT_NAME')));
                                $SFN_A = explode('/',strrev($SFN));
                                $acc = array();
                                foreach ($SN_A as $kk => $vv) {
@@ -3684,44 +4088,47 @@ final class t3lib_div {
                                $retVal = $DR;
                        break;
                        case 'TYPO3_HOST_ONLY':
-                               $p = explode(':',t3lib_div::getIndpEnv('HTTP_HOST'));
+                               $p = explode(':',self::getIndpEnv('HTTP_HOST'));
                                $retVal = $p[0];
                        break;
                        case 'TYPO3_PORT':
-                               $p = explode(':',t3lib_div::getIndpEnv('HTTP_HOST'));
+                               $p = explode(':',self::getIndpEnv('HTTP_HOST'));
                                $retVal = $p[1];
                        break;
                        case 'TYPO3_REQUEST_HOST':
-                               $retVal = (t3lib_div::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://').
-                                       t3lib_div::getIndpEnv('HTTP_HOST');
+                               $retVal = (self::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://').
+                                       self::getIndpEnv('HTTP_HOST');
                        break;
                        case 'TYPO3_REQUEST_URL':
-                               $retVal = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::getIndpEnv('REQUEST_URI');
+                               $retVal = self::getIndpEnv('TYPO3_REQUEST_HOST').self::getIndpEnv('REQUEST_URI');
                        break;
                        case 'TYPO3_REQUEST_SCRIPT':
-                               $retVal = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::getIndpEnv('SCRIPT_NAME');
+                               $retVal = self::getIndpEnv('TYPO3_REQUEST_HOST').self::getIndpEnv('SCRIPT_NAME');
                        break;
                        case 'TYPO3_REQUEST_DIR':
-                               $retVal = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::dirname(t3lib_div::getIndpEnv('SCRIPT_NAME')).'/';
+                               $retVal = self::getIndpEnv('TYPO3_REQUEST_HOST').self::dirname(self::getIndpEnv('SCRIPT_NAME')).'/';
                        break;
                        case 'TYPO3_SITE_URL':
                                if (defined('PATH_thisScript') && defined('PATH_site')) {
                                        $lPath = substr(dirname(PATH_thisScript),strlen(PATH_site)).'/';
-                                       $url = t3lib_div::getIndpEnv('TYPO3_REQUEST_DIR');
+                                       $url = self::getIndpEnv('TYPO3_REQUEST_DIR');
                                        $siteUrl = substr($url,0,-strlen($lPath));
                                        if (substr($siteUrl,-1)!='/')   $siteUrl.='/';
                                        $retVal = $siteUrl;
                                }
                        break;
+                       case 'TYPO3_SITE_PATH':
+                               $retVal = substr(self::getIndpEnv('TYPO3_SITE_URL'), strlen(self::getIndpEnv('TYPO3_REQUEST_HOST')));
+                       break;
                        case 'TYPO3_SITE_SCRIPT':
-                               $retVal = substr(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'),strlen(t3lib_div::getIndpEnv('TYPO3_SITE_URL')));
+                               $retVal = substr(self::getIndpEnv('TYPO3_REQUEST_URL'),strlen(self::getIndpEnv('TYPO3_SITE_URL')));
                        break;
                        case 'TYPO3_SSL':
                                $proxySSL = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxySSL']);
                                if ($proxySSL == '*') {
                                        $proxySSL = $GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyIP'];
                                }
-                               if (t3lib_div::cmpIP($_SERVER['REMOTE_ADDR'], $proxySSL))       {
+                               if (self::cmpIP($_SERVER['REMOTE_ADDR'], $proxySSL))    {
                                        $retVal = true;
                                } else {
                                        $retVal = $_SERVER['SSL_SESSION_ID'] || !strcasecmp($_SERVER['HTTPS'], 'on') || !strcmp($_SERVER['HTTPS'], '1') ? true : false; // see http://bugs.typo3.org/view.php?id=3909
@@ -3730,7 +4137,7 @@ final class t3lib_div {
                        case '_ARRAY':
                                $out = array();
                                        // Here, list ALL possible keys to this function for debug display.
-                               $envTestVars = t3lib_div::trimExplode(',','
+                               $envTestVars = self::trimExplode(',','
                                        HTTP_HOST,
                                        TYPO3_HOST_ONLY,
                                        TYPO3_PORT,
@@ -3754,7 +4161,7 @@ final class t3lib_div {
                                        HTTP_USER_AGENT,
                                        HTTP_ACCEPT_LANGUAGE',1);
                                foreach ($envTestVars as $v) {
-                                       $out[$v]=t3lib_div::getIndpEnv($v);
+                                       $out[$v]=self::getIndpEnv($v);
                                }
                                reset($out);
                                $retVal = $out;
@@ -3764,15 +4171,12 @@ final class t3lib_div {
        }
 
        /**
-        * milliseconds
-        * microtime recalculated to t3lib_div::milliseconds(1/1000 sec)
-        * Usage: 20
+        * Gets the unixtime as milliseconds.
         *
-        * @return      integer
+        * @return      integer         The unixtime as milliseconds
         */
-       public static function milliseconds()   {
-               $p=explode(' ',microtime());
-               return round(($p[0]+$p[1])*1000);
+       public static function milliseconds() {
+               return round(microtime(true) * 1000);
        }
 
        /**
@@ -3783,7 +4187,7 @@ final class t3lib_div {
         * @return      array           Parsed information about the HTTP_USER_AGENT in categories BROWSER, VERSION, SYSTEM and FORMSTYLE
         */
        public static function clientInfo($useragent='')        {
-               if (!$useragent) $useragent=t3lib_div::getIndpEnv('HTTP_USER_AGENT');
+               if (!$useragent) $useragent=self::getIndpEnv('HTTP_USER_AGENT');
 
                $bInfo=array();
                        // Which browser?
@@ -3795,6 +4199,8 @@ final class t3lib_div {
                        $bInfo['BROWSER']= 'msie';
                } elseif (strpos($useragent, 'Mozilla') !== false) {
                        $bInfo['BROWSER']='net';
+               } elseif (strpos($useragent, 'Flash') !== false) {
+                       $bInfo['BROWSER'] = 'flash';
                }
                if ($bInfo['BROWSER'])  {
                                // Browser version
@@ -3807,11 +4213,11 @@ final class t3lib_div {
                                break;
                                case 'msie':
                                        $tmp = strstr($useragent,'MSIE');
-                                       $bInfo['VERSION'] = doubleval(ereg_replace('^[^0-9]*','',substr($tmp,4)));
+                                       $bInfo['VERSION'] = doubleval(preg_replace('/^[^0-9]*/','',substr($tmp,4)));
                                break;
                                case 'opera':
                                        $tmp = strstr($useragent,'Opera');
-                                       $bInfo['VERSION'] = doubleval(ereg_replace('^[^0-9]*','',substr($tmp,5)));
+                                       $bInfo['VERSION'] = doubleval(preg_replace('/^[^0-9]*/','',substr($tmp,5)));
                                break;
                                case 'konqu':
                                        $tmp = strstr($useragent,'Konqueror/');
@@ -3842,8 +4248,10 @@ final class t3lib_div {
         */
        public static function getHostname($requestHost=TRUE)   {
                $host = '';
+                       // If not called from the command-line, resolve on getIndpEnv()
+                       // Note that TYPO3_REQUESTTYPE is not used here as it may not yet be defined
                if ($requestHost && (!defined('TYPO3_cliMode') || !TYPO3_cliMode))      {
-                       $host = t3lib_div::getIndpEnv('HTTP_HOST');
+                       $host = self::getIndpEnv('HTTP_HOST');
                }
                if (!$host)     {
                                // will fail for PHP 4.1 and 4.2
@@ -3916,12 +4324,12 @@ final class t3lib_div {
                        if (strcmp($extKey,'') && t3lib_extMgm::isLoaded($extKey) && strcmp($local,'')) {
                                $filename = t3lib_extMgm::extPath($extKey).$local;
                        }
-               } elseif (!t3lib_div::isAbsPath($filename))     {       // relative. Prepended with $relPathPrefix
+               } elseif (!self::isAbsPath($filename))  {       // relative. Prepended with $relPathPrefix
                        $filename=$relPathPrefix.$filename;
-               } elseif ($onlyRelative && !t3lib_div::isFirstPartOfStr($filename,$relPathPrefix)) {    // absolute, but set to blank if not allowed
+               } elseif ($onlyRelative && !self::isFirstPartOfStr($filename,$relPathPrefix)) { // absolute, but set to blank if not allowed
                        $filename='';
                }
-               if (strcmp($filename,'') && t3lib_div::validPathStr($filename)) {       // checks backpath.
+               if (strcmp($filename,'') && self::validPathStr($filename))      {       // checks backpath.
                        return $filename;
                }
        }
@@ -3962,11 +4370,11 @@ final class t3lib_div {
         * @return      boolean
         */
        public static function isAllowedAbsPath($path)  {
-               if (t3lib_div::isAbsPath($path) &&
-                       t3lib_div::validPathStr($path) &&
-                               (       t3lib_div::isFirstPartOfStr($path,PATH_site)
+               if (self::isAbsPath($path) &&
+                       self::validPathStr($path) &&
+                               (       self::isFirstPartOfStr($path,PATH_site)
                                        ||
-                                       ($GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath'] && t3lib_div::isFirstPartOfStr($path,$GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']))
+                                       ($GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath'] && self::isFirstPartOfStr($path,$GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']))
                                )
                        )       return true;
        }
@@ -3980,13 +4388,56 @@ final class t3lib_div {
         */
        public static function verifyFilenameAgainstDenyPattern($filename)      {
                if (strcmp($filename,'') && strcmp($GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],''))    {
-                       $result = eregi($GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],$filename);
+                       $result = preg_match('/'.$GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'].'/i',$filename);
                        if ($result)    return false;   // so if a matching filename is found, return false;
                }
                return true;
        }
 
        /**
+        * Checks if a given string is a valid frame URL to be loaded in the
+        * backend.
+        *
+        * @param string $url potential URL to check
+        *
+        * @return string either $url if $url is considered to be harmless, or an
+        *                empty string otherwise
+        */
+       public static function sanitizeLocalUrl($url = '') {
+               $sanitizedUrl = '';
+               $decodedUrl = rawurldecode($url);
+
+               if (!empty($url) && self::removeXSS($decodedUrl) === $decodedUrl) {
+                       $testAbsoluteUrl = self::resolveBackPath($decodedUrl);
+                       $testRelativeUrl = self::resolveBackPath(
+                               self::dirname(self::getIndpEnv('SCRIPT_NAME')) . '/' . $decodedUrl
+                       );
+
+                               // Pass if URL is on the current host:
+                       if (self::isValidUrl($decodedUrl)) {
+                               if (self::isOnCurrentHost($decodedUrl) && strpos($decodedUrl, self::getIndpEnv('TYPO3_SITE_URL')) === 0) {
+                                       $sanitizedUrl = $url;
+                               }
+                               // Pass if URL is an absolute file path:
+                       } elseif (self::isAbsPath($decodedUrl) && self::isAllowedAbsPath($decodedUrl)) {
+                               $sanitizedUrl = $url;
+                               // Pass if URL is absolute and below TYPO3 base directory:
+                       } elseif (strpos($testAbsoluteUrl, self::getIndpEnv('TYPO3_SITE_PATH')) === 0 && substr($decodedUrl, 0, 1) === '/') {
+                               $sanitizedUrl = $url;
+                               // Pass if URL is relative and below TYPO3 base directory:
+                       } elseif (strpos($testRelativeUrl, self::getIndpEnv('TYPO3_SITE_PATH')) === 0 && substr($decodedUrl, 0, 1) !== '/') {
+                               $sanitizedUrl = $url;
+                       }
+               }
+
+               if (!empty($url) && empty($sanitizedUrl)) {
+                       self::sysLog('The URL "' . $url . '" is not considered to be local and was denied.', 'Core', self::SYSLOG_SEVERITY_NOTICE);
+               }
+
+               return $sanitizedUrl;
+       }
+
+       /**
         * Moves $source file to $destination if uploaded, otherwise try to make a copy
         * Usage: 4
         *
@@ -4006,7 +4457,7 @@ final class t3lib_div {
                        @copy($source,$destination);
                }
 
-               t3lib_div::fixPermissions($destination);        // Change the permissions of the file
+               self::fixPermissions($destination);     // Change the permissions of the file
 
                        // If here the file is copied and the temporary $source is still around, so when returning false the user can try unlink to delete the $source
                return $uploaded ? $uploadedResult : FALSE;
@@ -4024,7 +4475,7 @@ final class t3lib_div {
         */
        public static function upload_to_tempfile($uploadedFileName)    {
                if (is_uploaded_file($uploadedFileName))        {
-                       $tempFile = t3lib_div::tempnam('upload_temp_');
+                       $tempFile = self::tempnam('upload_temp_');
                        move_uploaded_file($uploadedFileName, $tempFile);
                        return @is_file($tempFile) ? $tempFile : '';
                }
@@ -4041,7 +4492,7 @@ final class t3lib_div {
         * @see upload_to_tempfile(), tempnam()
         */
        public static function unlink_tempfile($uploadedTempFileName)   {
-               if ($uploadedTempFileName && t3lib_div::validPathStr($uploadedTempFileName) && t3lib_div::isFirstPartOfStr($uploadedTempFileName,PATH_site.'typo3temp/') && @is_file($uploadedTempFileName))    {
+               if ($uploadedTempFileName && self::validPathStr($uploadedTempFileName) && self::isFirstPartOfStr($uploadedTempFileName,PATH_site.'typo3temp/') && @is_file($uploadedTempFileName))      {
                        if (unlink($uploadedTempFileName))      return TRUE;
                }
        }
@@ -4074,7 +4525,7 @@ final class t3lib_div {
                if (is_array($uid_or_record))   {
                        $recCopy_temp=array();
                        if ($fields)    {
-                               $fieldArr = t3lib_div::trimExplode(',',$fields,1);
+                               $fieldArr = self::trimExplode(',',$fields,1);
                                foreach ($fieldArr as $k => $v) {
                                        $recCopy_temp[$k]=$uid_or_record[$v];
                                }
@@ -4097,7 +4548,7 @@ final class t3lib_div {
         *
         * @param       string          Query-parameters: "&xxx=yyy&zzz=uuu"
         * @return      array           Array with key/value pairs of query-parameters WITHOUT a certain list of variable names (like id, type, no_cache etc.) and WITH a variable, encryptionKey, specific for this server/installation
-        * @see tslib_fe::makeCacheHash(), tslib_cObj::typoLink()
+        * @see tslib_fe::makeCacheHash(), tslib_cObj::typoLink(), t3lib_div::calculateCHash()
         */
        public static function cHashParams($addQueryParams) {
                $params = explode('&',substr($addQueryParams,1));       // Splitting parameters up
@@ -4106,7 +4557,7 @@ final class t3lib_div {
                $pA = array();
                foreach($params as $theP)       {
                        $pKV = explode('=', $theP);     // Splitting single param by '=' sign
-                       if (!t3lib_div::inList('id,type,no_cache,cHash,MP,ftu',$pKV[0]) && !preg_match('/TSFE_ADMIN_PANEL\[.*?\]/',$pKV[0]))    {
+                       if (!self::inList('id,type,no_cache,cHash,MP,ftu',$pKV[0]) && !preg_match('/TSFE_ADMIN_PANEL\[.*?\]/',$pKV[0])) {
                                $pA[rawurldecode($pKV[0])] = (string)rawurldecode($pKV[1]);
                        }
                }
@@ -4121,7 +4572,7 @@ final class t3lib_div {
                                );
                                $hookReference = null;
                                foreach ($cHashParamsHook as $hookFunction)     {
-                                       t3lib_div::callUserFunction($hookFunction, $hookParameters, $hookReference);
+                                       self::callUserFunction($hookFunction, $hookParameters, $hookReference);
                                }
                        }
                }
@@ -4133,6 +4584,30 @@ final class t3lib_div {
        }
 
        /**
+        * Returns the cHash based on provided query parameters and added values from internal call
+        *
+        * @param       string          Query-parameters: "&xxx=yyy&zzz=uuu"
+        * @return      string          Hash of all the values
+        * @see t3lib_div::cHashParams(), t3lib_div::calculateCHash()
+        */
+       public static function generateCHash($addQueryParams) {
+               $cHashParams = self::cHashParams($addQueryParams);
+               $cHash = self::calculateCHash($cHashParams);
+               return $cHash;
+       }
+
+       /**
+        * Calculates the cHash based on the provided parameters
+        *
+        * @param       array           Array of key-value pairs
+        * @return      string          Hash of all the values
+        */
+       public static function calculateCHash($params) {
+               $cHash = md5(serialize($params));
+               return $cHash;
+       }
+
+       /**
         * Responds on input localization setting value whether the page it comes from should be hidden if no translation exists or not.
         *
         * @param       integer         Value from "l18n_cfg" field of a page record
@@ -4152,30 +4627,50 @@ final class t3lib_div {
         * @param       string          Input is a file-reference (see t3lib_div::getFileAbsFileName). That file is expected to be a 'locallang.php' file containing a $LOCAL_LANG array (will be included!) or a 'locallang.xml' file conataining a valid XML TYPO3 language structure.
         * @param       string          Language key
         * @param       string          Character set (option); if not set, determined by the language key
-        * @return      array           Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
+        * @param       integer         Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception
+        * @return      array           Value of $LOCAL_LANG found in the included file. If that array is found it  will returned.
+        *                                              Otherwise an empty array and it is FALSE in error case.
         */
-       public static function readLLfile($fileRef, $langKey, $charset='')      {
+       public static function readLLfile($fileRef, $langKey, $charset = '', $errorMode = 0)    {
 
-               $file = t3lib_div::getFileAbsFileName($fileRef);
+               $result = FALSE;
+               $file = self::getFileAbsFileName($fileRef);
                if ($file)      {
                        $baseFile = preg_replace('/\.(php|xml)$/', '', $file);
 
                        if (@is_file($baseFile.'.xml')) {
-                               $LOCAL_LANG = t3lib_div::readLLXMLfile($baseFile.'.xml', $langKey, $charset);
+                               $LOCAL_LANG = self::readLLXMLfile($baseFile.'.xml', $langKey, $charset);
                        } elseif (@is_file($baseFile.'.php'))   {
                                if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] || $charset)  {
-                                       $LOCAL_LANG = t3lib_div::readLLPHPfile($baseFile.'.php', $langKey, $charset);
+                                       $LOCAL_LANG = self::readLLPHPfile($baseFile.'.php', $langKey, $charset);
                                } else {
                                        include($baseFile.'.php');
                                        if (is_array($LOCAL_LANG))      {
                                                $LOCAL_LANG = array('default'=>$LOCAL_LANG['default'], $langKey=>$LOCAL_LANG[$langKey]); }
                                }
                        } else {
-                               die('File "' . $fileRef. '" not found!');
+                               $errorMsg = 'File "' . $fileRef. '" not found!';
+                               if ($errorMode == 2) {
+                                       throw new t3lib_exception($errorMsg);
+                               } elseif(!$errorMode) {
+                                       self::sysLog($errorMsg, 'Core', self::SYSLOG_SEVERITY_ERROR);
+                               }
+                               $fileNotFound = TRUE;
+                       }
+                       if (is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$fileRef])) {
+                               foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$fileRef] as $overrideFile) {
+                                       $languageOverrideFileName = self::getFileAbsFileName($overrideFile);
+                                       if (@is_file($languageOverrideFileName)) {
+                                               $languageOverrideArray = self::readLLXMLfile($languageOverrideFileName, $langKey, $charset);
+                                               $LOCAL_LANG = self::array_merge_recursive_overrule($LOCAL_LANG, $languageOverrideArray);
+                                       }
+                               }
                        }
                }
-
-               return is_array($LOCAL_LANG) ? $LOCAL_LANG : array();
+               if ($fileNotFound !== TRUE)     {
+                       $result = is_array($LOCAL_LANG) ? $LOCAL_LANG : array();
+               }
+               return $result;
        }
 
        /**
@@ -4190,11 +4685,11 @@ final class t3lib_div {
        public static function readLLPHPfile($fileRef, $langKey, $charset='')   {
 
                if (is_object($GLOBALS['LANG']))        {
-                       $csConvObj = &$GLOBALS['LANG']->csConvObj;
+                       $csConvObj = $GLOBALS['LANG']->csConvObj;
                } elseif (is_object($GLOBALS['TSFE']))  {
-                       $csConvObj = &$GLOBALS['TSFE']->csConvObj;
+                       $csConvObj = $GLOBALS['TSFE']->csConvObj;
                } else {
-                       $csConvObj = t3lib_div::makeInstance('t3lib_cs');
+                       $csConvObj = self::makeInstance('t3lib_cs');
                }
 
                if (@is_file($fileRef) && $langKey)     {
@@ -4214,7 +4709,7 @@ final class t3lib_div {
                        $hashSource = substr($fileRef,strlen(PATH_site)).'|'.date('d-m-Y H:i:s',filemtime($fileRef)).'|version=2.3';
                        $cacheFileName = PATH_site.'typo3temp/llxml/'.
                                                        substr(basename($fileRef),10,15).
-                                                       '_'.t3lib_div::shortMD5($hashSource).'.'.$langKey.'.'.$targetCharset.'.cache';
+                                                       '_'.self::shortMD5($hashSource).'.'.$langKey.'.'.$targetCharset.'.cache';
                                // Check if cache file exists...
                        if (!@is_file($cacheFileName))  {       // ... if it doesn't, create content and write it:
 
@@ -4222,7 +4717,10 @@ final class t3lib_div {
                                include($fileRef);
                                if (!is_array($LOCAL_LANG))     {
                                        $fileName = substr($fileRef, strlen(PATH_site));
-                                       die('\'' . $fileName . '\' is no TYPO3 language file)!');
+                                       throw new RuntimeException(
+                                               'TYPO3 Fatal Error: "' . $fileName . '" is no TYPO3 language file!',
+                                               1270853900
+                                       );
                                }
 
                                        // converting the default language (English)
@@ -4241,11 +4739,16 @@ final class t3lib_div {
 
                                        // Cache the content now:
                                $serContent = array('origFile'=>$hashSource, 'LOCAL_LANG'=>array('default'=>$LOCAL_LANG['default'], $langKey=>$LOCAL_LANG[$langKey]));
-                               $res = t3lib_div::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
-                               if ($res)       die('ERROR: '.$res);
+                               $res = self::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
+                               if ($res) {
+                                       throw new RuntimeException(
+                                               'TYPO3 Fatal Error: "' . $res,
+                                               1270853901
+                                       );
+                               }
                        } else {
                                        // Get content from cache:
-                               $serContent = unserialize(t3lib_div::getUrl($cacheFileName));
+                               $serContent = unserialize(self::getUrl($cacheFileName));
                                $LOCAL_LANG = $serContent['LOCAL_LANG'];
                        }
 
@@ -4265,11 +4768,11 @@ final class t3lib_div {
        public static function readLLXMLfile($fileRef, $langKey, $charset='')   {
 
                if (is_object($GLOBALS['LANG']))        {
-                       $csConvObj = &$GLOBALS['LANG']->csConvObj;
+                       $csConvObj = $GLOBALS['LANG']->csConvObj;
                } elseif (is_object($GLOBALS['TSFE']))  {
-                       $csConvObj = &$GLOBALS['TSFE']->csConvObj;
+                       $csConvObj = $GLOBALS['TSFE']->csConvObj;
                } else {
-                       $csConvObj = t3lib_div::makeInstance('t3lib_cs');
+                       $csConvObj = self::makeInstance('t3lib_cs');
                }
 
                $LOCAL_LANG = NULL;
@@ -4289,17 +4792,20 @@ final class t3lib_div {
                        $hashSource = substr($fileRef,strlen(PATH_site)).'|'.date('d-m-Y H:i:s',filemtime($fileRef)).'|version=2.3';
                        $cacheFileName = PATH_site.'typo3temp/llxml/'.
                                                substr(basename($fileRef),10,15).
-                                               '_'.t3lib_div::shortMD5($hashSource).'.'.$langKey.'.'.$targetCharset.'.cache';
+                                               '_'.self::shortMD5($hashSource).'.'.$langKey.'.'.$targetCharset.'.cache';
 
                                // Check if cache file exists...
                        if (!@is_file($cacheFileName))  {       // ... if it doesn't, create content and write it:
 
                                        // Read XML, parse it.
-                               $xmlString = t3lib_div::getUrl($fileRef);
-                               $xmlContent = t3lib_div::xml2array($xmlString);
+                               $xmlString = self::getUrl($fileRef);
+                               $xmlContent = self::xml2array($xmlString);
                                if (!is_array($xmlContent)) {
                                        $fileName = substr($fileRef, strlen(PATH_site));
-                                       die('The file "' . $fileName . '" is no TYPO3 language file!');
+                                       throw new RuntimeException(
+                                               'TYPO3 Fatal Error: The file "' . $fileName . '" is no TYPO3 language file!',
+                                               1270853902
+                                       );
                                }
 
                                        // Set default LOCAL_LANG array content:
@@ -4321,8 +4827,8 @@ final class t3lib_div {
                                if ($langKey!='default')        {
 
                                                // If no entry is found for the language key, then force a value depending on meta-data setting. By default an automated filename will be used:
-                                       $LOCAL_LANG[$langKey] = t3lib_div::llXmlAutoFileName($fileRef, $langKey);
-                                       $localized_file = t3lib_div::getFileAbsFileName($LOCAL_LANG[$langKey]);
+                                       $LOCAL_LANG[$langKey] = self::llXmlAutoFileName($fileRef, $langKey);
+                                       $localized_file = self::getFileAbsFileName($LOCAL_LANG[$langKey]);
                                        if (!@is_file($localized_file) && isset($xmlContent['data'][$langKey])) {
                                                $LOCAL_LANG[$langKey] = $xmlContent['data'][$langKey];
                                        }
@@ -4337,11 +4843,16 @@ final class t3lib_div {
 
                                        // Cache the content now:
                                $serContent = array('origFile'=>$hashSource, 'LOCAL_LANG'=>array('default'=>$LOCAL_LANG['default'], $langKey=>$LOCAL_LANG[$langKey]));
-                               $res = t3lib_div::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
-                               if ($res)       die('ERROR: '.$res);
+                               $res = self::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
+                               if ($res) {
+                                       throw new RuntimeException(
+                                               'TYPO3 Fatal Error: ' . $res,
+                                               1270853903
+                                       );
+                               }
                        } else {
                                        // Get content from cache:
-                               $serContent = unserialize(t3lib_div::getUrl($cacheFileName));
+                               $serContent = unserialize(self::getUrl($cacheFileName));
                                $LOCAL_LANG = $serContent['LOCAL_LANG'];
                        }
 
@@ -4349,24 +4860,27 @@ final class t3lib_div {
                        if ($langKey!='default' && is_string($LOCAL_LANG[$langKey]) && strlen($LOCAL_LANG[$langKey]))   {
 
                                        // Look for localized file:
-                               $localized_file = t3lib_div::getFileAbsFileName($LOCAL_LANG[$langKey]);
+                               $localized_file = self::getFileAbsFileName($LOCAL_LANG[$langKey]);
                                if ($localized_file && @is_file($localized_file))       {
 
                                                // Cache file name:
                                        $hashSource = substr($localized_file,strlen(PATH_site)).'|'.date('d-m-Y H:i:s',filemtime($localized_file)).'|version=2.3';
                                        $cacheFileName = PATH_site.'typo3temp/llxml/EXT_'.
                                                                        substr(basename($localized_file),10,15).
-                                                                       '_'.t3lib_div::shortMD5($hashSource).'.'.$langKey.'.'.$targetCharset.'.cache';
+                                                                       '_'.self::shortMD5($hashSource).'.'.$langKey.'.'.$targetCharset.'.cache';
 
                                                // Check if cache file exists...
                                        if (!@is_file($cacheFileName))  {       // ... if it doesn't, create content and write it:
 
                                                        // Read and parse XML content:
-                                               $local_xmlString = t3lib_div::getUrl($localized_file);
-                                               $local_xmlContent = t3lib_div::xml2array($local_xmlString);
+                                               $local_xmlString = self::getUrl($localized_file);
+                                               $local_xmlContent = self::xml2array($local_xmlString);
                                                if (!is_array($local_xmlContent)) {
                                                        $fileName = substr($localized_file, strlen(PATH_site));
-                                                       die('The file "' . $fileName . '" is no TYPO3 language file!');
+                                                       throw new RuntimeException(
+                                                               'TYPO3 Fatal Error: The file "' . $fileName . '" is no TYPO3 language file!',
+                                                               1270853904
+                                                       );
                                                }
                                                $LOCAL_LANG[$langKey] = is_array($local_xmlContent['data'][$langKey]) ? $local_xmlContent['data'][$langKey] : array();
 
@@ -4379,13 +4893,16 @@ final class t3lib_div {
 
                                                        // Cache the content now:
                                                $serContent = array('extlang'=>$langKey, 'origFile'=>$hashSource, 'EXT_DATA'=>$LOCAL_LANG[$langKey]);
-                                               $res = t3lib_div::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
+                                               $res = self::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
                                                if ($res) {
-                                                       die('ERROR: '.$res);
+                                                       throw new RuntimeException(
+                                                               'TYPO3 Fatal Error: ' . $res,
+                                                               1270853905
+                                                       );
                                                }
                                        } else {
                                                        // Get content from cache:
-                                               $serContent = unserialize(t3lib_div::getUrl($cacheFileName));
+                                               $serContent = unserialize(self::getUrl($cacheFileName));
                                                $LOCAL_LANG[$langKey] = $serContent['EXT_DATA'];
                                        }
                                } else {
@@ -4407,12 +4924,12 @@ final class t3lib_div {
        public static function llXmlAutoFileName($fileRef,$language)    {
                        // Analyse file reference:
                $location = 'typo3conf/l10n/'.$language.'/';    // Default location of translations
-               if (t3lib_div::isFirstPartOfStr($fileRef,PATH_typo3.'sysext/')) {       // Is system:
+               if (self::isFirstPartOfStr($fileRef,PATH_typo3.'sysext/'))      {       // Is system:
                        $validatedPrefix = PATH_typo3.'sysext/';
                        #$location = 'EXT:csh_'.$language.'/';  // For system extensions translations are found in "csh_*" extensions (language packs)
-               } elseif (t3lib_div::isFirstPartOfStr($fileRef,PATH_typo3.'ext/'))      {       // Is global:
+               } elseif (self::isFirstPartOfStr($fileRef,PATH_typo3.'ext/'))   {       // Is global:
                        $validatedPrefix = PATH_typo3.'ext/';
-               } elseif (t3lib_div::isFirstPartOfStr($fileRef,PATH_typo3conf.'ext/'))  {       // Is local:
+               } elseif (self::isFirstPartOfStr($fileRef,PATH_typo3conf.'ext/'))       {       // Is local:
                        $validatedPrefix = PATH_typo3conf.'ext/';
                } else {
                        $validatedPrefix = '';
@@ -4422,7 +4939,7 @@ final class t3lib_div {
 
                                // Divide file reference into extension key, directory (if any) and base name:
                        list($file_extKey,$file_extPath) = explode('/',substr($fileRef,strlen($validatedPrefix)),2);
-                       $temp = t3lib_div::revExplode('/',$file_extPath,2);
+                       $temp = self::revExplode('/',$file_extPath,2);
                        if (count($temp)==1)    array_unshift($temp,'');        // Add empty first-entry if not there.
                        list($file_extPath,$file_fileName) = $temp;
 
@@ -4463,7 +4980,7 @@ final class t3lib_div {
                                if ($dcf) {
                                        if (!strcmp(substr($dcf,0,6),'T3LIB:')) {
                                                include(PATH_t3lib.'stddb/'.substr($dcf,6));
-                                       } elseif (t3lib_div::isAbsPath($dcf) && @is_file($dcf)) {       // Absolute path...
+                                       } elseif (self::isAbsPath($dcf) && @is_file($dcf))      {       // Absolute path...
                                                include($dcf);
                                        } else include(PATH_typo3conf.$dcf);
                                }
@@ -4492,9 +5009,9 @@ final class t3lib_div {
 
                                // If not an array, but still set, then regard it as a relative reference to a file:
                        if ($dataStruct && !is_array($dataStruct))      {
-                               $file = t3lib_div::getFileAbsFileName($dataStruct);
+                               $file = self::getFileAbsFileName($dataStruct);
                                if ($file && @is_file($file))   {
-                                       $dataStruct = t3lib_div::xml2array(t3lib_div::getUrl($file));
+                                       $dataStruct = self::xml2array(self::getUrl($file));
                                }
                        }
                } else {
@@ -4517,13 +5034,13 @@ final class t3lib_div {
                if (is_array($dataStructArray['sheets']))       {
                        $out=array('sheets'=>array());
                        foreach($dataStructArray['sheets'] as $sheetId => $sDat)        {
-                               list($ds,$aS) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheetId);
+                               list($ds,$aS) = self::resolveSheetDefInDS($dataStructArray,$sheetId);
                                if ($sheetId==$aS)      {
                                        $out['sheets'][$aS]=$ds;
                                }
                        }
                } else {
-                       list($ds) = t3lib_div::resolveSheetDefInDS($dataStructArray);
+                       list($ds) = self::resolveSheetDefInDS($dataStructArray);
                        $out = array('sheets' => array('sDEF' => $ds));
                }
                return $out;
@@ -4557,9 +5074,9 @@ final class t3lib_div {
 
                        // Check file-reference prefix; if found, require_once() the file (should be library of code)
                if (strpos($funcName,':') !== false)    {
-                       list($file,$funcRef) = t3lib_div::revExplode(':',$funcName,2);
-                       $requireFile = t3lib_div::getFileAbsFileName($file);
-                       if ($requireFile) t3lib_div::requireOnce($requireFile);
+                       list($file,$funcRef) = self::revExplode(':',$funcName,2);
+                       $requireFile = self::getFileAbsFileName($file);
+                       if ($requireFile) self::requireOnce($requireFile);
                } else {
                        $funcRef = $funcName;
                }
@@ -4574,14 +5091,14 @@ final class t3lib_div {
 
                        // Check prefix is valid:
                if ($checkPrefix &&
-                       !t3lib_div::isFirstPartOfStr(trim($funcRef),$checkPrefix) &&
-                       !t3lib_div::isFirstPartOfStr(trim($funcRef),'tx_')
+                       !self::isFirstPartOfStr(trim($funcRef),$checkPrefix) &&
+                       !self::isFirstPartOfStr(trim($funcRef),'tx_')
                        )       {
                        $errorMsg = "Function/class '$funcRef' was not prepended with '$checkPrefix'";
                        if ($errorMode == 2) {
                                throw new Exception($errorMsg);
                        } elseif(!$errorMode)   {
-                               debug($errorMsg, 1);
+                               debug($errorMsg, 't3lib_div::callUserFunction');
                        }
                        return false;
                }
@@ -4596,11 +5113,11 @@ final class t3lib_div {
                                        // Get/Create object of class:
                                if ($storePersistentObject)     {       // Get reference to current instance of class:
                                        if (!is_object($GLOBALS['T3_VAR']['callUserFunction_classPool'][$parts[0]]))    {
-                                               $GLOBALS['T3_VAR']['callUserFunction_classPool'][$parts[0]] = &t3lib_div::makeInstance($parts[0]);
+                                               $GLOBALS['T3_VAR']['callUserFunction_classPool'][$parts[0]] = self::makeInstance($parts[0]);
                                        }
-                                       $classObj = &$GLOBALS['T3_VAR']['callUserFunction_classPool'][$parts[0]];
+                                       $classObj = $GLOBALS['T3_VAR']['callUserFunction_classPool'][$parts[0]];
                                } else {        // Create new object:
-                                       $classObj = &t3lib_div::makeInstance($parts[0]);
+                                       $classObj = self::makeInstance($parts[0]);
                                }
 
                                if (method_exists($classObj, $parts[1]))        {
@@ -4622,7 +5139,7 @@ final class t3lib_div {
                                        if ($errorMode == 2) {
                                                throw new Exception($errorMsg);
                                        } elseif(!$errorMode)   {
-                                               debug($errorMsg, 1);
+                                               debug($errorMsg, 't3lib_div::callUserFunction');
                                        }
                                }
                        } else {
@@ -4630,7 +5147,7 @@ final class t3lib_div {
                                if ($errorMode == 2) {
                                        throw new Exception($errorMsg);
                                } elseif(!$errorMode)   {
-                                       debug($errorMsg, 1);
+                                       debug($errorMsg, 't3lib_div::callUserFunction');
                                }
                        }
                } else {        // Function
@@ -4641,7 +5158,7 @@ final class t3lib_div {
                                if ($errorMode == 2) {
                                        throw new Exception($errorMsg);
                                } elseif(!$errorMode)   {
-                                       debug($errorMsg, 1);
+                                       debug($errorMsg, 't3lib_div::callUserFunction');
                                }
                        }
                }
@@ -4659,7 +5176,7 @@ final class t3lib_div {
         * @return      object          The instance of the class asked for. Instance is created with t3lib_div::makeInstance
         * @see callUserFunction()
         */
-       public static function &getUserObj($classRef,$checkPrefix='user_',$silent=0)    {
+       public static function getUserObj($classRef, $checkPrefix='user_', $silent=false) {
                global $TYPO3_CONF_VARS;
                        // Check persistent object and if found, call directly and exit.
                if (is_object($GLOBALS['T3_VAR']['getUserObj'][$classRef]))     {
@@ -4668,9 +5185,9 @@ final class t3lib_div {
 
                                // Check file-reference prefix; if found, require_once() the file (should be library of code)
                        if (strpos($classRef,':') !== false)    {
-                               list($file,$class) = t3lib_div::revExplode(':',$classRef,2);
-                               $requireFile = t3lib_div::getFileAbsFileName($file);
-                               if ($requireFile)       t3lib_div::requireOnce($requireFile);
+                               list($file,$class) = self::revExplode(':',$classRef,2);
+                               $requireFile = self::getFileAbsFileName($file);
+                               if ($requireFile)       self::requireOnce($requireFile);
                        } else {
                                $class = $classRef;
                        }
@@ -4685,25 +5202,25 @@ final class t3lib_div {
 
                                // Check prefix is valid:
                        if ($checkPrefix &&
-                               !t3lib_div::isFirstPartOfStr(trim($class),$checkPrefix) &&
-                               !t3lib_div::isFirstPartOfStr(trim($class),'tx_')
+                               !self::isFirstPartOfStr(trim($class),$checkPrefix) &&
+                               !self::isFirstPartOfStr(trim($class),'tx_')
                                )       {
-                               if (!$silent)   debug("Class '".$class."' was not prepended with '".$checkPrefix."'",1);
+                               if (!$silent)   debug("Class '".$class."' was not prepended with '".$checkPrefix."'", 't3lib_div::getUserObj');
                                return FALSE;
                        }
 
                                // Check if class exists:
                        if (class_exists($class))       {
-                               $classObj = &t3lib_div::makeInstance($class);
+                               $classObj = self::makeInstance($class);
 
                                        // If persistent object should be created, set reference:
                                if ($storePersistentObject)     {
-                                       $GLOBALS['T3_VAR']['getUserObj'][$classRef] = &$classObj;
+                                       $GLOBALS['T3_VAR']['getUserObj'][$classRef] = $classObj;
                                }
 
                                return $classObj;
                        } else {
-                               if (!$silent)   debug("<strong>ERROR:</strong> No class named: ".$class,1);
+                               if (!$silent)   debug("<strong>ERROR:</strong> No class named: ".$class, 't3lib_div::getUserObj');
                        }
                }
        }
@@ -4718,17 +5235,10 @@ final class t3lib_div {
         * @param       string          Class name to instantiate
         * @return      object          A reference to the object
         */
-       public static function &makeInstance($className)        {
+       public static function makeInstance($className) {
                        // holds references of singletons
                static $instances = array();
 
-                       // Load class file if not found:
-               if (!class_exists($className))  {
-                       if (substr($className,0,6) == 't3lib_') {
-                               t3lib_div::requireOnce(PATH_t3lib.'class.'.strtolower($className).'.php');
-                       }
-               }
-
                        // Get final classname
                $className = self::getClassName($className);
 
@@ -4769,7 +5279,7 @@ final class t3lib_div {
        public static function makeInstanceClassName($className)        {
                self::logDeprecatedFunction();
 
-               return class_exists('ux_'.$className) ? t3lib_div::makeInstanceClassName('ux_'.$className) : $className;
+               return (class_exists($className) && class_exists('ux_'.$className, false) ? self::makeInstanceClassName('ux_' . $className) : $className);
        }
 
        /**
@@ -4780,7 +5290,7 @@ final class t3lib_div {
         * @return      string          Final class name to instantiate with "new [classname]"
         */
        protected function getClassName($className) {
-               return class_exists('ux_' . $className) ? self::getClassName('ux_' . $className) : $className;
+               return (class_exists($className) && class_exists('ux_' . $className, false) ? self::getClassName('ux_' . $className) : $className);
        }
 
        /**
@@ -4793,13 +5303,13 @@ final class t3lib_div {
         * @return      object          The service object or an array with error info's.
         * @author      René Fritz <r.fritz@colorcube.de>
         */
-       public static function &makeInstanceService($serviceType, $serviceSubType='', $excludeServiceKeys=array())      {
+       public static function makeInstanceService($serviceType, $serviceSubType='', $excludeServiceKeys=array()) {
                global $T3_SERVICES, $T3_VAR, $TYPO3_CONF_VARS;
 
                $error = FALSE;
 
                if (!is_array($excludeServiceKeys) ) {
-                       $excludeServiceKeys = t3lib_div::trimExplode(',', $excludeServiceKeys, 1);
+                       $excludeServiceKeys = self::trimExplode(',', $excludeServiceKeys, 1);
                }
                while ($info = t3lib_extMgm::findService($serviceType, $serviceSubType, $excludeServiceKeys))   {
 
@@ -4811,20 +5321,20 @@ final class t3lib_div {
 
                                // include file and create object
                        } else {
-                               $requireFile = t3lib_div::getFileAbsFileName($info['classFile']);
+                               $requireFile = self::getFileAbsFileName($info['classFile']);
                                if (@is_file($requireFile)) {
-                                       t3lib_div::requireOnce ($requireFile);
-                                       $obj = t3lib_div::makeInstance($info['className']);
+                                       self::requireOnce ($requireFile);
+                                       $obj = self::makeInstance($info['className']);
                                        if (is_object($obj)) {
                                                if(!@is_callable(array($obj,'init')))   {
                                                                // use silent logging??? I don't think so.
-                                                       die ('Broken service:'.t3lib_div::view_array($info));
+                                                       die ('Broken service:'.self::view_array($info));
                                                }
                                                $obj->info = $info;
                                                if ($obj->init()) { // service available?
 
                                                                // create persistent object
-                                                       $T3_VAR['makeInstanceService'][$info['className']] = &$obj;
+                                                       $T3_VAR['makeInstanceService'][$info['className']] = $obj;
 
                                                                // needed to delete temp files
                                                        register_shutdown_function(array(&$obj, '__destruct'));
@@ -4853,6 +5363,19 @@ final class t3lib_div {
        }
 
        /**
+        * Requires a class for TYPO3
+        * Useful to require classes from inside other classes (not global scope).
+        * A limited set of global variables are available (see function)
+        *
+        * @param       string          $requireFile: Path of the file to be included
+        * @return      void
+        */
+       public static function requireFile($requireFile) {
+               global $T3_SERVICES, $T3_VAR, $TYPO3_CONF_VARS;
+               require $requireFile;
+       }
+
+       /**
         * Simple substitute for the PHP function mail() which allows you to specify encoding and character set
         * The fifth parameter ($encoding) will allow you to specify 'base64' encryption for the output (set $encoding=base64)
         * Further the output has the charset set to ISO-8859-1 by default.
@@ -4861,7 +5384,7 @@ final class t3lib_div {
         * @param       string          Email address to send to. (see PHP function mail())
         * @param       string          Subject line, non-encoded. (see PHP function mail())
         * @param       string          Message content, non-encoded. (see PHP function mail())
-        * @param       string          Headers, separated by chr(10)
+        * @param       string          Headers, separated by LF
         * @param       string          Encoding type: "base64", "quoted-printable", "8bit". Default value is "quoted-printable".
         * @param       string          Charset used in encoding-headers (only if $encoding is set to a valid value which produces such a header)
         * @param       boolean         If set, the header content will not be encoded.
@@ -4876,62 +5399,59 @@ final class t3lib_div {
                if (!$dontEncodeHeader) {
                                // Mail headers must be ASCII, therefore we convert the whole header to either base64 or quoted_printable
                        $newHeaders=array();
-                       foreach (explode(chr(10),$headers) as $line)    {       // Split the header in lines and convert each line separately
+                       foreach (explode(LF,$headers) as $line) {       // Split the header in lines and convert each line separately
                                $parts = explode(': ',$line,2); // Field tags must not be encoded
                                if (count($parts)==2)   {
                                        if (0 == strcasecmp($parts[0], 'from')) {
                                                $parts[1] = self::normalizeMailAddress($parts[1]);
                                        }
-                                       $parts[1] = t3lib_div::encodeHeader($parts[1],$encoding,$charset);
+                                       $parts[1] = self::encodeHeader($parts[1],$encoding,$charset);
                                        $newHeaders[] = implode(': ',$parts);
                                } else {
                                        $newHeaders[] = $line;  // Should never happen - is such a mail header valid? Anyway, just add the unchanged line...
                                }
                        }
-                       $headers = implode(chr(10),$newHeaders);
+                       $headers = implode(LF,$newHeaders);
                        unset($newHeaders);
 
-                       $email = t3lib_div::encodeHeader($email,$encoding,$charset);            // Email address must not be encoded, but it could be appended by a name which should be so (e.g. "Kasper Skårhøj <kasperYYYY@typo3.com>")
-                       $subject = t3lib_div::encodeHeader($subject,$encoding,$charset);
+                       $email = self::encodeHeader($email,$encoding,$charset);         // Email address must not be encoded, but it could be appended by a name which should be so (e.g. "Kasper Skårhøj <kasperYYYY@typo3.com>")
+                       $subject = self::encodeHeader($subject,$encoding,$charset);
                }
 
                switch ((string)$encoding)      {
                        case 'base64':
-                               $headers=trim($headers).chr(10).
-                               'Mime-Version: 1.0'.chr(10).
-                               'Content-Type: text/plain; charset="'.$charset.'"'.chr(10).
+                               $headers=trim($headers).LF.
+                               'Mime-Version: 1.0'.LF.
+                               'Content-Type: text/plain; charset="'.$charset.'"'.LF.
                                'Content-Transfer-Encoding: base64';
 
-                               $message=trim(chunk_split(base64_encode($message.chr(10)))).chr(10);    // Adding chr(10) because I think MS outlook 2002 wants it... may be removed later again.
+                               $message=trim(chunk_split(base64_encode($message.LF))).LF;      // Adding LF because I think MS outlook 2002 wants it... may be removed later again.
                        break;
                        case '8bit':
-                               $headers=trim($headers).chr(10).
-                               'Mime-Version: 1.0'.chr(10).
-                               'Content-Type: text/plain; charset='.$charset.chr(10).
+                               $headers=trim($headers).LF.
+                               'Mime-Version: 1.0'.LF.
+                               'Content-Type: text/plain; charset='.$charset.LF.
                                'Content-Transfer-Encoding: 8bit';
                        break;
                        case 'quoted-printable':
                        default:
-                               $headers=trim($headers).chr(10).
-                               'Mime-Version: 1.0'.chr(10).
-                               'Content-Type: text/plain; charset='.$charset.chr(10).
+                               $headers=trim($headers).LF.
+                               'Mime-Version: 1.0'.LF.
+                               'Content-Type: text/plain; charset='.$charset.LF.
                                'Content-Transfer-Encoding: quoted-printable';
 
-                               $message=t3lib_div::quoted_printable($message);
+                               $message=self::quoted_printable($message);
                        break;
                }
 
                // Headers must be separated by CRLF according to RFC 2822, not just LF.
                // But many servers (Gmail, for example) behave incorectly and want only LF.
                // So we stick to LF in all cases.
-               $headers = trim(implode(chr(10), t3lib_div::trimExplode(chr(10), $headers, true)));     // Make sure no empty lines are there.
+               $headers = trim(implode(LF, self::trimExplode(LF, $headers, true)));    // Make sure no empty lines are there.
 
-               $ret = @mail($email, $subject, $message, $headers);
-               if (!$ret)      {
-                       t3lib_div::sysLog('Mail to "'.$email.'" could not be sent (Subject: "'.$subject.'").', 'Core', 3);
+
+               return t3lib_utility_Mail::mail($email, $subject, $message, $headers);
                }
-               return $ret;
-       }
 
        /**
         * Implementation of quoted-printable encode.
@@ -4945,16 +5465,16 @@ final class t3lib_div {
         */
        public static function quoted_printable($string,$maxlen=76)     {
                        // Make sure the string contains only Unix linebreaks
-               $string = str_replace(chr(13).chr(10), chr(10), $string);       // Replace Windows breaks (\r\n)
-               $string = str_replace(chr(13), chr(10), $string);               // Replace Mac breaks (\r)
+               $string = str_replace(CRLF, LF, $string);       // Replace Windows breaks (\r\n)
+               $string = str_replace(CR, LF, $string);         // Replace Mac breaks (\r)
 
-               $linebreak = chr(10);                   // Default line break for Unix systems.
+               $linebreak = LF;                        // Default line break for Unix systems.
                if (TYPO3_OS=='WIN')    {
-                       $linebreak = chr(13).chr(10);   // Line break for Windows. This is needed because PHP on Windows systems send mails via SMTP instead of using sendmail, and thus the linebreak needs to be \r\n.
+                       $linebreak = CRLF;      // Line break for Windows. This is needed because PHP on Windows systems send mails via SMTP instead of using sendmail, and thus the linebreak needs to be \r\n.
                }
 
                $newString = '';
-               $theLines = explode(chr(10),$string);   // Split lines
+               $theLines = explode(LF,$string);        // Split lines
                foreach ($theLines as $val)     {
                        $newVal = '';
                        $theValLen = strlen($val);
@@ -4975,7 +5495,7 @@ final class t3lib_div {
                                }
                        }
                        $newVal = preg_replace('/'.chr(32).'$/','=20',$newVal);         // Replaces a possible SPACE-character at the end of a line
-                       $newVal = preg_replace('/'.chr(9).'$/','=09',$newVal);          // Replaces a possible TAB-character at the end of a line
+                       $newVal = preg_replace('/'.TAB.'$/','=09',$newVal);             // Replaces a possible TAB-character at the end of a line
                        $newString.=$newVal.$linebreak;
                }
                return preg_replace('/'.$linebreak.'$/','',$newString);         // Remove last newline
@@ -5011,9 +5531,14 @@ final class t3lib_div {
                                break;
                                case 'quoted-printable':
                                default:
-                                       $qpValue = t3lib_div::quoted_printable($part,1000);
+                                       $qpValue = self::quoted_printable($part,1000);
                                        if ($part!=$qpValue)    {
-                                               $qpValue = str_replace(' ','_',$qpValue);       // Encoded words in the header should not contain non-encoded spaces. "_" is a shortcut for "=20". See RFC 2047 for details.
+                                               // Encoded words in the header should not contain non-encoded:
+                                               // * spaces. "_" is a shortcut for "=20". See RFC 2047 for details.
+                                               // * question mark. See RFC 1342 (http://tools.ietf.org/html/rfc1342)
+                                               $search = array(' ', '?');
+                                               $replace = array('_', '=3F');
+                                               $qpValue = str_replace($search, $replace, $qpValue);
                                                $part = '=?'.$charset.'?Q?'.$qpValue.'?=';
                                        }
                                break;
@@ -5047,10 +5572,10 @@ final class t3lib_div {
 
                                        switch ((string)$urlmode)       {
                                                case 'all':
-                                                       $newURL = t3lib_div::makeRedirectUrl($newURL,0,$index_script_url);
+                                                       $newURL = self::makeRedirectUrl($newURL,0,$index_script_url);
                                                break;
                                                case '76':
-                                                       $newURL = t3lib_div::makeRedirectUrl($newURL,76,$index_script_url);
+                                                       $newURL = self::makeRedirectUrl($newURL,76,$index_script_url);
                                                break;
                                        }
                                        $v = $newURL . substr($v,strlen($newParts[0]));
@@ -5082,14 +5607,14 @@ final class t3lib_div {
                        if (!$count) {
                                $insertFields = array(
                                        'md5hash' => $md5,
-                                       'tstamp' => time(),
+                                       'tstamp' => $GLOBALS['EXEC_TIME'],
                                        'type' => 2,
                                        'params' => $inUrl
                                );
 
                                $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_md5params', $insertFields);
                        }
-                       $inUrl=($index_script_url ? $index_script_url : t3lib_div::getIndpEnv('TYPO3_REQUEST_DIR').'index.php').
+                       $inUrl=($index_script_url ? $index_script_url : self::getIndpEnv('TYPO3_REQUEST_DIR').'index.php').
                                '?RDCT='.$md5;
                }
 
@@ -5119,12 +5644,13 @@ final class t3lib_div {
                global $TYPO3_CONF_VARS;
 
                        // for CLI logging name is <fqdn-hostname>:<TYPO3-path>
+                       // Note that TYPO3_REQUESTTYPE is not used here as it may not yet be defined
                if (defined('TYPO3_cliMode') && TYPO3_cliMode)  {
-                       $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] = t3lib_div::getHostname($requestHost=FALSE).':'.PATH_site;
+                       $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] = self::getHostname($requestHost=FALSE).':'.PATH_site;
                }
                        // for Web logging name is <protocol>://<request-hostame>/<site-path>
                else {
-                       $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
+                       $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] = self::getIndpEnv('TYPO3_SITE_URL');
                }
 
                        // init custom logging
@@ -5132,7 +5658,7 @@ final class t3lib_div {
                        $params = array('initLog'=>TRUE);
                        $fakeThis = FALSE;
                        foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'] as $hookMethod)       {
-                               t3lib_div::callUserFunction($hookMethod,$params,$fakeThis);
+                               self::callUserFunction($hookMethod,$params,$fakeThis);
                        }
                }
 
@@ -5141,7 +5667,6 @@ final class t3lib_div {
                        list($type,$destination) = explode(',',$log,3);
 
                        if ($type == 'syslog')  {
-                               define_syslog_variables();
                                if (TYPO3_OS == 'WIN')  {
                                        $facility = LOG_USER;
                                } else {
@@ -5151,7 +5676,7 @@ final class t3lib_div {
                        }
                }
 
-               $TYPO3_CONF_VARS['SYS']['systemLogLevel'] = t3lib_div::intInRange($TYPO3_CONF_VARS['SYS']['systemLogLevel'],0,4);
+               $TYPO3_CONF_VARS['SYS']['systemLogLevel'] = self::intInRange($TYPO3_CONF_VARS['SYS']['systemLogLevel'],0,4);
                $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogInit'] = TRUE;
        }
 
@@ -5169,22 +5694,23 @@ final class t3lib_div {
        public static function sysLog($msg, $extKey, $severity=0) {
                global $TYPO3_CONF_VARS;
 
-               $severity = t3lib_div::intInRange($severity,0,4);
+               $severity = self::intInRange($severity,0,4);
 
                        // is message worth logging?
                if (intval($TYPO3_CONF_VARS['SYS']['systemLogLevel']) > $severity)      return;
 
                        // initialize logging
                if (!$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogInit'])      {
-                       t3lib_div::initSysLog();
+                       self::initSysLog();
                }
 
                        // do custom logging
-               if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'])) {
-                       $params = array('msg'=>$msg, 'extKey'=>$extKey, 'backTrace'=>debug_backtrace());
+               if (isset($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog']) &&
+                               is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'])) {
+                       $params = array('msg'=>$msg, 'extKey'=>$extKey, 'backTrace'=>debug_backtrace(), 'severity'=>$severity);
                        $fakeThis = FALSE;
                        foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'] as $hookMethod)       {
-                               t3lib_div::callUserFunction($hookMethod,$params,$fakeThis);
+                               self::callUserFunction($hookMethod,$params,$fakeThis);
                        }
                }
 
@@ -5208,7 +5734,7 @@ final class t3lib_div {
                                $file = fopen($destination, 'a');
                                if ($file)     {
                                        flock($file, LOCK_EX);  // try locking, but ignore if not available (eg. on NFS and FAT)
-                                       fwrite($file, date($dateFormat.' '.$timeFormat).$msgLine.chr(10));
+                                       fwrite($file, date($dateFormat.' '.$timeFormat).$msgLine.LF);
                                        flock($file, LOCK_UN);    // release the lock
                                        fclose($file);
                                }
@@ -5216,11 +5742,11 @@ final class t3lib_div {
                                // send message per mail
                        elseif ($type == 'mail')        {
                                list($to,$from) = explode('/',$destination);
-                               mail($to, 'Warning - error in TYPO3 installation',
-                                       'Host: '.$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost']."\n".
-                                       'Extension: '.$extKey."\n".
-                                       'Severity: '.$severity."\n".
-                                       "\n".$msg,
+                               t3lib_utility_Mail::mail($to, 'Warning - error in TYPO3 installation',
+                                       'Host: '.$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'].LF.
+                                       'Extension: '.$extKey.LF.
+                                       'Severity: '.$severity.LF.
+                                       LF.$msg,
                                        ($from ? 'From: '.$from : '')
                                );
                        }
@@ -5257,7 +5783,7 @@ final class t3lib_div {
                        $params = array('msg'=>$msg, 'extKey'=>$extKey, 'severity'=>$severity, 'dataVar'=>$dataVar);
                        $fakeThis = FALSE;
                        foreach($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['devLog'] as $hookMethod)   {
-                               t3lib_div::callUserFunction($hookMethod,$params,$fakeThis);
+                               self::callUserFunction($hookMethod,$params,$fakeThis);
                        }
                }
        }
@@ -5269,27 +5795,64 @@ final class t3lib_div {
         * @return      void
         */
        public static function deprecationLog($msg) {
-               // write a longer message to the deprecation log
-               $destination = PATH_typo3conf . '/deprecation_' . t3lib_div::shortMD5(PATH_site . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']) . '.log';
-               $file = @fopen($destination, 'a');
-               if ($file) {
-                       $date = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'] . ': ');
-                       flock($file, LOCK_EX);  // try locking, but ignore if not available (eg. on NFS and FAT)
-                       @fwrite($file, $date.$msg.chr(10));
-                       flock($file, LOCK_UN);    // release the lock
-                       @fclose($file);
+               if (!$GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog']) {
+                       return;
                }
 
-               // copy message also to the developer log
-               self::devLog($msg, 'Core', self::SYSLOG_SEVERITY_WARNING);
+               $log = $GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog'];
+               $date = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'] . ': ');
+
+                       // legacy values (no strict comparison, $log can be boolean, string or int)
+               if ($log === TRUE || $log == '1') {
+                       $log = 'file';
+               }
+
+               if (stripos($log, 'file') !== FALSE) {
+                               // write a longer message to the deprecation log
+                       $destination = self::getDeprecationLogFileName();
+                       $file = @fopen($destination, 'a');
+                       if ($file) {
+                               flock($file, LOCK_EX);  // try locking, but ignore if not available (eg. on NFS and FAT)
+                               @fwrite($file, $date . $msg . LF);
+                               flock($file, LOCK_UN);    // release the lock
+                               @fclose($file);
+                       }
+               }
+
+               if (stripos($log, 'devlog') !== FALSE) {
+                               // copy message also to the developer log
+                       self::devLog($msg, 'Core', self::SYSLOG_SEVERITY_WARNING);
+               }
+
+               if (stripos($log, 'console') !== FALSE) {
+                       self::debug($msg, $date, 'Deprecation Log');
+               }
+       }
+
+       /**
+        * Gets the absolute path to the deprecation log file.
+        *
+        * @return      string  absolute path to the deprecation log file
+        */
+       public static function getDeprecationLogFileName() {
+               return PATH_typo3conf .
+                       'deprecation_' .
+                       self::shortMD5(
+                               PATH_site . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
+                       ) .
+                       '.log';
        }
 
        /**
         * Logs a call to a deprecated function.
-        * The log message will b etaken from the annotation.
+        * The log message will btaken from the annotation.
         * @return      void
         */
        public static function logDeprecatedFunction() {
+               if (!$GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog']) {
+                       return;
+               }
+
                $trail = debug_backtrace();
 
                if ($trail[1]['type']) {
@@ -5297,29 +5860,20 @@ final class t3lib_div {
                } else {
                        $function = new ReflectionFunction($trail[1]['function']);
                }
-               if (!$msg) {
-                       if (preg_match('/@deprecated\s+(.*)/', $function->getDocComment(), $match)) {
-                               $msg = $match[1];
-                       }
+
+               $msg = '';
+               if (preg_match('/@deprecated\s+(.*)/', $function->getDocComment(), $match)) {
+                       $msg = $match[1];
                }
 
-               // trigger PHP error with a short message: <function> is deprecated (called from <source>, defined in <source>)
+                       // trigger PHP error with a short message: <function> is deprecated (called from <source>, defined in <source>)
                $errorMsg = 'Function ' . $trail[1]['function'];
                if ($trail[1]['class']) {
                        $errorMsg .= ' of class ' . $trail[1]['class'];
                }
                $errorMsg .= ' is deprecated (called from '.$trail[1]['file'] . '#' . $trail[1]['line'] . ', defined in ' . $function->getFileName() . '#' . $function->getStartLine() . ')';
 
-// michael@typo3.org: Temporary disabled until error handling is implemented (follows later this week...)
-/*
-               if (defined('E_USER_DEPRECATED')) {
-                       trigger_error($errorMsg, E_USER_DEPRECATED);    // PHP 5.3
-               } else {
-                       trigger_error($errorMsg, E_USER_NOTICE);        // PHP 5.2
-               }
-*/
-
-               // write a longer message to the deprecation log: <function> <annotion> - <trace> (<source>)
+                       // write a longer message to the deprecation log: <function> <annotion> - <trace> (<source>)
                $logMsg = $trail[1]['class'] . $trail[1]['type'] . $trail[1]['function'];
                $logMsg .= '() - ' . $msg.' - ' . self::debug_trail();
                $logMsg .= ' (' . substr($function->getFileName(), strlen(PATH_site)) . '#' . $function->getStartLine() . ')';
@@ -5338,12 +5892,12 @@ final class t3lib_div {
        public static function arrayToLogString(array $arr, $valueList=array(), $valueLength=20) {
                $str = '';
                if (!is_array($valueList))      {
-                       $valueList = t3lib_div::trimExplode(',', $valueList, 1);
+                       $valueList = self::trimExplode(',', $valueList, 1);
                }
                $valListCnt = count($valueList);
                foreach ($arr as $key => $value)        {
                        if (!$valListCnt || in_array($key, $valueList)) {
-                               $str .= (string)$key.trim(': '.t3lib_div::fixed_lgd_cs(str_replace("\n",'|',(string)$value), $valueLength)).'; ';
+                               $str .= (string)$key.trim(': '.self::fixed_lgd_cs(str_replace(LF,'|',(string)$value), $valueLength)).'; ';
                        }
                }
                return $str;
@@ -5381,10 +5935,10 @@ final class t3lib_div {
                }
 
                        // strip profile information for thumbnails and reduce their size
-               if ($command != 'identify' && $gfxConf['im_useStripProfileByDefault'] && $gfxConf['im_stripProfileCommand'] != '') {
+               if ($parameters && $command != 'identify' && $gfxConf['im_useStripProfileByDefault'] && $gfxConf['im_stripProfileCommand'] != '') {
                        if (strpos($parameters, $gfxConf['im_stripProfileCommand']) === false) {
                                        // Determine whether the strip profile action has be disabled by TypoScript:
-                               if ($parameters !== '' && $parameters !== '-version' && strpos($parameters, '###SkipStripProfile###') === false) {
+                               if ($parameters !== '-version' && strpos($parameters, '###SkipStripProfile###') === false) {
                                        $parameters = $gfxConf['im_stripProfileCommand'] . ' ' . $parameters;
                                } else {
                                        $parameters = str_replace('###SkipStripProfile###', '', $parameters);
@@ -5395,7 +5949,7 @@ final class t3lib_div {
                $cmdLine = $path.' '.$parameters;
 
                if($command=='composite' && $switchCompositeParameters) {       // Because of some weird incompatibilities between ImageMagick 4 and 6 (plus GraphicsMagick), it is needed to change the parameters order under some preconditions
-                       $paramsArr = t3lib_div::unQuoteFilenames($parameters);
+                       $paramsArr = self::unQuoteFilenames($parameters);
 
                        if(count($paramsArr)>5) {       // The mask image has been specified => swap the parameters
                                $tmp = $paramsArr[count($paramsArr)-3];
@@ -5424,12 +5978,12 @@ final class t3lib_div {
                        if($quoteActive > -1)   {
                                $paramsArr[$quoteActive] .= ' '.$v;
                                unset($paramsArr[$k]);
-                               if(ereg('"$', $v))      { $quoteActive = -1; }
+                               if(preg_match('/"$/', $v))      { $quoteActive = -1; }
 
                        } elseif(!trim($v))     {
                                unset($paramsArr[$k]);  // Remove empty elements
 
-                       } elseif(ereg('^"', $v))        {
+                       } elseif(preg_match('/^"/', $v))        {
                                $quoteActive = $k;
                        }
                }
@@ -5439,23 +5993,70 @@ final class t3lib_div {
                                $val = preg_replace('/(^"|"$)/','',$val);
                        }
                }
-               return $paramsArr;
+               // return reindexed array
+               return array_values($paramsArr);
+       }
+
+
+       /**
+        * Quotes a string for usage as JS parameter. Depends whether the value is
+        * used in script tags (it doesn't need/must not get htmlspecialchar'ed in
+        * this case).
+        *
+        * @param string $value the string to encode, may be empty
+        * @param boolean $withinCData
+        *        whether the escaped data is expected to be used as CDATA and thus
+        *        does not need to be htmlspecialchared
+        *
+        * @return string the encoded value already quoted (with single quotes),
+        *                will not be empty
+        */
+       static public function quoteJSvalue($value, $withinCData = false)       {
+               $escapedValue = addcslashes(
+                       $value, '\'' . '"' . '\\' . TAB . LF . CR
+               );
+               if (!$withinCData) {
+                       $escapedValue = htmlspecialchars($escapedValue);
+               }
+               return '\'' . $escapedValue . '\'';
        }
 
 
        /**
-        * Quotes a string for usage as JS parameter. Depends wheter the value is used in script tags (it doesn't need/must not get htmlspecialchar'ed in this case)
+        * Ends and cleans all output buffers
         *
-        * @param       string          The string to encode.
-        * @param       boolean         If the values get's used in <script> tags.
-        * @return      string          The encoded value already quoted
+        * @return      void
         */
-       public static function quoteJSvalue($value, $inScriptTags = false)      {
-               $value = addcslashes($value, '\''.'"'.chr(10).chr(13));
-               if (!$inScriptTags) {
-                       $value = htmlspecialchars($value);
+       public static function cleanOutputBuffers() {
+               while (ob_end_clean());
+               header('Content-Encoding: None', TRUE);
+       }
+
+
+       /**
+        *  Ends and flushes all output buffers
+        *
+        * @return      void
+        */
+       public static function flushOutputBuffers() {
+               $obContent = '';
+
+               while ($obContent .= ob_get_clean());
+
+                       // if previously a "Content-Encoding: whatever" has been set, we have to unset it
+               if (!headers_sent()) {
+                       $headersList = headers_list();
+                       foreach ($headersList as $header) {
+                                       // split it up at the :
+                               list($key, $value) = self::trimExplode(':', $header, TRUE);
+                                       // check if we have a Content-Encoding other than 'None'
+                               if (strtolower($key) === 'content-encoding' && strtolower($value) !== 'none') {
+                                       header('Content-Encoding: None');
+                                       break;
+                               }
+                       }
                }
-               return '\''.$value.'\'';
+               echo $obContent;
        }
 }