### Eclipse Workspace Patch 1.0 #P moodle Index: lib/textlib.class.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/textlib.class.php,v retrieving revision 1.13 diff -u -r1.13 textlib.class.php --- lib/textlib.class.php 31 Oct 2006 05:03:54 -0000 1.13 +++ lib/textlib.class.php 22 Jan 2007 08:13:12 -0000 @@ -40,7 +40,6 @@ } else { $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_convMethod'] = ''; } - /// If mbstring is available, lets Typo3 library use it for functions if (extension_loaded('mbstring')) { $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] = 'mbstring'; @@ -48,7 +47,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] = ''; } -/// And this directory must exist to allow Typo to cache conversion +/// And this directory must exist to allow Typo to cache conversion /// tables when using internal functions make_upload_directory('temp/typo3temp/cs'); @@ -63,19 +62,11 @@ define ('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':''); -/// As we implement the singleton pattern to use this class (only one instance -/// is shared globally), we need this helper function - -/// IMPORTANT Note: Typo3 libraries always expect lowercase charsets to use 100% -/// its capabilities so, don't forget to make the conversion -/// from every wrapper function! - +/** + * Old compatibility function, not needed any more. + */ function textlib_get_instance () { - static $instance; - if (!is_object($instance)) { - $instance = new textlib(); - } - return $instance; + return new textlib(); } /** @@ -83,119 +74,138 @@ * utf-8 text become mandatory a pool of safe functions under this encoding * become necessary. The name of the methods is exactly the * same than their PHP originals. -* +* +* You do not need to instantiate this class, use textlib::strtoupper(), etc. directly. +* * A big part of this class acts as a wrapper over the Typo3 charset library, * really a cool group of utilities to handle texts and encoding conversion. * -* Take a look to its own copyright and license details. +* Take a look at its own copyright and license details. */ class textlib { - var $typo3cs; - - /* Standard constructor of the class. All it does is to instantiate - * a new t3lib_cs object to have all their functions ready. - * - * Instead of istantiating a lot of objects of this class everytime - * some of their functions is going to be used, you can invoke the: - * textlib_get_instance() function, avoiding the creation of them - * (following the singleton pattern) - */ - function textlib() { - /// Instantiate a conversor object some of the methods in typo3 - /// reference to $this and cannot be executed in a static context - $this->typo3cs = new t3lib_cs(); + /** + * This function emulates static class properties that are not available in PHP. + */ + function _get_static_typo3cs() { + static $typo3cs = false; + if ($typo3cs === false) { + $typo3cs = new t3lib_cs(); // there can be only one instance of t3lib_cs class!! + } + return $typo3cs; } - /* Converts the text between different encodings. It will use iconv, mbstring - * or internal (typo3) methods to try such conversion. Returns false if fails. - */ + /** + * Converts the text between different encodings. It will use iconv, mbstring + * or internal (typo3) methods to try such conversion. Returns false if fails. + */ function convert($text, $fromCS, $toCS='utf-8') { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); /// Call Typo3 conv() function. It will do all the work - $result = $this->typo3cs->conv($text, strtolower($fromCS), strtolower($toCS)); + $result = $typo3cs->conv($text, strtolower($fromCS), strtolower($toCS)); /// Restore original debug level error_reporting($oldlevel); return $result; } - /* Multibyte safe substr() function, uses mbstring if available. */ + /** + * Multibyte safe substr() function, uses mbstring if available. + */ function substr($text, $start, $len=null, $charset='utf-8') { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); /// Call Typo3 substr() function. It will do all the work - $result = $this->typo3cs->substr(strtolower($charset),$text,$start,$len); + $result = $typo3cs->substr(strtolower($charset),$text,$start,$len); /// Restore original debug level error_reporting($oldlevel); return $result; } - /* Multibyte safe strlen() function, uses mbstring if available. */ + /** + * Multibyte safe strlen() function, uses mbstring if available. + */ function strlen($text, $charset='utf-8') { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); /// Call Typo3 strlen() function. It will do all the work - $result = $this->typo3cs->strlen(strtolower($charset),$text); + $result = $typo3cs->strlen(strtolower($charset),$text); /// Restore original debug level error_reporting($oldlevel); return $result; } - /* Multibyte safe strtolower() function, uses mbstring if available. */ + /** + * Multibyte safe strtolower() function, uses mbstring if available. + */ function strtolower($text, $charset='utf-8') { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); /// Call Typo3 conv_case() function. It will do all the work - $result = $this->typo3cs->conv_case(strtolower($charset),$text,'toLower'); + $result = $typo3cs->conv_case(strtolower($charset),$text,'toLower'); /// Restore original debug level error_reporting($oldlevel); return $result; } - /* Multibyte safe strtoupper() function, uses mbstring if available. */ + /** + * Multibyte safe strtoupper() function, uses mbstring if available. + */ function strtoupper($text, $charset='utf-8') { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); /// Call Typo3 conv_case() function. It will do all the work - $result = $this->typo3cs->conv_case(strtolower($charset),$text,'toUpper'); + $result = $typo3cs->conv_case(strtolower($charset),$text,'toUpper'); /// Restore original debug level error_reporting($oldlevel); return $result; } - /* UTF-8 ONLY safe strpos() function, uses mbstring if available. */ + /** + * UTF-8 ONLY safe strpos() function, uses mbstring if available. + */ function strpos($haystack,$needle,$offset=0) { + $typo3cs = textlib::_get_static_typo3cs(); /// Call Typo3 utf8_strpos() function. It will do all the work - return $this->typo3cs->utf8_strpos($haystack,$needle,$offset); + return $typo3cs->utf8_strpos($haystack,$needle,$offset); } - /* UTF-8 ONLY safe strrpos() function, uses mbstring if available. */ + /** + * UTF-8 ONLY safe strrpos() function, uses mbstring if available. + */ function strrpos($haystack,$needle) { + $typo3cs = textlib::_get_static_typo3cs(); /// Call Typo3 utf8_strrpos() function. It will do all the work - return $this->typo3cs->utf8_strrpos($haystack,$needle); + return $typo3cs->utf8_strrpos($haystack,$needle); } - /* Try to convert upper unicode characters to plain ascii, - * the returned string may cantain unconverted unicode characters. - */ - + /** + * Try to convert upper unicode characters to plain ascii, + * the returned string may cantain unconverted unicode characters. + */ function specialtoascii($text,$charset='utf-8') { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); - $result = $this->typo3cs->specCharsToASCII(strtolower($charset),$text); + $result = $typo3cs->specCharsToASCII(strtolower($charset),$text); /// Restore original debug level error_reporting($oldlevel); return $result; } - /* Generate a correct base64 encoded header to be used in MIME mail messages. + /** + * Generate a correct base64 encoded header to be used in MIME mail messages. * This function seems to be 100% compliant with RFC1342. Credits go to: * paravoid (http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283). */ function encode_mimeheader($text, $charset='utf-8') { /// If the text is pure ASCII, we don't need to encode it - if ($this->convert($text, $charset, 'ascii') == $text) { + if (textlib::convert($text, $charset, 'ascii') == $text) { return $text; } /// Although RFC says that line feed should be \r\n, it seems that @@ -209,17 +219,17 @@ /// Max line length is 75 (including start and end) $length = 75 - strlen($start) - strlen($end); /// Multi-byte ratio - $ratio = $this->strlen($text, $charset) / strlen($text); + $ratio = textlib::strlen($text, $charset) / strlen($text); /// Base64 ratio $magic = $avglength = floor(3 * $length * $ratio / 4); /// Iterate over the string in magic chunks - for ($i=0; $i <= $this->strlen($text, $charset); $i+=$magic) { + for ($i=0; $i <= textlib::strlen($text, $charset); $i+=$magic) { $magic = $avglength; $offset = 0; /// Ensure the chunk fits in length, reduding magic if necessary do { $magic -= $offset; - $chunk = $this->substr($text, $i, $magic, $charset); + $chunk = textlib::substr($text, $i, $magic, $charset); $chunk = base64_encode($chunk); $offset++; } while (strlen($chunk) > $length); @@ -227,7 +237,7 @@ if ($chunk) $encoded .= ' '.$start.$chunk.$end.$linefeed; } - /// Strip the first space and the last linefeed + /// Strip the first space and the last linefeed $encoded = substr($encoded, 1, -strlen($linefeed)); return $encoded; @@ -242,12 +252,13 @@ * @return string converted string */ function utf8_to_entities($str, $dec=false, $nonnum=false) { + $typo3cs = textlib::_get_static_typo3cs(); /// Avoid some notices from Typo3 code $oldlevel = error_reporting(E_PARSE); if ($nonnum) { - $str = $this->typo3cs->entities_to_utf8($str, true); + $str = $typo3cs->entities_to_utf8($str, true); } - $result = $this->typo3cs->utf8_to_entities($str); + $result = $typo3cs->utf8_to_entities($str); if ($dec) { $result = preg_replace('/&#x([0-9a-f]+);/ie', "'&#'.hexdec('$1').';'", $result); }