ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
422  function setOptions($options)
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  $this->findBlocks($this->template);
697  // we don't need it any more
698  $this->template = '';
699  $this->buildBlockvariablelist();
700  } // end func init
701 
710  function free()
711  {
712  $this->err = array();
713 
714  $this->currentBlock = '__global__';
715 
716  $this->variableCache = array();
717  $this->blocklist = array();
718  $this->touchedBlocks = array();
719 
720  $this->flagBlocktrouble = false;
721  $this->flagGlobalParsed = false;
722  } // end func free
723 
738  $removeEmptyBlocks = true)
739  {
740  $this->removeUnknownVariables = $removeUnknownVariables;
741  $this->removeEmptyBlocks = $removeEmptyBlocks;
742 
743  if ($template == '' && $this->flagCacheTemplatefile) {
744  $this->variableCache = array();
745  $this->blockdata = array();
746  $this->touchedBlocks = array();
747  $this->currentBlock = '__global__';
748  } else {
749  $this->template = '<!-- BEGIN __global__ -->' . $template .
750  '<!-- END __global__ -->';
751  $this->init();
752  }
753 
754  if ($this->flagBlocktrouble) {
755  return false;
756  }
757 
758  return true;
759  } // end func setTemplate
760 
774  $removeEmptyBlocks = true )
775  {
776  $template = '';
777  if (!$this->flagCacheTemplatefile ||
778  $this->lastTemplatefile != $filename
779  ) {
780  $template = $this->getFile($filename);
781  }
782  $this->lastTemplatefile = $filename;
783 
784  return $template != '' ?
785  $this->setTemplate(
787  ) : false;
788  } // end func LoadTemplatefile
789 
801  function setRoot($root)
802  {
803  if ($root != '' && substr($root, -1) != '/') {
804  $root .= '/';
805  }
806 
807  $this->fileRoot = $root;
808  } // end func setRoot
809 
814  {
815  foreach ($this->blocklist as $name => $content) {
816  preg_match_all($this->variablesRegExp, $content, $regs);
817 
818  if (count($regs[1]) != 0) {
819  foreach ($regs[1] as $k => $var) {
820  $this->blockvariables[$name][$var] = true;
821  }
822  } else {
823  $this->blockvariables[$name] = array();
824  }
825  }
826  } // end func buildBlockvariablelist
827 
832  {
833  $regs = array();
834  $values = array();
835 
836  foreach ($this->blockvariables['__global__'] as $allowedvar => $v) {
837  if (isset($this->variableCache[$allowedvar])) {
838  $regs[] = '@' . $this->openingDelimiter .
839  $allowedvar . $this->closingDelimiter . '@';
840  $values[] = $this->variableCache[$allowedvar];
841  unset($this->variableCache[$allowedvar]);
842  }
843  }
844 
845  return array($regs, $values);
846  } // end func getGlobalvariables
847 
854  function findBlocks($string)
855  {
856  $blocklist = array();
857 
858  if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
859  foreach ($regs as $k => $match) {
860  $blockname = $match[1];
861  $blockcontent = $match[2];
862 
863  if (isset($this->blocklist[$blockname])) {
864  $this->err[] = PEAR::raiseError(
865  $this->errorMessage(
866  IT_BLOCK_DUPLICATE, $blockname),
868  );
869  $this->flagBlocktrouble = true;
870  }
871 
872  $this->blocklist[$blockname] = $blockcontent;
873  $this->blockdata[$blockname] = "";
874 
875  $blocklist[] = $blockname;
876 
877  $inner = $this->findBlocks($blockcontent);
878  foreach ($inner as $k => $name) {
879  $pattern = sprintf(
880  '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
881  $name,
882  $name
883  );
884 
885  $this->blocklist[$blockname] = preg_replace(
886  $pattern,
887  $this->openingDelimiter .
888  '__' . $name . '__' .
889  $this->closingDelimiter,
890  $this->blocklist[$blockname]
891  );
892  $this->blockinner[$blockname][] = $name;
893  $this->blockparents[$name] = $blockname;
894  }
895  }
896  }
897 
898  return $blocklist;
899  } // end func findBlocks
900 
906  function getFile($filename)
907  {
908  if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') {
909  $filename = substr($filename, 1);
910  }
911 
912  $filename = $this->fileRoot . $filename;
913 
914  if (!($fh = @fopen($filename, 'r'))) {
915  $this->err[] = PEAR::raiseError(
917  ': "' .$filename .'"',
919  );
920  return "";
921  }
922 
923  $fsize = filesize($filename);
924  if ($fsize < 1) {
925  fclose($fh);
926  return '';
927  }
928 
929  $content = fread($fh, $fsize);
930  fclose($fh);
931 
932  return preg_replace(
933  "#<!-- INCLUDE (.*) -->#ime", "\$this->getFile('\\1')", $content
934  );
935  } // end func getFile
936 
944  function _addPregDelimiters($str)
945  {
946  return '@' . $str . '@';
947  }
948 
956  {
957  return (false === strpos($str, $this->openingDelimiter))?
958  $str:
959  str_replace(
960  $this->openingDelimiter,
961  $this->openingDelimiter .
962  '%preserved%' . $this->closingDelimiter,
963  $str
964  );
965  }
966 
975  function errorMessage($value, $blockname = '')
976  {
977  static $errorMessages;
978  if (!isset($errorMessages)) {
979  $errorMessages = array(
980  IT_OK => '',
981  IT_ERROR => 'unknown error',
982  IT_TPL_NOT_FOUND => 'Cannot read the template file',
983  IT_BLOCK_NOT_FOUND => 'Cannot find this block',
984  IT_BLOCK_DUPLICATE => 'The name of a block must be'.
985  ' uniquewithin a template.'.
986  ' Found "' . $blockname . '" twice.'.
987  'Unpredictable results '.
988  'may appear.',
989  IT_UNKNOWN_OPTION => 'Unknown option'
990  );
991  }
992 
993  if (PEAR::isError($value)) {
994  $value = $value->getCode();
995  }
996 
997  return isset($errorMessages[$value]) ?
998  $errorMessages[$value] : $errorMessages[IT_ERROR];
999  }
1000 } // end class IntegratedTemplate
1001 ?>