ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 include_once("./Services/UICore/lib/html-it/exceptions/class.ilTemplateException.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 
243 
249 
276 
283 
295 
305  var $clearCacheOnParse = false;
306 
313  var $fileRoot = '';
314 
319  var $flagBlocktrouble = false;
320 
325  var $flagGlobalParsed = false;
326 
340 
345 
355  'preserve_data' => false,
356  'use_preg' => true
357  );
358 
370  function __construct($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  throw (new ilTemplateException($this->errorMessage(IT_UNKNOWN_OPTION) . ": '{$option}'"));
404  }
405 
419  {
420  if (is_array($options)) {
421  foreach ($options as $option => $value) {
422  $error = $this->setOption($option, $value);
423  }
424  }
425 
426  return IT_OK;
427  }
428 
433  function show($block = '__global__')
434  {
435  print $this->get($block);
436  } // end func show
437 
447  function get($block = '__global__')
448  {
449  if ($block == '__global__' && !$this->flagGlobalParsed) {
450  $this->parse('__global__');
451  }
452 
453  if (!isset($this->blocklist[$block])) {
455  '"' . $block . "'"));
456  }
457 
458  if (isset($this->blockdata[$block])) {
459  $ret = $this->blockdata[$block];
460  if ($this->clearCache) {
461  unset($this->blockdata[$block]);
462  }
463  if ($this->_options['preserve_data']) {
464  $ret = str_replace(
465  $this->openingDelimiter .
466  '%preserved%' . $this->closingDelimiter,
467  $this->openingDelimiter,
468  $ret
469  );
470  }
471  return $ret;
472  }
473 
474  return '';
475  } // end func get()
476 
485  function parse($block = '__global__', $flag_recursion = false)
486  {
487  static $regs, $values;
488 
489  if (!isset($this->blocklist[$block])) {
491  '"' . $block . "'"));
492  }
493 
494  if ($block == '__global__') {
495  $this->flagGlobalParsed = true;
496  }
497 
498  if (!$flag_recursion) {
499  $regs = array();
500  $values = array();
501  }
502  $outer = $this->blocklist[$block];
503  $empty = true;
504 
505  if ($this->clearCacheOnParse) {
506  foreach ($this->variableCache as $name => $value) {
507  $regs[] = $this->openingDelimiter .
508  $name . $this->closingDelimiter;
509  $values[] = $value;
510  $empty = false;
511  }
512  $this->variableCache = array();
513  } else {
514  foreach ($this->blockvariables[$block] as $allowedvar => $v) {
515 
516  if (isset($this->variableCache[$allowedvar])) {
517  $regs[] = $this->openingDelimiter .
518  $allowedvar . $this->closingDelimiter;
519  $values[] = $this->variableCache[$allowedvar];
520  unset($this->variableCache[$allowedvar]);
521  $empty = false;
522  }
523  }
524  }
525 
526  if (isset($this->blockinner[$block])) {
527  foreach ($this->blockinner[$block] as $k => $innerblock) {
528 
529  $this->parse($innerblock, true);
530  if ($this->blockdata[$innerblock] != '') {
531  $empty = false;
532  }
533 
534  $placeholder = $this->openingDelimiter . "__" .
535  $innerblock . "__" . $this->closingDelimiter;
536  $outer = str_replace(
537  $placeholder,
538  $this->blockdata[$innerblock], $outer
539  );
540  $this->blockdata[$innerblock] = "";
541  }
542 
543  }
544 
545  if (!$flag_recursion && 0 != count($values)) {
546  if ($this->_options['use_preg']) {
547  $regs = array_map(array(
548  &$this, '_addPregDelimiters'),
549  $regs
550  );
551  $funcReplace = 'preg_replace';
552  } else {
553  $funcReplace = 'str_replace';
554  }
555 
556  if ($this->_options['preserve_data']) {
557  $values = array_map(
558  array(&$this, '_preserveOpeningDelimiter'), $values
559  );
560  }
561 
562  $outer = $funcReplace($regs, $values, $outer);
563 
564  if ($this->removeUnknownVariables) {
565  $outer = preg_replace($this->removeVariablesRegExp, "", $outer);
566  }
567  }
568 
569  if ($empty) {
570  if (!$this->removeEmptyBlocks) {
571  $this->blockdata[$block ].= $outer;
572  } else {
573  if (isset($this->touchedBlocks[$block])) {
574  $this->blockdata[$block] .= $outer;
575  unset($this->touchedBlocks[$block]);
576  }
577  }
578  } else {
579  if (empty($this->blockdata[$block])) {
580  $this->blockdata[$block] = $outer;
581  } else {
582  $this->blockdata[$block] .= $outer;
583  }
584  }
585 
586  return $empty;
587  } // end func parse
588 
594  function parseCurrentBlock()
595  {
596  return $this->parse($this->currentBlock);
597  } // end func parseCurrentBlock
598 
613  function setVariable($variable, $value = '')
614  {
615  if (is_array($variable)) {
616  $this->variableCache = array_merge(
617  $this->variableCache, $variable
618  );
619  } else {
620  $this->variableCache[$variable] = $value;
621  }
622  } // end func setVariable
623 
633  function setCurrentBlock($block = '__global__')
634  {
635 
636  if (!isset($this->blocklist[$block])) {
638  '"' . $block . "'"));
639  }
640 
641  $this->currentBlock = $block;
642 
643  return true;
644  } // end func setCurrentBlock
645 
655  function touchBlock($block)
656  {
657  if (!isset($this->blocklist[$block])) {
659  '"' . $block . "'"));
660  }
661 
662  $this->touchedBlocks[$block] = true;
663 
664  return true;
665  } // end func touchBlock
666 
677  function init()
678  {
679  $this->free();
680  require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
682 
683  if ($blockdata = $blocks->get($this->real_filename)) {
684  $this->blockdata = $blockdata['blockdata'];
685  $this->blocklist = $blockdata['blocklist'];
686  } else {
688  $this->findBlocks($this->template);
689  $blockdata['blockdata'] = $this->blockdata;
690  $blockdata['blocklist'] = $this->blocklist;
691  $blocks->set($this->real_filename, $blockdata, 60);
692  }
693 
694  // we don't need it any more
695  $this->template = '';
696 
698  if ($blockvariables = $variables->get($this->real_filename)) {
699  $this->blockvariables = $blockvariables;
700  } else {
701  $this->buildBlockvariablelist();
702  $variables->set($this->real_filename, $this->blockvariables, 60);
703  }
704  } // end func init
705 
714  function free()
715  {
716  $this->err = array();
717 
718  $this->currentBlock = '__global__';
719 
720  $this->variableCache = array();
721  $this->blocklist = array();
722  $this->touchedBlocks = array();
723 
724  $this->flagBlocktrouble = false;
725  $this->flagGlobalParsed = false;
726  } // end func free
727 
741  function setTemplate( $template, $removeUnknownVariables = true,
742  $removeEmptyBlocks = true)
743  {
744  $this->removeUnknownVariables = $removeUnknownVariables;
745  $this->removeEmptyBlocks = $removeEmptyBlocks;
746 
747  if ($template == '' && $this->flagCacheTemplatefile) {
748  $this->variableCache = array();
749  $this->blockdata = array();
750  $this->touchedBlocks = array();
751  $this->currentBlock = '__global__';
752  } else {
753  $this->template = '<!-- BEGIN __global__ -->' . $template .
754  '<!-- END __global__ -->';
755  $this->init();
756  }
757 
758  if ($this->flagBlocktrouble) {
759  return false;
760  }
761 
762  return true;
763  } // end func setTemplate
764 
777  $removeUnknownVariables = true,
778  $removeEmptyBlocks = true )
779  {
780  $template = '';
781  if (!$this->flagCacheTemplatefile ||
782  $this->lastTemplatefile != $filename
783  ) {
784  $template = $this->getFile($filename);
785  }
786  $this->lastTemplatefile = $filename;
787 
788  return $template != '' ?
789  $this->setTemplate(
790  $template,$removeUnknownVariables, $removeEmptyBlocks
791  ) : false;
792  } // end func LoadTemplatefile
793 
805  function setRoot($root)
806  {
807  if ($root != '' && substr($root, -1) != '/') {
808  $root .= '/';
809  }
810 
811  $this->fileRoot = $root;
812  } // end func setRoot
813 
818  {
819  foreach ($this->blocklist as $name => $content) {
820  preg_match_all($this->variablesRegExp, $content, $regs);
821 
822  if (count($regs[1]) != 0) {
823  foreach ($regs[1] as $k => $var) {
824  $this->blockvariables[$name][$var] = true;
825  }
826  } else {
827  $this->blockvariables[$name] = array();
828  }
829  }
830  } // end func buildBlockvariablelist
831 
836  {
837  $regs = array();
838  $values = array();
839 
840  foreach ($this->blockvariables['__global__'] as $allowedvar => $v) {
841  if (isset($this->variableCache[$allowedvar])) {
842  $regs[] = '@' . $this->openingDelimiter .
843  $allowedvar . $this->closingDelimiter . '@';
844  $values[] = $this->variableCache[$allowedvar];
845  unset($this->variableCache[$allowedvar]);
846  }
847  }
848 
849  return array($regs, $values);
850  } // end func getGlobalvariables
851 
858  function findBlocks($string)
859  {
860  $blocklist = array();
861  if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
862  foreach ($regs as $k => $match) {
863  $blockname = $match[1];
864  $blockcontent = $match[2];
865 
866  if (isset($this->blocklist[$blockname])) {
867  throw (new ilTemplateException($this->errorMessage(
868  IT_BLOCK_DUPLICATE, $blockname)));
869  }
870 
871  $this->blocklist[$blockname] = $blockcontent;
872  $this->blockdata[$blockname] = "";
873 
874  $blocklist[] = $blockname;
875 
876  $inner = $this->findBlocks($blockcontent);
877  foreach ($inner as $k => $name) {
878  $pattern = sprintf(
879  '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
880  $name,
881  $name
882  );
883 
884  $this->blocklist[$blockname] = preg_replace(
885  $pattern,
886  $this->openingDelimiter .
887  '__' . $name . '__' .
888  $this->closingDelimiter,
889  $this->blocklist[$blockname]
890  );
891  $this->blockinner[$blockname][] = $name;
892  $this->blockparents[$name] = $blockname;
893  }
894  }
895  }
896 
897  return $blocklist;
898  } // end func findBlocks
899 
905  function getFile($filename)
906  {
907  if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') {
908  $filename = substr($filename, 1);
909  }
910 
911  $filename = $this->fileRoot . $filename;
912 
913  require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
914  $this->real_filename = $filename;
916  if(!$content = $ilGlobalCache->get($filename)) {
917  if (!($fh = @fopen($filename, 'r'))) {
919  ': "' .$filename .'"'));
920  }
921 
922  $fsize = filesize($filename);
923  if ($fsize < 1) {
924  fclose($fh);
925  return '';
926  }
927 
928  $content = fread($fh, $fsize);
929  $ilGlobalCache->set($filename, $content, 60);
930  fclose($fh);
931  }
932 
933 
934  return preg_replace_callback(
935  "#<!-- INCLUDE (.*) -->#im",
936  function ($hit) {
937  return $this->getFile($hit[1]);
938  },
939  $content
940  );
941  } // end func getFile
942 
950  function _addPregDelimiters($str)
951  {
952  return '@' . $str . '@';
953  }
954 
962  {
963  return (false === strpos($str, $this->openingDelimiter))?
964  $str:
965  str_replace(
966  $this->openingDelimiter,
967  $this->openingDelimiter .
968  '%preserved%' . $this->closingDelimiter,
969  $str
970  );
971  }
972 
981  function errorMessage($value, $blockname = '')
982  {
983  static $errorMessages;
984  if (!isset($errorMessages)) {
985  $errorMessages = array(
986  IT_OK => '',
987  IT_ERROR => 'unknown error',
988  IT_TPL_NOT_FOUND => 'Cannot read the template file',
989  IT_BLOCK_NOT_FOUND => 'Cannot find this block',
990  IT_BLOCK_DUPLICATE => 'The name of a block must be'.
991  ' uniquewithin a template.'.
992  ' Found "' . $blockname . '" twice.'.
993  'Unpredictable results '.
994  'may appear.',
995  IT_UNKNOWN_OPTION => 'Unknown option'
996  );
997  }
998 
999  return isset($errorMessages[$value]) ?
1000  $errorMessages[$value] : $errorMessages[IT_ERROR];
1001  }
1002 } // end class IntegratedTemplate
1003 ?>
$error
Definition: Error.php:17
loadTemplatefile( $filename, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Reads a template file from the disk.
Definition: IT.php:776
touchBlock($block)
Preserves an empty block even if removeEmptyBlocks is true.
Definition: IT.php:655
show($block='__global__')
Print a certain block with all replacements done.
Definition: IT.php:433
_addPregDelimiters($str)
Adds delimiters to a string, so it can be used as a pattern in preg_* functions.
Definition: IT.php:950
$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:835
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:981
$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:633
setOptions($options)
Sets the options for the template class.
Definition: IT.php:418
free()
Clears all datafields of the object.
Definition: IT.php:714
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:613
if(!is_array($argv)) $options
setRoot($root)
Sets the file root.
Definition: IT.php:805
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
Create styles array
The data for the language used.
parse($block='__global__', $flag_recursion=false)
Parses the given block.
Definition: IT.php:485
parseCurrentBlock()
Parses the current block.
Definition: IT.php:594
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:817
static log($message, $log_level)
$_hiddenBlocks
List of blocks which should not be shown even if not "empty".
Definition: IT.php:282
__construct($root='', $options=null)
Builds some complex regular expressions and optinally sets the file root directory.
Definition: IT.php:370
$clearCacheOnParse
Definition: IT.php:305
$removeEmptyBlocks
Definition: IT.php:202
setTemplate( $template, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:741
$touchedBlocks
List of blocks to preverse even if they are "empty".
Definition: IT.php:275
_preserveOpeningDelimiter($str)
Replaces an opening delimiter by a special string.
Definition: IT.php:961
$ret
Definition: parser.php:6
init()
Clears all datafields of the object and rebuild the internal blocklist.
Definition: IT.php:677
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:858
$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:905
Integrated Template - IT.