"Fossies" - the Fresh Open Source Software Archive

Member "mythreads/lib/phplib/template.inc" (30 May 2015, 9216 Bytes) of package /linux/privat/mythreads-links_1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) fasm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "template.inc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.0_vs_1.2.1.

    1 <?php
    2 /*
    3  * Session Management for PHP3
    4  *
    5  * (C) Copyright 1999-2000 NetUSE GmbH
    6  *                    Kristian Koehntopp
    7  *
    8  * $Id: template.inc,v 1.2 2015-05-30 11:48:11 ldrolez Exp $
    9  *
   10  */ 
   11 
   12 class Template {
   13   var $classname = "Template";
   14 
   15   /* if set, echo assignments */
   16   var $debug     = false;
   17 
   18   /* $file[handle] = "filename"; */
   19   var $file  = array();
   20 
   21   /* relative filenames are relative to this pathname */
   22   var $root   = "";
   23 
   24   /* $varkeys[key] = "key"; $varvals[key] = "value"; */
   25   var $varkeys = array();
   26   var $varvals = array();
   27 
   28   /* "remove"  => remove undefined variables
   29    * "comment" => replace undefined variables with comments
   30    * "keep"    => keep undefined variables
   31    */
   32   var $unknowns = "remove";
   33   
   34   /* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly */
   35   var $halt_on_error  = "yes";
   36   
   37   /* last error message is retained here */
   38   var $last_error     = "";
   39 
   40 
   41   /***************************************************************************/
   42   /* public: Constructor.
   43    * root:     template directory.
   44    * unknowns: how to handle unknown variables.
   45    */
   46   function Template($root = ".", $unknowns = "remove") {
   47     $this->set_root($root);
   48     $this->set_unknowns($unknowns);
   49   }
   50 
   51   /* public: setroot(pathname $root)
   52    * root:   new template directory.
   53    */  
   54   function set_root($root) {
   55     if (!is_dir($root)) {
   56       $this->halt("set_root: $root is not a directory.");
   57       return false;
   58     }
   59     
   60     $this->root = $root;
   61     return true;
   62   }
   63 
   64   /* public: set_unknowns(enum $unknowns)
   65    * unknowns: "remove", "comment", "keep"
   66    *
   67    */
   68   function set_unknowns($unknowns = "keep") {
   69     $this->unknowns = $unknowns;
   70   }
   71 
   72   /* public: set_file(array $filelist)
   73    * filelist: array of handle, filename pairs.
   74    *
   75    * public: set_file(string $handle, string $filename)
   76    * handle: handle for a filename,
   77    * filename: name of template file
   78    */
   79   function set_file($handle, $filename = "") {
   80     if (!is_array($handle)) {
   81       if ($filename == "") {
   82         $this->halt("set_file: For handle $handle filename is empty.");
   83         return false;
   84       }
   85       $this->file[$handle] = $this->filename($filename);
   86     } else {
   87       reset($handle);
   88       while(list($h, $f) = each($handle)) {
   89         $this->file[$h] = $this->filename($f);
   90       }
   91     }
   92   }
   93 
   94   /* public: set_block(string $parent, string $handle, string $name = "")
   95    * extract the template $handle from $parent, 
   96    * place variable {$name} instead.
   97    */
   98   function set_block($parent, $handle, $name = "") {
   99     if (!$this->loadfile($parent)) {
  100       $this->halt("subst: unable to load $parent.");
  101       return false;
  102     }
  103     if ($name == "")
  104       $name = $handle;
  105 
  106     $str = $this->get_var($parent);
  107     $reg = "/<!--\s+BEGIN $handle\s+-->(.*)\n\s*<!--\s+END $handle\s+-->/sm";
  108     preg_match_all($reg, $str, $m);
  109     $str = preg_replace($reg, "{" . "$name}", $str);
  110     $this->set_var($handle, $m[1][0]);
  111     $this->set_var($parent, $str);
  112   }
  113   
  114   /* public: set_var(array $values)
  115    * values: array of variable name, value pairs.
  116    *
  117    * public: set_var(string $varname, string $value)
  118    * varname: name of a variable that is to be defined
  119    * value:   value of that variable
  120    */
  121   function set_var($varname, $value = "") {
  122     if (!is_array($varname)) {
  123       if (!empty($varname))
  124         if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n";
  125         $this->varkeys[$varname] = "/".$this->varname($varname)."/";
  126         $this->varvals[$varname] = $value;
  127     } else {
  128       reset($varname);
  129       while(list($k, $v) = each($varname)) {
  130         if (!empty($k))
  131           if ($this->debug) print "array: set *$k* to *$v*<br>\n";
  132           $this->varkeys[$k] = "/".$this->varname($k)."/";
  133           $this->varvals[$k] = $v;
  134       }
  135     }
  136   }
  137 
  138   /* public: subst(string $handle)
  139    * handle: handle of template where variables are to be substituted.
  140    */
  141   function subst($handle) {
  142     if (!$this->loadfile($handle)) {
  143       $this->halt("subst: unable to load $handle.");
  144       return false;
  145     }
  146 
  147     $str = $this->get_var($handle);
  148     $str = @preg_replace($this->varkeys, $this->varvals, $str);
  149     return $str;
  150   }
  151   
  152   /* public: psubst(string $handle)
  153    * handle: handle of template where variables are to be substituted.
  154    */
  155   function psubst($handle) {
  156     print $this->subst($handle);
  157     
  158     return false;
  159   }
  160 
  161   /* public: parse(string $target, string $handle, boolean append)
  162    * public: parse(string $target, array  $handle, boolean append)
  163    * target: handle of variable to generate
  164    * handle: handle of template to substitute
  165    * append: append to target handle
  166    */
  167   function parse($target, $handle, $append = false) {
  168     if (!is_array($handle)) {
  169       $str = $this->subst($handle);
  170       if ($append) {
  171         $this->set_var($target, $this->get_var($target) . $str);
  172       } else {
  173         $this->set_var($target, $str);
  174       }
  175     } else {
  176       reset($handle);
  177       while(list($i, $h) = each($handle)) {
  178         $str = $this->subst($h);
  179         $this->set_var($target, $str);
  180       }
  181     }
  182     
  183     return $str;
  184   }
  185   
  186   function pparse($target, $handle, $append = false) {
  187     print $this->parse($target, $handle, $append);
  188     return false;
  189   }
  190   
  191   /* public: get_vars()
  192    */
  193   function get_vars() {
  194     reset($this->varkeys);
  195     while(list($k, $v) = each($this->varkeys)) {
  196       $result[$k] = $this->varvals[$k];
  197     }
  198     
  199     return $result;
  200   }
  201   
  202   /* public: get_var(string varname)
  203    * varname: name of variable.
  204    *
  205    * public: get_var(array varname)
  206    * varname: array of variable names
  207    */
  208   function get_var($varname) {
  209     if (!is_array($varname)) {
  210       return $this->varvals[$varname];
  211     } else {
  212       reset($varname);
  213       while(list($k, $v) = each($varname)) {
  214         $result[$k] = $this->varvals[$k];
  215       }
  216       
  217       return $result;
  218     }
  219   }
  220   
  221   /* public: get_undefined($handle)
  222    * handle: handle of a template.
  223    */
  224   function get_undefined($handle) {
  225     if (!$this->loadfile($handle)) {
  226       $this->halt("get_undefined: unable to load $handle.");
  227       return false;
  228     }
  229     
  230     preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
  231     $m = $m[1];
  232     if (!is_array($m))
  233       return false;
  234 
  235     reset($m);
  236     while(list($k, $v) = each($m)) {
  237       if (!isset($this->varkeys[$v]))
  238         $result[$v] = $v;
  239     }
  240     
  241     if (count($result))
  242       return $result;
  243     else
  244       return false;
  245   }
  246 
  247   /* public: finish(string $str)
  248    * str: string to finish.
  249    */
  250   function finish($str) {
  251     switch ($this->unknowns) {
  252       case "keep":
  253       break;
  254       
  255       case "remove":
  256         $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);
  257       break;
  258 
  259       case "comment":
  260         $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
  261       break;
  262     }
  263     
  264     return $str;
  265   }
  266 
  267   /* public: p(string $varname)
  268    * varname: name of variable to print.
  269    */
  270   function p($varname) {
  271     print $this->finish($this->get_var($varname));
  272   }
  273 
  274   function get($varname) {
  275     return $this->finish($this->get_var($varname));
  276   }
  277     
  278   /***************************************************************************/
  279   /* private: filename($filename)
  280    * filename: name to be completed.
  281    */
  282   function filename($filename) {
  283     if (substr($filename, 0, 1) != "/") {
  284       $filename = $this->root."/".$filename;
  285     }
  286 
  287     if (!file_exists($filename)) {
  288       # try a directory in the parent template
  289       # to allow for easy dynamic sub templates
  290       $newfile = preg_replace("#(.*)/(\w+)/\w+/#", "$1/$2/", $filename);
  291       if (file_exists($newfile)) {
  292     return $newfile;
  293       } else {
  294         $this->halt("filename: files $filename or $newfile do not exist. ");
  295       }
  296     }
  297     return $filename;
  298   }
  299 
  300   /* private: varname($varname)
  301    * varname: name of a replacement variable to be protected.
  302    */
  303   function varname($varname) {
  304     return preg_quote("{".$varname."}");
  305   }
  306 
  307   /* private: loadfile(string $handle)
  308    * handle:  load file defined by handle, if it is not loaded yet.
  309    */
  310   function loadfile($handle) {
  311     if (isset($this->varkeys[$handle]) and !empty($this->varvals[$handle]))
  312       return true;
  313 
  314     if (!isset($this->file[$handle])) {
  315       $this->halt("loadfile: $handle is not a valid handle.");
  316       return false;
  317     }
  318     $filename = $this->file[$handle];
  319 
  320     $str = implode("", @file($filename));
  321     if (empty($str)) {
  322       $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
  323       return false;
  324     }
  325 
  326     $this->set_var($handle, $str);
  327     
  328     return true;
  329   }
  330 
  331   /***************************************************************************/
  332   /* public: halt(string $msg)
  333    * msg:    error message to show.
  334    */
  335   function halt($msg) {
  336     $this->last_error = $msg;
  337     
  338     if ($this->halt_on_error != "no")
  339       $this->haltmsg($msg);
  340     
  341     if ($this->halt_on_error == "yes")
  342       die("<b>Halted.</b>");
  343     
  344     return false;
  345   }
  346   
  347   /* public, override: haltmsg($msg)
  348    * msg: error message to show.
  349    */
  350   function haltmsg($msg) {
  351     printf("<b>Template Error:</b> %s<br>\n", $msg);
  352   }
  353 }
  354 ?>