xtemplate.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. <?php
  2. // When developing uncomment the line below, re-comment before making public
  3. //error_reporting(E_ALL);
  4. /**
  5. * XTemplate PHP templating engine
  6. *
  7. * @package XTemplate
  8. * @author Barnabas Debreceni [cranx@users.sourceforge.net]
  9. * @copyright Barnabas Debreceni 2000-2001
  10. * @author Jeremy Coates [cocomp@users.sourceforge.net]
  11. * @copyright Jeremy Coates 2002-2007
  12. * @see license.txt LGPL / BSD license
  13. * @since PHP 5
  14. * @link $HeadURL: https://xtpl.svn.sourceforge.net/svnroot/xtpl/trunk/xtemplate.class.php $
  15. * @version $Id: xtemplate.class.php 21 2007-05-29 18:01:15Z cocomp $
  16. *
  17. *
  18. * XTemplate class - http://www.phpxtemplate.org/ (x)html / xml generation with templates - fast & easy
  19. * Latest stable & Subversion versions available @ http://sourceforge.net/projects/xtpl/
  20. * License: LGPL / BSD - see license.txt
  21. * Changelog: see changelog.txt
  22. */
  23. class XTemplate {
  24. /**
  25. * Properties
  26. */
  27. /**
  28. * Raw contents of the template file
  29. *
  30. * @access public
  31. * @var string
  32. */
  33. public $filecontents = '';
  34. /**
  35. * Unparsed blocks
  36. *
  37. * @access public
  38. * @var array
  39. */
  40. public $blocks = array();
  41. /**
  42. * Parsed blocks
  43. *
  44. * @var unknown_type
  45. */
  46. public $parsed_blocks = array();
  47. /**
  48. * Preparsed blocks (for file includes)
  49. *
  50. * @access public
  51. * @var array
  52. */
  53. public $preparsed_blocks = array();
  54. /**
  55. * Block parsing order for recursive parsing
  56. * (Sometimes reverse :)
  57. *
  58. * @access public
  59. * @var array
  60. */
  61. public $block_parse_order = array();
  62. /**
  63. * Store sub-block names
  64. * (For fast resetting)
  65. *
  66. * @access public
  67. * @var array
  68. */
  69. public $sub_blocks = array();
  70. /**
  71. * Variables array
  72. *
  73. * @access public
  74. * @var array
  75. */
  76. public $vars = array();
  77. /**
  78. * File variables array
  79. *
  80. * @access public
  81. * @var array
  82. */
  83. public $filevars = array();
  84. /**
  85. * Filevars' parent block
  86. *
  87. * @access public
  88. * @var array
  89. */
  90. public $filevar_parent = array();
  91. /**
  92. * File caching during duration of script
  93. * e.g. files only cached to speed {FILE "filename"} repeats
  94. *
  95. * @access public
  96. * @var array
  97. */
  98. public $filecache = array();
  99. /**
  100. * Location of template files
  101. *
  102. * @access public
  103. * @var string
  104. */
  105. public $tpldir = '';
  106. /**
  107. * Filenames lookup table
  108. *
  109. * @access public
  110. * @var null
  111. */
  112. public $files = null;
  113. /**
  114. * Template filename
  115. *
  116. * @access public
  117. * @var string
  118. */
  119. public $filename = '';
  120. // moved to setup method so uses the tag_start & end_delims
  121. /**
  122. * RegEx for file includes
  123. *
  124. * "/\{FILE\s*\"([^\"]+)\"\s*\}/m";
  125. *
  126. * @access public
  127. * @var string
  128. */
  129. public $file_delim = '';
  130. /**
  131. * RegEx for file include variable
  132. *
  133. * "/\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}/m";
  134. *
  135. * @access public
  136. * @var string
  137. */
  138. public $filevar_delim = '';
  139. /**
  140. * RegEx for file includes with newlines
  141. *
  142. * "/^\s*\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}\s*\n/m";
  143. *
  144. * @access public
  145. * @var string
  146. */
  147. public $filevar_delim_nl = '';
  148. /**
  149. * Template block start delimiter
  150. *
  151. * @access public
  152. * @var string
  153. */
  154. public $block_start_delim = '<!-- ';
  155. /**
  156. * Template block end delimiter
  157. *
  158. * @access public
  159. * @var string
  160. */
  161. public $block_end_delim = '-->';
  162. /**
  163. * Template block start word
  164. *
  165. * @access public
  166. * @var string
  167. */
  168. public $block_start_word = 'BEGIN:';
  169. /**
  170. * Template block end word
  171. *
  172. * The last 3 properties and this make the delimiters look like:
  173. * @example <!-- BEGIN: block_name -->
  174. * if you use the default syntax.
  175. *
  176. * @access public
  177. * @var string
  178. */
  179. public $block_end_word = 'END:';
  180. /**
  181. * Template tag start delimiter
  182. *
  183. * This makes the delimiters look like:
  184. * @example {tagname}
  185. * if you use the default syntax.
  186. *
  187. * @access public
  188. * @var string
  189. */
  190. public $tag_start_delim = '{';
  191. /**
  192. * Template tag end delimiter
  193. *
  194. * This makes the delimiters look like:
  195. * @example {tagname}
  196. * if you use the default syntax.
  197. *
  198. * @access public
  199. * @var string
  200. */
  201. public $tag_end_delim = '}';
  202. /* this makes the delimiters look like: {tagname} if you use my syntax. */
  203. /**
  204. * Regular expression element for comments within tags and blocks
  205. *
  206. * @example {tagname#My Comment}
  207. * @example {tagname #My Comment}
  208. * @example <!-- BEGIN: blockname#My Comment -->
  209. * @example <!-- BEGIN: blockname #My Comment -->
  210. *
  211. * @access public
  212. * @var string
  213. */
  214. public $comment_preg = '( ?#.*?)?';
  215. /**
  216. * Default main template block name
  217. *
  218. * @access public
  219. * @var string
  220. */
  221. public $mainblock = 'main';
  222. /**
  223. * Script output type
  224. *
  225. * @access public
  226. * @var string
  227. */
  228. public $output_type = 'HTML';
  229. /**
  230. * Debug mode
  231. *
  232. * @access public
  233. * @var boolean
  234. */
  235. public $debug = false;
  236. /**
  237. * Null string for unassigned vars
  238. *
  239. * @access protected
  240. * @var array
  241. */
  242. protected $_null_string = array('' => '');
  243. /**
  244. * Null string for unassigned blocks
  245. *
  246. * @access protected
  247. * @var array
  248. */
  249. protected $_null_block = array('' => '');
  250. /**
  251. * Errors
  252. *
  253. * @access protected
  254. * @var string
  255. */
  256. protected $_error = '';
  257. /**
  258. * Auto-reset sub blocks
  259. *
  260. * @access protected
  261. * @var boolean
  262. */
  263. protected $_autoreset = true;
  264. /**
  265. * Set to FALSE to generate errors if a non-existant blocks is referenced
  266. *
  267. * @author NW
  268. * @since 2002/10/17
  269. * @access protected
  270. * @var boolean
  271. */
  272. protected $_ignore_missing_blocks = true;
  273. /**
  274. * PHP 5 Constructor - Instantiate the object
  275. *
  276. * @param string $file Template file to work on
  277. * @param string/array $tpldir Location of template files (useful for keeping files outside web server root)
  278. * @param array $files Filenames lookup
  279. * @param string $mainblock Name of main block in the template
  280. * @param boolean $autosetup If true, run setup() as part of constuctor
  281. * @return XTemplate
  282. */
  283. public function __construct($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) {
  284. $this->restart($file, $tpldir, $files, $mainblock, $autosetup, $this->tag_start_delim, $this->tag_end_delim);
  285. }
  286. /**
  287. * PHP 4 Constructor - Instantiate the object
  288. *
  289. * @deprecated Use PHP 5 constructor instead
  290. * @param string $file Template file to work on
  291. * @param string/array $tpldir Location of template files (useful for keeping files outside web server root)
  292. * @param array $files Filenames lookup
  293. * @param string $mainblock Name of main block in the template
  294. * @param boolean $autosetup If true, run setup() as part of constuctor
  295. * @return XTemplate
  296. */
  297. public function XTemplate ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) {
  298. assert('Deprecated - use PHP 5 constructor');
  299. }
  300. /***************************************************************************/
  301. /***[ public stuff ]********************************************************/
  302. /***************************************************************************/
  303. /**
  304. * Restart the class - allows one instantiation with several files processed by restarting
  305. * e.g. $xtpl = new XTemplate('file1.xtpl');
  306. * $xtpl->parse('main');
  307. * $xtpl->out('main');
  308. * $xtpl->restart('file2.xtpl');
  309. * $xtpl->parse('main');
  310. * $xtpl->out('main');
  311. * (Added in response to sf:641407 feature request)
  312. *
  313. * @param string $file Template file to work on
  314. * @param string/array $tpldir Location of template files
  315. * @param array $files Filenames lookup
  316. * @param string $mainblock Name of main block in the template
  317. * @param boolean $autosetup If true, run setup() as part of restarting
  318. * @param string $tag_start {
  319. * @param string $tag_end }
  320. */
  321. public function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}') {
  322. $this->filename = $file;
  323. // From SF Feature request 1202027
  324. // Kenneth Kalmer
  325. $this->tpldir = $tpldir;
  326. if (defined('XTPL_DIR') && empty($this->tpldir)) {
  327. $this->tpldir = XTPL_DIR;
  328. }
  329. if (is_array($files)) {
  330. $this->files = $files;
  331. }
  332. $this->mainblock = $mainblock;
  333. $this->tag_start_delim = $tag_start;
  334. $this->tag_end_delim = $tag_end;
  335. // Start with fresh file contents
  336. $this->filecontents = '';
  337. // Reset the template arrays
  338. $this->blocks = array();
  339. $this->parsed_blocks = array();
  340. $this->preparsed_blocks = array();
  341. $this->block_parse_order = array();
  342. $this->sub_blocks = array();
  343. $this->vars = array();
  344. $this->filevars = array();
  345. $this->filevar_parent = array();
  346. $this->filecache = array();
  347. if ($autosetup) {
  348. $this->setup();
  349. }
  350. }
  351. /**
  352. * setup - the elements that were previously in the constructor
  353. *
  354. * @access public
  355. * @param boolean $add_outer If true is passed when called, it adds an outer main block to the file
  356. */
  357. public function setup ($add_outer = false) {
  358. $this->tag_start_delim = preg_quote($this->tag_start_delim);
  359. $this->tag_end_delim = preg_quote($this->tag_end_delim);
  360. // Setup the file delimiters
  361. // regexp for file includes
  362. $this->file_delim = "/" . $this->tag_start_delim . "FILE\s*\"([^\"]+)\"" . $this->comment_preg . $this->tag_end_delim . "/m";
  363. // regexp for file includes
  364. $this->filevar_delim = "/" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "/m";
  365. // regexp for file includes w/ newlines
  366. $this->filevar_delim_nl = "/^\s*" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "\s*\n/m";
  367. if (empty($this->filecontents)) {
  368. // read in template file
  369. $this->filecontents = $this->_r_getfile($this->filename);
  370. }
  371. if ($add_outer) {
  372. $this->_add_outer_block();
  373. }
  374. // preprocess some stuff
  375. $this->blocks = $this->_maketree($this->filecontents, '');
  376. $this->filevar_parent = $this->_store_filevar_parents($this->blocks);
  377. $this->scan_globals();
  378. }
  379. /**
  380. * assign a variable
  381. *
  382. * @example Simplest case:
  383. * @example $xtpl->assign('name', 'value');
  384. * @example {name} in template
  385. *
  386. * @example Array assign:
  387. * @example $xtpl->assign(array('name' => 'value', 'name2' => 'value2'));
  388. * @example {name} {name2} in template
  389. *
  390. * @example Value as array assign:
  391. * @example $xtpl->assign('name', array('key' => 'value', 'key2' => 'value2'));
  392. * @example {name.key} {name.key2} in template
  393. *
  394. * @example Reset array:
  395. * @example $xtpl->assign('name', array('key' => 'value', 'key2' => 'value2'));
  396. * @example // Other code then:
  397. * @example $xtpl->assign('name', array('key3' => 'value3'), false);
  398. * @example {name.key} {name.key2} {name.key3} in template
  399. *
  400. * @access public
  401. * @param string $name Variable to assign $val to
  402. * @param string / array $val Value to assign to $name
  403. * @param boolean $reset_array Reset the variable array if $val is an array
  404. */
  405. public function assign ($name, $val = '', $reset_array = true) {
  406. if (is_array($name)) {
  407. foreach ($name as $k => $v) {
  408. $this->vars[$k] = $v;
  409. }
  410. } elseif (is_array($val)) {
  411. // Clear the existing values
  412. if ($reset_array) {
  413. $this->vars[$name] = array();
  414. }
  415. foreach ($val as $k => $v) {
  416. $this->vars[$name][$k] = $v;
  417. }
  418. } else {
  419. $this->vars[$name] = $val;
  420. }
  421. }
  422. /**
  423. * assign a file variable
  424. *
  425. * @access public
  426. * @param string $name Variable to assign $val to
  427. * @param string / array $val Values to assign to $name
  428. */
  429. public function assign_file ($name, $val = '') {
  430. if (is_array($name)) {
  431. foreach ($name as $k => $v) {
  432. $this->_assign_file_sub($k, $v);
  433. }
  434. } else {
  435. $this->_assign_file_sub($name, $val);
  436. }
  437. }
  438. /**
  439. * parse a block
  440. *
  441. * @access public
  442. * @param string $bname Block name to parse
  443. */
  444. public function parse ($bname) {
  445. if (isset($this->preparsed_blocks[$bname])) {
  446. $copy = $this->preparsed_blocks[$bname];
  447. } elseif (isset($this->blocks[$bname])) {
  448. $copy = $this->blocks[$bname];
  449. } elseif ($this->_ignore_missing_blocks) {
  450. // ------------------------------------------------------
  451. // NW : 17 Oct 2002. Added default of ignore_missing_blocks
  452. // to allow for generalised processing where some
  453. // blocks may be removed from the HTML without the
  454. // processing code needing to be altered.
  455. // ------------------------------------------------------
  456. // JRC: 3/1/2003 added set error to ignore missing functionality
  457. $this->_set_error("parse: blockname [$bname] does not exist");
  458. return;
  459. } else {
  460. $this->_set_error("parse: blockname [$bname] does not exist");
  461. }
  462. /* from there we should have no more {FILE } directives */
  463. if (!isset($copy)) {
  464. die('Block: ' . $bname);
  465. }
  466. $copy = preg_replace($this->filevar_delim_nl, '', $copy);
  467. $var_array = array();
  468. /* find & replace variables+blocks */
  469. preg_match_all("|" . $this->tag_start_delim . "([A-Za-z0-9\._]+?" . $this->comment_preg . ")" . $this->tag_end_delim. "|", $copy, $var_array);
  470. $var_array = $var_array[1];
  471. foreach ($var_array as $k => $v) {
  472. // Are there any comments in the tags {tag#a comment for documenting the template}
  473. $any_comments = explode('#', $v);
  474. $v = rtrim($any_comments[0]);
  475. if (sizeof($any_comments) > 1) {
  476. $comments = $any_comments[1];
  477. } else {
  478. $comments = '';
  479. }
  480. $sub = explode('.', $v);
  481. if ($sub[0] == '_BLOCK_') {
  482. unset($sub[0]);
  483. $bname2 = implode('.', $sub);
  484. // trinary operator eliminates assign error in E_ALL reporting
  485. $var = isset($this->parsed_blocks[$bname2]) ? $this->parsed_blocks[$bname2] : null;
  486. $nul = (!isset($this->_null_block[$bname2])) ? $this->_null_block[''] : $this->_null_block[$bname2];
  487. if ($var === '') {
  488. if ($nul == '') {
  489. // -----------------------------------------------------------
  490. // Removed requirement for blocks to be at the start of string
  491. // -----------------------------------------------------------
  492. // $copy=preg_replace("/^\s*\{".$v."\}\s*\n*/m","",$copy);
  493. // Now blocks don't need to be at the beginning of a line,
  494. //$copy=preg_replace("/\s*" . $this->tag_start_delim . $v . $this->tag_end_delim . "\s*\n*/m","",$copy);
  495. $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", '', $copy);
  496. } else {
  497. $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$nul", $copy);
  498. }
  499. } else {
  500. //$var = trim($var);
  501. switch (true) {
  502. case preg_match('/^\n/', $var) && preg_match('/\n$/', $var):
  503. $var = substr($var, 1, -1);
  504. break;
  505. case preg_match('/^\n/', $var):
  506. $var = substr($var, 1);
  507. break;
  508. case preg_match('/\n$/', $var):
  509. $var = substr($var, 0, -1);
  510. break;
  511. }
  512. // SF Bug no. 810773 - thanks anonymous
  513. $var = str_replace('\\', '\\\\', $var);
  514. // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04
  515. $var = str_replace('$', '\\$', $var);
  516. // Replaced str_replaces with preg_quote
  517. //$var = preg_quote($var);
  518. $var = str_replace('\\|', '|', $var);
  519. $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$var", $copy);
  520. if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) {
  521. $copy = substr($copy, 1, -1);
  522. }
  523. }
  524. } else {
  525. $var = $this->vars;
  526. foreach ($sub as $v1) {
  527. // NW 4 Oct 2002 - Added isset and is_array check to avoid NOTICE messages
  528. // JC 17 Oct 2002 - Changed EMPTY to stlen=0
  529. // if (empty($var[$v1])) { // this line would think that zeros(0) were empty - which is not true
  530. if (!isset($var[$v1]) || (!is_array($var[$v1]) && strlen($var[$v1]) == 0)) {
  531. // Check for constant, when variable not assigned
  532. if (defined($v1)) {
  533. $var[$v1] = constant($v1);
  534. } else {
  535. $var[$v1] = null;
  536. }
  537. }
  538. $var = $var[$v1];
  539. }
  540. $nul = (!isset($this->_null_string[$v])) ? ($this->_null_string[""]) : ($this->_null_string[$v]);
  541. $var = (!isset($var)) ? $nul : $var;
  542. if ($var === '') {
  543. // -----------------------------------------------------------
  544. // Removed requriement for blocks to be at the start of string
  545. // -----------------------------------------------------------
  546. // $copy=preg_replace("|^\s*\{".$v." ?#?".$comments."\}\s*\n|m","",$copy);
  547. $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", '', $copy);
  548. }
  549. $var = trim($var);
  550. // SF Bug no. 810773 - thanks anonymous
  551. $var = str_replace('\\', '\\\\', $var);
  552. // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04
  553. $var = str_replace('$', '\\$', $var);
  554. // Replace str_replaces with preg_quote
  555. //$var = preg_quote($var);
  556. $var = str_replace('\\|', '|', $var);
  557. $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", "$var", $copy);
  558. if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) {
  559. $copy = substr($copy, 1);
  560. }
  561. }
  562. }
  563. if (isset($this->parsed_blocks[$bname])) {
  564. $this->parsed_blocks[$bname] .= $copy;
  565. } else {
  566. $this->parsed_blocks[$bname] = $copy;
  567. }
  568. /* reset sub-blocks */
  569. if ($this->_autoreset && (!empty($this->sub_blocks[$bname]))) {
  570. reset($this->sub_blocks[$bname]);
  571. foreach ($this->sub_blocks[$bname] as $k => $v) {
  572. $this->reset($v);
  573. }
  574. }
  575. }
  576. /**
  577. * returns the parsed text for a block, including all sub-blocks.
  578. *
  579. * @access public
  580. * @param string $bname Block name to parse
  581. */
  582. public function rparse ($bname) {
  583. if (!empty($this->sub_blocks[$bname])) {
  584. reset($this->sub_blocks[$bname]);
  585. foreach ($this->sub_blocks[$bname] as $k => $v) {
  586. if (!empty($v)) {
  587. $this->rparse($v);
  588. }
  589. }
  590. }
  591. $this->parse($bname);
  592. }
  593. /**
  594. * inserts a loop ( call assign & parse )
  595. *
  596. * @access public
  597. * @param string $bname Block name to assign
  598. * @param string $var Variable to assign values to
  599. * @param string / array $value Value to assign to $var
  600. */
  601. public function insert_loop ($bname, $var, $value = '') {
  602. $this->assign($var, $value);
  603. $this->parse($bname);
  604. }
  605. /**
  606. * parses a block for every set of data in the values array
  607. *
  608. * @access public
  609. * @param string $bname Block name to loop
  610. * @param string $var Variable to assign values to
  611. * @param array $values Values to assign to $var
  612. */
  613. public function array_loop ($bname, $var, &$values) {
  614. if (is_array($values)) {
  615. foreach($values as $v) {
  616. $this->insert_loop($bname, $var, $v);
  617. }
  618. }
  619. }
  620. /**
  621. * returns the parsed text for a block
  622. *
  623. * @access public
  624. * @param string $bname Block name to return
  625. * @return string
  626. */
  627. public function text ($bname = '') {
  628. $text = '';
  629. if ($this->debug && $this->output_type == 'HTML') {
  630. // JC 20/11/02 echo the template filename if in development as
  631. // html comment
  632. $text .= '<!-- XTemplate: ' . realpath($this->filename) . " -->\n";
  633. }
  634. $bname = !empty($bname) ? $bname : $this->mainblock;
  635. $text .= isset($this->parsed_blocks[$bname]) ? $this->parsed_blocks[$bname] : $this->get_error();
  636. return str_replace(array("\r", "\n"), '', $text);//my edit
  637. //return $text;
  638. }
  639. /**
  640. * prints the parsed text
  641. *
  642. * @access public
  643. * @param string $bname Block name to echo out
  644. */
  645. public function out ($bname) {
  646. $out = $this->text($bname);
  647. // $length=strlen($out);
  648. //header("Content-Length: ".$length); // TODO: Comment this back in later
  649. echo $out;
  650. }
  651. /**
  652. * prints the parsed text to a specified file
  653. *
  654. * @access public
  655. * @param string $bname Block name to write out
  656. * @param string $fname File name to write to
  657. */
  658. public function out_file ($bname, $fname) {
  659. if (!empty($bname) && !empty($fname) && is_writeable($fname)) {
  660. $fp = fopen($fname, 'w');
  661. fwrite($fp, $this->text($bname));
  662. fclose($fp);
  663. }
  664. }
  665. /**
  666. * resets the parsed text
  667. *
  668. * @access public
  669. * @param string $bname Block to reset
  670. */
  671. public function reset ($bname) {
  672. $this->parsed_blocks[$bname] = '';
  673. }
  674. /**
  675. * returns true if block was parsed, false if not
  676. *
  677. * @access public
  678. * @param string $bname Block name to test
  679. * @return boolean
  680. */
  681. public function parsed ($bname) {
  682. return (!empty($this->parsed_blocks[$bname]));
  683. }
  684. /**
  685. * sets the string to replace in case the var was not assigned
  686. *
  687. * @access public
  688. * @param string $str Display string for null block
  689. * @param string $varname Variable name to apply $str to
  690. */
  691. public function set_null_string($str, $varname = '') {
  692. $this->_null_string[$varname] = $str;
  693. }
  694. /**
  695. * Backwards compatibility only
  696. *
  697. * @param string $str
  698. * @param string $varname
  699. * @deprecated Change to set_null_string to keep in with rest of naming convention
  700. */
  701. public function SetNullString ($str, $varname = '') {
  702. $this->set_null_string($str, $varname);
  703. }
  704. /**
  705. * sets the string to replace in case the block was not parsed
  706. *
  707. * @access public
  708. * @param string $str Display string for null block
  709. * @param string $bname Block name to apply $str to
  710. */
  711. public function set_null_block ($str, $bname = '') {
  712. $this->_null_block[$bname] = $str;
  713. }
  714. /**
  715. * Backwards compatibility only
  716. *
  717. * @param string $str
  718. * @param string $bname
  719. * @deprecated Change to set_null_block to keep in with rest of naming convention
  720. */
  721. public function SetNullBlock ($str, $bname = '') {
  722. $this->set_null_block($str, $bname);
  723. }
  724. /**
  725. * sets AUTORESET to 1. (default is 1)
  726. * if set to 1, parse() automatically resets the parsed blocks' sub blocks
  727. * (for multiple level blocks)
  728. *
  729. * @access public
  730. */
  731. public function set_autoreset () {
  732. $this->_autoreset = true;
  733. }
  734. /**
  735. * sets AUTORESET to 0. (default is 1)
  736. * if set to 1, parse() automatically resets the parsed blocks' sub blocks
  737. * (for multiple level blocks)
  738. *
  739. * @access public
  740. */
  741. public function clear_autoreset () {
  742. $this->_autoreset = false;
  743. }
  744. /**
  745. * scans global variables and assigns to PHP array
  746. *
  747. * @access public
  748. */
  749. public function scan_globals () {
  750. reset($GLOBALS);
  751. foreach ($GLOBALS as $k => $v) {
  752. $GLOB[$k] = $v;
  753. }
  754. /**
  755. * Access global variables as:
  756. * @example {PHP._SERVER.HTTP_HOST}
  757. * in your template!
  758. */
  759. $this->assign('PHP', $GLOB);
  760. }
  761. /**
  762. * gets error condition / string
  763. *
  764. * @access public
  765. * @return boolean / string
  766. */
  767. public function get_error () {
  768. // JRC: 3/1/2003 Added ouptut wrapper and detection of output type for error message output
  769. $retval = false;
  770. if ($this->_error != '') {
  771. switch ($this->output_type) {
  772. case 'HTML':
  773. case 'html':
  774. $retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>';
  775. break;
  776. default:
  777. $retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error);
  778. break;
  779. }
  780. }
  781. return $retval;
  782. }
  783. /***************************************************************************/
  784. /***[ private stuff ]*******************************************************/
  785. /***************************************************************************/
  786. /**
  787. * generates the array containing to-be-parsed stuff:
  788. * $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc.
  789. * also builds the reverse parse order.
  790. *
  791. * @access public - aiming for private
  792. * @param string $con content to be processed
  793. * @param string $parentblock name of the parent block in the block hierarchy
  794. */
  795. public function _maketree ($con, $parentblock='') {
  796. $blocks = array();
  797. $con2 = explode($this->block_start_delim, $con);
  798. if (!empty($parentblock)) {
  799. $block_names = explode('.', $parentblock);
  800. $level = sizeof($block_names);
  801. } else {
  802. $block_names = array();
  803. $level = 0;
  804. }
  805. // JRC 06/04/2005 Added block comments (on BEGIN or END) <!-- BEGIN: block_name#Comments placed here -->
  806. //$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)";
  807. $patt = "(" . $this->block_start_word . "|" . $this->block_end_word . ")\s*(\w+)" . $this->comment_preg . "\s*" . $this->block_end_delim . "(.*)";
  808. foreach($con2 as $k => $v) {
  809. $res = array();
  810. if (preg_match_all("/$patt/ims", $v, $res, PREG_SET_ORDER)) {
  811. // $res[0][1] = BEGIN or END
  812. // $res[0][2] = block name
  813. // $res[0][3] = comment
  814. // $res[0][4] = kinda content
  815. $block_word = $res[0][1];
  816. $block_name = $res[0][2];
  817. $comment = $res[0][3];
  818. $content = $res[0][4];
  819. if (strtoupper($block_word) == $this->block_start_word) {
  820. $parent_name = implode('.', $block_names);
  821. // add one level - array("main","table","row")
  822. $block_names[++$level] = $block_name;
  823. // make block name (main.table.row)
  824. $cur_block_name=implode('.', $block_names);
  825. // build block parsing order (reverse)
  826. $this->block_parse_order[] = $cur_block_name;
  827. //add contents. trinary operator eliminates assign error in E_ALL reporting
  828. $blocks[$cur_block_name] = isset($blocks[$cur_block_name]) ? $blocks[$cur_block_name] . $content : $content;
  829. // add {_BLOCK_.blockname} string to parent block
  830. $blocks[$parent_name] .= str_replace('\\', '', $this->tag_start_delim) . '_BLOCK_.' . $cur_block_name . str_replace('\\', '', $this->tag_end_delim);
  831. // store sub block names for autoresetting and recursive parsing
  832. $this->sub_blocks[$parent_name][] = $cur_block_name;
  833. // store sub block names for autoresetting
  834. $this->sub_blocks[$cur_block_name][] = '';
  835. } else if (strtoupper($block_word) == $this->block_end_word) {
  836. unset($block_names[$level--]);
  837. $parent_name = implode('.', $block_names);
  838. // add rest of block to parent block
  839. $blocks[$parent_name] .= $content;
  840. }
  841. } else {
  842. // no block delimiters found
  843. // Saves doing multiple implodes - less overhead
  844. $tmp = implode('.', $block_names);
  845. if ($k) {
  846. $blocks[$tmp] .= $this->block_start_delim;
  847. }
  848. // trinary operator eliminates assign error in E_ALL reporting
  849. $blocks[$tmp] = isset($blocks[$tmp]) ? $blocks[$tmp] . $v : $v;
  850. }
  851. }
  852. return $blocks;
  853. }
  854. /**
  855. * Sub processing for assign_file method
  856. *
  857. * @access private
  858. * @param string $name
  859. * @param string $val
  860. */
  861. private function _assign_file_sub ($name, $val) {
  862. if (isset($this->filevar_parent[$name])) {
  863. if ($val != '') {
  864. $val = $this->_r_getfile($val);
  865. foreach($this->filevar_parent[$name] as $parent) {
  866. if (isset($this->preparsed_blocks[$parent]) && !isset($this->filevars[$name])) {
  867. $copy = $this->preparsed_blocks[$parent];
  868. } elseif (isset($this->blocks[$parent])) {
  869. $copy = $this->blocks[$parent];
  870. }
  871. $res = array();
  872. preg_match_all($this->filevar_delim, $copy, $res, PREG_SET_ORDER);
  873. if (is_array($res) && isset($res[0])) {
  874. // Changed as per solution in SF bug ID #1261828
  875. foreach ($res as $v) {
  876. // Changed as per solution in SF bug ID #1261828
  877. if ($v[1] == $name) {
  878. // Changed as per solution in SF bug ID #1261828
  879. $copy = preg_replace("/" . preg_quote($v[0]) . "/", "$val", $copy);
  880. $this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent));
  881. $this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks));
  882. }
  883. }
  884. }
  885. }
  886. }
  887. }
  888. $this->filevars[$name] = $val;
  889. }
  890. /**
  891. * store container block's name for file variables
  892. *
  893. * @access public - aiming for private
  894. * @param array $blocks
  895. * @return array
  896. */
  897. public function _store_filevar_parents ($blocks){
  898. $parents = array();
  899. foreach ($blocks as $bname => $con) {
  900. $res = array();
  901. preg_match_all($this->filevar_delim, $con, $res);
  902. foreach ($res[1] as $k => $v) {
  903. $parents[$v][] = $bname;
  904. }
  905. }
  906. return $parents;
  907. }
  908. /**
  909. * Set the error string
  910. *
  911. * @access private
  912. * @param string $str
  913. */
  914. private function _set_error ($str) {
  915. // JRC: 3/1/2003 Made to append the error messages
  916. $this->_error .= '* ' . $str . " *\n";
  917. // JRC: 3/1/2003 Removed trigger error, use this externally if you want it eg. trigger_error($xtpl->get_error())
  918. //trigger_error($this->get_error());
  919. }
  920. /**
  921. * returns the contents of a file
  922. *
  923. * @access protected
  924. * @param string $file
  925. * @return string
  926. */
  927. protected function _getfile ($file) {
  928. if (!isset($file)) {
  929. // JC 19/12/02 added $file to error message
  930. $this->_set_error('!isset file name!' . $file);
  931. return '';
  932. }
  933. // check if filename is mapped to other filename
  934. if (isset($this->files)) {
  935. if (isset($this->files[$file])) {
  936. $file = $this->files[$file];
  937. }
  938. }
  939. // prepend template dir
  940. if (!empty($this->tpldir)) {
  941. /**
  942. * Support hierarchy of file locations to search
  943. *
  944. * @example Supply array of filepaths when instantiating
  945. * First path supplied that has the named file is prioritised
  946. * $xtpl = new XTemplate('myfile.xtpl', array('.','/mypath', '/mypath2'));
  947. * @since 29/05/2007
  948. */
  949. if (is_array($this->tpldir)) {
  950. foreach ($this->tpldir as $dir) {
  951. if (is_readable($dir . DIRECTORY_SEPARATOR . $file)) {
  952. $file = $dir . DIRECTORY_SEPARATOR . $file;
  953. break;
  954. }
  955. }
  956. } else {
  957. $file = $this->tpldir. DIRECTORY_SEPARATOR . $file;
  958. }
  959. }
  960. $file_text = '';
  961. if (isset($this->filecache[$file])) {
  962. $file_text .= $this->filecache[$file];
  963. if ($this->debug) {
  964. $file_text = '<!-- XTemplate debug cached: ' . realpath($file) . ' -->' . "\n" . $file_text;
  965. }
  966. } else {
  967. if (is_file($file) && is_readable($file)) {
  968. if (filesize($file)) {
  969. if (!($fh = fopen($file, 'r'))) {
  970. $this->_set_error('Cannot open file: ' . realpath($file));
  971. return '';
  972. }
  973. $file_text .= fread($fh,filesize($file));
  974. fclose($fh);
  975. }
  976. if ($this->debug) {
  977. $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' -->' . "\n" . $file_text;
  978. }
  979. } elseif (str_replace('.', '', phpversion()) >= '430' && $file_text = @file_get_contents($file, true)) {
  980. // Enable use of include path by using file_get_contents
  981. // Implemented at suggestion of SF Feature Request ID #1529478 michaelgroh
  982. if ($file_text === false) {
  983. $this->_set_error("[" . realpath($file) . "] ($file) does not exist");
  984. $file_text = "<b>__XTemplate fatal error: file [$file] does not exist in the include path__</b>";
  985. } elseif ($this->debug) {
  986. $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' (via include path) -->' . "\n" . $file_text;
  987. }
  988. } elseif (!is_file($file)) {
  989. // NW 17 Oct 2002 : Added realpath around the file name to identify where the code is searching.
  990. $this->_set_error("[" . realpath($file) . "] ($file) does not exist");
  991. $file_text .= "<b>__XTemplate fatal error: file [$file] does not exist__</b>";
  992. } elseif (!is_readable($file)) {
  993. $this->_set_error("[" . realpath($file) . "] ($file) is not readable");
  994. $file_text .= "<b>__XTemplate fatal error: file [$file] is not readable__</b>";
  995. }
  996. $this->filecache[$file] = $file_text;
  997. }
  998. return $file_text;
  999. }
  1000. /**
  1001. * recursively gets the content of a file with {FILE "filename.tpl"} directives
  1002. *
  1003. * @access public - aiming for private
  1004. * @param string $file
  1005. * @return string
  1006. */
  1007. public function _r_getfile ($file) {
  1008. $text = $this->_getfile($file);
  1009. $res = array();
  1010. while (preg_match($this->file_delim,$text,$res)) {
  1011. $text2 = $this->_getfile($res[1]);
  1012. $text = preg_replace("'".preg_quote($res[0])."'",$text2,$text);
  1013. }
  1014. return $text;
  1015. }
  1016. /**
  1017. * add an outer block delimiter set useful for rtfs etc - keeps them editable in word
  1018. *
  1019. * @access private
  1020. */
  1021. private function _add_outer_block () {
  1022. $before = $this->block_start_delim . $this->block_start_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
  1023. $after = $this->block_start_delim . $this->block_end_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
  1024. $this->filecontents = $before . "\n" . $this->filecontents . "\n" . $after;
  1025. }
  1026. /**
  1027. * Debug function - var_dump wrapped in '<pre></pre>' tags
  1028. *
  1029. * @access private
  1030. * @param multiple var_dumps all the supplied arguments
  1031. */
  1032. private function _pre_var_dump ($args) {
  1033. if ($this->debug) {
  1034. echo '<pre>';
  1035. var_dump(func_get_args());
  1036. echo '</pre>';
  1037. }
  1038. }
  1039. } /* end of XTemplate class. */
  1040. ?>