phpGraphy PHP Cross Reference Image Galleries

Source: /base/3rd-part/class.template/class.template.php - 475 lines - 15623 bytes - Summary - Text - Print

   1  <?php
   2  /*
   3   * Project:    Flat-Frog, a smarter template engine
   4   * File:    class.compiler.php
   5   * Author:    Paul Lockaby <paul@paullockaby.com>
   6   * Version:    2.2.11
   7   * Copyright:    2003,2004,2005 by Paul Lockaby
   8   * Copyright :    2006 by Serge Gilette
   9   * Credit:    This work is a revised and lighter version of Smarty:
  10   *        the PHP compiling
  11   *        template engine, v2.5.0-CVS. Smarty was originally
  12   *        programmed by Monte Ohrt and Andrei Zmievski and can be
  13   *        found at http://smarty.php.net
  14   *
  15   * This library is free software; you can redistribute it and/or
  16   * modify it under the terms of the GNU Lesser General Public
  17   * License as published by the Free Software Foundation; either
  18   * version 2.1 of the License, or (at your option) any later version.
  19   *
  20   * This library is distributed in the hope that it will be useful,
  21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  23   * Lesser General Public License for more details.
  24   *
  25   * You should have received a copy of the GNU Lesser General Public
  26   * License along with this library; if not, write to the Free Software
  27   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  28   *
  29   * You may contact the maintainer of Flat-Frog by e-mail at:
  30   * sgilette@yahoo.com
  31   *
  32   * The latest version of Flat-Frog can be obtained from:
  33   * http://sourceforge.net
  34   *
  35   */
  36  
  37  
  38  class template {
  39      // public configuration variables
  40      var $left_tag                = "<%";        // the left delimiter for template tags
  41      var $right_tag                = "%>";        // the right delimiter for template tags
  42      var $cache                    = false;    // whether or not to allow caching of files
  43      var $force_compile            = false;    // force a compile regardless of saved state
  44      var $template_dir            = "templates";    // where the templates are to be found
  45      var $plugin_dir                = "plugins";    // where the plugins are to be found
  46      var $compile_dir            = "compiled";    // the directory to store the compiled files in
  47      var $config_dir                = "templates";    // where the config files are
  48      var $cache_dir                = "cached";    // where cache files are stored
  49      var $config_overwrite        = false;
  50      var $config_booleanize        = true;
  51      var $config_fix_new_lines    = true;
  52      var $config_read_hidden        = true;
  53      var $cache_lifetime            = 0;        // how long the file in cache should be considered "fresh"
  54  
  55      // private internal variables
  56      var $_vars            = array();    // stores all internal assigned variables
  57      var $_confs            = array();    // stores all internal config variables
  58      var $_plugins        = array();    // stores all internal plugins
  59      var $_linenum        = 0;        // the current line number in the file we are processing
  60      var $_file            = "";        // the current file we are processing
  61      var $_config_obj    = null;
  62      var $_compile_obj    = null;
  63      var $_cache_id        = null;
  64      var $_cache_dir        = "";        // stores where this specific file is going to be cached
  65      var $_cache_info    = array('config' => array(), 'template' => array());
  66      var $_sl_md5        = '39fc70570b8b60cbc1b85839bf242aff';
  67  
  68  	function assign($key, $value = null) {
  69          if (is_array($key)) {
  70              foreach($key as $var => $val)
  71                  if ($var != "" && !is_numeric($var))
  72                      $this->_vars[$var] = $val;
  73          } else {
  74              if ($key != "" && !is_numeric($key))
  75                  $this->_vars[$key] = $value;
  76          }
  77      }
  78  
  79  	function assign_config($key, $value = null) {
  80          if (is_array($key)) {
  81              foreach($key as $var => $val)
  82                  if ($var != "" && !is_numeric($var))
  83                      $this->_confs[$var] = $val;
  84          } else {
  85              if ($key != "" && !is_numeric($key))
  86                  $this->_confs[$key] = $value;
  87          }
  88      }
  89  
  90  	function clear($key = null) {
  91          if ($key == null) {
  92              $this->_vars = array();
  93          } else {
  94              if (is_array($key)) {
  95                  foreach($key as $index => $value)
  96                      if (in_array($value, $this->_vars))
  97                          unset($this->_vars[$index]);
  98              } else {
  99                  if (in_array($key, $this->_vars))
 100                      unset($this->_vars[$index]);
 101              }
 102          }
 103      }
 104  
 105  	function clear_config($key = null) {
 106          if ($key == null) {
 107              $this->_conf = array();
 108          } else {
 109              if (is_array($key)) {
 110                  foreach($key as $index => $value)
 111                      if (in_array($value, $this->_conf))
 112                          unset($this->_conf[$index]);
 113              } else {
 114                  if (in_array($key, $this->_conf))
 115                      unset($this->_conf[$key]);
 116              }
 117          }
 118      }
 119  
 120      function &get_vars($key = null) {
 121          if ($key == null) {
 122              return $this->_vars;
 123          } else {
 124              if (isset($this->_vars[$key]))
 125                  return $this->_vars[$key];
 126              else
 127                  return null;
 128          }
 129      }
 130  
 131      function &get_config_vars($key = null) {
 132          if ($key == null) {
 133              return $this->_confs;
 134          } else {
 135              if (isset($this->_confs[$key]))
 136                  return $this->_confs[$key];
 137              else
 138                  return null;
 139          }
 140      }
 141      
 142  	function clear_compiled($file = null) {
 143          $this->_destroy_dir($file, null, $this->_get_dir($this->compile_dir));
 144      }
 145  
 146  	function clear_cached($file = null, $cache_id = null) {
 147          if (!$this->cache)
 148              return;
 149          $this->_destroy_dir($file, $cache_id, $this->_get_dir($this->cache_dir));
 150      }
 151  
 152  	function is_cached($file, $cache_id = null) {
 153          if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id)) {
 154              return true;
 155          } else {
 156              return false;
 157          }
 158      }
 159  
 160  	function register_modifier($modifier, $implementation) {
 161          $this->_plugins['modifier'][$modifier] = $implementation;
 162      }
 163  
 164  	function unregister_modifier($modifier) {
 165          if (isset($this->_plugins['modifier'][$modifier]))
 166              unset($this->_plugins['modifier'][$modifier]);
 167      }
 168  
 169  	function register_function($function, $implementation) {
 170          $this->_plugins['function'][$function] = $implementation;
 171      }
 172  
 173  	function unregister_function($function) {
 174          if (isset($this->_plugins['function'][$function]))
 175              unset($this->_plugins['function'][$function]);
 176      }
 177  
 178  	function register_block($function, $implementation) {
 179          $this->_plugins['block'][$function] = $implementation;
 180      }
 181  
 182  	function unregister_block($function) {
 183          if (isset($this->_plugins['block'][$function]))
 184              unset($this->_plugins['block'][$function]);
 185      }
 186  
 187  	function template_exists($file) {
 188          if (file_exists($this->_get_dir($this->template_dir).$file)) {
 189              return true;
 190          } else {
 191              return false;
 192          }
 193      }
 194  
 195  	function display($file, $cache_id = null) {
 196          $this->fetch($file, $cache_id, true);
 197      }
 198  
 199  	function fetch($file, $cache_id = null, $display = false) {
 200          $this->_cache_id = $cache_id;
 201          $this->template_dir = $this->_get_dir($this->template_dir);
 202          $this->compile_dir = $this->_get_dir($this->compile_dir);
 203          if ($this->cache)
 204              $this->_cache_dir = $this->_build_dir($this->cache_dir, $this->_cache_id);
 205          $name = md5($this->template_dir.$file).'.php';
 206  
 207          // don't display any errors
 208          $this->_error_level = error_reporting(error_reporting() & ~E_NOTICE);
 209  
 210          if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id)) {
 211              ob_start();
 212              include($this->_cache_dir.$name);
 213              $output = ob_get_contents();
 214              ob_end_clean();
 215              $output = substr($output, strpos($output, "\n") + 1);
 216          } else {
 217              $output = $this->_fetch_compile($file, $cache_id);
 218              if ($this->cache) {
 219                  $f = fopen($this->_cache_dir.$name, "w");
 220                  fwrite($f, serialize($this->_cache_info) . "\n$output");
 221                  fclose($f);
 222              }
 223          }
 224  
 225          if (strpos($output, $this->_sl_md5) !== false) {
 226              preg_match_all('!' . $this->_sl_md5 . '{_run_insert (.*)}' . $this->_sl_md5 . '!U',$output,$_match);
 227              foreach($_match[1] as $value) {
 228                  $arguments = unserialize($value);
 229                  $output = str_replace($this->_sl_md5 . '{_run_insert ' . $value . '}' . $this->_sl_md5, call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)), $output);
 230              }
 231          }
 232  
 233          // return error reporting to normal
 234          error_reporting($this->_error_level);
 235  
 236          if ($display) {
 237              echo $output;
 238          } else {
 239              return $output;
 240          }
 241      }
 242  
 243  	function config_load($file, $section_name = null, $var_name = null) {
 244          $this->template_dir = $this->_get_dir($this->template_dir);
 245          $this->config_dir = $this->_get_dir($this->config_dir);
 246          $this->compile_dir = $this->_get_dir($this->compile_dir);
 247          $name = md5($this->template_dir.$file.$section_name.$var_name).'.php';
 248  
 249          if ($this->cache) {
 250              array_push($this->_cache_info['config'], $file);
 251          }
 252  
 253          if (!$this->force_compile && file_exists($this->compile_dir.'c_'.$name) && (filemtime($this->compile_dir.'c_'.$name) > filemtime($this->config_dir.$file))) {
 254              include($this->compile_dir.'c_'.$name);
 255              return true;
 256          }
 257  
 258          if (!is_object($this->_config_obj)) {
 259              require_once ("class.config.php");
 260              $this->_config_obj = new config;
 261              $this->_config_obj->overwrite = $this->config_overwrite;
 262              $this->_config_obj->booleanize = $this->config_booleanize;
 263              $this->_config_obj->fix_new_lines = $this->config_fix_new_lines;
 264              $this->_config_obj->read_hidden = $this->config_read_hidden;
 265          }
 266          if (!($_result = $this->_config_obj->config_load($this->config_dir.$file, $section_name, $var_name))) {
 267              return false;
 268          }
 269  
 270          if (!empty($var_name) || !empty($section_name)) {
 271              $output = "\$this->_confs = " . var_export($_result, true) . ";";
 272          } else {
 273              // must shift of the bottom level of the array to get rid of the section labels
 274              $_temp = array();
 275              foreach($_result as $value)
 276                  $_temp = array_merge($_temp, $value);
 277              $output = "\$this->_confs = " . var_export($_temp, true) . ";";
 278          }
 279  
 280          $f = fopen($this->compile_dir.'c_'.$name, "w");
 281          fwrite($f, '<?php ' . $output . ' ?>');
 282          fclose($f);
 283          eval($output);
 284          return true;
 285      }
 286  
 287  	function _is_cached($file, $cache_id) {
 288          $this->_cache_dir = $this->_get_dir($this->cache_dir, $cache_id);
 289          $this->config_dir = $this->_get_dir($this->config_dir);
 290          $this->template_dir = $this->_get_dir($this->template_dir);
 291          $name = md5($this->template_dir.$file).'.php';
 292  
 293          if (file_exists($this->_cache_dir.$name) && (((time() - filemtime($this->_cache_dir.$name)) < $this->cache_lifetime) || $this->cache_lifetime == -1) && (filemtime($this->_cache_dir.$name) > filemtime($this->template_dir.$file))) {
 294              $fh = fopen($this->_cache_dir.$name, "r");
 295              if (!feof($fh) && ($line = fgets($fh, filesize($this->_cache_dir.$name)))) {
 296                  $includes = unserialize($line);
 297                  if (isset($includes['template']))
 298                      foreach($includes['template'] as $value)
 299                          if (!(file_exists($this->template_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->template_dir.$value))))
 300                              return false;
 301                  if (isset($includes['config']))
 302                      foreach($includes['config'] as $value)
 303                          if (!(file_exists($this->config_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->config_dir.$value))))
 304                              return false;
 305              }
 306              fclose($fh);
 307          } else {
 308              return false;
 309          }
 310          return true;
 311      }
 312  
 313  	function _fetch_compile($file) {
 314          $this->template_dir = $this->_get_dir($this->template_dir);
 315          $name = md5($this->template_dir.$file).'.php';
 316  
 317          if ($this->cache) {
 318              array_push($this->_cache_info['template'], $file);
 319          }
 320  
 321          if (!$this->force_compile && file_exists($this->compile_dir.'c_'.$name) && (filemtime($this->compile_dir.'c_'.$name) > filemtime($this->template_dir.$file))) {
 322              ob_start();
 323              include($this->compile_dir.'c_'.$name);
 324              $output = ob_get_contents();
 325              ob_end_clean();
 326              error_reporting($this->_error_level);
 327              return $output;
 328          }
 329  
 330          if ($this->template_exists($file)) {
 331              $f = fopen($this->template_dir.$file, "r");
 332              $size = filesize($this->template_dir.$file);
 333              if ($size > 0) {
 334                  $file_contents = fread($f, filesize($this->template_dir.$file));
 335              } else {
 336                  $file_contents = "";
 337              }
 338              $this->_file = $file;
 339              fclose($f);
 340          } else {
 341              $this->trigger_error("file '$file' does not exist", E_USER_ERROR);
 342          }
 343  
 344          if (!is_object($this->_compile_obj)) {
 345              require_once ("class.compiler.php");
 346              $this->_compile_obj = new compiler;
 347          }
 348          $this->_compile_obj->left_tag = $this->left_tag;
 349          $this->_compile_obj->right_tag = $this->right_tag;
 350          $this->_compile_obj->plugin_dir = &$this->plugin_dir;
 351          $this->_compile_obj->template_dir = &$this->template_dir;
 352          $this->_compile_obj->_vars = &$this->_vars;
 353          $this->_compile_obj->_confs = &$this->_confs;
 354          $this->_compile_obj->_plugins = &$this->_plugins;
 355          $this->_compile_obj->_linenum = &$this->_linenum;
 356          $this->_compile_obj->_file = &$this->_file;
 357          $output = $this->_compile_obj->_compile_file($file_contents);
 358  
 359          $f = fopen($this->compile_dir.'c_'.$name, "w");
 360          fwrite($f, $output);
 361          fclose($f);
 362  
 363          ob_start();
 364          eval(' ?>' . $output . '<?php ');
 365          $output = ob_get_contents();
 366          ob_end_clean();
 367          return $output;
 368      }
 369  
 370  	function _run_modifier() {
 371          $arguments = func_get_args();
 372          list($variable, $modifier, $_map_array) = array_splice($arguments, 0, 3);
 373          array_unshift($arguments, $variable);
 374          if ($_map_array && is_array($variable))
 375              foreach($variable as $key => $value)
 376                  $variable[$key] = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
 377          else
 378              $variable = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
 379          return $variable;
 380      }
 381  
 382  	function _run_insert($arguments) {
 383          if ($this->cache) {
 384              return $this->_sl_md5 . '{_run_insert ' . serialize((array)$arguments) . '}' . $this->_sl_md5;
 385          } else {
 386              if (!function_exists('insert_' . $arguments['name']))
 387                  $this->trigger_error("function 'insert_" . $arguments['name'] . "' does not exist in 'insert'", E_USER_ERROR);
 388              if (isset($arguments['assign']))
 389                  $this->assign($arguments['assign'], call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)));
 390              else
 391                  return call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this));
 392          }
 393      }
 394  
 395  	function _get_dir($dir, $id = null) {
 396          if (empty($dir))
 397              $dir = '.';
 398          if (substr($dir, -1) != DIRECTORY_SEPARATOR)
 399              $dir .= DIRECTORY_SEPARATOR;
 400          if (!empty($id)) {
 401              $_args = explode('|', $id);
 402              if (count($_args) == 1 && empty($_args[0]))
 403                  return $dir;
 404              foreach($_args as $value)
 405                  $dir .= $value.DIRECTORY_SEPARATOR;
 406          }
 407          return $dir;
 408      }
 409  
 410  	function _build_dir($dir, $id) {
 411          $_args = explode('|', $id);
 412          if (count($_args) == 1 && empty($_args[0]))
 413              return $this->_get_dir($dir);
 414          $_result = $this->_get_dir($dir);
 415          foreach($_args as $value) {
 416              $_result .= $value.DIRECTORY_SEPARATOR;
 417              if (!is_dir($_result)) @mkdir($_result, 0777);
 418          }
 419          return $_result;
 420      }
 421  
 422  	function _destroy_dir($file, $id, $dir) {
 423          if ($file == null && $id == null) {
 424              if (is_dir($dir))
 425                  if($d = opendir($dir))
 426                      while(($f = readdir($d)) !== false)
 427                          if ($f != '.' && $f != '..')
 428                              $this->_rm_dir($dir.$f.DIRECTORY_SEPARATOR);
 429          } else {
 430              if ($id == null) {
 431                  $this->template_dir = $this->_get_dir($this->template_dir);
 432                  @unlink($dir.md5($this->template_dir.$file).'.php');
 433              } else {
 434                  $_args = "";
 435                  foreach(explode('|', $id) as $value)
 436                      $_args .= $value.DIRECTORY_SEPARATOR;
 437                  $this->_rm_dir($dir.DIRECTORY_SEPARATOR.$_args);
 438              }
 439          }
 440      }
 441  
 442  	function _rm_dir($dir) {
 443          if (is_file(substr($dir, 0, -1))) {
 444              @unlink(substr($dir, 0, -1));
 445              return;
 446          }
 447          if ($d = opendir($dir)) {
 448              while(($f = readdir($d)) !== false) {
 449                  if ($f != '.' && $f != '..') {
 450                      $this->_rm_dir($dir.$f.DIRECTORY_SEPARATOR);
 451                  }
 452              }
 453              @rmdir($dir.$f);
 454          }
 455      }
 456  
 457  	function _escape_chars($string) {
 458          // taken directly from Smarty at http://smarty.php.net/
 459          if(!is_array($string)) {
 460              $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
 461              $string = htmlspecialchars($string);
 462              $string = str_replace(array('%%%SMARTY_START%%%','%%%SMARTY_END%%%'), array('&',';'), $string);
 463          }
 464          return $string;
 465      }
 466  
 467  	function trigger_error($error_msg, $error_type = E_USER_ERROR, $file = null, $line = null) {
 468          if(isset($file) && isset($line))
 469              $info = ' ('.basename($file).", line $line)";
 470          else
 471              $info = null;
 472          trigger_error('TPL: [in ' . $this->_file . ' line ' . $this->_linenum . "]: syntax error: $error_msg$info", $error_type);
 473      }
 474  }
 475  ?>

title

Description

title

Description

title

Description

title

title

Body