Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/textlib.class.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00024 defined('MOODLE_INTERNAL') || die();
00025 
00033 function textlib_get_instance() {
00034     return new textlib();
00035 }
00036 
00037 
00058 class textlib {
00059 
00064     protected static function typo3() {
00065         static $typo3cs = null;
00066 
00067         if (isset($typo3cs)) {
00068             return $typo3cs;
00069         }
00070 
00071         global $CFG;
00072 
00073         // Required files
00074         require_once($CFG->libdir.'/typo3/class.t3lib_cs.php');
00075         require_once($CFG->libdir.'/typo3/class.t3lib_div.php');
00076 
00077         // do not use mbstring or recode because it may return invalid results in some corner cases
00078         $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_convMethod'] = 'iconv';
00079         $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] = 'iconv';
00080 
00081         // Tell Typo3 we are curl enabled always (mandatory since 2.0)
00082         $GLOBALS['TYPO3_CONF_VARS']['SYS']['curlUse'] = '1';
00083 
00084         // And this directory must exist to allow Typo to cache conversion
00085         // tables when using internal functions
00086         make_temp_directory('typo3temp/cs');
00087 
00088         // Make sure typo is using our dir permissions
00089         $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = decoct($CFG->directorypermissions);
00090 
00091         // Default mask for Typo
00092         $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = $CFG->directorypermissions;
00093 
00094         // This full path constants must be defined too, transforming backslashes
00095         // to forward slashed because Typo3 requires it.
00096         define ('PATH_t3lib', str_replace('\\','/',$CFG->libdir.'/typo3/'));
00097         define ('PATH_typo3', str_replace('\\','/',$CFG->libdir.'/typo3/'));
00098         define ('PATH_site', str_replace('\\','/',$CFG->tempdir.'/'));
00099         define ('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
00100 
00101         $typo3cs = new t3lib_cs();
00102 
00103         return $typo3cs;
00104     }
00105 
00115     public static function parse_charset($charset) {
00116         $charset = strtolower($charset);
00117 
00118         // shortcuts so that we do not have to load typo3 on every page
00119 
00120         if ($charset === 'utf8' or $charset === 'utf-8') {
00121             return 'utf-8';
00122         }
00123 
00124         if (preg_match('/^(cp|win|windows)-?(12[0-9]{2})$/', $charset, $matches)) {
00125             return 'windows-'.$matches[2];
00126         }
00127 
00128         if (preg_match('/^iso-8859-[0-9]+$/', $charset, $matches)) {
00129             return $charset;
00130         }
00131 
00132         if ($charset === 'euc-jp') {
00133             return 'euc-jp';
00134         }
00135         if ($charset === 'iso-2022-jp') {
00136             return 'iso-2022-jp';
00137         }
00138         if ($charset === 'shift-jis' or $charset === 'shift_jis') {
00139             return 'shift_jis';
00140         }
00141         if ($charset === 'gb2312') {
00142             return 'gb2312';
00143         }
00144         if ($charset === 'gb18030') {
00145             return 'gb18030';
00146         }
00147 
00148         // fallback to typo3
00149         return self::typo3()->parse_charset($charset);
00150     }
00151 
00162     public static function convert($text, $fromCS, $toCS='utf-8') {
00163         $fromCS = self::parse_charset($fromCS);
00164         $toCS   = self::parse_charset($toCS);
00165 
00166         $text = (string)$text; // we can work only with strings
00167 
00168         if ($text === '') {
00169             return '';
00170         }
00171 
00172         $result = iconv($fromCS, $toCS.'//TRANSLIT', $text);
00173 
00174         if ($result === false or $result === '') {
00175             // note: iconv is prone to return empty string when invalid char encountered, or false if encoding unsupported
00176             $oldlevel = error_reporting(E_PARSE);
00177             $result = self::typo3()->conv($text, $fromCS, $toCS);
00178             error_reporting($oldlevel);
00179         }
00180 
00181         return $result;
00182     }
00183 
00193     public static function substr($text, $start, $len=null, $charset='utf-8') {
00194         $charset = self::parse_charset($charset);
00195 
00196         if ($charset === 'utf-8') {
00197             if (function_exists('mb_substr')) {
00198                 // this is much faster than iconv - see MDL-31142
00199                 if ($len === null) {
00200                     $oldcharset = mb_internal_encoding();
00201                     mb_internal_encoding('UTF-8');
00202                     $result = mb_substr($text, $start);
00203                     mb_internal_encoding($oldcharset);
00204                     return $result;
00205                 } else {
00206                     return mb_substr($text, $start, $len, 'UTF-8');
00207                 }
00208 
00209             } else {
00210                 if ($len === null) {
00211                     $len = iconv_strlen($text, 'UTF-8');
00212                 }
00213                 return iconv_substr($text, $start, $len, 'UTF-8');
00214             }
00215         }
00216 
00217         $oldlevel = error_reporting(E_PARSE);
00218         if ($len === null) {
00219             $result = self::typo3()->substr($charset, $text, $start);
00220         } else {
00221             $result = self::typo3()->substr($charset, $text, $start, $len);
00222         }
00223         error_reporting($oldlevel);
00224 
00225         return $result;
00226     }
00227 
00235     public static function strlen($text, $charset='utf-8') {
00236         $charset = self::parse_charset($charset);
00237 
00238         if ($charset === 'utf-8') {
00239             if (function_exists('mb_strlen')) {
00240                 return mb_strlen($text, 'UTF-8');
00241             } else {
00242                 return iconv_strlen($text, 'UTF-8');
00243             }
00244         }
00245 
00246         $oldlevel = error_reporting(E_PARSE);
00247         $result = self::typo3()->strlen($charset, $text);
00248         error_reporting($oldlevel);
00249 
00250         return $result;
00251     }
00252 
00260     public static function strtolower($text, $charset='utf-8') {
00261         $charset = self::parse_charset($charset);
00262 
00263         if ($charset === 'utf-8' and function_exists('mb_strtolower')) {
00264             return mb_strtolower($text, 'UTF-8');
00265         }
00266 
00267         $oldlevel = error_reporting(E_PARSE);
00268         $result = self::typo3()->conv_case($charset, $text, 'toLower');
00269         error_reporting($oldlevel);
00270 
00271         return $result;
00272     }
00273 
00281     public static function strtoupper($text, $charset='utf-8') {
00282         $charset = self::parse_charset($charset);
00283 
00284         if ($charset === 'utf-8' and function_exists('mb_strtoupper')) {
00285             return mb_strtoupper($text, 'UTF-8');
00286         }
00287 
00288         $oldlevel = error_reporting(E_PARSE);
00289         $result = self::typo3()->conv_case($charset, $text, 'toUpper');
00290         error_reporting($oldlevel);
00291 
00292         return $result;
00293     }
00294 
00303     public static function strpos($haystack, $needle, $offset=0) {
00304         if (function_exists('mb_strpos')) {
00305             return mb_strpos($haystack, $needle, $offset, 'UTF-8');
00306         } else {
00307             return iconv_strpos($haystack, $needle, $offset, 'UTF-8');
00308         }
00309     }
00310 
00318     public static function strrpos($haystack, $needle) {
00319         if (function_exists('mb_strpos')) {
00320             return mb_strrpos($haystack, $needle, null, 'UTF-8');
00321         } else {
00322             return iconv_strrpos($haystack, $needle, 'UTF-8');
00323         }
00324     }
00325 
00334     public static function specialtoascii($text, $charset='utf-8') {
00335         $charset = self::parse_charset($charset);
00336         $oldlevel = error_reporting(E_PARSE);
00337         $result = self::typo3()->specCharsToASCII($charset, $text);
00338         error_reporting($oldlevel);
00339         return $result;
00340     }
00341 
00351     public static function encode_mimeheader($text, $charset='utf-8') {
00352         if (empty($text)) {
00353             return (string)$text;
00354         }
00355         // Normalize charset
00356         $charset = self::parse_charset($charset);
00357         // If the text is pure ASCII, we don't need to encode it
00358         if (self::convert($text, $charset, 'ascii') == $text) {
00359             return $text;
00360         }
00361         // Although RFC says that line feed should be \r\n, it seems that
00362         // some mailers double convert \r, so we are going to use \n alone
00363         $linefeed="\n";
00364         // Define start and end of every chunk
00365         $start = "=?$charset?B?";
00366         $end = "?=";
00367         // Accumulate results
00368         $encoded = '';
00369         // Max line length is 75 (including start and end)
00370         $length = 75 - strlen($start) - strlen($end);
00371         // Multi-byte ratio
00372         $multilength = self::strlen($text, $charset);
00373         // Detect if strlen and friends supported
00374         if ($multilength === false) {
00375             if ($charset == 'GB18030' or $charset == 'gb18030') {
00376                 while (strlen($text)) {
00377                     // try to encode first 22 chars - we expect most chars are two bytes long
00378                     if (preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,22}/m', $text, $matches)) {
00379                         $chunk = $matches[0];
00380                         $encchunk = base64_encode($chunk);
00381                         if (strlen($encchunk) > $length) {
00382                             // find first 11 chars - each char in 4 bytes - worst case scenario
00383                             preg_match('/^(([\x00-\x7f])|([\x81-\xfe][\x40-\x7e])|([\x81-\xfe][\x80-\xfe])|([\x81-\xfe][\x30-\x39]..)){1,11}/m', $text, $matches);
00384                             $chunk = $matches[0];
00385                             $encchunk = base64_encode($chunk);
00386                         }
00387                         $text = substr($text, strlen($chunk));
00388                         $encoded .= ' '.$start.$encchunk.$end.$linefeed;
00389                     } else {
00390                         break;
00391                     }
00392                 }
00393                 $encoded = trim($encoded);
00394                 return $encoded;
00395             } else {
00396                 return false;
00397             }
00398         }
00399         $ratio = $multilength / strlen($text);
00400         // Base64 ratio
00401         $magic = $avglength = floor(3 * $length * $ratio / 4);
00402         // basic infinite loop protection
00403         $maxiterations = strlen($text)*2;
00404         $iteration = 0;
00405         // Iterate over the string in magic chunks
00406         for ($i=0; $i <= $multilength; $i+=$magic) {
00407             if ($iteration++ > $maxiterations) {
00408                 return false; // probably infinite loop
00409             }
00410             $magic = $avglength;
00411             $offset = 0;
00412             // Ensure the chunk fits in length, reducing magic if necessary
00413             do {
00414                 $magic -= $offset;
00415                 $chunk = self::substr($text, $i, $magic, $charset);
00416                 $chunk = base64_encode($chunk);
00417                 $offset++;
00418             } while (strlen($chunk) > $length);
00419             // This chunk doesn't break any multi-byte char. Use it.
00420             if ($chunk)
00421                 $encoded .= ' '.$start.$chunk.$end.$linefeed;
00422         }
00423         // Strip the first space and the last linefeed
00424         $encoded = substr($encoded, 1, -strlen($linefeed));
00425 
00426         return $encoded;
00427     }
00428 
00444     public static function entities_to_utf8($str, $htmlent=true) {
00445         static $trans_tbl; // Going to use static transliteration table
00446 
00447         // Replace numeric entities
00448         $result = preg_replace('~&#x([0-9a-f]+);~ei', 'textlib::code2utf8(hexdec("\\1"))', $str);
00449         $result = preg_replace('~&#([0-9]+);~e', 'textlib::code2utf8(\\1)', $result);
00450 
00451         // Replace literal entities (if desired)
00452         if ($htmlent) {
00453             // Generate/create $trans_tbl
00454             if (!isset($trans_tbl)) {
00455                 $trans_tbl = array();
00456                 foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key) {
00457                     $trans_tbl[$key] = utf8_encode($val);
00458                 }
00459             }
00460             $result = strtr($result, $trans_tbl);
00461         }
00462         // Return utf8-ised string
00463         return $result;
00464     }
00465 
00474     public static function utf8_to_entities($str, $dec=false, $nonnum=false) {
00475         // Avoid some notices from Typo3 code
00476         $oldlevel = error_reporting(E_PARSE);
00477         if ($nonnum) {
00478             $str = self::typo3()->entities_to_utf8($str, true);
00479         }
00480         $result = self::typo3()->utf8_to_entities($str);
00481         if ($dec) {
00482             $result = preg_replace('/&#x([0-9a-f]+);/ie', "'&#'.hexdec('$1').';'", $result);
00483         }
00484         // Restore original debug level
00485         error_reporting($oldlevel);
00486         return $result;
00487     }
00488 
00495     public static function trim_utf8_bom($str) {
00496         $bom = "\xef\xbb\xbf";
00497         if (strpos($str, $bom) === 0) {
00498             return substr($str, strlen($bom));
00499         }
00500         return $str;
00501     }
00502 
00507     public static function get_encodings() {
00508         $encodings = array();
00509         $encodings['UTF-8'] = 'UTF-8';
00510         $winenc = strtoupper(get_string('localewincharset', 'langconfig'));
00511         if ($winenc != '') {
00512             $encodings[$winenc] = $winenc;
00513         }
00514         $nixenc = strtoupper(get_string('oldcharset', 'langconfig'));
00515         $encodings[$nixenc] = $nixenc;
00516 
00517         foreach (self::typo3()->synonyms as $enc) {
00518             $enc = strtoupper($enc);
00519             $encodings[$enc] = $enc;
00520         }
00521         return $encodings;
00522     }
00523 
00531     public static function code2utf8($num) {
00532         if ($num < 128) {
00533             return chr($num);
00534         }
00535         if ($num < 2048) {
00536             return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
00537         }
00538         if ($num < 65536) {
00539             return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
00540         }
00541         if ($num < 2097152) {
00542             return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
00543         }
00544         return '';
00545     }
00546 
00554     public static function strtotitle($text) {
00555         if (empty($text)) {
00556             return $text;
00557         }
00558 
00559         if (function_exists('mb_convert_case')) {
00560             return mb_convert_case($text, MB_CASE_TITLE, 'UTF-8');
00561         }
00562 
00563         $text = self::strtolower($text);
00564         $words = explode(' ', $text);
00565         foreach ($words as $i=>$word) {
00566             $length = self::strlen($word);
00567             if (!$length) {
00568                 continue;
00569 
00570             } else if ($length == 1) {
00571                 $words[$i] = self::strtoupper($word);
00572 
00573             } else {
00574                 $letter = self::substr($word, 0, 1);
00575                 $letter = self::strtoupper($letter);
00576                 $rest   = self::substr($word, 1);
00577                 $words[$i] = $letter.$rest;
00578             }
00579         }
00580         return implode(' ', $words);
00581     }
00582 
00590     public static function asort(array &$arr, $sortflag = null) {
00591         debugging('textlib::asort has been superseeded by collatorlib::asort please upgrade your code to use that', DEBUG_DEVELOPER);
00592         collatorlib::asort($arr, $sortflag);
00593     }
00594 }
00595 
00604 abstract class collatorlib {
00605 
00607     protected static $collator = null;
00608 
00610     protected static $locale = null;
00611 
00617     protected static function ensure_collator_available() {
00618         global $CFG;
00619 
00620         $locale = get_string('locale', 'langconfig');
00621         if (is_null(self::$collator) || $locale != self::$locale) {
00622             self::$collator = false;
00623             self::$locale = $locale;
00624             if (class_exists('Collator', false)) {
00625                 $collator = new Collator($locale);
00626                 if (!empty($collator) && $collator instanceof Collator) {
00627                     // Check for non fatal error messages. This has to be done immediately
00628                     // after instantiation as any further calls to collation will cause
00629                     // it to reset to 0 again (or another error code if one occurred)
00630                     $errorcode = $collator->getErrorCode();
00631                     $errormessage = $collator->getErrorMessage();
00632                     // Check for an error code, 0 means no error occurred
00633                     if ($errorcode !== 0) {
00634                         // Get the actual locale being used, e.g. en, he, zh
00635                         $localeinuse = $collator->getLocale(Locale::ACTUAL_LOCALE);
00636                         // Check for the common fallback warning error codes. If this occurred
00637                         // there is normally little to worry about:
00638                         // - U_USING_DEFAULT_WARNING (127)  - default fallback locale used (pt => UCA)
00639                         // - U_USING_FALLBACK_WARNING (128) - fallback locale used (de_CH => de)
00640                         // (UCA: Unicode Collation Algorithm http://unicode.org/reports/tr10/)
00641                         if ($errorcode === -127 || $errorcode === -128) {
00642                             // Check if the locale in use is UCA default one ('root') or
00643                             // if it is anything like the locale we asked for
00644                             if ($localeinuse !== 'root' && strpos($locale, $localeinuse) !== 0) {
00645                                 // The locale we asked for is completely different to the locale
00646                                 // we have received, let the user know via debugging
00647                                 debugging('Invalid locale: "' . $locale . '", with warning (not fatal) "' . $errormessage .
00648                                     '", falling back to "' . $collator->getLocale(Locale::VALID_LOCALE) . '"');
00649                             } else {
00650                                 // Nothing to do here, this is expected!
00651                                 // The Moodle locale setting isn't what the collator expected but
00652                                 // it is smart enough to match the first characters of our locale
00653                                 // to find the correct locale or to use UCA collation
00654                             }
00655                         } else {
00656                             // We've recieved some other sort of non fatal warning - let the
00657                             // user know about it via debugging.
00658                             debugging('Problem with locale: "' . $locale . '", with message "' . $errormessage .
00659                                 '", falling back to "' . $collator->getLocale(Locale::VALID_LOCALE) . '"');
00660                         }
00661                     }
00662                     // Store the collator object now that we can be sure it is in a workable condition
00663                     self::$collator = $collator;
00664                 } else {
00665                     // Fatal error while trying to instantiate the collator... something went wrong
00666                     debugging('Error instantiating collator for locale: "' . $locale . '", with error [' .
00667                         intl_get_error_code() . '] ' . intl_get_error_message($collator));
00668                 }
00669             }
00670         }
00671         return (self::$collator instanceof Collator);
00672     }
00673 
00681     public static function asort(array &$arr, $sortflag = null) {
00682         if (self::ensure_collator_available()) {
00683             if (!isset($sortflag)) {
00684                 $sortflag = Collator::SORT_REGULAR;
00685             }
00686             self::$collator->asort($arr, $sortflag);
00687             return;
00688         }
00689         asort($arr, SORT_LOCALE_STRING);
00690     }
00691 
00702     public static function compare($str1, $str2) {
00703         if (self::ensure_collator_available()) {
00704             return self::$collator->compare($str1, $str2);
00705         }
00706         return strcmp($str1, $str2);
00707     }
00708 
00716     public static function asort_objects_by_property(array &$objects, $property) {
00717         $comparison = new collatorlib_property_comparison($property);
00718         return uasort($objects, array($comparison, 'compare'));
00719     }
00720 
00728     public static function asort_objects_by_method(array &$objects, $method) {
00729         $comparison = new collatorlib_method_comparison($method);
00730         return uasort($objects, array($comparison, 'compare'));
00731     }
00732 }
00733 
00743 abstract class collatorlib_comparison {
00757     public abstract function compare($a, $b);
00758 }
00759 
00768 class collatorlib_property_comparison extends collatorlib_comparison {
00769 
00771     protected $property;
00772 
00776     public function __construct($property) {
00777         $this->property = $property;
00778     }
00779 
00790     public function compare($obja, $objb) {
00791         $resulta = $obja->{$this->property};
00792         $resultb = $objb->{$this->property};
00793         return collatorlib::compare($resulta, $resultb);
00794     }
00795 }
00796 
00805 class collatorlib_method_comparison extends collatorlib_comparison {
00806 
00808     protected $method;
00809 
00813     public function __construct($method) {
00814         $this->method = $method;
00815     }
00816 
00827     public function compare($obja, $objb) {
00828         $resulta = $obja->{$this->method}();
00829         $resultb = $objb->{$this->method}();
00830         return collatorlib::compare($resulta, $resultb);
00831     }
00832 }
 All Data Structures Namespaces Files Functions Variables Enumerations