xtemplate.class.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. if ($var !== null) {
  501. $var = trim($var);
  502. }else{
  503. $var = "";
  504. }
  505. switch (true) {
  506. case preg_match('/^\n/', $var) && preg_match('/\n$/', $var):
  507. $var = substr($var, 1, -1);
  508. break;
  509. case preg_match('/^\n/', $var):
  510. $var = substr($var, 1);
  511. break;
  512. case preg_match('/\n$/', $var):
  513. $var = substr($var, 0, -1);
  514. break;
  515. }
  516. // SF Bug no. 810773 - thanks anonymous
  517. $var = str_replace('\\', '\\\\', $var);
  518. // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04
  519. $var = str_replace('$', '\\$', $var);
  520. // Replaced str_replaces with preg_quote
  521. //$var = preg_quote($var);
  522. $var = str_replace('\\|', '|', $var);
  523. $copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", "$var", $copy);
  524. if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) {
  525. $copy = substr($copy, 1, -1);
  526. }
  527. }
  528. } else {
  529. $var = $this->vars;
  530. foreach ($sub as $v1) {
  531. // NW 4 Oct 2002 - Added isset and is_array check to avoid NOTICE messages
  532. // JC 17 Oct 2002 - Changed EMPTY to stlen=0
  533. // if (empty($var[$v1])) { // this line would think that zeros(0) were empty - which is not true
  534. if (!isset($var[$v1]) || (!is_array($var[$v1]) && strlen($var[$v1]) == 0)) {
  535. // Check for constant, when variable not assigned
  536. if (defined($v1)) {
  537. $var[$v1] = constant($v1);
  538. } else {
  539. $var[$v1] = null;
  540. }
  541. }
  542. $var = $var[$v1];
  543. }
  544. $nul = (!isset($this->_null_string[$v])) ? ($this->_null_string[""]) : ($this->_null_string[$v]);
  545. $var = (!isset($var)) ? $nul : $var;
  546. if ($var === '') {
  547. // -----------------------------------------------------------
  548. // Removed requriement for blocks to be at the start of string
  549. // -----------------------------------------------------------
  550. // $copy=preg_replace("|^\s*\{".$v." ?#?".$comments."\}\s*\n|m","",$copy);
  551. $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", '', $copy);
  552. }
  553. $var = trim($var);
  554. // SF Bug no. 810773 - thanks anonymous
  555. $var = str_replace('\\', '\\\\', $var);
  556. // Ensure dollars in strings are not evaluated reported by SadGeezer 31/3/04
  557. $var = str_replace('$', '\\$', $var);
  558. // Replace str_replaces with preg_quote
  559. //$var = preg_quote($var);
  560. $var = str_replace('\\|', '|', $var);
  561. $copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", "$var", $copy);
  562. if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) {
  563. $copy = substr($copy, 1);
  564. }
  565. }
  566. }
  567. if (isset($this->parsed_blocks[$bname])) {
  568. $this->parsed_blocks[$bname] .= $copy;
  569. } else {
  570. $this->parsed_blocks[$bname] = $copy;
  571. }
  572. /* reset sub-blocks */
  573. if ($this->_autoreset && (!empty($this->sub_blocks[$bname]))) {
  574. reset($this->sub_blocks[$bname]);
  575. foreach ($this->sub_blocks[$bname] as $k => $v) {
  576. $this->reset($v);
  577. }
  578. }
  579. }
  580. /**
  581. * returns the parsed text for a block, including all sub-blocks.
  582. *
  583. * @access public
  584. * @param string $bname Block name to parse
  585. */
  586. public function rparse ($bname) {
  587. if (!empty($this->sub_blocks[$bname])) {
  588. reset($this->sub_blocks[$bname]);
  589. foreach ($this->sub_blocks[$bname] as $k => $v) {
  590. if (!empty($v)) {
  591. $this->rparse($v);
  592. }
  593. }
  594. }
  595. $this->parse($bname);
  596. }
  597. /**
  598. * inserts a loop ( call assign & parse )
  599. *
  600. * @access public
  601. * @param string $bname Block name to assign
  602. * @param string $var Variable to assign values to
  603. * @param string / array $value Value to assign to $var
  604. */
  605. public function insert_loop ($bname, $var, $value = '') {
  606. $this->assign($var, $value);
  607. $this->parse($bname);
  608. }
  609. /**
  610. * parses a block for every set of data in the values array
  611. *
  612. * @access public
  613. * @param string $bname Block name to loop
  614. * @param string $var Variable to assign values to
  615. * @param array $values Values to assign to $var
  616. */
  617. public function array_loop ($bname, $var, &$values) {
  618. if (is_array($values)) {
  619. foreach($values as $v) {
  620. $this->insert_loop($bname, $var, $v);
  621. }
  622. }
  623. }
  624. /**
  625. * returns the parsed text for a block
  626. *
  627. * @access public
  628. * @param string $bname Block name to return
  629. * @return string
  630. */
  631. public function text ($bname = '') {
  632. $text = '';
  633. if ($this->debug && $this->output_type == 'HTML') {
  634. // JC 20/11/02 echo the template filename if in development as
  635. // html comment
  636. $text .= '<!-- XTemplate: ' . realpath($this->filename) . " -->\n";
  637. }
  638. $bname = !empty($bname) ? $bname : $this->mainblock;
  639. $text .= isset($this->parsed_blocks[$bname]) ? $this->parsed_blocks[$bname] : $this->get_error();
  640. return str_replace(array("\r", "\n"), '', $text);//my edit
  641. //return $text;
  642. }
  643. /**
  644. * prints the parsed text
  645. *
  646. * @access public
  647. * @param string $bname Block name to echo out
  648. */
  649. public function out ($bname) {
  650. $out = $this->text($bname);
  651. // $length=strlen($out);
  652. //header("Content-Length: ".$length); // TODO: Comment this back in later
  653. echo $out;
  654. }
  655. /**
  656. * prints the parsed text to a specified file
  657. *
  658. * @access public
  659. * @param string $bname Block name to write out
  660. * @param string $fname File name to write to
  661. */
  662. public function out_file ($bname, $fname) {
  663. if (!empty($bname) && !empty($fname) && is_writeable($fname)) {
  664. $fp = fopen($fname, 'w');
  665. fwrite($fp, $this->text($bname));
  666. fclose($fp);
  667. }
  668. }
  669. /**
  670. * resets the parsed text
  671. *
  672. * @access public
  673. * @param string $bname Block to reset
  674. */
  675. public function reset ($bname) {
  676. $this->parsed_blocks[$bname] = '';
  677. }
  678. /**
  679. * returns true if block was parsed, false if not
  680. *
  681. * @access public
  682. * @param string $bname Block name to test
  683. * @return boolean
  684. */
  685. public function parsed ($bname) {
  686. return (!empty($this->parsed_blocks[$bname]));
  687. }
  688. /**
  689. * sets the string to replace in case the var was not assigned
  690. *
  691. * @access public
  692. * @param string $str Display string for null block
  693. * @param string $varname Variable name to apply $str to
  694. */
  695. public function set_null_string($str, $varname = '') {
  696. $this->_null_string[$varname] = $str;
  697. }
  698. /**
  699. * Backwards compatibility only
  700. *
  701. * @param string $str
  702. * @param string $varname
  703. * @deprecated Change to set_null_string to keep in with rest of naming convention
  704. */
  705. public function SetNullString ($str, $varname = '') {
  706. $this->set_null_string($str, $varname);
  707. }
  708. /**
  709. * sets the string to replace in case the block was not parsed
  710. *
  711. * @access public
  712. * @param string $str Display string for null block
  713. * @param string $bname Block name to apply $str to
  714. */
  715. public function set_null_block ($str, $bname = '') {
  716. $this->_null_block[$bname] = $str;
  717. }
  718. /**
  719. * Backwards compatibility only
  720. *
  721. * @param string $str
  722. * @param string $bname
  723. * @deprecated Change to set_null_block to keep in with rest of naming convention
  724. */
  725. public function SetNullBlock ($str, $bname = '') {
  726. $this->set_null_block($str, $bname);
  727. }
  728. /**
  729. * sets AUTORESET to 1. (default is 1)
  730. * if set to 1, parse() automatically resets the parsed blocks' sub blocks
  731. * (for multiple level blocks)
  732. *
  733. * @access public
  734. */
  735. public function set_autoreset () {
  736. $this->_autoreset = true;
  737. }
  738. /**
  739. * sets AUTORESET to 0. (default is 1)
  740. * if set to 1, parse() automatically resets the parsed blocks' sub blocks
  741. * (for multiple level blocks)
  742. *
  743. * @access public
  744. */
  745. public function clear_autoreset () {
  746. $this->_autoreset = false;
  747. }
  748. /**
  749. * scans global variables and assigns to PHP array
  750. *
  751. * @access public
  752. */
  753. public function scan_globals () {
  754. /*reset($GLOBALS);
  755. foreach ($GLOBALS as $k => $v) {
  756. $GLOB[$k] = $v;
  757. }*/
  758. $my_globals = $GLOBALS;
  759. reset($my_globals);
  760. foreach ($GLOBALS as $k => $v) {
  761. $GLOB[$k] = $v;
  762. }
  763. /**
  764. * Access global variables as:
  765. * @example {PHP._SERVER.HTTP_HOST}
  766. * in your template!
  767. */
  768. $this->assign('PHP', $GLOB);
  769. }
  770. /**
  771. * gets error condition / string
  772. *
  773. * @access public
  774. * @return boolean / string
  775. */
  776. public function get_error () {
  777. // JRC: 3/1/2003 Added ouptut wrapper and detection of output type for error message output
  778. $retval = false;
  779. if ($this->_error != '') {
  780. switch ($this->output_type) {
  781. case 'HTML':
  782. case 'html':
  783. $retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>';
  784. break;
  785. default:
  786. $retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error);
  787. break;
  788. }
  789. }
  790. return $retval;
  791. }
  792. /***************************************************************************/
  793. /***[ private stuff ]*******************************************************/
  794. /***************************************************************************/
  795. /**
  796. * generates the array containing to-be-parsed stuff:
  797. * $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc.
  798. * also builds the reverse parse order.
  799. *
  800. * @access public - aiming for private
  801. * @param string $con content to be processed
  802. * @param string $parentblock name of the parent block in the block hierarchy
  803. */
  804. public function _maketree ($con, $parentblock='') {
  805. $blocks = array();
  806. $con2 = explode($this->block_start_delim, $con);
  807. if (!empty($parentblock)) {
  808. $block_names = explode('.', $parentblock);
  809. $level = sizeof($block_names);
  810. } else {
  811. $block_names = array();
  812. $level = 0;
  813. }
  814. // JRC 06/04/2005 Added block comments (on BEGIN or END) <!-- BEGIN: block_name#Comments placed here -->
  815. //$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)";
  816. $patt = "(" . $this->block_start_word . "|" . $this->block_end_word . ")\s*(\w+)" . $this->comment_preg . "\s*" . $this->block_end_delim . "(.*)";
  817. foreach($con2 as $k => $v) {
  818. $res = array();
  819. if (preg_match_all("/$patt/ims", $v, $res, PREG_SET_ORDER)) {
  820. // $res[0][1] = BEGIN or END
  821. // $res[0][2] = block name
  822. // $res[0][3] = comment
  823. // $res[0][4] = kinda content
  824. $block_word = $res[0][1];
  825. $block_name = $res[0][2];
  826. $comment = $res[0][3];
  827. $content = $res[0][4];
  828. if (strtoupper($block_word) == $this->block_start_word) {
  829. $parent_name = implode('.', $block_names);
  830. // add one level - array("main","table","row")
  831. $block_names[++$level] = $block_name;
  832. // make block name (main.table.row)
  833. $cur_block_name=implode('.', $block_names);
  834. // build block parsing order (reverse)
  835. $this->block_parse_order[] = $cur_block_name;
  836. //add contents. trinary operator eliminates assign error in E_ALL reporting
  837. $blocks[$cur_block_name] = isset($blocks[$cur_block_name]) ? $blocks[$cur_block_name] . $content : $content;
  838. // add {_BLOCK_.blockname} string to parent block
  839. $blocks[$parent_name] .= str_replace('\\', '', $this->tag_start_delim) . '_BLOCK_.' . $cur_block_name . str_replace('\\', '', $this->tag_end_delim);
  840. // store sub block names for autoresetting and recursive parsing
  841. $this->sub_blocks[$parent_name][] = $cur_block_name;
  842. // store sub block names for autoresetting
  843. $this->sub_blocks[$cur_block_name][] = '';
  844. } else if (strtoupper($block_word) == $this->block_end_word) {
  845. unset($block_names[$level--]);
  846. $parent_name = implode('.', $block_names);
  847. // add rest of block to parent block
  848. $blocks[$parent_name] .= $content;
  849. }
  850. } else {
  851. // no block delimiters found
  852. // Saves doing multiple implodes - less overhead
  853. $tmp = implode('.', $block_names);
  854. if ($k) {
  855. $blocks[$tmp] .= $this->block_start_delim;
  856. }
  857. // trinary operator eliminates assign error in E_ALL reporting
  858. $blocks[$tmp] = isset($blocks[$tmp]) ? $blocks[$tmp] . $v : $v;
  859. }
  860. }
  861. return $blocks;
  862. }
  863. /**
  864. * Sub processing for assign_file method
  865. *
  866. * @access private
  867. * @param string $name
  868. * @param string $val
  869. */
  870. private function _assign_file_sub ($name, $val) {
  871. if (isset($this->filevar_parent[$name])) {
  872. if ($val != '') {
  873. $val = $this->_r_getfile($val);
  874. foreach($this->filevar_parent[$name] as $parent) {
  875. if (isset($this->preparsed_blocks[$parent]) && !isset($this->filevars[$name])) {
  876. $copy = $this->preparsed_blocks[$parent];
  877. } elseif (isset($this->blocks[$parent])) {
  878. $copy = $this->blocks[$parent];
  879. }
  880. $res = array();
  881. preg_match_all($this->filevar_delim, $copy, $res, PREG_SET_ORDER);
  882. if (is_array($res) && isset($res[0])) {
  883. // Changed as per solution in SF bug ID #1261828
  884. foreach ($res as $v) {
  885. // Changed as per solution in SF bug ID #1261828
  886. if ($v[1] == $name) {
  887. // Changed as per solution in SF bug ID #1261828
  888. $copy = preg_replace("/" . preg_quote($v[0]) . "/", "$val", $copy);
  889. $this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent));
  890. $this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks));
  891. }
  892. }
  893. }
  894. }
  895. }
  896. }
  897. $this->filevars[$name] = $val;
  898. }
  899. /**
  900. * store container block's name for file variables
  901. *
  902. * @access public - aiming for private
  903. * @param array $blocks
  904. * @return array
  905. */
  906. public function _store_filevar_parents ($blocks){
  907. $parents = array();
  908. foreach ($blocks as $bname => $con) {
  909. $res = array();
  910. preg_match_all($this->filevar_delim, $con, $res);
  911. foreach ($res[1] as $k => $v) {
  912. $parents[$v][] = $bname;
  913. }
  914. }
  915. return $parents;
  916. }
  917. /**
  918. * Set the error string
  919. *
  920. * @access private
  921. * @param string $str
  922. */
  923. private function _set_error ($str) {
  924. // JRC: 3/1/2003 Made to append the error messages
  925. $this->_error .= '* ' . $str . " *\n";
  926. // JRC: 3/1/2003 Removed trigger error, use this externally if you want it eg. trigger_error($xtpl->get_error())
  927. //trigger_error($this->get_error());
  928. }
  929. /**
  930. * returns the contents of a file
  931. *
  932. * @access protected
  933. * @param string $file
  934. * @return string
  935. */
  936. protected function _getfile ($file) {
  937. if (!isset($file)) {
  938. // JC 19/12/02 added $file to error message
  939. $this->_set_error('!isset file name!' . $file);
  940. return '';
  941. }
  942. // check if filename is mapped to other filename
  943. if (isset($this->files)) {
  944. if (isset($this->files[$file])) {
  945. $file = $this->files[$file];
  946. }
  947. }
  948. // prepend template dir
  949. if (!empty($this->tpldir)) {
  950. /**
  951. * Support hierarchy of file locations to search
  952. *
  953. * @example Supply array of filepaths when instantiating
  954. * First path supplied that has the named file is prioritised
  955. * $xtpl = new XTemplate('myfile.xtpl', array('.','/mypath', '/mypath2'));
  956. * @since 29/05/2007
  957. */
  958. if (is_array($this->tpldir)) {
  959. foreach ($this->tpldir as $dir) {
  960. if (is_readable($dir . DIRECTORY_SEPARATOR . $file)) {
  961. $file = $dir . DIRECTORY_SEPARATOR . $file;
  962. break;
  963. }
  964. }
  965. } else {
  966. $file = $this->tpldir. DIRECTORY_SEPARATOR . $file;
  967. }
  968. }
  969. $file_text = '';
  970. if (isset($this->filecache[$file])) {
  971. $file_text .= $this->filecache[$file];
  972. if ($this->debug) {
  973. $file_text = '<!-- XTemplate debug cached: ' . realpath($file) . ' -->' . "\n" . $file_text;
  974. }
  975. } else {
  976. if (is_file($file) && is_readable($file)) {
  977. if (filesize($file)) {
  978. if (!($fh = fopen($file, 'r'))) {
  979. $this->_set_error('Cannot open file: ' . realpath($file));
  980. return '';
  981. }
  982. $file_text .= fread($fh,filesize($file));
  983. fclose($fh);
  984. }
  985. if ($this->debug) {
  986. $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' -->' . "\n" . $file_text;
  987. }
  988. } elseif (str_replace('.', '', phpversion()) >= '430' && $file_text = @file_get_contents($file, true)) {
  989. // Enable use of include path by using file_get_contents
  990. // Implemented at suggestion of SF Feature Request ID #1529478 michaelgroh
  991. if ($file_text === false) {
  992. $this->_set_error("[" . realpath($file) . "] ($file) does not exist");
  993. $file_text = "<b>__XTemplate fatal error: file [$file] does not exist in the include path__</b>";
  994. } elseif ($this->debug) {
  995. $file_text = '<!-- XTemplate debug: ' . realpath($file) . ' (via include path) -->' . "\n" . $file_text;
  996. }
  997. } elseif (!is_file($file)) {
  998. // NW 17 Oct 2002 : Added realpath around the file name to identify where the code is searching.
  999. $this->_set_error("[" . realpath($file) . "] ($file) does not exist");
  1000. $file_text .= "<b>__XTemplate fatal error: file [$file] does not exist__</b>";
  1001. } elseif (!is_readable($file)) {
  1002. $this->_set_error("[" . realpath($file) . "] ($file) is not readable");
  1003. $file_text .= "<b>__XTemplate fatal error: file [$file] is not readable__</b>";
  1004. }
  1005. $this->filecache[$file] = $file_text;
  1006. }
  1007. return $file_text;
  1008. }
  1009. /**
  1010. * recursively gets the content of a file with {FILE "filename.tpl"} directives
  1011. *
  1012. * @access public - aiming for private
  1013. * @param string $file
  1014. * @return string
  1015. */
  1016. public function _r_getfile ($file) {
  1017. $text = $this->_getfile($file);
  1018. $res = array();
  1019. while (preg_match($this->file_delim,$text,$res)) {
  1020. $text2 = $this->_getfile($res[1]);
  1021. $text = preg_replace("'".preg_quote($res[0])."'",$text2,$text);
  1022. }
  1023. return $text;
  1024. }
  1025. /**
  1026. * add an outer block delimiter set useful for rtfs etc - keeps them editable in word
  1027. *
  1028. * @access private
  1029. */
  1030. private function _add_outer_block () {
  1031. $before = $this->block_start_delim . $this->block_start_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
  1032. $after = $this->block_start_delim . $this->block_end_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
  1033. $this->filecontents = $before . "\n" . $this->filecontents . "\n" . $after;
  1034. }
  1035. /**
  1036. * Debug function - var_dump wrapped in '<pre></pre>' tags
  1037. *
  1038. * @access private
  1039. * @param multiple var_dumps all the supplied arguments
  1040. */
  1041. private function _pre_var_dump ($args) {
  1042. if ($this->debug) {
  1043. echo '<pre>';
  1044. var_dump(func_get_args());
  1045. echo '</pre>';
  1046. }
  1047. }
  1048. } /* end of XTemplate class. */
  1049. ?>