/kernel/common/ezsimpletagsoperator.php

https://github.com/lserwatka/ezpublish · PHP · 225 lines · 169 code · 14 blank · 42 comment · 24 complexity · 56d0af18d4f1eb43cc7516065ba52897 MD5 · raw file

  1. <?php
  2. //
  3. // Definition of eZSimpleTagsOperator class
  4. //
  5. // Created on: <07-Feb-2003 09:39:55 bf>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. class eZSimpleTagsOperator
  31. {
  32. function eZSimpleTagsOperator( $name = 'simpletags' )
  33. {
  34. $this->Operators = array( $name );
  35. }
  36. /*!
  37. Returns the operators in this class.
  38. */
  39. function operatorList()
  40. {
  41. return $this->Operators;
  42. }
  43. /*!
  44. See eZTemplateOperator::namedParameterList()
  45. */
  46. function namedParameterList()
  47. {
  48. return array( 'listname' => array( 'type' => 'string',
  49. 'required' => false,
  50. 'default' => false ) );
  51. }
  52. /*!
  53. \private
  54. Makes sure extra includes are loaded (include_once) so extra functions can be used.
  55. \note This function will only run one time, if called multiple times it will simple return
  56. */
  57. function initializeIncludes()
  58. {
  59. // If we have this global variable we shouldn't do any processing
  60. if ( !empty( $GLOBALS['eZSimpleTagsInit'] ) )
  61. return;
  62. $GLOBALS['eZSimpleTagsInit'] = true;
  63. $ini = eZINI::instance( 'template.ini' );
  64. $extensions = $ini->variable( 'SimpleTagsOperator', 'Extensions' );
  65. $pathList = eZExtension::expandedPathList( $extensions, 'simpletags' );
  66. $includeList = $ini->variable( 'SimpleTagsOperator', 'IncludeList' );
  67. foreach ( $includeList as $includeFile )
  68. {
  69. foreach ( $pathList as $path )
  70. {
  71. $file = $path . '/' . $includeFile;
  72. if ( file_exists( $file ) )
  73. {
  74. include_once( $file );
  75. }
  76. }
  77. }
  78. }
  79. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
  80. {
  81. $elements = preg_split( "#(</?[a-zA-Z0-9_-]+>)#",
  82. $operatorValue,
  83. false,
  84. PREG_SPLIT_DELIM_CAPTURE );
  85. $newElements = array();
  86. $i = 0;
  87. foreach ( $elements as $element )
  88. {
  89. if ( ( $i % 2 ) == 1 )
  90. {
  91. $tagText = $element;
  92. if ( preg_match( "#<(/?)([a-zA-Z0-9_-]+)>#", $tagText, $matches ) )
  93. {
  94. $isEndTag = false;
  95. if ( $matches[1] )
  96. $isEndTag = true;
  97. $tag = $matches[2];
  98. $element = array( $tag, $isEndTag, $tagText );
  99. }
  100. }
  101. $newElements[] = $element;
  102. ++$i;
  103. }
  104. $tagListName = 'TagList';
  105. if ( $namedParameters['listname'] )
  106. $tagListName .= '_' . $namedParameters['listname'];
  107. $this->initializeIncludes();
  108. $tagMap = array();
  109. $ini = eZINI::instance( 'template.ini' );
  110. $tagList = $ini->variable( 'SimpleTagsOperator', $tagListName );
  111. foreach ( $tagList as $tag => $tagItem )
  112. {
  113. $elements = explode( ';', $tagItem );
  114. $pre = $elements[0];
  115. $post = $elements[1];
  116. $phpFunctions = array();
  117. if ( isset( $elements[2] ) )
  118. {
  119. $phpFunctionList = explode( ',', $elements[2] );
  120. $phpFunctions = array();
  121. foreach ( $phpFunctionList as $phpFunction )
  122. {
  123. if ( function_exists( $phpFunction ) )
  124. $phpFunctions[] = $phpFunction;
  125. }
  126. }
  127. $tagMap[$tag] = array( 'pre' => $pre,
  128. 'post' => $post,
  129. 'phpfunctions' => $phpFunctions );
  130. }
  131. $textPHPFunctions = array( 'htmlspecialchars' );
  132. $textPre = false;
  133. $textPost = false;
  134. if ( isset( $tagMap['text']['pre'] ) )
  135. $textPre = $tagMap['text']['pre'];
  136. if ( isset( $tagMap['text']['post'] ) )
  137. $textPost = $tagMap['text']['post'];
  138. if ( isset( $tagMap['text']['phpfunctions'] ) )
  139. $textPHPFunctions = $tagMap['text']['phpfunctions'];
  140. $textElements = array();
  141. for ( $i = 0; $i < count( $newElements ); ++$i )
  142. {
  143. $element = $newElements[$i];
  144. if ( is_string( $element ) )
  145. {
  146. $text = $element;
  147. foreach ( $textPHPFunctions as $textPHPFunction )
  148. {
  149. $text = $textPHPFunction( $text );
  150. }
  151. $textElements[] = $textPre . $text . $textPost;
  152. }
  153. else if ( is_array( $element ) )
  154. {
  155. $tag = $element[0];
  156. $isEndTag = $element[1];
  157. $originalText = $element[2];
  158. if ( isset( $tagMap[$tag] ) )
  159. {
  160. $tagOptions = $tagMap[$tag];
  161. $phpFunctions = $tagOptions['phpfunctions'];
  162. if ( !$isEndTag )
  163. {
  164. $tagElements = array();
  165. for ( $j = $i + 1; $j < count( $newElements ); ++$j )
  166. {
  167. $tagElement = $newElements[$j];
  168. if ( is_string( $tagElement ) )
  169. {
  170. $tagElements[] = $tagElement;
  171. }
  172. else if ( is_array( $tagElement ) )
  173. {
  174. if ( $tagElement[0] == $tag and
  175. $tagElement[1] )
  176. {
  177. break;
  178. }
  179. $text = $tagElement[2];
  180. $tagElements[] = $text;
  181. }
  182. }
  183. $i = $j;
  184. $textElements[] = $tagOptions['pre'];
  185. $text = implode( '', $tagElements );
  186. foreach ( $phpFunctions as $phpFunction )
  187. {
  188. $text = $phpFunction( $text );
  189. }
  190. $textElements[] = $text;
  191. $textElements[] = $tagOptions['post'];
  192. }
  193. }
  194. else
  195. {
  196. $text = $originalText;
  197. foreach ( $textPHPFunctions as $textPHPFunction )
  198. {
  199. $text = $textPHPFunction( $text );
  200. }
  201. $textElements[] = $textPre . $text . $textPost;
  202. }
  203. }
  204. }
  205. $operatorValue = implode( '', $textElements );
  206. }
  207. /// \privatesection
  208. public $Operators;
  209. };
  210. ?>