[TASK] Fix deprecation comment for t3lib_div::int_from_ver()
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_div.php
index 97a29e6..fc48aef 100644 (file)
@@ -383,39 +383,14 @@ final class t3lib_div {
         * Breaks up a single line of text for emails
         *
         * @param string $str The string to break up
-        * @param string $implChar The string to implode the broken lines with (default/typically \n)
-        * @param integer $charWidth The line length
-        * @return string
+        * @param string $newlineChar The string to implode the broken lines with (default/typically \n)
+        * @param integer $lineWidth The line width
+        * @return string reformatted text
+        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_Mail::breakLinesForPlainEmail()
         */
-       public static function breakLinesForEmail($str, $implChar = LF, $charWidth = 76) {
-               $lines = array();
-               $l = $charWidth;
-               $p = 0;
-               while (strlen($str) > $p) {
-                       $substr = substr($str, $p, $l);
-                       if (strlen($substr) == $l) {
-                               $count = count(explode(' ', trim(strrev($substr))));
-                               if ($count > 1) { // OK...
-                                       $parts = explode(' ', strrev($substr), 2);
-                                       $theLine = strrev($parts[1]);
-                               } else {
-                                       $afterParts = explode(' ', substr($str, $l + $p), 2);
-                                       $theLine = $substr . $afterParts[0];
-                               }
-                               if (!strlen($theLine)) {
-                                       break;
-                               } // Error, because this would keep us in an endless loop.
-                       } else {
-                               $theLine = $substr;
-                       }
-
-                       $lines[] = trim($theLine);
-                       $p += strlen($theLine);
-                       if (!trim(substr($str, $p, $l))) {
-                               break;
-                       } // added...
-               }
-               return implode($implChar, $lines);
+       public static function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
+               self::logDeprecatedFunction();
+               return t3lib_utility_Mail::breakLinesForPlainEmail($str, $newlineChar, $lineWidth);
        }
 
        /**
@@ -496,29 +471,38 @@ final class t3lib_div {
 
                $values = self::trimExplode(',', $list, 1);
                foreach ($values as $test) {
-                       list($test, $mask) = explode('/', $test);
+                       $testList = explode('/', $test);
+                       if (count($testList) == 2) {
+                               list($test, $mask) = $testList;
+                       } else {
+                               $mask = FALSE;
+                       }
+
                        if (self::validIPv6($test)) {
                                $test = self::normalizeIPv6($test);
-                               if (intval($mask)) {
-                                       switch ($mask) { // test on /48 /64
-                                               case '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(self::IPv6Hex2Bin($test), 0, 64);
-                                                       $baseIPBin = substr(self::IPv6Hex2Bin($baseIP), 0, 64);
-                                                       $success = strcmp($testBin, $baseIPBin) == 0 ? TRUE : FALSE;
-                                                       break;
-                                               default:
-                                                       $success = FALSE;
-                                       }
+                               $maskInt = intval($mask) ? intval($mask) : 128;
+                               if ($mask === '0') { // special case; /0 is an allowed mask - equals a wildcard
+                                       $success = TRUE;
+                               } elseif ($maskInt == 128) {
+                                       $success = ($test === $baseIP);
                                } else {
-                                       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;
+                                       $testBin = self::IPv6Hex2Bin($test);
+                                       $baseIPBin = self::IPv6Hex2Bin($baseIP);
+                                       $success = TRUE;
+
+                                       // modulo is 0 if this is a 8-bit-boundary
+                                       $maskIntModulo = $maskInt % 8;
+                                       $numFullCharactersUntilBoundary = intval($maskInt / 8);
+
+                                       if (substr($testBin, 0, $numFullCharactersUntilBoundary) !== substr($baseIPBin, 0, $numFullCharactersUntilBoundary)) {
+                                               $success = FALSE;
+                                       } elseif ($maskIntModulo > 0) {
+                                               // if not an 8-bit-boundary, check bits of last character
+                                               $testLastBits = str_pad(decbin(ord(substr($testBin, $numFullCharactersUntilBoundary, 1))), 8, '0', STR_PAD_LEFT);
+                                               $baseIPLastBits = str_pad(decbin(ord(substr($baseIPBin, $numFullCharactersUntilBoundary, 1))), 8, '0', STR_PAD_LEFT);
+                                               if (strncmp($testLastBits, $baseIPLastBits, $maskIntModulo) != 0) {
+                                                       $success = FALSE;
+                                               }
                                        }
                                }
                        }
@@ -530,17 +514,19 @@ final class t3lib_div {
        }
 
        /**
-        * Convert an IPv6 address to its binary representation
+        * Transform a regular IPv6 address from hex-representation into binary
         *
-        * @param string $hex IPv6 address in hexadecimal form
-        * @return integer
+        * @param string $hex IPv6 address in hex-presentation
+        * @return string Binary representation (16 characters, 128 characters)
+        * @see normalizeIPv6()
         */
        public static function IPv6Hex2Bin($hex) {
-               $bin = '';
-               $hex = str_replace(':', '', $hex); // Replace colon to nothing
-               for ($i = 0; $i < strlen($hex); $i = $i + 2) {
-                       $bin .= chr(hexdec(substr($hex, $i, 2)));
+               // normalized representation has 39 characters (0000:0000:0000:0000:0000:0000:0000:0000)
+               if (strlen($hex) < 39) {
+                       $hex = self::normalizeIPv6($hex);
                }
+               $hex = str_replace(':', '', $hex); // Replace colon to nothing
+               $bin = pack("H*" , $hex);
                return $bin;
        }
 
@@ -771,7 +757,7 @@ final class t3lib_div {
         *
         * @param string $verNumberStr Version number on format x.x.x
         * @return integer Integer version of version number (where each part can count to 999)
-        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_Math::convertVersionNumberToInteger() instead
+        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_VersionNumber::convertVersionNumberToInteger() instead
         */
        public static function int_from_ver($verNumberStr) {
                self::logDeprecatedFunction();
@@ -972,12 +958,12 @@ final class t3lib_div {
         *
         * @param mixed $var Any input variable to test
         * @return boolean Returns TRUE if string is an integer
+        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_Math::canBeInterpretedAsInteger() instead
         */
        public static function testInt($var) {
-               if ($var === '') {
-                       return FALSE;
-               }
-               return (string) intval($var) === (string) $var;
+               self::logDeprecatedFunction();
+
+               return t3lib_utility_Math::canBeInterpretedAsInteger($var);
        }
 
        /**
@@ -1065,55 +1051,12 @@ final class t3lib_div {
         * @param string $string Input string, eg "123 + 456 / 789 - 4"
         * @return integer Calculated value. Or error string.
         * @see calcParenthesis()
+        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_Math::calculateWithPriorityToAdditionAndSubtraction() instead
         */
        public static function calcPriority($string) {
-               $string = preg_replace('/[[:space:]]*/', '', $string); // removing all whitespace
-               $string = '+' . $string; // Ensuring an operator for the first entrance
-               $qm = '\*\/\+-^%';
-               $regex = '([' . $qm . '])([' . $qm . ']?[0-9\.]*)';
-                       // split the expression here:
-               $reg = array();
-               preg_match_all('/' . $regex . '/', $string, $reg);
-
-               reset($reg[2]);
-               $number = 0;
-               $Msign = '+';
-               $err = '';
-               $buffer = doubleval(current($reg[2]));
-               next($reg[2]); // Advance pointer
-
-               while (list($k, $v) = each($reg[2])) {
-                       $v = doubleval($v);
-                       $sign = $reg[1][$k];
-                       if ($sign == '+' || $sign == '-') {
-                               $number = $Msign == '-' ? $number -= $buffer : $number += $buffer;
-                               $Msign = $sign;
-                               $buffer = $v;
-                       } else {
-                               if ($sign == '/') {
-                                       if ($v) {
-                                               $buffer /= $v;
-                                       } else {
-                                               $err = 'dividing by zero';
-                                       }
-                               }
-                               if ($sign == '%') {
-                                       if ($v) {
-                                               $buffer %= $v;
-                                       } else {
-                                               $err = 'dividing by zero';
-                                       }
-                               }
-                               if ($sign == '*') {
-                                       $buffer *= $v;
-                               }
-                               if ($sign == '^') {
-                                       $buffer = pow($buffer, $v);
-                               }
-                       }
-               }
-               $number = $Msign == '-' ? $number -= $buffer : $number += $buffer;
-               return $err ? 'ERROR: ' . $err : $number;
+               self::logDeprecatedFunction();
+
+               return t3lib_utility_Math::calculateWithPriorityToAdditionAndSubtraction($string);
        }
 
        /**
@@ -1122,26 +1065,12 @@ final class t3lib_div {
         * @param string $string Input string, eg "(123 + 456) / 789 - 4"
         * @return integer Calculated value. Or error string.
         * @see calcPriority(), tslib_cObj::stdWrap()
+        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_Math::calculateWithParentheses() instead
         */
        public static function calcParenthesis($string) {
-               $securC = 100;
-               do {
-                       $valueLenO = strcspn($string, '(');
-                       $valueLenC = strcspn($string, ')');
-                       if ($valueLenC == strlen($string) || $valueLenC < $valueLenO) {
-                               $value = self::calcPriority(substr($string, 0, $valueLenC));
-                               $string = $value . substr($string, $valueLenC + 1);
-                               return $string;
-                       } else {
-                               $string = substr($string, 0, $valueLenO) . self::calcParenthesis(substr($string, $valueLenO + 1));
-                       }
-                               // Security:
-                       $securC--;
-                       if ($securC <= 0) {
-                               break;
-                       }
-               } while ($valueLenO < strlen($string));
-               return $string;
+               self::logDeprecatedFunction();
+
+               return t3lib_utility_Math::calculateWithParentheses($string);
        }
 
        /**
@@ -4131,8 +4060,8 @@ final class t3lib_div {
         *                                               Otherwise an empty array and it is FALSE in error case.
         */
        public static function readLLfile($fileRef, $langKey, $charset = '', $errorMode = 0) {
-               /** @var $languageFactory tx_lang_Factory */
-               $languageFactory = t3lib_div::makeInstance('tx_lang_Factory');
+               /** @var $languageFactory t3lib_l10n_Factory */
+               $languageFactory = t3lib_div::makeInstance('t3lib_l10n_Factory');
                return $languageFactory->getParsedData($fileRef, $langKey, $charset, $errorMode);
        }
 
@@ -4750,8 +4679,8 @@ final class t3lib_div {
         * @return object the created instance
         */
        public static function makeInstance($className) {
-               if ($className === '') {
-                       throw new InvalidArgumentException('$classname must not be empty.', 1288965219);
+               if (!is_string($className) || empty($className)) {
+                       throw new InvalidArgumentException('$className must be a non empty string.', 1288965219);
                }
 
                        // Determine final class name which must be instantiated, this takes XCLASS handling