Fixed bug #15383: [Unit tests] Add tests for t3lib_div::validEmail
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_div.php
index fd82e84..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
@@ -1134,15 +1134,16 @@ final class t3lib_div {
         */
        public static function split_fileref($fileref)  {
                $reg = array();
-               if (preg_match('/(.*\/)(.*)$/',$fileref,$reg)   )       {
+               if (preg_match('/(.*\/)(.*)$/', $fileref, $reg)) {
                        $info['path'] = $reg[1];
                        $info['file'] = $reg[2];
                } else {
                        $info['path'] = '';
                        $info['file'] = $fileref;
                }
-               $reg='';
-               if (    preg_match('/(.*)\.([^\.]*$)/',$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];
@@ -1234,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),
+               ));
        }
 
        /**
@@ -1250,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);
        }
 
        /**
@@ -1582,16 +1599,38 @@ 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);
                }
@@ -1599,6 +1638,16 @@ final class t3lib_div {
        }
 
        /**
+        * 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
         *
@@ -1811,7 +1860,6 @@ final class t3lib_div {
                                unset($array[$k]);
                        }
                }
-               reset($array);
                return $array;
        }
 
@@ -2075,7 +2123,7 @@ final class t3lib_div {
                                $differenceArray[$key] = $value;
                        } elseif (is_array($value)) {
                                if (is_array($array2[$key])) {
-                                       $differenceArray[$key] = t3lib_div::arrayDiffAssocRecursive($value, $array2[$key]);
+                                       $differenceArray[$key] = self::arrayDiffAssocRecursive($value, $array2[$key]);
                                }
                        }
                }
@@ -2817,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);
@@ -3024,9 +3073,9 @@ final class t3lib_div {
                                                unset($recursionResult);
                                                if ($file !== '.' && $file !== '..') {
                                                        if (@is_file($path . '/' . $file)) {
-                                                               $recursionResult = t3lib_div::fixPermissions($path . '/' . $file);
+                                                               $recursionResult = self::fixPermissions($path . '/' . $file);
                                                        } elseif (@is_dir($path . '/' . $file)) {
-                                                               $recursionResult = t3lib_div::fixPermissions($path . '/' . $file, TRUE);
+                                                               $recursionResult = self::fixPermissions($path . '/' . $file, TRUE);
                                                        }
                                                        if (isset($recursionResult) && !$recursionResult) {
                                                                $result = FALSE;
@@ -3519,7 +3568,7 @@ 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>
@@ -3602,13 +3651,13 @@ final class t3lib_div {
                if (is_array($var))     {
                        $debug .= self::view_array($var);
                } elseif (is_object($var)) {
-                       $debug .=  '<b>|Object:<pre>';
+                       $debug .=  '<strong>|Object:<pre>';
                        $debug .= print_r($var, TRUE);
-                       $debug .=  '</pre>|</b>';
+                       $debug .=  '</pre>|</strong>';
                } elseif ((string) $var !== '') {
-                       $debug .= '<b>|' . htmlspecialchars((string)$var) . '|</b>';
+                       $debug .= '<strong>|' . htmlspecialchars((string)$var) . '|</strong>';
                } else {
-                       $debug .= '<b>| debug |</b>';
+                       $debug .= '<strong>| debug |</strong>';
                }
 
                if ($header) {
@@ -3624,9 +3673,9 @@ final class t3lib_div {
                        if ($header !== '') {
                                $tabHeader = htmlspecialchars($header);
                        } else {
-                       $tabHeader = 'Debug';
+                               $tabHeader = 'Debug';
                        }
-                       
+
                        if (is_object($var)) {
                                $debug = str_replace(
                                        array('"', '/', '<', "\n", "\r"),
@@ -3642,22 +3691,36 @@ final class t3lib_div {
                        }
 
                        $script = '
-                               var TYPO3ViewportInstance = null;
-                               var debugMessage = "' . $debug . '";
-                               var header = "' . $tabHeader . '";
-                               var group = "' . $group . '";
-
-                               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;
-                               }
+                               (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 (TYPO3ViewportInstance !== null) {
-                                       TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
-                               } else {
-                                       document.write(debugMessage);
-                               }
+                                       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 {
@@ -4185,6 +4248,8 @@ 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 = self::getIndpEnv('HTTP_HOST');
                }
@@ -5033,7 +5098,7 @@ final class t3lib_div {
                        if ($errorMode == 2) {
                                throw new Exception($errorMsg);
                        } elseif(!$errorMode)   {
-                               debug($errorMsg, 1);
+                               debug($errorMsg, 't3lib_div::callUserFunction');
                        }
                        return false;
                }
@@ -5074,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 {
@@ -5082,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
@@ -5093,7 +5158,7 @@ final class t3lib_div {
                                if ($errorMode == 2) {
                                        throw new Exception($errorMsg);
                                } elseif(!$errorMode)   {
-                                       debug($errorMsg, 1);
+                                       debug($errorMsg, 't3lib_div::callUserFunction');
                                }
                        }
                }
@@ -5140,7 +5205,7 @@ final class t3lib_div {
                                !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;
                        }
 
@@ -5155,7 +5220,7 @@ final class t3lib_div {
 
                                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');
                        }
                }
        }
@@ -5579,6 +5644,7 @@ 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'] = self::getHostname($requestHost=FALSE).':'.PATH_site;
                }
@@ -5742,8 +5808,8 @@ final class t3lib_div {
                }
 
                if (stripos($log, 'file') !== FALSE) {
-                       // write a longer message to the deprecation log
-                       $destination = PATH_typo3conf . '/deprecation_' . self::shortMD5(PATH_site . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']) . '.log';
+                               // 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)
@@ -5754,7 +5820,7 @@ final class t3lib_div {
                }
 
                if (stripos($log, 'devlog') !== FALSE) {
-                       // copy message also to the developer log
+                               // copy message also to the developer log
                        self::devLog($msg, 'Core', self::SYSLOG_SEVERITY_WARNING);
                }
 
@@ -5764,6 +5830,20 @@ final class t3lib_div {
        }
 
        /**
+        * 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 be taken from the annotation.
         * @return      void
@@ -5948,9 +6028,7 @@ final class t3lib_div {
         * @return      void
         */
        public static function cleanOutputBuffers() {
-               while (ob_get_level()) {
-                       ob_end_clean();
-               }
+               while (ob_end_clean());
                header('Content-Encoding: None', TRUE);
        }
 
@@ -5963,9 +6041,7 @@ final class t3lib_div {
        public static function flushOutputBuffers() {
                $obContent = '';
 
-               while (ob_get_level()) {
-                       $obContent .= ob_get_clean();
-               }
+               while ($obContent .= ob_get_clean());
 
                        // if previously a "Content-Encoding: whatever" has been set, we have to unset it
                if (!headers_sent()) {