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