ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
IT.php
Go to the documentation of this file.
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | Copyright (c) 1997-2005 Ulf Wendel, Pierre-Alain Joye |
5 // +----------------------------------------------------------------------+
6 // | This source file is subject to the New BSD license, That is bundled |
7 // | with this package in the file LICENSE, and is available through |
8 // | the world-wide-web at |
9 // | http://www.opensource.org/licenses/bsd-license.php |
10 // | If you did not receive a copy of the new BSDlicense and are unable |
11 // | to obtain it through the world-wide-web, please send a note to |
12 // | pajoye@php.net so we can mail you a copy immediately. |
13 // +----------------------------------------------------------------------+
14 // | Author: Ulf Wendel <ulf.wendel@phpdoc.de> |
15 // | Pierre-Alain Joye <pajoye@php.net> |
16 // +----------------------------------------------------------------------+
17 //
18 // $Id: IT.php,v 1.20 2006/08/17 15:47:22 dsp Exp $
19 //
20 
21 require_once 'PEAR.php';
22 
23 define('IT_OK', 1);
24 define('IT_ERROR', -1);
25 define('IT_TPL_NOT_FOUND', -2);
26 define('IT_BLOCK_NOT_FOUND', -3);
27 define('IT_BLOCK_DUPLICATE', -4);
28 define('IT_UNKNOWN_OPTION', -6);
125 {
132  var $err = array();
133 
138  var $clearCache = false;
139 
146  var $openingDelimiter = '{';
147 
154  var $closingDelimiter = '}';
155 
164  var $blocknameRegExp = '[\.0-9A-Za-z_-]+';
165 
174  var $variablenameRegExp = '[\.0-9A-Za-z_-]+';
175 
183 
189 
196 
202  var $removeEmptyBlocks = true;
203 
209  var $blockRegExp = '';
210 
215  var $currentBlock = '__global__';
216 
221  var $template = '';
222 
229  var $blocklist = array();
230 
236  var $blockdata = array();
237 
242  var $blockvariables = array();
243 
248  var $blockinner = array();
249 
275  var $touchedBlocks = array();
276 
282  var $_hiddenBlocks = array();
283 
294  var $variableCache = array();
295 
305  var $clearCacheOnParse = false;
306 
313  var $fileRoot = '';
314 
319  var $flagBlocktrouble = false;
320 
325  var $flagGlobalParsed = false;
326 
340 
345 
354  var $_options = array(
355  'preserve_data' => false,
356  'use_preg' => true
357  );
358 
370  function HTML_Template_IT($root = '', $options = null)
371  {
372  if (!is_null($options)) {
373  $this->setOptions($options);
374  }
375  $this->variablesRegExp = '@' . $this->openingDelimiter .
376  '(' . $this->variablenameRegExp . ')' .
377  $this->closingDelimiter . '@sm';
378  $this->removeVariablesRegExp = '@' . $this->openingDelimiter .
379  "\s*(" . $this->variablenameRegExp .
380  ")\s*" . $this->closingDelimiter .'@sm';
381 
382  $this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
383  ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
384 
385  $this->setRoot($root);
386  } // end constructor
387 
388 
397  function setOption($option, $value)
398  {
399  if (array_key_exists($option, $this->_options)) {
400  $this->_options[$option] = $value;
401  return IT_OK;
402  }
403 
404  return PEAR::raiseError(
405  $this->errorMessage(IT_UNKNOWN_OPTION) . ": '{$option}'",
407  );
408  }
409 
423  {
424  if (is_array($options)) {
425  foreach ($options as $option => $value) {
426  $error = $this->setOption($option, $value);
427  if (PEAR::isError($error)) {
428  return $error;
429  }
430  }
431  }
432 
433  return IT_OK;
434  }
435 
440  function show($block = '__global__')
441  {
442  print $this->get($block);
443  } // end func show
444 
454  function get($block = '__global__')
455  {
456  if ($block == '__global__' && !$this->flagGlobalParsed) {
457  $this->parse('__global__');
458  }
459 
460  if (!isset($this->blocklist[$block])) {
461  $this->err[] = PEAR::raiseError(
463  '"' . $block . "'",
465  );
466  return '';
467  }
468 
469  if (isset($this->blockdata[$block])) {
470  $ret = $this->blockdata[$block];
471  if ($this->clearCache) {
472  unset($this->blockdata[$block]);
473  }
474  if ($this->_options['preserve_data']) {
475  $ret = str_replace(
476  $this->openingDelimiter .
477  '%preserved%' . $this->closingDelimiter,
478  $this->openingDelimiter,
479  $ret
480  );
481  }
482  return $ret;
483  }
484 
485  return '';
486  } // end func get()
487 
496  function parse($block = '__global__', $flag_recursion = false)
497  {
498  static $regs, $values;
499 
500  if (!isset($this->blocklist[$block])) {
501  return PEAR::raiseError(
502  $this->errorMessage( IT_BLOCK_NOT_FOUND ) . '"' . $block . "'",
504  );
505  }
506 
507  if ($block == '__global__') {
508  $this->flagGlobalParsed = true;
509  }
510 
511  if (!$flag_recursion) {
512  $regs = array();
513  $values = array();
514  }
515  $outer = $this->blocklist[$block];
516  $empty = true;
517 
518  if ($this->clearCacheOnParse) {
519  foreach ($this->variableCache as $name => $value) {
520  $regs[] = $this->openingDelimiter .
521  $name . $this->closingDelimiter;
522  $values[] = $value;
523  $empty = false;
524  }
525  $this->variableCache = array();
526  } else {
527  foreach ($this->blockvariables[$block] as $allowedvar => $v) {
528 
529  if (isset($this->variableCache[$allowedvar])) {
530  $regs[] = $this->openingDelimiter .
531  $allowedvar . $this->closingDelimiter;
532  $values[] = $this->variableCache[$allowedvar];
533  unset($this->variableCache[$allowedvar]);
534  $empty = false;
535  }
536  }
537  }
538 
539  if (isset($this->blockinner[$block])) {
540  foreach ($this->blockinner[$block] as $k => $innerblock) {
541 
542  $this->parse($innerblock, true);
543  if ($this->blockdata[$innerblock] != '') {
544  $empty = false;
545  }
546 
547  $placeholder = $this->openingDelimiter . "__" .
548  $innerblock . "__" . $this->closingDelimiter;
549  $outer = str_replace(
550  $placeholder,
551  $this->blockdata[$innerblock], $outer
552  );
553  $this->blockdata[$innerblock] = "";
554  }
555 
556  }
557 
558  if (!$flag_recursion && 0 != count($values)) {
559  if ($this->_options['use_preg']) {
560  $regs = array_map(array(
561  &$this, '_addPregDelimiters'),
562  $regs
563  );
564  $funcReplace = 'preg_replace';
565  } else {
566  $funcReplace = 'str_replace';
567  }
568 
569  if ($this->_options['preserve_data']) {
570  $values = array_map(
571  array(&$this, '_preserveOpeningDelimiter'), $values
572  );
573  }
574 
575  $outer = $funcReplace($regs, $values, $outer);
576 
577  if ($this->removeUnknownVariables) {
578  $outer = preg_replace($this->removeVariablesRegExp, "", $outer);
579  }
580  }
581 
582  if ($empty) {
583  if (!$this->removeEmptyBlocks) {
584  $this->blockdata[$block ].= $outer;
585  } else {
586  if (isset($this->touchedBlocks[$block])) {
587  $this->blockdata[$block] .= $outer;
588  unset($this->touchedBlocks[$block]);
589  }
590  }
591  } else {
592  if (empty($this->blockdata[$block])) {
593  $this->blockdata[$block] = $outer;
594  } else {
595  $this->blockdata[$block] .= $outer;
596  }
597  }
598 
599  return $empty;
600  } // end func parse
601 
607  function parseCurrentBlock()
608  {
609  return $this->parse($this->currentBlock);
610  } // end func parseCurrentBlock
611 
626  function setVariable($variable, $value = '')
627  {
628  if (is_array($variable)) {
629  $this->variableCache = array_merge(
630  $this->variableCache, $variable
631  );
632  } else {
633  $this->variableCache[$variable] = $value;
634  }
635  } // end func setVariable
636 
646  function setCurrentBlock($block = '__global__')
647  {
648 
649  if (!isset($this->blocklist[$block])) {
650  return PEAR::raiseError(
651  $this->errorMessage( IT_BLOCK_NOT_FOUND ) .
652  '"' . $block . "'", IT_BLOCK_NOT_FOUND
653  );
654  }
655 
656  $this->currentBlock = $block;
657 
658  return true;
659  } // end func setCurrentBlock
660 
670  function touchBlock($block)
671  {
672  if (!isset($this->blocklist[$block])) {
673  return PEAR::raiseError(
675  '"' . $block . "'", IT_BLOCK_NOT_FOUND);
676  }
677 
678  $this->touchedBlocks[$block] = true;
679 
680  return true;
681  } // end func touchBlock
682 
693  function init()
694  {
695  $this->free();
696  require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
698 
699  if ($blockdata = $blocks->get($this->real_filename)) {
700  $this->blockdata = $blockdata['blockdata'];
701  $this->blocklist = $blockdata['blocklist'];
702  } else {
704  $this->findBlocks($this->template);
705  $blockdata['blockdata'] = $this->blockdata;
706  $blockdata['blocklist'] = $this->blocklist;
707  $blocks->set($this->real_filename, $blockdata, 60);
708  }
709 
710  // we don't need it any more
711  $this->template = '';
712 
714  if ($blockvariables = $variables->get($this->real_filename)) {
715  $this->blockvariables = $blockvariables;
716  } else {
717  $this->buildBlockvariablelist();
718  $variables->set($this->real_filename, $this->blockvariables, 60);
719  }
720  } // end func init
721 
730  function free()
731  {
732  $this->err = array();
733 
734  $this->currentBlock = '__global__';
735 
736  $this->variableCache = array();
737  $this->blocklist = array();
738  $this->touchedBlocks = array();
739 
740  $this->flagBlocktrouble = false;
741  $this->flagGlobalParsed = false;
742  } // end func free
743 
757  function setTemplate( $template, $removeUnknownVariables = true,
758  $removeEmptyBlocks = true)
759  {
760  $this->removeUnknownVariables = $removeUnknownVariables;
761  $this->removeEmptyBlocks = $removeEmptyBlocks;
762 
763  if ($template == '' && $this->flagCacheTemplatefile) {
764  $this->variableCache = array();
765  $this->blockdata = array();
766  $this->touchedBlocks = array();
767  $this->currentBlock = '__global__';
768  } else {
769  $this->template = '<!-- BEGIN __global__ -->' . $template .
770  '<!-- END __global__ -->';
771  $this->init();
772  }
773 
774  if ($this->flagBlocktrouble) {
775  return false;
776  }
777 
778  return true;
779  } // end func setTemplate
780 
793  $removeUnknownVariables = true,
794  $removeEmptyBlocks = true )
795  {
796  $template = '';
797  if (!$this->flagCacheTemplatefile ||
798  $this->lastTemplatefile != $filename
799  ) {
800  $template = $this->getFile($filename);
801  }
802  $this->lastTemplatefile = $filename;
803 
804  return $template != '' ?
805  $this->setTemplate(
806  $template,$removeUnknownVariables, $removeEmptyBlocks
807  ) : false;
808  } // end func LoadTemplatefile
809 
821  function setRoot($root)
822  {
823  if ($root != '' && substr($root, -1) != '/') {
824  $root .= '/';
825  }
826 
827  $this->fileRoot = $root;
828  } // end func setRoot
829 
834  {
835  foreach ($this->blocklist as $name => $content) {
836  preg_match_all($this->variablesRegExp, $content, $regs);
837 
838  if (count($regs[1]) != 0) {
839  foreach ($regs[1] as $k => $var) {
840  $this->blockvariables[$name][$var] = true;
841  }
842  } else {
843  $this->blockvariables[$name] = array();
844  }
845  }
846  } // end func buildBlockvariablelist
847 
852  {
853  $regs = array();
854  $values = array();
855 
856  foreach ($this->blockvariables['__global__'] as $allowedvar => $v) {
857  if (isset($this->variableCache[$allowedvar])) {
858  $regs[] = '@' . $this->openingDelimiter .
859  $allowedvar . $this->closingDelimiter . '@';
860  $values[] = $this->variableCache[$allowedvar];
861  unset($this->variableCache[$allowedvar]);
862  }
863  }
864 
865  return array($regs, $values);
866  } // end func getGlobalvariables
867 
874  function findBlocks($string)
875  {
876  $blocklist = array();
877  if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
878  foreach ($regs as $k => $match) {
879  $blockname = $match[1];
880  $blockcontent = $match[2];
881 
882  if (isset($this->blocklist[$blockname])) {
883  $this->err[] = PEAR::raiseError(
884  $this->errorMessage(
885  IT_BLOCK_DUPLICATE, $blockname),
887  );
888  $this->flagBlocktrouble = true;
889  }
890 
891  $this->blocklist[$blockname] = $blockcontent;
892  $this->blockdata[$blockname] = "";
893 
894  $blocklist[] = $blockname;
895 
896  $inner = $this->findBlocks($blockcontent);
897  foreach ($inner as $k => $name) {
898  $pattern = sprintf(
899  '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
900  $name,
901  $name
902  );
903 
904  $this->blocklist[$blockname] = preg_replace(
905  $pattern,
906  $this->openingDelimiter .
907  '__' . $name . '__' .
908  $this->closingDelimiter,
909  $this->blocklist[$blockname]
910  );
911  $this->blockinner[$blockname][] = $name;
912  $this->blockparents[$name] = $blockname;
913  }
914  }
915  }
916 
917  return $blocklist;
918  } // end func findBlocks
919 
925  function getFile($filename)
926  {
927  if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') {
928  $filename = substr($filename, 1);
929  }
930 
931  $filename = $this->fileRoot . $filename;
932 
933  require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
934  $this->real_filename = $filename;
936  if(!$content = $ilGlobalCache->get($filename)) {
937  if (!($fh = @fopen($filename, 'r'))) {
938  $this->err[] = PEAR::raiseError(
940  ': "' .$filename .'"',
942  );
943  return "";
944  }
945 
946  $fsize = filesize($filename);
947  if ($fsize < 1) {
948  fclose($fh);
949  return '';
950  }
951 
952  $content = fread($fh, $fsize);
953  $ilGlobalCache->set($filename, $content, 60);
954  fclose($fh);
955  }
956 
957 
958  return preg_replace_callback(
959  "#<!-- INCLUDE (.*) -->#im",
960  function ($hit) {
961  return $this->getFile($hit[1]);
962  },
963  $content
964  );
965  } // end func getFile
966 
974  function _addPregDelimiters($str)
975  {
976  return '@' . $str . '@';
977  }
978 
986  {
987  return (false === strpos($str, $this->openingDelimiter))?
988  $str:
989  str_replace(
990  $this->openingDelimiter,
991  $this->openingDelimiter .
992  '%preserved%' . $this->closingDelimiter,
993  $str
994  );
995  }
996 
1005  function errorMessage($value, $blockname = '')
1006  {
1007  static $errorMessages;
1008  if (!isset($errorMessages)) {
1009  $errorMessages = array(
1010  IT_OK => '',
1011  IT_ERROR => 'unknown error',
1012  IT_TPL_NOT_FOUND => 'Cannot read the template file',
1013  IT_BLOCK_NOT_FOUND => 'Cannot find this block',
1014  IT_BLOCK_DUPLICATE => 'The name of a block must be'.
1015  ' uniquewithin a template.'.
1016  ' Found "' . $blockname . '" twice.'.
1017  'Unpredictable results '.
1018  'may appear.',
1019  IT_UNKNOWN_OPTION => 'Unknown option'
1020  );
1021  }
1022 
1023  if (PEAR::isError($value)) {
1024  $value = $value->getCode();
1025  }
1026 
1027  return isset($errorMessages[$value]) ?
1028  $errorMessages[$value] : $errorMessages[IT_ERROR];
1029  }
1030 } // end class IntegratedTemplate
1031 ?>
loadTemplatefile( $filename, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Reads a template file from the disk.
Definition: IT.php:792
touchBlock($block)
Preserves an empty block even if removeEmptyBlocks is true.
Definition: IT.php:670
show($block='__global__')
Print a certain block with all replacements done.
Definition: IT.php:440
_addPregDelimiters($str)
Adds delimiters to a string, so it can be used as a pattern in preg_* functions.
Definition: IT.php:974
$removeVariablesRegExp
RegExp used to strip unused variable placeholder.
Definition: IT.php:188
$variablenameRegExp
Definition: IT.php:174
getGlobalvariables()
Returns a list of all global variables.
Definition: IT.php:851
static getInstance($component)
const IT_OK
Definition: IT.php:23
errorMessage($value, $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:1005
$removeUnknownVariables
Definition: IT.php:195
setCurrentBlock($block='__global__')
Sets the name of the current block that is the block where variables are added.
Definition: IT.php:646
setOptions($options)
Sets the options for the template class.
Definition: IT.php:422
HTML_Template_IT($root='', $options=null)
Builds some complex regular expressions and optinally sets the file root directory.
Definition: IT.php:370
free()
Clears all datafields of the object.
Definition: IT.php:730
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:626
if(!is_array($argv)) $options
setRoot($root)
Sets the file root.
Definition: IT.php:821
const IT_UNKNOWN_OPTION
Definition: IT.php:28
const IT_BLOCK_NOT_FOUND
Definition: IT.php:26
$flagCacheTemplatefile
Definition: IT.php:339
$_options
$_options[&#39;preserve_data&#39;] Whether to substitute variables and remove empty placeholders in data pass...
Definition: IT.php:354
parse($block='__global__', $flag_recursion=false)
Parses the given block.
Definition: IT.php:496
$filename
Definition: buildRTE.php:89
parseCurrentBlock()
Parses the current block.
Definition: IT.php:607
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:833
static log($message, $log_level)
$_hiddenBlocks
List of blocks which should not be shown even if not "empty".
Definition: IT.php:282
$clearCacheOnParse
Definition: IT.php:305
$removeEmptyBlocks
Definition: IT.php:202
setTemplate( $template, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:757
$touchedBlocks
List of blocks to preverse even if they are "empty".
Definition: IT.php:275
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
_preserveOpeningDelimiter($str)
Replaces an opening delimiter by a special string.
Definition: IT.php:985
init()
Clears all datafields of the object and rebuild the internal blocklist.
Definition: IT.php:693
setOption($option, $value)
Sets the option for the template class.
Definition: IT.php:397
const IT_BLOCK_DUPLICATE
Definition: IT.php:27
findBlocks($string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:874
$lastTemplatefile
EXPERIMENTAL! FIXME!
Definition: IT.php:344
const IT_ERROR
Definition: IT.php:24
const IT_TPL_NOT_FOUND
Definition: IT.php:25
getFile($filename)
Reads a file from disk and returns its content.
Definition: IT.php:925
Integrated Template - IT.
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279