Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezcodepagemapper.php
Go to the documentation of this file.
1 <?php
17 class eZCodePageMapper
18 {
22 public $RequestedInputCharsetCode;
23 public $InputCharsetCode;
27 public $RequestedOutputCharsetCode;
28 public $OutputCharsetCode;
29 public $Valid;
30 public $InputOutputMap;
31 public $OutputInputMap;
32 public $SubstituteOutputChar;
33 public $SubstituteInputChar;
34 public $SubstituteCharValue;
35 const CACHE_CODE_DATE = 1026316422;
36
44 public function __construct( $input_charset_code, $output_charset_code, $use_cache = true )
45 {
46 $this->RequestedInputCharsetCode = $input_charset_code;
47 $this->InputCharsetCode = eZCharsetInfo::realCharsetCode( $input_charset_code );
48 $this->RequestedOutputCharsetCode = $output_charset_code;
49 $this->OutputCharsetCode = eZCharsetInfo::realCharsetCode( $output_charset_code );
50 $this->Valid = false;
51 $this->load( $use_cache );
52 $this->setSubstituteCharacter( 63 ); // ?
53 }
54
55 function isValid()
56 {
57 return $this->Valid;
58 }
59
60 function &mapInputCode( $in_code )
61 {
62 if ( isset( $this->InputOutputMap[$in_code] ) )
63 return $this->InputOutputMap[$in_code];
64 $retValue = null;
65 return $retValue;
66 }
67
68 function &mapOutputCode( $out_code )
69 {
70 if ( isset( $this->OutputInputMap[$out_code] ) )
71 return $this->OutputInputMap[$out_code];
72 $retValue = null;
73 return $retValue;
74 }
75
76 function mapInputChar( $in_char )
77 {
78 $in_code = ord( $in_char );
79 if ( isset( $this->InputOutputMap[$in_code] ) )
80 return chr( $this->InputOutputMap[$in_code] );
81 return $this->SubstituteOutputChar;
82 }
83
84 function mapOutputChar( $out_char )
85 {
86 $out_code = ord( $out_char );
87 if ( isset( $this->OutputInputMap[$out_code] ) )
88 return chr( $this->OutputInputMap[$out_code] );
89 return $this->SubstituteInputChar;
90 }
91
92 function substituteCharacterFor( $char )
93 {
94 }
95
96 function substituteCharacter()
97 {
98 return $this->SubstituteCharValue;
99 }
100
101 function convertString( $str )
102 {
103 $out = "";
104 $len = strlen( $str );
105 for ( $i = 0; $i < $len; ++$i )
106 {
107 $char = $str[$i];
108 $out .= $this->mapInputChar( $char );
109 }
110 return $out;
111 }
112
113 function strlen( $str )
114 {
115 return strlen( $str );
116 }
117
118 function strpos( $haystack, $needle, $offset = 0 )
119 {
120 return strpos( $haystack, $needle, $offset );
121 }
122
123 function strrpos( $haystack, $needle )
124 {
125 return strrpos( $haystack, $needle );
126 }
127
128 function substr( $str, $start, $length )
129 {
130 return substr( $str, $start, $length );
131 }
132
133 function setSubstituteCharacter( $char_code )
134 {
135 $this->SubstituteCharValue = $char_code;
136 $input_codepage = eZCodePage::instance( $this->InputCharsetCode );
137 $output_codepage = eZCodePage::instance( $this->OutputCharsetCode );
138 if ( !$input_codepage->isValid() )
139 {
140 eZDebug::writeError( "Input codepage for " . $this->InputCharsetCode . " is not valid", "eZCodePageMapper" );
141 return false;
142 }
143 if ( !$output_codepage->isValid() )
144 {
145 eZDebug::writeError( "Output codepage for " . $this->OutputCharsetCode . " is not valid", "eZCodePageMapper" );
146 return false;
147 }
148 $this->SubstituteInputChar = chr( $input_codepage->unicodeToCode( $char_code ) );
149 $this->SubstituteOutputChar = chr( $output_codepage->unicodeToCode( $char_code ) );
150 }
151
152 function load( $use_cache = true )
153 {
154 // temporarely hide the cache display problem
155 // http://ez.no/community/bugs/char_transform_cache_file_is_not_valid_php
156 //$use_cache = false;
157 $cache_dir = "var/cache/codepages/";
158 $cache_filename = md5( $this->InputCharsetCode . $this->OutputCharsetCode );
159 $cache = $cache_dir . $cache_filename . ".php";
160
161 if ( !eZCodePage::exists( $this->InputCharsetCode ) )
162 {
163 $input_file = eZCodePage::fileName( $this->InputCharsetCode );
164 eZDebug::writeWarning( "Couldn't load input codepage file $input_file", "eZCodePageMapper" );
165 return false;
166 }
167 if ( !eZCodePage::exists( $this->OutputCharsetCode ) )
168 {
169 $output_file = eZCodePage::fileName( $this->OutputCharsetCode );
170 eZDebug::writeWarning( "Couldn't load output codepage file $output_file", "eZCodePageMapper" );
171 return false;
172 }
173
174 $this->Valid = false;
175 if ( file_exists( $cache ) and $use_cache )
176 {
177 $cache_m = filemtime( $cache );
178 if ( eZCodePage::fileModification( $this->InputCharsetCode ) <= $cache_m and
179 eZCodePage::fileModification( $this->OutputCharsetCode ) <= $cache_m )
180 {
181 unset( $eZCodePageMapperCacheCodeDate );
182 $in_out_map =& $this->InputOutputMap;
183 $out_in_map =& $this->OutputInputMap;
184 eZDebug::writeDebug( 'loading cache from: ' . $cache, __METHOD__ );
185 include( $cache );
186 if ( isset( $eZCodePageMapperCacheCodeDate ) or
187 $eZCodePageMapperCacheCodeDate == self::CACHE_CODE_DATE )
188 {
189 $this->Valid = true;
190 return;
191 }
192 }
193 }
194
195 $this->InputOutputMap = array();
196 $this->OutputInputMap = array();
197
198 $input_codepage = eZCodePage::instance( $this->InputCharsetCode );
199 $output_codepage = eZCodePage::instance( $this->OutputCharsetCode );
200
201 if ( !$input_codepage->isValid() )
202 {
203 eZDebug::writeError( "Input codepage for " . $this->InputCharsetCode . " is not valid", "eZCodePageMapper" );
204 return false;
205 }
206 if ( !$output_codepage->isValid() )
207 {
208 eZDebug::writeError( "Output codepage for " . $this->OutputCharsetCode . " is not valid", "eZCodePageMapper" );
209 return false;
210 }
211
212 $min = max( $input_codepage->minCharValue(),
213 $output_codepage->minCharValue() );
214 $max = min( $input_codepage->maxCharValue(),
215 $output_codepage->maxCharValue() );
216
217 for ( $i = $min; $i <= $max; ++$i )
218 {
219 $code = $i;
220 $unicode = $input_codepage->codeToUnicode( $code );
221 if ( $unicode !== null )
222 {
223 $output_code = $output_codepage->unicodeToCode( $unicode );
224 if ( $output_code !== null )
225 {
226 $this->InputOutputMap[$code] = $output_code;
227 $this->OutputInputMap[$output_code] = $code;
228 }
229 }
230 }
231 }
232
242 static function instance( $input_charset_code, $output_charset_code, $use_cache = true )
243 {
244 $globalsKey = "eZCodePageMapper-$input_charset_code-$output_charset_code";
245
246 if ( !isset( $GLOBALS[$globalsKey] ) ||
247 !( $GLOBALS[$globalsKey] instanceof eZCodePageMapper ) )
248 {
249 $GLOBALS[$globalsKey] = new eZCodePageMapper( $input_charset_code, $output_charset_code, $use_cache );
250 }
251
252 return $GLOBALS[$globalsKey];
253 }
254
255 }
256
257 ?>
eZCharsetInfo\realCharsetCode
static realCharsetCode( $charsetCode)
Definition: ezcharsetinfo.php:186
eZCodePageMapper
The class eZCodePageMapper does.
Definition: ezcodepagemapper.php:18
eZCodePageMapper\strrpos
strrpos( $haystack, $needle)
Definition: ezcodepagemapper.php:123
eZCodePageMapper\$OutputInputMap
$OutputInputMap
Definition: ezcodepagemapper.php:31
eZCodePageMapper\strpos
strpos( $haystack, $needle, $offset=0)
Definition: ezcodepagemapper.php:118
eZCodePageMapper\$SubstituteCharValue
$SubstituteCharValue
Definition: ezcodepagemapper.php:34
eZCodePageMapper\substr
substr( $str, $start, $length)
Definition: ezcodepagemapper.php:128
eZCodePageMapper\__construct
__construct( $input_charset_code, $output_charset_code, $use_cache=true)
Definition: ezcodepagemapper.php:44
eZCodePageMapper\$InputCharsetCode
$InputCharsetCode
Definition: ezcodepagemapper.php:23
eZCodePageMapper\substituteCharacterFor
substituteCharacterFor( $char)
Definition: ezcodepagemapper.php:92
eZCodePageMapper\instance
static instance( $input_charset_code, $output_charset_code, $use_cache=true)
Definition: ezcodepagemapper.php:242
eZCodePageMapper\$RequestedOutputCharsetCode
$RequestedOutputCharsetCode
Definition: ezcodepagemapper.php:27
eZCodePageMapper\$RequestedInputCharsetCode
$RequestedInputCharsetCode
Definition: ezcodepagemapper.php:22
eZCodePageMapper\$SubstituteOutputChar
$SubstituteOutputChar
Definition: ezcodepagemapper.php:32
eZCodePageMapper\isValid
isValid()
Definition: ezcodepagemapper.php:55
eZCodePageMapper\$OutputCharsetCode
$OutputCharsetCode
Definition: ezcodepagemapper.php:28
eZCodePageMapper\substituteCharacter
substituteCharacter()
Definition: ezcodepagemapper.php:96
eZCodePageMapper\$SubstituteInputChar
$SubstituteInputChar
Definition: ezcodepagemapper.php:33
eZCodePageMapper\CACHE_CODE_DATE
const CACHE_CODE_DATE
Definition: ezcodepagemapper.php:35
eZCodePageMapper\convertString
convertString( $str)
Definition: ezcodepagemapper.php:101
eZCodePageMapper\$InputOutputMap
$InputOutputMap
Definition: ezcodepagemapper.php:30
eZCodePageMapper\mapInputCode
& mapInputCode( $in_code)
Definition: ezcodepagemapper.php:60
eZCodePageMapper\mapOutputChar
mapOutputChar( $out_char)
Definition: ezcodepagemapper.php:84
eZCodePageMapper\load
load( $use_cache=true)
Definition: ezcodepagemapper.php:152
eZCodePageMapper\setSubstituteCharacter
setSubstituteCharacter( $char_code)
Definition: ezcodepagemapper.php:133
eZCodePageMapper\mapOutputCode
& mapOutputCode( $out_code)
Definition: ezcodepagemapper.php:68
eZCodePageMapper\mapInputChar
mapInputChar( $in_char)
Definition: ezcodepagemapper.php:76
eZCodePageMapper\$Valid
$Valid
Definition: ezcodepagemapper.php:29
eZCodePageMapper\strlen
strlen( $str)
Definition: ezcodepagemapper.php:113
eZCodePage\fileName
static fileName( $charset_code)
Definition: ezcodepage.php:345
eZCodePage\instance
static instance( $charset_code, $use_cache=true)
Definition: ezcodepage.php:713
eZCodePage\fileModification
fileModification( $charset_code)
Definition: ezcodepage.php:365
eZCodePage\exists
static exists( $charset_code)
Definition: ezcodepage.php:335
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeDebug
static writeDebug( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:588
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.