Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezchartransform.php
Go to the documentation of this file.
1 <?php
19 class eZCharTransform
20 {
21 public $GroupRules;
28 const CODE_DATE = 1177423380;
29
38 function transform( $text, $rule, $charset = false, $useCache = true )
39 {
40 if ( $text === '' )
41 {
42 return $text;
43 }
44
45 if ( $useCache )
46 {
47 // CRC32 is used for speed, MD5 would be more unique but is slower
48 $key = eZSys::ezcrc32( 'Rule: ' . ( is_array( $rule ) ? implode( ',', $rule ) : $rule ) . '-' . $charset );
49 $filepath = $this->cacheFilePath( 'rule-',
50 '-' . $charsetName,
51 $key );
52
53 $charsetName = ( $charset === false ? eZTextCodec::internalCharset() : eZCharsetInfo::realCharsetCode( $charset ) );
54
55 // Try to execute code in the cache file, if it succeeds
56 // \a $text will/ transformated
57 $retText = $this->executeCacheFile( $text, $filepath );
58 if ( $retText !== false )
59 {
60 return $retText;
61 }
62 }
63
64 // Make sure we have a mapper
65 $mapper = new eZCodeMapper();
66
67 $mapper->loadTransformationFiles( $charsetName, false );
68
69 // First generate a unicode based mapping table from the rules
70 $unicodeTable = $mapper->generateMappingCode( $rule );
71 unset($unicodeTable[0]);
72 // Then transform that to a table that works with the current charset
73 // Any character not available in the current charset will be removed
74 $charsetTable = $mapper->generateCharsetMappingTable( $unicodeTable, $charset );
75 $transformationData = array( 'table' => $charsetTable );
76 unset( $unicodeTable );
77
78 if ( $useCache )
79 {
80 $extraCode = '';
81 $this->storeCacheFile( $filepath, $transformationData,
82 $extraCode,
83 'Rule', $charsetName );
84 }
85
86 // Execute transformations
87 return strtr( $text, $transformationData['table'] );
88 }
89
98 function transformByGroup( $text, $group, $charset = false, $useCache = true )
99 {
100 if ( $text === '' )
101 {
102 return $text;
103 }
104 $charsetName = ( $charset === false ? eZTextCodec::internalCharset() : eZCharsetInfo::realCharsetCode( $charset ) );
105 if ( $useCache )
106 {
107 // CRC32 is used for speed, MD5 would be more unique but is slower
108 $keyText = 'Group:' . $group;
109 $key = eZSys::ezcrc32( $keyText . '-' . $charset );
110 $filepath = $this->cacheFilePath( 'g-' . $group . '-',
111 '-' . $charsetName,
112 $key);
113
114 // Try to execute code in the cache file, if it succeeds
115 // \a $text will/ transformated
116 $retText = $this->executeCacheFile( $text, $filepath );
117 if ( $retText !== false )
118 {
119 return $retText;
120 }
121 }
122
123 $commands = $this->groupCommands( $group );
124 if ( $commands === false )
125 return false;
126
127 $mapper = new eZCodeMapper();
128
129 $mapper->loadTransformationFiles( $charsetName, $group );
130
131 $rules = array();
132 foreach ( $commands as $command )
133 {
134 $rules = array_merge( $rules,
135 $mapper->decodeCommand( $command['command'], $command['parameters'] ) );
136 }
137
138 // First generate a unicode based mapping table from the rules
139 $unicodeTable = $mapper->generateMappingCode( $rules );
140 unset($unicodeTable[0]);
141 // Then transform that to a table that works with the current charset
142 // Any character not available in the current charset will be removed
143 $charsetTable = $mapper->generateCharsetMappingTable( $unicodeTable, $charset );
144 $transformationData = array( 'table' => $charsetTable );
145 unset( $unicodeTable );
146
147 if ( $useCache )
148 {
149 $extraCode = '';
150 foreach ( $commands as $command )
151 {
152 $code = $mapper->generateCommandCode( $command, $charsetName );
153 if ( $code !== false )
154 {
155 $extraCode .= $code . "\n";
156 }
157 }
158 $this->storeCacheFile( $filepath, $transformationData,
159 $extraCode,
160 'Group:' . $group, $charsetName );
161 }
162
163 // Execute transformations
164 $text = strtr( $text, $transformationData['table'] );
165
166 // Execute custom code
167 foreach ( $commands as $command )
168 {
169 $mapper->executeCommandCode( $text, $command, $charsetName );
170 }
171
172 return $text;
173 }
174
180 function cachedTransformationPath()
181 {
182 $dir =& $GLOBALS['eZCodeMapperCachePath'];
183 if ( isset( $dir ) )
184 return $dir;
185
186 $sys = eZSys::instance();
187 $dir = $sys->cacheDirectory() . '/trans';
188 return $dir;
189 }
190
201 function groupCommands( $group )
202 {
203 $rules =& $this->GroupRules[$group];
204 if ( isset( $rules ) )
205 return $rules;
206
207 $ini = eZINI::instance( 'transform.ini' );
208 $groups = $ini->variable( 'Transformation', 'Groups' );
209 if ( !in_array( $group, $groups ) )
210 {
211 eZDebug::writeError( "Transformation group $group is not part of the active group list Groups in transform.ini", __METHOD__ );
212 return false;
213 }
214
215 if ( !$ini->hasGroup( $group ) )
216 {
217 eZDebug::writeError( "Transformation group $group is missing in transform.ini", __METHOD__ );
218 return false;
219 }
220
221 $rules = array();
222 $ruleTexts = $ini->variable( $group, 'Commands' );
223 foreach ( $ruleTexts as $ruleText )
224 {
225 if ( preg_match( "#^([a-zA-Z][a-zA-Z0-9_-]+)(\‍((.+)\‍))?$#", $ruleText, $matches ) )
226 {
227 $command = $matches[1];
228 $parameters = array();
229 if ( isset( $matches[2] ) )
230 {
231 $parameters = explode( ',', $matches[3] );
232 }
233 $rules[] = array( 'command' => $command,
234 'parameters' => $parameters );
235 }
236 }
237
238 return $rules;
239 }
240
250 function cacheFilePath( $prefix, $suffix, $key )
251 {
252 $path = eZCharTransform::cachedTransformationPath();
253 if ( !file_exists( $path ) )
254 {
255 eZDir::mkdir( $path, false, true );
256 }
257 return $path . '/' . $prefix . sprintf( "%u", $key ) . $suffix . '.ctt.php'; // ctt=charset transform table
258 }
259
269 protected function executeCacheFile( $text, $filepath, $timestamp = false )
270 {
271 if ( file_exists( $filepath ) )
272 {
273 $time = filemtime( $filepath );
274 $ini = eZINI::instance( 'transform.ini' );
275 if ( $ini->CacheFile && file_exists( $ini->CacheFile ) && $time < filemtime( $ini->CacheFile ) )
276 {
277 return false;
278 }
279 if ( $time >= max( self::CODE_DATE, $timestamp ) )
280 {
281 // Execute the PHP file causing $text will be transformed
282 include "$filepath";
283 return $text;
284 }
285 }
286 return false;
287 }
288
293 function storeCacheFile( $filepath, $transformationData,$extraCode, $type, $charsetName )
294 {
295 $file = basename( $filepath );
296 $dir = dirname( $filepath );
297 $php = new eZPHPCreator( $dir, $file );
298
299 $php->addComment( "Cached transformation data" );
300 $php->addComment( "Type: $type" );
301 $php->addComment( "Charset: $charsetName" );
302 $php->addComment( "Cached transformation data" );
303
304 $php->addCodePiece( '$data = ' . eZCharTransform::varExport( $transformationData ) . ";\n" );
305 $php->addCodePiece( "\$text = strtr( \$text, \$data['table'] );\n" );
306
307 if ( $extraCode )
308 {
309 $php->addCodePiece( $extraCode );
310 }
311
312 return $php->store( true );
313 }
314
328 static function varExport( $value )
329 {
330 return var_export( $value, true );
331 }
332
338 static function wordSeparator()
339 {
340 if ( isset( $GLOBALS['eZCharTransform_wordSeparator'] ) )
341 {
342 return $GLOBALS['eZCharTransform_wordSeparator'];
343 }
344 else
345 {
346 $ini = eZINI::instance();
347 $separator = strtolower( $ini->variable( "URLTranslator", "WordSeparator" ) );
348 switch ( $separator )
349 {
350 case 'dash':
351 $separator = '-';
352 break;
353 case 'underscore':
354 $separator = '_';
355 break;
356 case 'space':
357 $separator = ' ';
358 break;
359 default:
360 return '-';
361 }
362 $GLOBALS['eZCharTransform_wordSeparator'] = $separator;
363 return $separator;
364 }
365 }
366
370 function setWordSeparator( $char )
371 {
372 $GLOBALS['eZCharTransform_wordSeparator'] = $char;
373 }
374
375 static function commandUrlCleanupCompat( $text, $charsetName )
376 {
377 // Old style of url alias with lowercase only and underscores for separators
378 $text = strtolower( $text );
379 $text = preg_replace( array( "#[^a-z0-9]+#",
380 "#^_+|_+$#" ),
381 array( "_",
382 "" ),
383 $text );
384 return $text;
385 }
386
387 static function commandUrlCleanup( $text, $charsetName )
388 {
389 $sep = eZCharTransform::wordSeparator();
390 $sepQ = preg_quote( $sep );
391 $text = preg_replace( array( "#[^a-zA-Z0-9_!\.-]+#",
392 "#\.\.+#", # Remove double dots
393 "#[{$sepQ}]+#", # Turn multiple separators into one
394 "#^[\.{$sepQ}]+|[!\.{$sepQ}]+$#" ), # Strip "!", dots and separator from beginning/end
395 array( $sep,
396 $sep,
397 $sep,
398 "" ),
399 $text );
400 return $text;
401 }
402
403 static function commandUrlCleanupIRI( $text, $charsetName )
404 {
405 // With IRI support we keep all characters except some reserved ones,
406 // they are space, tab, ampersand, semi-colon, forward slash, colon, equal sign, question mark,
407 // square brackets, parenthesis, plus.
408 //
409 // Note: Spaces and tabs are turned into a dash to make it easier for people to
410 // paste urls from the system and have the whole url recognized
411 // instead of being broken off
412 $sep = eZCharTransform::wordSeparator();
413 $sepQ = preg_quote( $sep );
414 $prepost = " ." . $sepQ;
415 if ( $sep != "-" )
416 $prepost .= "-";
417 $text = preg_replace( array( "#[ \t\\\\%\#&;/:=?\[\]()+]+#",
418 "#\.\.+#", # Remove double dots
419 "#[{$sepQ}]+#", # Turn multiple separators into one
420 "#^[{$prepost}]+|[!{$prepost}]+$#" ), # Strip "!", dots and separator from beginning/end
421 array( $sep,
422 $sep,
423 $sep,
424 "" ),
425 $text );
426 return $text;
427 }
428
434 static function instance()
435 {
436 $instance =& $GLOBALS['eZCharTransformInstance'];
437 if ( !isset( $instance ) )
438 {
439 $instance = new eZCharTransform();
440 }
441 return $instance;
442 }
443 }
444
445 ?>
eZCharTransform
Performs rule based transformation of characters in a string.
Definition: ezchartransform.php:20
eZCharTransform\wordSeparator
static wordSeparator()
Definition: ezchartransform.php:338
eZCharTransform\transform
transform( $text, $rule, $charset=false, $useCache=true)
Definition: ezchartransform.php:38
eZCharTransform\groupCommands
groupCommands( $group)
Definition: ezchartransform.php:201
eZCharTransform\$GroupRules
$GroupRules
Definition: ezchartransform.php:21
eZCharTransform\commandUrlCleanupCompat
static commandUrlCleanupCompat( $text, $charsetName)
Definition: ezchartransform.php:375
eZCharTransform\commandUrlCleanup
static commandUrlCleanup( $text, $charsetName)
Definition: ezchartransform.php:387
eZCharTransform\storeCacheFile
storeCacheFile( $filepath, $transformationData, $extraCode, $type, $charsetName)
Definition: ezchartransform.php:293
eZCharTransform\varExport
static varExport( $value)
Definition: ezchartransform.php:328
eZCharTransform\transformByGroup
transformByGroup( $text, $group, $charset=false, $useCache=true)
Definition: ezchartransform.php:98
eZCharTransform\setWordSeparator
setWordSeparator( $char)
Definition: ezchartransform.php:370
eZCharTransform\instance
static instance()
Definition: ezchartransform.php:434
eZCharTransform\cacheFilePath
cacheFilePath( $prefix, $suffix, $key)
Definition: ezchartransform.php:250
eZCharTransform\CODE_DATE
const CODE_DATE
Definition: ezchartransform.php:28
eZCharTransform\commandUrlCleanupIRI
static commandUrlCleanupIRI( $text, $charsetName)
Definition: ezchartransform.php:403
eZCharTransform\executeCacheFile
executeCacheFile( $text, $filepath, $timestamp=false)
Definition: ezchartransform.php:269
eZCharTransform\cachedTransformationPath
cachedTransformationPath()
Definition: ezchartransform.php:180
eZCharsetInfo\realCharsetCode
static realCharsetCode( $charsetCode)
Definition: ezcharsetinfo.php:186
eZCodeMapper
Handles mapping of character codes.
Definition: ezcodemapper.php:19
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZPHPCreator
eZPHPCreator provides a simple interface for creating and executing PHP code.
eZSys\instance
static instance()
Definition: ezsys.php:1272
eZSys\ezcrc32
static ezcrc32( $string)
Definition: ezsys.php:1297
eZTextCodec\internalCharset
static internalCharset()
Definition: eztextcodec.php:546
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.