Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

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