File: template.inc

package info (click to toggle)
phplib 1%3A7.3dev-3.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,752 kB
  • ctags: 247
  • sloc: php: 6,659; perl: 323; pascal: 157; makefile: 102; sh: 7
file content (423 lines) | stat: -rw-r--r-- 11,670 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/*
 * Session Management for PHP3
 *
 * (C) Copyright 1999 NetUSE GmbH
 *                    Kristian Koehntopp
 *
 * $Id: template.inc,v 1.9 1999/11/01 05:52:11 kk Exp $
 *
 */ 

class Template {
  var $classname = "Template";

  /* if set, echo assignments */
  /* 1 = debug set, 2 = debug get, 4 = debug internals */
  var $debug     = false;

  /* $file[varname] = "filename"; */
  var $file  = array();
  
  /* $block[varname]["parent"] = "parent";
   * $block[varname]["alias"]  = "alias";
   */
  var $block = array();

  /* Template searches for files in one of these directories */
  var $root   = array( "." );

  /* $varkeys[key] = "key"; $varvals[key] = "value"; */
  var $varkeys = array();
  var $varvals = array();

  /* "remove"  => remove undefined variables
   * "comment" => replace undefined variables with comments
   * "keep"    => keep undefined variables
  var $unknowns = "remove";
  
  /* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly */
  var $halt_on_error  = "yes";
  
  /* last error message is retained here */
  var $last_error     = "";

  /***************************************************************************/
  /* public: Constructor.
   * root:     template directory.
   * unknowns: how to handle unknown variables.
   */
  function Template($root = ".", $unknowns = "remove") {
    $this->set_root($root);
    $this->set_unknowns($unknowns);
  }

  /* public: setroot(pathname $root)
   * root:   array with template directories.
   */  
  function set_root($root) {
    if (!is_array($root)) {
      if (!is_dir($root)) {
        $this->halt("set_root (scalar): $root is not a directory.");
        return false;
      }
      
      $this->root[] = $root;

    } else {
      reset($root);
      while(list($k, $v) = each($root)) {
        if (!is_dir($v)) {
          $this->halt("set_root (array): $v (entry $k of root) is not a directory.");
          return false;
        }
        
        $this->root[] = $v;
      }
    }

    return true;
  }

  /* public: set_unknowns(enum $unknowns)
   * unknowns: "remove", "comment", "keep"
   */
  function set_unknowns($unknowns = "remove") {
    $this->unknowns = $unknowns;
  }

  /* public: set_file(array $filelist)
   * filelist: array of varname, filename pairs.
   *
   * public: set_file(string $varname, string $filename)
   * varname: varname for a filename,
   * filename: name of template file
   */
  function set_file($varname, $filename = "") {
    if (!is_array($varname)) {
    
      if ($filename == "") {
        $this->halt("set_file: For varname $varname filename is empty.");
        return false;
      }
      $this->file[$varname] = $this->filename($filename);

    } else {

      reset($varname);
      while(list($h, $f) = each($varname)) {
        if ($f == "") {
          $this->halt("set_file: For varname $h filename is empty.");
          return false;
        }
        $this->file[$h] = $this->filename($f);
      }
    }

    return true;
  }

  /* public: set_block(string $parent, string $varname, string $name = "")
   * mark a variable as a subblock
   */
  function set_block($parent, $varname, $name = "") {
    if ($name == "")
      $name = $varname;

    $this->block[$varname]["parent"] = $parent;
    $this->block[$varname]["alias"]  = $name;

    return true;
  }
  
  /* public: set_var(array $values)
   * values: array of variable name, value pairs.
   *
   * public: set_var(string $varname, string $value)
   * varname: name of a variable that is to be defined
   * value:   value of that variable
   */
  function set_var($varname, $value = "") {
    if (!is_array($varname)) {
      if (!empty($varname))
        if ($this->debug & 1) 
          printf("<b>set_var:</b> (with scalar) <b>%s</b> = *%s*<br>\n", $varname, htmlentities($value));
        $this->varkeys[$varname] = "/".$this->varname($varname)."/";
        $this->varvals[$varname] = $value;
    } else {
      reset($varname);
      while(list($k, $v) = each($varname)) {
        if (!empty($k))
          if ($this->debug & 1) 
            printf("<b>set_var:</b> (with array) <b>%s</b> = *%s*<br>\n", $k, htmlentities($v));
          $this->varkeys[$k] = "/".$this->varname($k)."/";
          $this->varvals[$k] = $v;
      }
    }
  }

  /***************************************************************************/
  /* public: subst(string $varname)
   * varname: varname of template where variables are to be substituted.
   */
  function subst($varname) {
    $str = $this->get_var($varname);
    $str = @preg_replace($this->varkeys, $this->varvals, $str);
    return $str;
  }
  
  /* public: psubst(string $varname)
   * varname: varname of template where variables are to be substituted.
   */
  function psubst($varname) {
    print $this->subst($varname);
    
    return false;
  }

  /* public: parse(string $target, string $varname, boolean append)
   * public: parse(string $target, array  $varname, boolean append)
   * target: varname of variable to generate
   * varname: varname of template to substitute
   * append: append to target varname
   */
  function parse($target, $varname, $append = false) {
    if (!is_array($varname)) {
      $str = $this->subst($varname);
      if ($append) {
        $this->set_var($target, $this->get_var($target) . $str);
      } else {
        $this->set_var($target, $str);
      }
    } else {
      reset($varname);
      while(list($i, $h) = each($varname)) {
        $str = $this->subst($h);
        $this->set_var($target, $str);
      }
    }
    
    return $str;
  }
  
  function pparse($target, $varname, $append = false) {
    print $this->parse($target, $varname, $append);
    return false;
  }
  
  /* public: get_vars()
   * return all variables as an array (mostly for debugging)
   */
  function get_vars() {
    reset($this->varkeys);
    while(list($k, $v) = each($this->varkeys)) {
      $result[$k] = $this->get_var($k);
    }
    
    return $result;
  }
  
  /* public: get_var(string varname)
   * varname: name of variable.
   *
   * public: get_var(array varname)
   * varname: array of variable names
   */
  function get_var($varname) {
    if (!is_array($varname)) {
    
      /* check for empty variable */
      if (!isset($this->varkeys[$varname]) or empty($this->varvals[$varname])) {
        /* if so, check if it should be loaded */
        if ($this->file[$varname])
          $this->loadfile($varname);
        /* if so, check if it is a subblock of another variable */
        if ($this->block[$varname])
          $this->implode_block($varname);
      }
      if ($this->debug & 2)
        printf ("<b>get_var</b> (with scalar) <b>%s</b> = *%s*<br>\n", $varname, htmlentities($this->varvals[$varname]));
      return $this->varvals[$varname];

    } else {
    
      reset($varname);
      while(list($k, $v) = each($varname)) {
        /* check for empty variable */
        if (!isset($this->varkeys[$varname]) or empty($this->varvals[$varname])) {
          /* if so, check if it should be loaded */
          if ($this->file[$v])
            $this->loadfile($v);
          /* if so, check if it is a subblock of another variable */
          if ($this->block[$v])
            $this->implode_block($v);
        }
        if ($this->debug & 2)
          printf ("<b>get_var:</b> (with array) <b>%s</b> = *%s*<br>\n", $v, htmlentities($this->varvals[$v]));
        $result[$v] = $this->varvals[$v];
      }
      
      return $result;
    }
  }
  
  /* public: get_undefined($varname)
   * varname: varname of a template.
   */
  function get_undefined($varname) {
    $str = $this->get_var($varname);
    preg_match_all("/\{([^}]+)\}/", $str, $m);
    $m = $m[1];
    if (!is_array($m))
      return false;

    reset($m);
    while(list($k, $v) = each($m)) {
      if (!isset($this->varkeys[$v]))
        $result[$v] = $v;
    }
    
    if (count($result))
      return $result;
    else
      return false;
  }

  /* public: finish(string $str)
   * str: string to finish.
   */
  function finish($str) {
    switch ($this->unkowns) {
      case "keep":
      break;
      
      case "remove":
        $str = preg_replace("/\{[^}]+\}/", "", $str);
      break;

      case "comment":
        $str = preg_replace("/\{([^}]+)\}/", "<!-- Template $varname: Variable \\1 undefined -->", $str);
      break;
    }
    
    return $str;
  }

  /* public: p(string $varname)
   * varname: name of variable to print.
   */
  function p($varname) {
    print $this->finish($this->get_var($varname));
  }

  function get($varname) {
    return $this->finish($this->get_var($varname));
  }
    
  /***************************************************************************/
  /* private: filename($filename)
   * filename: name to be completed.
   */
  function filename($filename) {
    /* short path for absolute filenames */
    if (substr($filename, 0, 1) == "/") {
      if (file_exists($filename))
        return $filename;
      else {
        $this->halt("filename (absolute): $filename does not exist.");
        return false;
      }
    }

    /* search path for a matching file */      
    reset($this->root);
    while(list($k, $v) = each($this->root)) {
      $f = "$v/$filename";
      
      if (file_exists($f))
        return $f;
    }

    $this->halt("filename (relative): file $filename does not exist anywhere in " . implode(" ", $this->root));
    return false;
  }
  
  /* private: varname($varname)
   * varname: name of a replacement variable to be protected.
   */
  function varname($varname) {
    return preg_quote("{".$varname."}");
  }

  /* private: loadfile(string $varname)
   * varname:  load file defined by varname, if it is not loaded yet.
   */
  function loadfile($varname) {
    if (!isset($this->file[$varname])) {
      $this->halt("loadfile: $varname is not a valid varname.");
      return false;
    }
    $filename = $this->filename($this->file[$varname]);

    $str = implode("", @file($filename));
    if (empty($str)) {
      $this->halt("loadfile: While loading $varname, $filename does not exist or is empty.");
      return false;
    }

    if ($this->debug & 4)
      printf("<b>loadfile:</b> loaded $filename into $varname<br>\n");
    $this->set_var($varname, $str);
    return true;
  }

  /* private: implode_block($varname)
   * varname: name of variable to implode
   */
  function implode_block($varname) {
    $parent = $this->block[$varname]["parent"];
    $alias  = $this->block[$varname]["alias"];
    
    /* get parent variable */
    $str = $this->get_var($parent);
    
    /* find the subblock we are looking for and extract it */
    $reg = "/<!--\s+BEGIN $varname\s+-->(.*)<!--\s+END $varname\s+-->/sm";
    preg_match_all($reg, $str, $m);
    
    /* implode the subblock to the requested alias */
    $str = preg_replace($reg, "{$alias}", $str);

    if ($this->debug & 4)
      printf("<b>implode_block:</b> extract <b>$varname</b> from <b>$parent</b>, leaving {$alias}<br>\n");
    
    /* update variables */
    $this->set_var($varname, $m[1][0]);
    $this->set_var($parent, $str);
  }
  

  /***************************************************************************/
  /* public: halt(string $msg)
   * msg:    error message to show.
   */
  function halt($msg) {
    $this->last_error = $msg;
    
    if ($this->halt_on_error != "no")
      $this->haltmsg($msg);
    
    if ($this->halt_on_error == "yes")
      die("<b>Halted.</b>");
    
    return false;
  }
  
  /* public, override: haltmsg($msg)
   * msg: error message to show.
   */
  function haltmsg($msg) {
    printf("<b>Template Error:</b> %s<br>\n", $msg);
  }
}
?>