00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 class eZSimpleTagsOperator
00032 {
00033
00034
00035 function eZSimpleTagsOperator( $name = 'simpletags' )
00036 {
00037 $this->Operators = array( $name );
00038 }
00039
00040
00041
00042
00043 function &operatorList()
00044 {
00045 return $this->Operators;
00046 }
00047
00048
00049
00050
00051 function namedParameterList()
00052 {
00053 return array( 'listname' => array( 'type' => 'string',
00054 'required' => false,
00055 'default' => false ) );
00056 }
00057
00058
00059
00060
00061
00062
00063
00064 function initializeIncludes()
00065 {
00066 $init =& $GLOBALS['eZSimpleTagsInit'];
00067
00068 if ( isset( $init ) and $init )
00069 return;
00070
00071 $init = true;
00072 $ini =& eZINI::instance( 'template.ini' );
00073 $extensions = $ini->variable( 'SimpleTagsOperator', 'Extensions' );
00074 include_once( 'lib/ezutils/classes/ezextension.php' );
00075 $pathList = eZExtension::expandedPathList( $extensions, 'simpletags' );
00076 $includeList = $ini->variable( 'SimpleTagsOperator', 'IncludeList' );
00077
00078 foreach ( $includeList as $includeFile )
00079 {
00080 foreach ( $pathList as $path )
00081 {
00082 $file = $path . '/' . $includeFile;
00083 if ( file_exists( $file ) )
00084 {
00085 include_once( $file );
00086 }
00087 }
00088 }
00089 }
00090
00091
00092
00093
00094 function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters )
00095 {
00096 $elements = preg_split( "#(</?[a-zA-Z0-9_-]+>)#",
00097 $operatorValue,
00098 false,
00099 PREG_SPLIT_DELIM_CAPTURE );
00100 $newElements = array();
00101 $i = 0;
00102 foreach ( $elements as $element )
00103 {
00104 if ( ( $i % 2 ) == 1 )
00105 {
00106 $tagText = $element;
00107 if ( preg_match( "#<(/?)([a-zA-Z0-9_-]+)>#", $tagText, $matches ) )
00108 {
00109 $isEndTag = false;
00110 if ( $matches[1] )
00111 $isEndTag = true;
00112 $tag = $matches[2];
00113 $element = array( $tag, $isEndTag, $tagText );
00114 }
00115 }
00116 $newElements[] = $element;
00117 ++$i;
00118 }
00119
00120 $tagListName = 'TagList';
00121 if ( $namedParameters['listname'] )
00122 $tagListName .= '_' . $namedParameters['listname'];
00123
00124 $this->initializeIncludes();
00125
00126 $tagMap = array();
00127 $ini =& eZINI::instance( 'template.ini' );
00128 $tagList = $ini->variable( 'SimpleTagsOperator', $tagListName );
00129 foreach ( $tagList as $tag => $tagItem )
00130 {
00131 $elements = explode( ';', $tagItem );
00132 $pre = $elements[0];
00133 $post = $elements[1];
00134 $phpFunctions = array();
00135 if ( isset( $elements[2] ) )
00136 {
00137 $phpFunctionList = explode( ',', $elements[2] );
00138 $phpFunctions = array();
00139 foreach ( $phpFunctionList as $phpFunction )
00140 {
00141 if ( function_exists( $phpFunction ) )
00142 $phpFunctions[] = $phpFunction;
00143 }
00144 }
00145 $tagMap[$tag] = array( 'pre' => $pre,
00146 'post' => $post,
00147 'phpfunctions' => $phpFunctions );
00148 }
00149
00150 $textPHPFunctions = array( 'htmlspecialchars' );
00151 $textPre = false;
00152 $textPost = false;
00153 if ( isset( $tagMap['text']['pre'] ) )
00154 $textPre = $tagMap['text']['pre'];
00155 if ( isset( $tagMap['text']['post'] ) )
00156 $textPost = $tagMap['text']['post'];
00157 if ( isset( $tagMap['text']['phpfunctions'] ) )
00158 $textPHPFunctions = $tagMap['text']['phpfunctions'];
00159 $textElements = array();
00160 for ( $i = 0; $i < count( $newElements ); ++$i )
00161 {
00162 $element = $newElements[$i];
00163 if ( is_string( $element ) )
00164 {
00165 $text = $element;
00166 foreach ( $textPHPFunctions as $textPHPFunction )
00167 {
00168 $text = $textPHPFunction( $text );
00169 }
00170 $textElements[] = $textPre . $text . $textPost;
00171 }
00172 else if ( is_array( $element ) )
00173 {
00174 $tag = $element[0];
00175 $isEndTag = $element[1];
00176 $originalText = $element[2];
00177 if ( isset( $tagMap[$tag] ) )
00178 {
00179 $tagOptions = $tagMap[$tag];
00180 $phpFunctions = $tagOptions['phpfunctions'];
00181 if ( !$isEndTag )
00182 {
00183 $tagElements = array();
00184 for ( $j = $i + 1; $j < count( $newElements ); ++$j )
00185 {
00186 $tagElement = $newElements[$j];
00187 if ( is_string( $tagElement ) )
00188 {
00189 $tagElements[] = $tagElement;
00190 }
00191 else if ( is_array( $tagElement ) )
00192 {
00193 if ( $tagElement[0] == $tag and
00194 $tagElement[1] )
00195 {
00196 break;
00197 }
00198 $text = $tagElement[2];
00199 $tagElements[] = $text;
00200 }
00201 }
00202 $i = $j;
00203 $textElements[] = $tagOptions['pre'];
00204 $text = implode( '', $tagElements );
00205 foreach ( $phpFunctions as $phpFunction )
00206 {
00207 $text = $phpFunction( $text );
00208 }
00209 $textElements[] = $text;
00210 $textElements[] = $tagOptions['post'];
00211 }
00212 }
00213 else
00214 {
00215 $text = $originalText;
00216 foreach ( $textPHPFunctions as $textPHPFunction )
00217 {
00218 $text = $textPHPFunction( $text );
00219 }
00220 $textElements[] = $textPre . $text . $textPost;
00221 }
00222 }
00223 }
00224
00225 $operatorValue = implode( '', $textElements );
00226 }
00227
00228
00229 var $Operators;
00230 };
00231
00232 ?>