eGroupWare PHP Cross Reference Groupware Applications

Source: /phpgwapi/inc/class.Template.inc.php - 493 lines - 12583 bytes - Summary - Text - Print

   1  <?php
   2    /**************************************************************************\
   3    * eGroupWare API - Template class                                          *
   4    * (C) Copyright 1999-2000 NetUSE GmbH Kristian Koehntopp                   *
   5    * ------------------------------------------------------------------------ *
   6    * This is not part of eGroupWare, but is used by eGroupWare.               *
   7    * http://www.egroupware.org/                                               *
   8    * ------------------------------------------------------------------------ *
   9    * This program is free software; you can redistribute it and/or modify it  *
  10    * under the terms of the GNU Lesser General Public License as published    *
  11    * by the Free Software Foundation; either version 2.1 of the License, or   *
  12    * any later version.                                                       *
  13    \**************************************************************************/
  14  
  15    /* $Id$ */
  16  
  17      class Template
  18      {
  19          var $classname = 'Template';
  20  
  21          /**
  22           * @var $rb_debug mixed False for no debug, string or array of strings with:
  23           *    - function-name like set_var to get eg. all assignments or
  24           *    - handle- / variable-names - if you are only interested in some variables ;-)
  25           */
  26          var $debug = False;    // array('cat_list','cat_list_t');
  27  
  28          /* $file[handle] = 'filename'; */
  29          var $file = array();
  30  
  31          /* relative filenames are relative to this pathname */
  32          var $root = '';
  33  
  34          /* $varkeys[key] = 'key'; $varvals[key] = 'value'; */
  35          var $varkeys = array();
  36          var $varvals = array();
  37  
  38          /* 'remove'  => remove undefined variables
  39           * 'comment' => replace undefined variables with comments
  40           * 'keep'    => keep undefined variables
  41           */
  42          var $unknowns = 'remove';
  43  
  44          /* 'yes' => halt, 'report' => report error, continue, 'no' => ignore error quietly */
  45          var $halt_on_error = 'yes';
  46  
  47          /* last error message is retained here */
  48          var $last_error = '';
  49  
  50          // if true change all phpGroupWare into eGroupWare in set_var
  51          var $egroupware_hack = False;
  52  
  53          /***************************************************************************/
  54          /* public: Constructor.
  55           * root:     template directory.
  56           * unknowns: how to handle unknown variables.
  57           */
  58  		function Template($root = '.', $unknowns = 'remove')
  59          {
  60              $this->set_root($root);
  61              $this->set_unknowns($unknowns);
  62          }
  63  
  64          /* public: setroot(pathname $root)
  65           * root:   new template directory.
  66           */
  67  		function set_root($root)
  68          {
  69              if ($this->debug && $this->check_debug('set_root'))
  70              {
  71                  echo "<p>Template::set_root('$root')</p>\n";
  72              }
  73              if (!is_dir($root))
  74              {
  75                  $this->halt("set_root: $root is not a directory.");
  76                  return false;
  77              }
  78              $this->root = $root;
  79              return true;
  80          }
  81  
  82          /* public: set_unknowns(enum $unknowns)
  83           * unknowns: 'remove', 'comment', 'keep'
  84           *
  85           */
  86  		function set_unknowns($unknowns = 'keep')
  87          {
  88              if ($this->debug && $this->check_debug('set_unknows'))
  89              {
  90                  echo "<p>Template::set_unknows('$unknowns')</p>\n";
  91              }
  92              $this->unknowns = $unknowns;
  93          }
  94  
  95          /* public: set_file(array $filelist)
  96           * filelist: array of handle, filename pairs.
  97           *
  98           * public: set_file(string $handle, string $filename)
  99           * handle: handle for a filename,
 100           * filename: name of template file
 101           */
 102  		function set_file($handle, $filename = '')
 103          {
 104              if ($this->debug && $this->check_debug('set_file',$handle,$filename))
 105              {
 106                  echo "<p>Template::set_file('".print_r($handle,true)."','$filename')</p>\n";
 107              }
 108              if (!is_array($handle))
 109              {
 110                  if ($filename == '')
 111                  {
 112                      $this->halt("set_file: For handle $handle filename is empty.");
 113                      return false;
 114                  }
 115                  $this->file[$handle] = $this->filename($filename);
 116              }
 117              else
 118              {
 119                  foreach($handle as $h => $f)
 120                  {
 121                      $this->file[$h] = $this->filename($f);
 122                  }
 123              }
 124          }
 125  
 126          /* public: set_block(string $parent, string $handle, string $name = '')
 127           * extract the template $handle from $parent,
 128           * place variable {$name} instead.
 129           */
 130  		function set_block($parent, $handle, $name = '')
 131          {
 132              if ($this->debug && $this->check_debug('set_block',$parent,$handle,$name))
 133              {
 134                  echo "<p>Template::set_block('$parent','$handle','$name')</p>\n";
 135              }
 136              if (!$this->loadfile($parent))
 137              {
 138                  $this->halt("set_block: unable to load '$parent'.");
 139                  return false;
 140              }
 141              if ($name == '')
 142              {
 143                  $name = $handle;
 144              }
 145              $str = $this->get_var($parent);
 146              $qhandle = preg_quote($handle);
 147              $reg = "/<!--\\s+BEGIN $qhandle\\s+-->(.*)\n\\s*<!--\\s+END $qhandle\\s+-->/s";
 148              if (!preg_match($reg,$str,$match))
 149              {
 150                  // unfortunaly some apps set non-existing blocks, therefor I have to disable this diagnostics again for now
 151                  $this->halt("set_block: unable to find block '$handle' in '$parent'=<pre>".htmlspecialchars($str)."</pre> this->root=$this->root");
 152                  // return False;
 153              }
 154              $this->set_var($handle,$match[1]);
 155              $this->set_var($parent,preg_replace($reg, '{' . "$name}",$str));
 156          }
 157  
 158          /* public: set_var(array $values)
 159           * values: array of variable name, value pairs.
 160           *
 161           * public: set_var(string $varname, string $value)
 162           * varname: name of a variable that is to be defined
 163           * value:   value of that variable
 164           */
 165  		function set_var($varname, $value = '')
 166          {
 167              if (!is_array($varname))
 168              {
 169                  if (empty($varname))
 170                  {
 171                      return;
 172                  }
 173                  $varname = array(
 174                      $varname => $value
 175                  );
 176              }
 177              foreach($varname as $k => $v)
 178              {
 179                  if (!empty($k))
 180                  {
 181                      if ($this->debug && $this->check_debug('set_var',$k))
 182                      {
 183                          echo "<p>Template::set_var('$k','$v')</p>\n";
 184                      }
 185                      $this->varkeys[$k] = $this->varname($k);
 186                      $this->varvals[$k] = $this->egroupware_hack ? str_replace(
 187                          array('phpGroupWare','www.phpgroupware.org'),
 188                          array('eGroupWare','www.eGroupWare.org'),$v
 189                      ) : $v;
 190                  }
 191              }
 192          }
 193  
 194          /* public: subst(string $handle)
 195           * handle: handle of template where variables are to be substituted.
 196           */
 197  		function subst($handle)
 198          {
 199              if ($this->debug && $this->check_debug('subst',$handle))
 200              {
 201                  echo "<p>Template::subst('$handle')</p>\n";
 202              }
 203              if (!$this->loadfile($handle))
 204              {
 205                  $this->halt("subst: unable to load $handle.");
 206                  return false;
 207              }
 208  
 209              $str = $this->get_var($handle);
 210              foreach($this->varkeys as $k => $v)
 211              {
 212                  $str = str_replace($v, $this->varvals[$k], $str);
 213              }
 214              return $str;
 215          }
 216  
 217          /* public: psubst(string $handle)
 218           * handle: handle of template where variables are to be substituted.
 219           */
 220  		function psubst($handle)
 221          {
 222              print $this->subst($handle);
 223              return false;
 224          }
 225  
 226          /* public: parse(string $target, string $handle, boolean append)
 227           * target: handle of variable to generate
 228           * handle: handle of template to substitute
 229           * append: append to target handle
 230           */
 231  		function parse($target, $handle, $append = false)
 232          {
 233              if (!is_array($handle))
 234              {
 235                  $str = $this->subst($handle);
 236                  if ($append)
 237                  {
 238                      $this->set_var($target, $this->get_var($target) . $str);
 239                  }
 240                  else
 241                  {
 242                      $this->set_var($target, $str);
 243                  }
 244              }
 245              else
 246              {
 247                  foreach($handle as $i => $h)
 248                  {
 249                      $str = $this->subst($h);
 250                      $this->set_var($target, $str);
 251                  }
 252              }
 253              return $str;
 254          }
 255  
 256  		function pparse($target, $handle, $append = false)
 257          {
 258              print $this->parse($target, $handle, $append);
 259              return false;
 260          }
 261  
 262          /* This is short for finish parse */
 263          function fp($target, $handle, $append = False)
 264          {
 265              return $this->finish($this->parse($target, $handle, $append));
 266          }
 267  
 268          /* This is a short cut for print finish parse */
 269  		function pfp($target, $handle, $append = False)
 270          {
 271              echo $this->finish($this->parse($target, $handle, $append));
 272          }
 273  
 274          /* public: get_vars()
 275           */
 276  		function get_vars()
 277          {
 278              foreach($this->varkeys as $k => $v)
 279              {
 280                  $result[$k] = $this->varvals[$k];
 281              }
 282              return $result;
 283          }
 284  
 285          /* public: get_var(string varname)
 286           * varname: name of variable.
 287           *
 288           * public: get_var(array varname)
 289           * varname: array of variable names
 290           */
 291  		function get_var($varname)
 292          {
 293              if (!is_array($varname))
 294              {
 295                  return $this->varvals[$varname];
 296              }
 297              else
 298              {
 299                  foreach($varname as $k => $v)
 300                  {
 301                      $result[$k] = $this->varvals[$k];
 302                  }
 303                  return $result;
 304              }
 305          }
 306  
 307          /* public: get_undefined($handle)
 308           * handle: handle of a template.
 309           */
 310  		function get_undefined($handle)
 311          {
 312              if (!$this->loadfile($handle))
 313              {
 314                  $this->halt("get_undefined: unable to load $handle.");
 315                  return false;
 316              }
 317  
 318              preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
 319              $m = $m[1];
 320              if (!is_array($m))
 321              {
 322                  return false;
 323              }
 324              foreach($m as $k => $v)
 325              {
 326                  if (!isset($this->varkeys[$v]))
 327                  {
 328                      $result[$v] = $v;
 329                  }
 330              }
 331  
 332              if (count($result))
 333              {
 334                  return $result;
 335              }
 336              else
 337              {
 338                  return false;
 339              }
 340          }
 341  
 342          /* public: finish(string $str)
 343           * str: string to finish.
 344           */
 345  		function finish($str)
 346          {
 347              switch ($this->unknowns)
 348              {
 349                  case 'keep':
 350                      break;
 351                  case 'remove':
 352                      $str = preg_replace('/{[a-z0-9_-]+}/i', '', $str);
 353                      break;
 354                  case 'comment':
 355                      $str = preg_replace('/{([a-z0-9_-]+)}/i', "<!-- Template $handle: Variable \\1 undefined -->", $str);
 356                      break;
 357              }
 358  
 359              return $str;
 360          }
 361  
 362          /* public: p(string $varname)
 363           * varname: name of variable to print.
 364           */
 365          function p($varname)
 366          {
 367              print $this->finish($this->get_var($varname));
 368          }
 369  
 370  		function get($varname)
 371          {
 372              return $this->finish($this->get_var($varname));
 373          }
 374  
 375          /***************************************************************************/
 376          /* private: filename($filename)
 377           * filename: name to be completed.
 378           */
 379  		function filename($filename,$root='',$time=1)
 380          {
 381              if($root == '')
 382              {
 383                  $root = $this->root;
 384              }
 385              if(substr($filename, 0, 1) != '/')
 386              {
 387                  $new_filename = $root . '/' . $filename;
 388              }
 389              else
 390              {
 391                  $new_filename = $filename;
 392              }
 393  
 394              if (!file_exists($new_filename))
 395              {
 396                  if($time==2)
 397                  {
 398                      $this->halt("filename: file $new_filename does not exist.");
 399                  }
 400                  else
 401                  {
 402  //                    $new_root = str_replace($GLOBALS['phpgw_info']['server']['template_set'],'default',$root);
 403  //                    $new_root = substr($root, 0, strrpos($root, $GLOBALS['phpgw_info']['server']['template_set'])).'default';
 404  //                    $new_root = substr($root, 0, strlen($root) - strlen($GLOBALS['phpgw_info']['server']['template_set'])) . 'default';
 405                      $new_root = dirname($root) . DIRECTORY_SEPARATOR . 'default';
 406                      $new_filename = $this->filename(str_replace($root.'/','',$new_filename),$new_root,2);
 407                  }
 408              }
 409              return $new_filename;
 410          }
 411  
 412          /* private: varname($varname)
 413           * varname: name of a replacement variable to be protected.
 414           */
 415  		function varname($varname)
 416          {
 417              return '{'.$varname.'}';
 418          }
 419  
 420          /* private: loadfile(string $handle)
 421           * handle:  load file defined by handle, if it is not loaded yet.
 422           */
 423  		function loadfile($handle)
 424          {
 425              if ($this->debug && $this->check_debug('loadfile',$handle))
 426              {
 427                  echo "<p>Template::loadfile('$handle') file=<pre>\n".print_r($this->file,True)."</pre>\n";
 428                  echo "<p>backtrace: ".function_backtrace()."</p>\n";
 429              }
 430              if (isset($this->varkeys[$handle]) && !empty($this->varvals[$handle]))
 431              {
 432                  return true;
 433              }
 434              if (!isset($this->file[$handle]))
 435              {
 436                  if ($this->debug && $this->check_debug('loadfile',$handle))
 437                  {
 438                      echo "varkeys =<pre>".print_r($this->varkeys,True)."</pre>varvals =<pre>".print_r($this->varvals,True)."</pre>\n";
 439                  }
 440                  $this->halt("loadfile: $handle is not a valid handle.");
 441                  return false;
 442              }
 443              $filename = $this->file[$handle];
 444  
 445              $str = file_get_contents($filename);
 446              if (empty($str))
 447              {
 448                  $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
 449                  return false;
 450              }
 451  
 452              $this->set_var($handle, $str);
 453              return true;
 454          }
 455  
 456          /**
 457           * Giving a error message and halt the execution (if $this->halt_on_error == 'yes')
 458           *
 459           * @param string $msg error message to show
 460           */
 461  		function halt($msg)
 462          {
 463              $this->last_error = $msg;
 464  
 465              switch ($this->halt_on_error)
 466              {
 467                  case 'no':
 468                      // ignore error quitely
 469                      break;
 470                  case 'report':
 471                      printf("<b>Template Error:</b> %s<br>\n", $msg);
 472                      echo "<b>Backtrace</b>: ".function_backtrace(2)."<br>\n";
 473                      break;
 474                  case 'yes':
 475                      throw new egw_exception_wrong_parameter('Template Error: '.$msg);
 476              }
 477          }
 478  
 479  		function check_debug($str)
 480          {
 481              if (!$this->debug) return False;
 482  
 483              foreach(func_get_args() as $arg)
 484              {
 485                  if (!is_array($this->debug) && $this->debug === $arg ||
 486                      (is_array($this->debug) && (@$this->debug[$arg] || in_array($arg,$this->debug,True))))
 487                  {
 488                      return True;
 489                  }
 490              }
 491              return False;
 492          }
 493      }

title

Description

title

Description

title

Description

title

title

Body