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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
<?php
/**
* Horde Template system. Adapted from bTemplate by Brian Lozier
* <brian@massassi.net>.
*
* $Horde: framework/Template/Template.php,v 1.38.10.10 2006/06/13 03:54:03 chuck Exp $
*
* Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @since Horde 3.0
* @package Horde_Template
*/
class Horde_Template {
/**
* Option values.
*
* @var array
*/
var $_options = array();
/**
* Directory that templates should be read from.
*
* @var string
*/
var $_basepath = '';
/**
* Reset template variables after parsing?
*
* @var boolean
*/
var $_resetVars = true;
/**
* Tag (scalar) values.
*
* @var array
*/
var $_scalars = array();
/**
* Loop tag values.
*
* @var array
*/
var $_arrays = array();
/**
* Cloop tag values.
*
* @var array
*/
var $_carrays = array();
/**
* If tag values.
*
* @var array
*/
var $_ifs = array();
/**
* Name of cached template file.
*
* @var string
*/
var $_templateFile = null;
/**
* Cached source of template file.
*
* @var string
*/
var $_template = null;
/**
* Constructor. Can set the template base path and whether or not
* to drop template variables after a parsing a template.
*
* @param string $basepath The directory where templates are read from.
* @param boolean $resetVars Drop template vars after parsing a template?
*/
function Horde_Template($basepath = null, $resetVars = true)
{
if (!is_null($basepath)) {
$this->_basepath = $basepath;
}
$this->_resetVars = (bool)$resetVars;
}
/**
* Sets an option.
*
* @param string $option The option name.
* @param mixed $val The option's value.
*/
function setOption($option, $val)
{
$this->_options[$option] = $val;
}
/**
* Set the template contents to a string.
*
* @param string $template The template text.
*/
function setTemplate($template)
{
$this->_template = $template;
$this->_templateFile = 'string';
}
/**
* Returns an option's value.
*
* @param string $option The option name.
*
* @return mixed The option's value.
*/
function getOption($option)
{
return isset($this->_options[$option]) ? $this->_options[$option] : null;
}
/**
* Sets a tag, loop, or if variable.
*
* @param string|array $tag Either the tag name or a hash with tag names
* as keys and tag values as values.
* @param mixed $var The value to replace the tag with.
* @param boolean $isIf Is this for an <if:> tag? (Default: no).
*/
function set($tag, $var, $isIf = false)
{
if (is_array($tag)) {
foreach ($tag as $tTag => $tVar) {
$this->set($tTag, $tVar, $isIf);
}
} elseif (is_array($var) || is_object($var)) {
$this->_arrays[$tag] = $var;
if ($isIf) {
// Just store the same variable that we stored in
// $this->_arrays - if we don't modify it, PHP's
// reference counting ensures we're not using any
// additional memory here.
$this->_ifs[$tag] = $var;
}
} else {
$this->_scalars[$tag] = $var;
if ($isIf) {
// Just store the same variable that we stored in
// $this->_scalars - if we don't modify it, PHP's
// reference counting ensures we're not using any
// additional memory here.
$this->_ifs[$tag] = $var;
}
}
}
/**
* Returns the value of a tag or loop.
*
* @param string $tag The tag name.
*
* @return mixed The tag value or null if the tag hasn't been set yet.
*/
function get($tag)
{
if (isset($this->_arrays[$tag])) {
return $this->_arrays[$tag];
}
if (isset($this->_scalars[$tag])) {
return $this->_scalars[$tag];
}
return null;
}
/**
* Sets values for a cloop.
*
* @param string $tag The name of the cloop.
* @param array $array The values for the cloop.
* @param array $cases The cases (test values) for the cloops.
*/
function setCloop($tag, $array, $cases)
{
$this->_carrays[$tag] = array(
'array' => $array,
'cases' => $cases
);
}
/**
* Resets the template variables.
*
* @param boolean $scalars Reset scalar (basic tag) variables?
* @param boolean $arrays Reset loop variables?
* @param boolean $carrays Reset cloop variables?
* @param boolean $ifs Reset if variables?
*/
function resetVars($scalars, $arrays, $carrays, $ifs)
{
if ($scalars === true) {
$this->_scalars = array();
}
if ($arrays === true) {
$this->_arrays = array();
}
if ($carrays === true) {
$this->_carrays = array();
}
if ($ifs === true) {
$this->_ifs = array();
}
}
/**
* Retrieve list of blocks present in the template source.
*
* @param string $filename The file to fetch the template from.
* @return mixed An array of applications, or PEAR_Error on failure.
*/
function listBlocks()
{
$blocks = array();
if (preg_match_all('<block:([a-zA-Z0-9_]+:[a-zA-Z0-9_]+)>',
$this->_template, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$blocks[] = $match[1];
}
}
return $blocks;
}
/**
* Fetches a template from the specified file and return the parsed
* contents.
*
* @param string $filename The file to fetch the template from.
*
* @return string The parsed template.
*/
function fetch($filename)
{
$contents = $this->_getTemplate($filename);
if (is_a($contents, 'PEAR_Error')) {
return $contents;
}
// Parse and return the contents.
return $this->parse($contents);
}
/**
* Parses all variables/tags in the template.
*
* @param string $contents The unparsed template.
*
* @return string The parsed template.
*/
function parse($contents = null)
{
if (!$contents) {
$contents = $this->_template;
}
// Process ifs.
if (!empty($this->_ifs)) {
foreach ($this->_ifs as $tag => $value) {
$contents = $this->_parseIf($tag, $contents);
}
}
// Process tags.
$search = array();
$replace = array();
foreach ($this->_scalars as $key => $value) {
$search[] = $this->_getTag($key);
$replace[] = $value;
}
if (count($search)) {
$contents = str_replace($search, $replace, $contents);
}
// Process loops and arrays.
foreach ($this->_arrays as $key => $array) {
$contents = $this->_parseLoop($key, $array, $contents);
}
// Process cloops.
foreach ($this->_carrays as $key => $array) {
$contents = $this->_parseCloop($key, $array, $contents);
}
// Parse gettext tags, if the option is enabled.
if ($this->getOption('gettext')) {
$contents = $this->_parseGettext($contents);
}
// Parse embedded blocks
if ($this->getOption('blocks')) {
$contents = $this->_parseBlocks($contents);
}
// Reset template data unless we're supposed to keep it
// around.
if ($this->_resetVars) {
$this->resetVars(false, true, true, false);
}
// Return parsed template.
return $contents;
}
/**
* Returns full start and end tags for a named tag.
*
* @access private
*
* @param string $tag
* @param string $directive The kind of tag - tag, if, loop, cloop.
*
* @return array 'b' => Start tag.
* 'e' => End tag.
*/
function _getTags($tag, $directive)
{
return array('b' => '<' . $directive . ':' . $tag . '>',
'e' => '</' . $directive . ':' . $tag . '>');
}
/**
* Formats a scalar tag (default format is <tag:name>).
*
* @access private
*
* @param string $tag The name of the tag.
*
* @return string The full tag with the current start/end delimiters.
*/
function _getTag($tag)
{
return '<tag:' . $tag . ' />';
}
/**
* Extracts a portion of a template.
*
* @access private
*
* @param array $t The tag to extract. Hash format is:
* $t['b'] - The start tag
* $t['e'] - The end tag
* @param string &$contents The template to extract from.
*/
function _getStatement($t, &$contents)
{
// Locate the statement.
$pos = strpos($contents, $t['b']);
if ($pos === false) {
return false;
}
$tag_length = strlen($t['b']);
$fpos = $pos + $tag_length;
$lpos = strpos($contents, $t['e']);
$length = $lpos - $fpos;
// Extract & return the statement.
return substr($contents, $fpos, $length);
}
/**
* Parses gettext tags.
*
* @param string $contents The unparsed content of the file.
*
* @return string The parsed contents of the gettext blocks.
*/
function _parseGettext($contents)
{
// Get the tags & loop.
$t = array('b' => '<gettext>',
'e' => '</gettext>');
while ($text = $this->_getStatement($t, $contents)) {
$contents = str_replace($t['b'] . $text . $t['e'], _($text), $contents);
}
return $contents;
}
/**
* Parses block tags.
*
* Here we parse block tags in the form:
*
* <block:app:name>
* <param:foo>foo parameter's value</param:foo>
* <param:bar>bar parameter's value</param:bar>
* <serial:baz>s:21:"baz parameter's value";</serial:baz>
* </block:app:name>
*
* ... and replace them with rendered application blocks.
*
* Block parameters are provided with the nested "param:foo" and
* "serial:foo" tags. The serial-style tags take a serialized PHP
* value, so you can use it to provide array, boolean, and complex
* type parameters. The param-style tags provide string parameters.
*
* @access private
*
* @param string $contents The unparsed contents of the file.
*
* @return string The parsed contents of the file.
*/
function _parseBlocks($contents)
{
return preg_replace('!<block:([a-zA-Z_0-9]+):([a-zA-Z_0-9]+)>(.*?)' .
'</block:([a-zA-Z_0-9]+):([a-zA-Z_0-9]+)>!se',
"\$this->_parseBlock('\\1', '\\2', '\\3')",
$contents);
}
/**
* Handle the contents of one block tag.
*
* @access private
*/
function _parseBlock($app, $name, $contents)
{
require_once 'Horde/Block.php';
require_once 'Horde/Block/Collection.php';
// Undo preg_replace() encoding strangeness.
$contents = str_replace('\\"', '"', $contents);
// Find block's parameters.
$params = array();
if (preg_match_all('!<param:([a-zA-Z_0-9]+)>(.*?)' .
'</param:([a-zA-Z_0-9]+)>!s', $contents, $matches,
PREG_SET_ORDER)) {
foreach ($matches as $match) {
$params[$match[1]] = html_entity_decode($match[2]);
}
}
if (preg_match_all('!<serial:([a-zA-Z_0-9]+)>(.*?)' .
'</serial:([a-zA-Z_0-9]+)>!s', $contents, $matches,
PREG_SET_ORDER)) {
foreach ($matches as $match) {
$params[$match[1]] = unserialize(html_entity_decode($match[2]));
}
}
// Construct the block.
$block = &Horde_Block_Collection::getBlock($app, $name, $params);
if (is_a($block, 'PEAR_Error')) {
$result = '<strong>' . sprintf(_("Error creating block: %s"),
$block->getMessage()) . '</strong>';
} else {
$result = $block->getContent();
if (is_a($result, 'PEAR_Error')) {
$result = '<strong>' . sprintf(_("Error getting content: %s"),
$result->getMessage()) . '</strong>';
}
}
return $result;
}
/**
* Parses a given if statement.
*
* @access private
*
* @param string $tag The name of the if block to parse.
* @param string $contents The unparsed contents of the if block.
*
* @return string The parsed contents of the if block.
*/
function _parseIf($tag, $contents, $key = null)
{
// Get the tags & if statement.
$t = $this->_getTags($tag, 'if');
$et = $this->_getTags($tag, 'else');
// explode the tag, so we have the correct keys for the array
if (isset($key)) {
list($tg, $k) = explode('.', $tag);
}
while (($if = $this->_getStatement($t, $contents)) !== false) {
// Check for else statement.
if ($else = $this->_getStatement($et, $if)) {
// Process the if statement.
if ((isset($key) && $this->_ifs[$tg][$key][$k]) ||
(isset($this->_ifs[$tag]) && $this->_ifs[$tag])) {
$replace = str_replace($et['b'] . $else . $et['e'], '', $if);
} else {
$replace = $else;
}
} else {
// Process the if statement.
if (isset($key)) {
$replace = $this->_ifs[$tg][$key][$k] ? $if : null;
} else {
$replace = $this->_ifs[$tag] ? $if : null;
}
}
// Parse the template.
$contents = str_replace($t['b'] . $if . $t['e'], $replace,
$contents);
}
// Return parsed template.
return $contents;
}
/**
* Parses the given array for any loops or other uses of the array.
*
* @access private
*
* @param string $tag The name of the loop to parse.
* @param array $array The values for the loop.
* @param string $contents The unparsed contents of the loop.
*
* @return string The parsed contents of the loop.
*/
function _parseLoop($tag, $array, $contents)
{
// Get the tags & loop.
$t = $this->_getTags($tag, 'loop');
$loop = $this->_getStatement($t, $contents);
// See if we have a divider.
$l = $this->_getTags($tag, 'divider');
$divider = $this->_getStatement($l, $loop);
$contents = str_replace($l['b'] . $divider . $l['e'], '', $contents);
// Process the array.
do {
$parsed = '';
$first = true;
foreach ($array as $key => $value) {
if (is_array($value) || is_object($value)) {
$i = $loop;
if (is_numeric($key)) {
foreach ($value as $key2 => $value2) {
if (!is_array($value2) && !is_object($value2)) {
// Replace associative array tags.
$aa_tag = $tag . '.' . $key2;
$i = str_replace($this->_getTag($aa_tag), $value2, $i);
$pos = strpos($tag, '.');
if (($pos !== false) &&
!empty($this->_ifs[substr($tag, 0, $pos)])) {
$this->_ifs[$aa_tag] = $value2;
$i = $this->_parseIf($aa_tag, $i);
unset($this->_ifs[$aa_tag]);
}
} else {
// Check to see if it's a nested loop.
$i = $this->_parseLoop($tag . '.' . $key2, $value2, $i);
}
}
}
$i = str_replace($this->_getTag($tag), $key, $i);
} elseif (is_string($key) && !is_array($value) && !is_object($value)) {
$contents = str_replace($this->_getTag($tag . '.' . $key), $value, $contents);
} elseif (!is_array($value) && !is_object($value)) {
$i = str_replace($this->_getTag($tag . ''), $value, $loop);
} else {
$i = null;
}
// Parse conditions in the array.
if (!empty($this->_ifs[$tag][$key]) && is_array($this->_ifs[$tag][$key]) && $this->_ifs[$tag][$key]) {
foreach ($this->_ifs[$tag][$key] as $cTag => $cValue) {
$i = $this->_parseIf($tag . '.' . $cTag, $i, $key);
}
}
// Add the parsed iteration.
if (isset($i)) {
// If it's not the first time through, prefix the
// loop divider, if there is one.
if (!$first) {
$i = $divider . $i;
}
$parsed .= rtrim($i);
}
// No longer the first time through.
$first = false;
}
// Replace the parsed pieces of the template.
$contents = str_replace($t['b'] . $loop . $t['e'], $parsed, $contents);
} while ($loop = $this->_getStatement($t, $contents));
return $contents;
}
/**
* Parses the given case loop (cloop).
*
* @access private
*
* @param string $tag The name of the cloop to parse.
* @param array $array The values for the cloop.
* @param string $contents The unparsed contents of the cloop.
*
* @return string The parsed contents of the cloop.
*/
function _parseCloop($tag, $array, $contents)
{
// Get the tags & cloop.
$t = $this->_getTags($tag, 'cloop');
while ($loop = $this->_getStatement($t, $contents)) {
// Set up the cases.
$array['cases'][] = 'default';
$case_content = array();
// Get the case strings.
foreach ($array['cases'] as $case) {
$ctags[$case] = $this->_getTags($case, 'case');
$case_content[$case] = $this->_getStatement($ctags[$case], $loop);
}
// Process the cloop.
$parsed = '';
foreach ($array['array'] as $key => $value) {
if (is_numeric($key) && (is_array($value) || is_object($value))) {
// Set up the cases.
if (isset($value['case'])) {
$current_case = $value['case'];
} else {
$current_case = 'default';
}
unset($value['case']);
$i = $case_content[$current_case];
// Loop through each value.
foreach ($value as $key2 => $value2) {
if (is_array($value2) || is_object($value2)) {
$i = $this->_parseLoop($tag . '.' . $key2, $value2, $i);
} else {
$i = str_replace($this->_getTag($tag . '.' . $key2), $value2, $i);
}
}
}
// Add the parsed iteration.
$parsed .= rtrim($i);
}
// Parse the cloop.
$contents = str_replace($t['b'] . $loop . $t['e'], $parsed, $contents);
}
return $contents;
}
/**
* Fetch the contents of a template into $this->_template; cache
* the filename in $this->_templateFile.
*
* @access private
*
* @param string $filename Location of template file on disk.
*
* @return string The loaded template content.
*/
function _getTemplate($filename = null)
{
if (!is_null($filename) && ($filename != $this->_templateFile)) {
$this->_template = null;
}
if (!is_null($this->_template)) {
return $this->_template;
}
// Get the contents of the file.
$file = $this->_basepath . $filename;
$contents = @file_get_contents($file);
if ($contents === false) {
require_once 'PEAR.php';
return PEAR::raiseError(sprintf(_("Template \"%s\" not found."), $file));
}
$this->_template = $contents;
$this->_templateFile = $filename;
return $this->_template;
}
}
|