PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/HTML/Template/Xipe/Main.php

https://bitbucket.org/adarshj/convenient_website
PHP | 761 lines | 524 code | 36 blank | 201 comment | 13 complexity | b4f14914927f385a58258e9369ca0f81 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
  17. // +----------------------------------------------------------------------+
  18. // $Id: Main.php,v 1.18 2003/12/01 21:54:39 cain Exp $
  19. //
  20. require_once 'PEAR.php';
  21. require_once 'HTML/Template/Xipe/Options.php';
  22. require_once 'HTML/Template/Xipe/Filter/Internal.php';
  23. require_once 'Log.php';
  24. /**
  25. * the intention is to use normal php in the template without the need to write
  26. * <?php or <?= all the time
  27. * but smarty, IT[X] and so on just dont give me enough power (i need referencing the reference of a variable and so on)
  28. * and i want to have the entire power of php inside the template, without
  29. * making the code look so ugly as it sometimes does, only because you want to write a varibale
  30. * so this template engine will not do much, but fulfill my needs
  31. *
  32. * @package HTML_Template_Xipe
  33. *
  34. */
  35. class HTML_Template_Xipe_Main extends HTML_Template_Xipe_Options
  36. {
  37. // FIXXME a problem we have here: is that if i use {include(a.php)} the varibales of this file overwrite the ones
  38. // set for the template where it is included, ... ***create a namespace for each file*** somehow, which is unique for every tempalte ... but how
  39. // without the need for the user to change its programming behaviour,
  40. // one solution would be copying everything from $GLOBALS into a unique varibale and replacing all variables in the
  41. // template using this unique variable, but this makes it a HUGE overhead, but would work
  42. // FIXXME2 by testing a site i realized, that a lot of php tags inside a tempalte slow slow down rendering
  43. // very much, somehoe it seems php is not too good in rendering html-pages with a lot of php tags inside
  44. // so we can write a filter, which can be applied that translates the entire template to use echo's only :-)
  45. // some work ha?
  46. // TODO
  47. // - enable a taglib/filter for sql statements inside the template, even though i will never use it :-)
  48. // if we put that in a seperate file we can load it only on request, saves some php-compiling time
  49. // - add a taglib/filter for translate, smthg like: {%t $phpValue %}, to explicitly translate a string
  50. // in case someone doesnt like to use the filter "applyTranslateFunction"
  51. // - add a filter for converting strings to html-entities, could work like "applyTranslateFunction" on
  52. // mark up delimiters, or could be a taglib-tag too ... whatever
  53. //
  54. // MAY BE features
  55. // - url obfuscating (or whatever that is called), rewrite urls and decode them when retreiving a call to a specific page ... somehow
  56. // i dont know if that belongs in a template engine, may be just write in the tutorial how to attach those products easily
  57. // using a filter or whatever
  58. // -
  59. //
  60. //
  61. /**
  62. * for customizing the class
  63. *
  64. * compileDir' => 'tmp', // by default its always the same one as where the template lies in, this might not be desired
  65. * delimiter' => array('{','}'),
  66. * templateDir' => '',
  67. * autoBraces' => true, // see method 'autoBraces' for explaination
  68. * makePhpTags' => true, // set this to false if you dont want the delimiters to be translated to php tags automativally
  69. * forceCompile' => false, // only suggested for debugging
  70. * xmlConfigFile' => 'config.xml', // name of the xml config file which might be found anywhere in the directory structure
  71. * locale' => 'en', // default language
  72. * cache' => array(
  73. 'time' => false// set this to the number of seconds for which the final (html-)file shall be cached
  74. // false means the file is not cached at all
  75. // if you set it to 0 it is cached forever
  76. ,'depends' => 0 // what does it depend on if the cache file can be reused
  77. // i.e. could be $_REQUEST $_COOKIES
  78. ),
  79. * logLevel' => 1, // 1 - only logs new compiles, 0 - logs nothing, 2 - logs everything even only deliveries
  80. * filterLevel' => 10, // 0 is no filter, use this if u want to register each filter u need yourself
  81. // 1-7 can still be defined :-)
  82. // 8 comments stay in the code
  83. // 9 as 10 only that the resulting HTML-code stays readable
  84. // 10 use all default filters, uses the allPre/Postfilters methods of each filter class
  85. * enable-XMLConfig'=>false,
  86. * enable-Cache' => false, // if you only turn on the Cache XMLConfig will be turned on too, since it is needed
  87. * verbose' => true, // set this to true if the engine shall output errors on the screen
  88. // in addition to returning a PEAR_Error
  89. // this makes getting the engine running easier
  90. // on a production system you should turn this off
  91. * logFileExtension'=>'log', // the extension for the log file
  92. * cacheFileExtension'=>'html',// file extension for the cached file
  93. * compiledTemplateExtension'=>'php',// the extension the generated template shall have
  94. *
  95. * The templateExtension is currently used in the Filter/TagLib, for determining if
  96. * an included file is a template file or a macro file. Templates are handled a bit
  97. * differently, they are included as often as the {%include%} tag occurs.
  98. * Macro-files are surrounded by an if() clause, to prevent multiple declarations
  99. * of macros (which are simply php-functions)
  100. * <br>
  101. * 'templateExtension'=> 'tpl',<br>
  102. * 'macroExtension'=> 'mcr'
  103. *
  104. * @access private
  105. * @var array $options the options for initializing the template class
  106. */
  107. var $options = array(
  108. 'compileDir' => 'tmp',
  109. 'delimiter' => array('{','}'),
  110. 'templateDir' => '',
  111. 'autoBraces' => true,
  112. 'makePhpTags' => true,
  113. 'forceCompile' => false,
  114. 'xmlConfigFile' => 'config.xml',
  115. 'locale' => 'en',
  116. 'cache' => array(
  117. 'time' => false
  118. ,'depends' => 0
  119. ),
  120. 'logLevel' => 1,
  121. 'filterLevel' => 10,
  122. 'enable-XMLConfig'=>false,
  123. 'enable-Cache' => false,
  124. 'verbose' => true,
  125. 'logFileExtension'=>'log',
  126. 'cacheFileExtension'=>'html',
  127. 'compiledTemplateExtension'=>'php',
  128. 'debug' => 0,
  129. 'templateExtension'=> 'tpl',
  130. 'macroExtension'=> 'mcr'
  131. );
  132. /**
  133. * the current template file for this instance
  134. */
  135. var $_templateFile = '';
  136. var $_compiledTemplate = '';
  137. var $_didSetup = false;
  138. var $_logFileName = '';
  139. var $_compiledFilePrefix = '';
  140. /**
  141. * @var boolean will be set to true if a recompile is needed,
  142. * this is when any xml-config file that applies to the current template has changed
  143. * or the template itself, or if the compiled template was removed, etc...
  144. */
  145. var $_needsRecompile = false;
  146. /**
  147. * saves the preFilters which will be applied before compilation
  148. *
  149. * @access private
  150. * @var array methods/functions that will be called as prefilter
  151. */
  152. var $_preFilters = array();
  153. /**
  154. * saves the postFilters which will be applied after compilation
  155. *
  156. * @access private
  157. * @var array methods/functions that will be called as postfilter
  158. */
  159. var $_postFilters = array();
  160. /**
  161. * @var float the time compiling/delivering took
  162. */
  163. var $_compileTime = 0;
  164. /**
  165. * @var boolean if the template was compiled
  166. * @see compiled(), compile()
  167. */
  168. var $_compiled = false;
  169. var $logObject = null;
  170. /**
  171. * the constructor, pass the options to it as needed
  172. *
  173. * @see $options
  174. * @version 01/12/03
  175. * @access public
  176. * @author Wolfram Kriesing <wolfram@kriesing.de>
  177. */
  178. function HTML_Template_Xipe_Main( $options=array() )
  179. {
  180. foreach( $options as $key=>$aOption )
  181. $this->setOption( $key , $aOption );
  182. // replace (multiple) backslashes with forward slashes and replace multiple slashes with single slashes
  183. // this way we can quitely sleep and use windows too :-) and only work with forward slashes
  184. // all over the template engine
  185. // the replacing of multiple slashes is doen because i realized that something like
  186. // $DOCUMENT_ROOT.'/libs/' might come out to be '/document/root//libs' depending on the apache configuration
  187. $this->setOption('compileDir' , preg_replace("/\\\+|\/+/" , DIRECTORY_SEPARATOR , $this->getOption('compileDir') ));
  188. $this->setOption('templateDir' , preg_replace("/\\\+|\/+/" , DIRECTORY_SEPARATOR , $this->getOption('templateDir') ));
  189. }
  190. /**
  191. * this method sets up an instance of the engine
  192. * since the philosophie has changed to manage a single object-instance for
  193. * each page we need a method here so we dont need to setup all the internal
  194. * vars many times
  195. *
  196. * @version 02/05/25
  197. * @access public
  198. * @author Wolfram Kriesing <wolfram@kriesing.de>
  199. * @param string the template filename for which this instance shall be setup
  200. * @return boolean if setup was necessary or not
  201. */
  202. function setup( $filename )
  203. {
  204. if( $this->_didSetup == true )
  205. return false;
  206. //
  207. // setup all the internal vars, like compiledTemplate, etc.
  208. //
  209. $this->_templateFile = $filename;
  210. /*
  211. $pathInfo = pathinfo($this->_templateFile);
  212. if( in_array($pathInfo['extension'],array('sxw','sxc','sxi','sxd')) )
  213. {
  214. }
  215. */
  216. // build the filename-prefix that will be used for the compiled file
  217. if( PEAR::isError( $ret = $this->_getCompiledFilePrefix() ) )
  218. {
  219. return $ret;
  220. }
  221. $this->_compiledFilePrefix = $ret;
  222. //print "this->_templateFile = $this->_templateFile<br>this->_compiledFilePrefix = $this->_compiledFilePrefix<br><br>";
  223. $this->_compiledTemplate = $this->_compiledFilePrefix.$this->getOption('compiledTemplateExtension');
  224. //print ".........BEFORE SETUP METHOD..........<br>";
  225. //print_r($this);print "<br><br>";
  226. //
  227. // do all the xml config stuff
  228. //
  229. if( $this->getOption('enable-XMLConfig') )
  230. {
  231. if( PEAR::isError($ret=$this->_applyXmlConfig()) )
  232. return $ret;
  233. }
  234. if( $this->getOption('enable-Cache') )
  235. {
  236. /* $this->_cachedFilename = $this->_getCacheFileName($templateFile);
  237. // set the internal values which are needed by the internal methods
  238. if( $this->_needsRecompile() || $this->_applyXmlConfigIfNeeded() )
  239. $this->doCompile = ??? // shall the compile method do anything or return true right away?
  240. */
  241. }
  242. //print ".........END OF SETUP METHOD..........<br>";
  243. //print_r($this);print "<br><br>";
  244. $this->_didSetup = true;
  245. return true;
  246. }
  247. /**
  248. * gets the destination file name prefix
  249. * i.e.
  250. * for a template in /path/to/tpl/file.tpl
  251. * it returns <templateDir>/<compileDir>/<rest of the path>/file.tpl.<locale>
  252. *
  253. * @access private
  254. * @version 2002/03/11
  255. * @author Wolfram Kriesing <wolfram@kriesing.de>
  256. * @return string the filename-prefix that will be used for the compiled file
  257. */
  258. function _getCompiledFilePrefix()
  259. {
  260. // remove the 'tempalteDir' from the templateFile name
  261. if( strpos( $this->_templateFile , $this->getOption('templateDir') ) === 0 )
  262. $this->_templateFile = str_replace( $this->getOption('templateDir') , '' , $this->_templateFile );
  263. // replace multiple slashes
  264. $this->_templateFile = preg_replace( '/\/+/' , DIRECTORY_SEPARATOR , $this->_templateFile );
  265. // set the template file property and remove leading slashes
  266. $this->_templateFile = preg_replace( '/^\/+/' , '' , $this->_templateFile );
  267. // if the compileDir doesnt contain the document root then we figure it starts under the
  268. // template dir, i.e. the template dir is: '/usr/local/httpd/htdocs/myProject'
  269. // and the compile dir is simply: '/tmp'
  270. if( strpos( $this->getOption('compileDir') , $_SERVER['DOCUMENT_ROOT'] )!==0 )
  271. {
  272. $compileDir = preg_replace('/^\//' , '' , $this->getOption('compileDir') ); // strip of a leading '/' to be sure not to have 2 slashes there :-)
  273. $this->setOption( 'compileDir' , $this->getOption('templateDir').DIRECTORY_SEPARATOR.$compileDir);
  274. }
  275. $compileDir = $this->getOption('compileDir');
  276. if (!@is_dir($compileDir)) { // check if the compile dir has been created
  277. return $this->_error( "'compileDir' could not be accessed <br>".
  278. "1. please create the 'compileDir' which is: <b>'$compileDir'</b><br>2. give write-rights to it" ,
  279. PEAR_ERROR_DIE );
  280. }
  281. if (!@is_writeable($compileDir)) {
  282. // i dont know how to check if "enter" rights are given
  283. return $this->_error( "can not write to 'compileDir', which is <b>'$compileDir'</b><br>".
  284. "1. please give write and enter-rights to it" , PEAR_ERROR_DIE );
  285. }
  286. //print "file=$file<br>";
  287. $directory = dirname( $this->_templateFile );
  288. $filename = basename( $this->_templateFile );
  289. // extract dirname to create directorie(s) in compileDir in case they dont exist yet
  290. // we just keep the directory structure as the application uses it, so we dont get into conflict with names
  291. // i dont see no reason for hashing the directories or the filenames
  292. if ($directory!='.') { // $directory is '.' also if no dir is given
  293. $path = explode(DIRECTORY_SEPARATOR,$directory);
  294. foreach ($path as $aDir) {
  295. $compileDir = $compileDir.DIRECTORY_SEPARATOR.$aDir;
  296. if (!@is_dir($compileDir)) {
  297. umask(0000); // make that the users of this group (mostly 'nogroup') can erase the compiled templates too
  298. if (!@mkdir($compileDir,0777)) {
  299. return $this->_error( "couldn't make directory: <b>'$aDir'</b> under <b>'".
  300. $this->getOption('compileDir')."'</b><br>".
  301. "1. please give write permission to the 'compileDir', ".
  302. "so Xipe can create directories inside" , PEAR_ERROR_DIE );
  303. }
  304. }
  305. }
  306. }
  307. // build the filename prefix, add locale only if given
  308. $ret = $this->getOption('compileDir').DIRECTORY_SEPARATOR.
  309. $this->_templateFile.
  310. ($this->getOption('locale') ? '.'.$this->getOption('locale') : '' ).
  311. '.';
  312. $this->_templateFile = $this->getOption('templateDir').DIRECTORY_SEPARATOR.$this->_templateFile;
  313. return $ret;
  314. }
  315. /**
  316. * DONT USE YET, since i didnt find a way to make it workin, because no
  317. * variable in the template is known if i include it here
  318. * use: $ tpl->compile('index.tpl');
  319. * include($ tpl->getCompiledTemplate());
  320. * instead
  321. *
  322. * @version 01/12/03
  323. * @access public
  324. * @author Wolfram Kriesing <wolfram@kriesing.de>
  325. */
  326. function show()
  327. {
  328. if( $this->compile($file) )
  329. {
  330. include( $this->_compiledTemplate );
  331. }
  332. else
  333. {
  334. return $this->_error( "ERROR: couldnt get compiled template!!!" , PEAR_ERROR_DIE );
  335. }
  336. }
  337. /**
  338. * here all the replacing, filtering and writing of the compiled file is done
  339. * well this is not much work, but still its in here :-)
  340. *
  341. * @access private
  342. * @version 01/12/03
  343. * @author Wolfram Kriesing <wolfram@kriesing.de>
  344. * @param
  345. * @return
  346. */
  347. function parse()
  348. {
  349. // read the entire file into one variable
  350. if( $input = @file($this->_templateFile) )
  351. $fileContent = implode( '' , $input );
  352. else
  353. $fileContent = ''; // if the file doesnt exist, write a template anyway, an empty one but write one
  354. // pass option to know the delimiter in the filter, but parse the xml-config before!!!, see line above
  355. $defaultFilter = new HTML_Template_Xipe_Filter_Internal($this->options);
  356. // apply pre filter
  357. $fileContent = $this->applyFilters( $fileContent , $this->_preFilters );
  358. // this filter does all the default replacement of the delimiters
  359. $internalFilters = array(); // empty them every time, in case this method is called multiple times in one script
  360. // if xml config is on, remove the xml-config tags now
  361. // do this first to reduce the file size and reduce later parsing times
  362. if( $this->getOption('enable-XMLConfig') == true )
  363. $internalFilters[] = array(&$defaultFilter,'removeXmlConfigString' );
  364. if( $this->getOption('makePhpTags') == true )
  365. $internalFilters[] = array(&$defaultFilter,'makePhpTags');
  366. // if the option autoBraces is on apply the _first_ postFilter right here
  367. // which does the autBracing
  368. if( $this->getOption('autoBraces') == true )
  369. $internalFilters[] = array(&$defaultFilter,'autoBraces');
  370. if( $this->getOption('enable-Cache') && $this->getOption('cache','time') !== false )
  371. $internalFilters[] = array( &$this , '_makeCacheable' );
  372. $fileContent = $this->applyFilters( $fileContent , $internalFilters );
  373. // apply post filter
  374. $fileContent = $this->applyFilters( $fileContent , $this->_postFilters );
  375. // write the compiled template into the compiledTemplate-File
  376. if( ($cfp = fopen( $this->_compiledTemplate , 'w' )) )
  377. {
  378. fwrite($cfp,$fileContent);
  379. fclose($cfp);
  380. chmod($this->_compiledTemplate,0777);
  381. }
  382. return true;
  383. }
  384. /**
  385. * compile the template
  386. *
  387. * @access public
  388. * @version 01/12/03
  389. * @author Wolfram Kriesing <wolfram@kriesing.de>
  390. * @param string $file relative to the 'templateDir' which you set when calling the constructor
  391. * @return
  392. */
  393. function compile()
  394. {
  395. //print $this->_templateFile.'<br>';
  396. // cant the log-class do that???
  397. $startTime = split(" ",microtime());
  398. $startTime = $startTime[1]+$startTime[0];
  399. $this->_compiled = false;
  400. if( $this->_needsRecompile() )
  401. {
  402. $this->_log( 'compile started' );
  403. $this->_log('Locale:'.$this->options['locale'] );
  404. $this->_log( 'needsRecompile is true' );
  405. if( !$this->parse() )
  406. return false;
  407. $endTime = split(" ",microtime());
  408. $endTime = $endTime[1]+$endTime[0];
  409. $itTook = ($endTime - $startTime)*100;
  410. $this->_log("(compilation and) deliverance took: $itTook ms" );
  411. $this->_compiled = true;
  412. }
  413. return true;
  414. }
  415. /**
  416. * tells if the current template needed to be compiled
  417. * if 'compile()' was called before and the template didnt
  418. * need to be recompiled this method will return false too
  419. *
  420. * @see compile()
  421. * @access private
  422. * @version 2003/01/10
  423. * @author Wolfram Kriesing <wolfram@kriesing.de>
  424. * @return boolean if the template was compiled
  425. */
  426. function compiled()
  427. {
  428. return $this->_compiled;
  429. }
  430. /**
  431. * checks if the current template needs to be recompiled
  432. * this is the case for either case:
  433. * - if forceCompile option is on
  434. * - if the template has changed/was removed, etc.
  435. *
  436. * @access private
  437. * @version 2002/03/11
  438. * @author Wolfram Kriesing <wolfram@kriesing.de>
  439. * @return true if the template should be recompiled, false otherwise
  440. */
  441. function _needsRecompile()
  442. {
  443. if( $this->getOption('forceCompile') )
  444. {
  445. $this->_log('recompile because option "forceCompile" is true');
  446. return true;
  447. }
  448. if( !$this->_isUpToDate() ) // check if the template has changed
  449. {
  450. $this->_log('recompile because tpl has changed/was removed: '.$this->_templateFile);
  451. return true;
  452. }
  453. if( $this->_needsRecompile == true ) // this will be set to true i.e. by the xml-config check, if any of the xml-files has changed
  454. return true;
  455. return false;
  456. }
  457. /**
  458. * checks if the compiled template is still up to date
  459. *
  460. * @access private
  461. * @version 01/12/03
  462. * @author Wolfram Kriesing <wolfram@kriesing.de>
  463. * @param string $fileToCheckAgainst if given this file is checked if it is newer than the compiled template
  464. * this is useful if for example only an xml-config file has changed but not the
  465. * template itself
  466. * @return boolean true if it is still up to date
  467. */
  468. function _isUpToDate( $fileToCheckAgainst='' )
  469. {
  470. if( $fileToCheckAgainst == '' )
  471. $checkFile = $this->_templateFile;
  472. else
  473. $checkFile = $fileToCheckAgainst;
  474. if( !file_exists( $this->_compiledTemplate ) ||
  475. !file_exists( $checkFile ) ||
  476. filemtime( $checkFile ) > filemtime( $this->_compiledTemplate )
  477. )
  478. {
  479. return false;
  480. }
  481. return true;
  482. }
  483. function getCompiledTemplate()
  484. {
  485. return $this->_compiledTemplate;
  486. }
  487. // filter stuff
  488. /**
  489. * register a prefilter, which will be executed BEFORE the template
  490. * is being compiled
  491. *
  492. * @access public
  493. * @version 01/12/03
  494. * @author Wolfram Kriesing <wolfram@kriesing.de>
  495. * @param mixed the funtion to call, or an array(&$object,'methodname')
  496. * @param mixed if given parameters are passed to the function/method
  497. */
  498. function registerPrefilter( $functionName , $params=null )
  499. {
  500. $this->_registerFilter( 'pre' , $functionName , $params );
  501. }
  502. /**
  503. * register a postfilter, which will be executed AFTER the template
  504. * was compiled
  505. *
  506. * @access public
  507. * @version 01/12/07
  508. * @author Wolfram Kriesing <wolfram@kriesing.de>
  509. * @param mixed the funtion to call, or an array(&$object,'methodname')
  510. * @param mixed if given parameters are passed to the function/method
  511. */
  512. function registerPostfilter( $functionName , $params=null )
  513. {
  514. $this->_registerFilter( 'post' , $functionName , $params );
  515. }
  516. /**
  517. * put the filter either in the pre or postFilter array
  518. * check for language filters too! those have to go first
  519. *
  520. * @access public
  521. * @version 02/09/22 (day of the elections in germany :-) )
  522. * @author Wolfram Kriesing <wolfram@kriesing.de>
  523. * @param mixed the funtion to call, or an array(&$object,'methodname')
  524. * @param mixed if given parameters are passed to the function/method
  525. */
  526. function _registerFilter( $which , $functionName , $params )
  527. {
  528. $langFilter = false;
  529. if( is_array($functionName) )
  530. {
  531. if( method_exists($functionName[0],'isLanguageFilter') )
  532. $langFilter = call_user_func( array($functionName[0],'isLanguageFilter') );
  533. }
  534. if( $params != null )
  535. {
  536. settype($params,'array');
  537. $thisFilter = array($functionName,$params); // use reference here !!! see comment above in registerPrefilter
  538. }
  539. else
  540. $thisFilter = $functionName;
  541. $arrayName = &$this->{'_'.$which.'Filters'};
  542. if( $langFilter ) // language filters have to be applied first, so all other filters also have effect on it
  543. array_unshift($arrayName,$thisFilter);
  544. else
  545. $arrayName[] = $thisFilter;
  546. }
  547. /**
  548. * unregister a filter
  549. *
  550. * @access public
  551. * @version 02/06/21
  552. * @author Wolfram Kriesing <wolfram@kriesing.de>
  553. * @param string the filter name, if not given all filters are unregistered
  554. */
  555. function unregisterFilter( $name=null )
  556. {
  557. if( $name==null )
  558. {
  559. $this->_postFilters = array();
  560. $this->_preFilters = array();
  561. }
  562. }
  563. /**
  564. * actually it will only be used to apply the pre and post filters
  565. *
  566. * @access public
  567. * @version 01/12/10
  568. * @author Wolfram Kriesing <wolfram@kriesing.de>
  569. * @param string $input the string to filter
  570. * @param array $filters an array of filters to apply
  571. * @return string the filtered string
  572. */
  573. function applyFilters( $input , $filters )
  574. {
  575. if( sizeof($filters) )
  576. foreach( $filters as $aFilter )
  577. {
  578. // FIXXME use log class
  579. $startTime = split(" ",microtime());
  580. $startTime = $startTime[1]+$startTime[0];
  581. $sizeBefore = strlen($input);
  582. if( !is_array($aFilter) || // is it not an array so it is simply a function name
  583. ( is_array($aFilter) && is_object($aFilter[0]) ) // or is it an array(&$object,'methodname')?
  584. )
  585. {
  586. $input = call_user_func_array( $aFilter , array($input) );
  587. $appliedFilter = $aFilter;
  588. if( is_array($aFilter) )
  589. $appliedFilter = $aFilter[1];
  590. }
  591. else
  592. {
  593. array_unshift($aFilter[1],$input);
  594. $input = call_user_func_array( $aFilter[0] , $aFilter[1] );
  595. $appliedFilter = $aFilter[0];
  596. if( is_array($aFilter[0]) )
  597. $appliedFilter = $aFilter[0][1];
  598. }
  599. $sizeAfter = strlen($input);
  600. // FIXXME use log class
  601. $endTime = split(" ",microtime());
  602. $endTime = $endTime[1]+$endTime[0];
  603. $itTook = ($endTime - $startTime)*100;
  604. $this->_log("applying filter: '$appliedFilter' \ttook=$itTook ms, \tsize before: $sizeBefore Byte, \tafter: $sizeAfter Byte");
  605. }
  606. return $input;
  607. }
  608. // private stuff
  609. /**
  610. * show an error on the html page, format it, so it is obvious
  611. *
  612. * @access private
  613. * @version 02/02/25
  614. * @param string $message the error message
  615. * @author Wolfram Kriesing <wolfram@kriesing.de>
  616. */
  617. function _error( $message , $mode = PEAR_ERROR_RETURN )
  618. {
  619. if( $this->getOption('verbose') == true )
  620. {
  621. echo '<span style="color:red; background-color:FBFEA1; font-weight:bold;">HTML_Template_Xipe ERROR</span><br>';
  622. echo '<span style="color:008000; background-color:FBFEA1;">';
  623. echo $message;
  624. echo '</span><br><br>';
  625. }
  626. return new PEAR_Error( $message , null , $mode );
  627. }
  628. /**
  629. * logs errors depending on the loglevel it does either write them
  630. * in a file or leaves it all the way
  631. *
  632. * @access private
  633. * @version 02/05/13
  634. * @author Wolfram Kriesing <wolfram@kriesing.de>
  635. * @param string the log message
  636. * @param int the log level of the message
  637. */
  638. function _log($message)
  639. {
  640. if ($this->getOption('logLevel') == 0) {
  641. return;
  642. }
  643. if ($this->getOption('debug') > 0) {
  644. print("LOG MESSAGE: $message.<br>");
  645. }
  646. // dont log the same messages multiple times
  647. if (!empty($this->_loggedMessages[md5($message)])) {
  648. return;
  649. }
  650. $this->_loggedMessages[md5($message)] = true;
  651. if ($this->logObject==null || !is_object($this->logObject) || !$this->_logFileName) {
  652. $this->_logFileName = $this->_compiledFilePrefix.$this->getOption('logFileExtension');
  653. $this->logObject =& Log::factory('file',$this->_logFileName);
  654. $this->_log('---------------------');
  655. //FIXXME write the options in the log-file but nicer!!! than here
  656. $this->_log('options: '.serialize($this->options));
  657. $this->_log( 'current logLevel is: '.$this->getOption('logLevel') );
  658. }
  659. $this->logObject->log($message);
  660. }
  661. }
  662. ?>