Applied patch #411
[elgg.git] / lib / textlib.class.php
blob977fad58506dc662df7dbf7c17b610d7d808b957
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// Required files
28 require_once($CFG->dirroot . 'lib/typo3/class.t3lib_cs.php');
29 require_once($CFG->dirroot . 'lib/typo3/class.t3lib_div.php');
31 /// If ICONV is available, lets Typo3 library use it for convert
32 if (extension_loaded('iconv')) {
33 $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_convMethod'] = 'iconv';
34 iconv_set_encoding('internal_encoding','utf-8');
35 /// Else if mbstring is available, lets Typo3 library use it
36 } else if (extension_loaded('mbstring')) {
37 $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_convMethod'] = 'mbstring';
38 mb_internal_encoding('utf-8');
39 } else {
40 $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_convMethod'] = '';
43 /// If mbstring is available, lets Typo3 library use it for functions
44 if (extension_loaded('mbstring')) {
45 $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] = 'mbstring';
46 } else {
47 $GLOBALS['TYPO3_CONF_VARS']['SYS']['t3lib_cs_utils'] = '';
48 /// And this directoy must exist to allow Typo to cache conversion
49 /// tables when using internal functions
50 make_upload_directory('temp/typo3temp/cs');
53 /// Default mask for Typo
54 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = $CFG->directorypermissions;
56 /// This full path constants must be defined too, transforming backslashes
57 /// to forward slashed beacuse Typo3 requires it.
58 define ('PATH_t3lib', str_replace('\\','/', $CFG->dirroot.'lib/typo3/'));
59 define ('PATH_typo3', str_replace('\\','/', $CFG->dirroot.'lib/typo3/'));
60 define ('PATH_site', str_replace('\\','/', $CFG->dataroot.'temp/'));
61 define ('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
64 /// As we implement the singleton pattern to use this class (only one instance
65 /// is shared globally), we need this helper function
67 /// IMPORTANT Note: Typo3 libraries always expect lowercase charsets to use 100%
68 /// its capabilities so, don't forget to make the conversion
69 /// from every wrapper function!
71 function textlib_get_instance () {
72 static $instance;
73 if (!is_object($instance)) {
74 $instance = new textlib();
76 return $instance;
79 /**
80 * This class is used to manipulate strings under Moodle 1.6 an later. As
81 * utf-8 text become mandatory a pool of safe functions under this encoding
82 * become necessary. The name of the methods is exactly the
83 * same than their PHP originals.
85 * A big part of this class acts as a wrapper over the Typo3 charset library,
86 * really a cool group of utilities to handle texts and encoding conversion.
88 * Take a look to its own copyright and license details.
90 class textlib {
92 var $typo3cs;
94 /* Standard constructor of the class. All it does is to instantiate
95 * a new t3lib_cs object to have all their functions ready.
97 * Instead of istantiating a lot of objects of this class everytime
98 * some of their functions is going to be used, you can invoke the:
99 * textlib_get_instance() function, avoiding the creation of them
100 * (following the singleton pattern)
102 function textlib() {
103 /// Instantiate a conversor object some of the methods in typo3
104 /// reference to $this and cannot be executed in a static context
105 $this->typo3cs = new t3lib_cs();
108 /* Converts the text between different encodings. It will use iconv, mbstring
109 * or internal (typo3) methods to try such conversion. Returns false if fails.
111 function convert($text, $fromCS, $toCS='utf-8') {
112 /// Avoid some notices from Typo3 code
113 $oldlevel = error_reporting(E_PARSE);
114 /// Call Typo3 conv() function. It will do all the work
115 $result = $this->typo3cs->conv($text, strtolower($fromCS), strtolower($toCS));
116 /// Restore original debug level
117 error_reporting($oldlevel);
118 return $result;
121 /* Multibyte safe substr() function, uses mbstring if available. */
122 function substr($text, $start, $len=null, $charset='utf-8') {
123 /// Call Typo3 substr() function. It will do all the work
124 return $this->typo3cs->substr(strtolower($charset),$text,$start,$len);
127 /* Multibyte safe strlen() function, uses mbstring if available. */
128 function strlen($text, $charset='utf-8') {
129 /// Avoid some notices from Typo3 code
130 $oldlevel = error_reporting(E_PARSE);
131 /// Call Typo3 strlen() function. It will do all the work
132 $result = $this->typo3cs->strlen(strtolower($charset),$text);
133 /// Restore original debug level
134 error_reporting($oldlevel);
135 return $result;
138 /* Multibyte safe strtolower() function, uses mbstring if available. */
139 function strtolower($text, $charset='utf-8') {
140 /// Avoid some notices from Typo3 code
141 $oldlevel = error_reporting(E_PARSE);
142 /// Call Typo3 conv_case() function. It will do all the work
143 $result = $this->typo3cs->conv_case(strtolower($charset),$text,'toLower');
144 /// Restore original debug level
145 error_reporting($oldlevel);
146 return $result;
149 /* Multibyte safe strtoupper() function, uses mbstring if available. */
150 function strtoupper($text, $charset='utf-8') {
151 /// Avoid some notices from Typo3 code
152 $oldlevel = error_reporting(E_PARSE);
153 /// Call Typo3 conv_case() function. It will do all the work
154 $result = $this->typo3cs->conv_case(strtolower($charset),$text,'toUpper');
155 /// Restore original debug level
156 error_reporting($oldlevel);
157 return $result;
160 /* UTF-8 ONLY safe strpos() function, uses mbstring if available. */
161 function strpos($haystack,$needle,$offset=0) {
162 /// Call Typo3 utf8_strpos() function. It will do all the work
163 return $this->typo3cs->utf8_strpos($haystack,$needle,$offset);
166 /* UTF-8 ONLY safe strrpos() function, uses mbstring if available. */
167 function strrpos($haystack,$needle) {
168 /// Call Typo3 utf8_strrpos() function. It will do all the work
169 return $this->typo3cs->utf8_strrpos($haystack,$needle);