PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/XTemplate/xtpl.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 569 lines | 444 code | 8 blank | 117 comment | 0 complexity | 4c82558a01dd18fa7b0c13fb35f3480c MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. Stas 2010-12-20 Added 'VERSION_MARK' to templates
  5. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  6. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  7. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  8. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  9. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  10. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  11. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  12. r36874 - 2008-06-19 12:09:05 -0700 (Thu, 19 Jun 2008) - roger - bug 22568: use md5 of unique key and version for js key.
  13. r32524 - 2008-03-06 15:51:02 -0800 (Thu, 06 Mar 2008) - dwong - Fix incorrect encoding on source code caused by IDE, e.g.
  14. utils.php r3048
  15. InboundEmail.php r17199
  16. header.php r13729
  17. r30876 - 2008-01-09 19:01:57 -0800 (Wed, 09 Jan 2008) - majed - initial check in for instances
  18. r29571 - 2007-11-13 10:49:09 -0800 (Tue, 13 Nov 2007) - eddy - Bug 17113
  19. Added check for array element prior to accessing it.
  20. XTemplate/xtpl.php
  21. r26822 - 2007-09-18 10:20:27 -0700 (Tue, 18 Sep 2007) - tswicegood - Refactor a bunch of the loops and such.
  22. This code doesn't have a future in Sugar, but there are some legacy
  23. areas that still rely on it. Refactoring these few areas cuts its impact
  24. on Sugar's main page by 40% (6.14% to 3.72%).
  25. r26819 - 2007-09-18 10:07:01 -0700 (Tue, 18 Sep 2007) - tswicegood - Reduces this execution time relatively by 25%
  26. r25238 - 2007-08-07 15:40:32 -0700 (Tue, 07 Aug 2007) - dwheeler - Bug 14129. Removed field from vardefs as it is no longer used, and should not be visible from mass update.
  27. r18355 - 2006-12-05 17:00:55 -0800 (Tue, 05 Dec 2006) - jenny - Bug 10292 - checking to see if we actually have an array before setting the array values.
  28. r13627 - 2006-05-31 11:01:53 -0700 (Wed, 31 May 2006) - majed - name change
  29. r12024 - 2006-03-09 23:42:27 -0800 (Thu, 09 Mar 2006) - majed - fixes bugs 4449 5050 4063 4976 4770
  30. r11291 - 2006-01-22 10:41:45 -0800 (Sun, 22 Jan 2006) - andrew - Removed the 'Log' CVS keyword.
  31. r10797 - 2005-12-21 18:10:38 -0800 (Wed, 21 Dec 2005) - wayne - sugar_version and js_custom_version xtpl assignment now in xtpl.php
  32. r9351 - 2005-11-15 15:39:37 -0800 (Tue, 15 Nov 2005) - andrew - Added another check for the $focus that needs to be in for PHP 5.0.3.
  33. r9270 - 2005-11-11 15:08:19 -0800 (Fri, 11 Nov 2005) - majed - Adds support for emails email marketing and email templates
  34. r8555 - 2005-10-19 12:26:13 -0700 (Wed, 19 Oct 2005) - majed - adds initial acl support
  35. r8508 - 2005-10-17 17:23:04 -0700 (Mon, 17 Oct 2005) - majed - adds initial acl support
  36. r5820 - 2005-06-21 14:22:24 -0700 (Tue, 21 Jun 2005) - majed - fixes issues with nusoap and with custom fields
  37. r4920 - 2005-04-29 00:38:19 -0700 (Fri, 29 Apr 2005) - jacob - Preventing conversion of array to string.
  38. r4743 - 2005-04-27 00:57:27 -0700 (Wed, 27 Apr 2005) - jacob - Adding support for "parsing" sections that do not exist in HTML. This provides backwards compatibility for old HTML files with new PHP files.
  39. r2016 - 2004-12-28 15:19:29 -0800 (Tue, 28 Dec 2004) - majed - added a function to scan through a block checking for a given variable
  40. r1228 - 2004-10-20 02:09:09 -0700 (Wed, 20 Oct 2004) - lam - update
  41. r1211 - 2004-10-19 21:55:03 -0700 (Tue, 19 Oct 2004) - lam - update
  42. r730 - 2004-09-09 20:14:02 -0700 (Thu, 09 Sep 2004) - sugarjacob - Cleaning up blanks
  43. r462 - 2004-08-25 17:43:37 -0700 (Wed, 25 Aug 2004) - sugarmsi - added an exists method to check if a block exists in a template
  44. r397 - 2004-08-08 02:28:36 -0700 (Sun, 08 Aug 2004) - sugarjacob - Fix: XTemplate changed to use <?php script declarations
  45. r297 - 2004-07-31 15:13:23 -0700 (Sat, 31 Jul 2004) - sugarjacob - Removing default setting of template language arrays.
  46. r295 - 2004-07-31 14:37:38 -0700 (Sat, 31 Jul 2004) - sugarjacob - Adding code to automatically assign the language strings to every template created.
  47. r268 - 2004-07-16 01:21:57 -0700 (Fri, 16 Jul 2004) - sugarjacob - Changing the XTemplate replacement mechanism to allow for '$' in the text being substituted.
  48. r80 - 2004-06-11 16:39:47 -0700 (Fri, 11 Jun 2004) - sugarjacob - Fixing issue with a variable not being an array in some cases.
  49. r78 - 2004-06-11 16:34:17 -0700 (Fri, 11 Jun 2004) - sugarjacob - Removing errors or notices about invalid indexs.
  50. r3 - 2004-05-26 22:30:56 -0700 (Wed, 26 May 2004) - sugarjacob - Moving project to SourceForge.
  51. */
  52. class XTemplate {
  53. /*
  54. xtemplate class 0.2.4-3
  55. html generation with templates - fast & easy
  56. copyright (c) 2000 barnabás debreceni [cranx@users.sourceforge.net]
  57. code optimization by Ivar Smolin <okul@linux.ee> 14-march-2001
  58. latest stable & CVS version always available @ http://sourceforge.net/projects/xtpl
  59. tested with php 3.0.11 and 4.0.4pl1
  60. This program is free software; you can redistribute it and/or
  61. modify it under the terms of the GNU Lesser General Public License
  62. version 2.1 as published by the Free Software Foundation.
  63. This library is distributed in the hope that it will be useful,
  64. but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. GNU Lesser General Public License for more details at
  67. http://www.gnu.org/copyleft/lgpl.html
  68. You should have received a copy of the GNU General Public License
  69. along with this program; if not, write to the Free Software
  70. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  71. */
  72. /***[ variables ]***********************************************************/
  73. var $filecontents=""; /* raw contents of template file */
  74. var $blocks=array(); /* unparsed blocks */
  75. var $parsed_blocks=array(); /* parsed blocks */
  76. var $block_parse_order=array(); /* block parsing order for recursive parsing (sometimes reverse:) */
  77. var $sub_blocks=array(); /* store sub-block names for fast resetting */
  78. var $VARS=array(); /* variables array */
  79. var $alternate_include_directory = "";
  80. var $file_delim="/\{FILE\s*\"([^\"]+)\"\s*\}/m"; /* regexp for file includes */
  81. var $block_start_delim="<!-- "; /* block start delimiter */
  82. var $block_end_delim="-->"; /* block end delimiter */
  83. var $block_start_word="BEGIN:"; /* block start word */
  84. var $block_end_word="END:"; /* block end word */
  85. /* this makes the delimiters look like: <!-- BEGIN: block_name --> if you use my syntax. */
  86. var $NULL_STRING=array(""=>""); /* null string for unassigned vars */
  87. var $NULL_BLOCK=array(""=>""); /* null string for unassigned blocks */
  88. var $mainblock="";
  89. var $ERROR="";
  90. var $AUTORESET=1; /* auto-reset sub blocks */
  91. /***[ constructor ]*********************************************************/
  92. function XTemplate ($file, $alt_include = "", $mainblock="main") {
  93. $this->alternate_include_directory = $alt_include;
  94. $this->mainblock=$mainblock;
  95. $this->filecontents=$this->r_getfile($file); /* read in template file */
  96. //if(substr_count($file, 'backup') == 1)_ppd($this->filecontents);
  97. $this->blocks=$this->maketree($this->filecontents,$mainblock); /* preprocess some stuff */
  98. //$this->scan_globals();
  99. }
  100. /***************************************************************************/
  101. /***[ public stuff ]********************************************************/
  102. /***************************************************************************/
  103. /***[ assign ]**************************************************************/
  104. /*
  105. assign a variable
  106. */
  107. function assign ($name,$val="") {
  108. if (is_array($name)) {
  109. foreach ($name as $k => $v) {
  110. $this->VARS[$k] = $v;
  111. }
  112. } else {
  113. $this->VARS[$name]=$val;
  114. }
  115. }
  116. function append ($varname, $name,$val="") {
  117. if(!isset($this->VARS[$varname])){
  118. $this->VARS[$varname] = array();
  119. }
  120. if(is_array($this->VARS[$varname])){
  121. $this->VARS[$varname][$name] = $val;
  122. }
  123. }
  124. /***[ parse ]***************************************************************/
  125. /*
  126. parse a block
  127. */
  128. function parse ($bname) {
  129. global $sugar_version, $sugar_config;
  130. $this->assign('SUGAR_VERSION', $GLOBALS['js_version_key']);
  131. $this->assign('JS_CUSTOM_VERSION', $sugar_config['js_custom_version']);
  132. $this->assign('VERSION_MARK', getVersionedPath(''));
  133. if(empty($this->blocks[$bname]))
  134. return;
  135. $copy=$this->blocks[$bname];
  136. if (!isset($this->blocks[$bname]))
  137. $this->set_error ("parse: blockname [$bname] does not exist");
  138. preg_match_all("/\{([A-Za-z0-9\._]+?)}/",$this->blocks[$bname],$var_array);
  139. $var_array=$var_array[1];
  140. foreach ($var_array as $k => $v) {
  141. $sub=explode(".",$v);
  142. if ($sub[0]=="_BLOCK_") {
  143. unset($sub[0]);
  144. $bname2=implode(".",$sub);
  145. if(isset($this->parsed_blocks[$bname2]))
  146. {
  147. $var=$this->parsed_blocks[$bname2];
  148. }
  149. else
  150. {
  151. $var = null;
  152. }
  153. $nul=(!isset($this->NULL_BLOCK[$bname2])) ? $this->NULL_BLOCK[""] : $this->NULL_BLOCK[$bname2];
  154. $var=(empty($var))?$nul:trim($var);
  155. // Commented out due to regular expression issue with '$' in replacement string.
  156. //$copy=preg_replace("/\{".$v."\}/","$var",$copy);
  157. // This should be faster and work better for '$'
  158. $copy=str_replace("{".$v."}",$var,$copy);
  159. } else {
  160. $var=$this->VARS;
  161. foreach ($sub as $k1 => $v1)
  162. {
  163. if(is_array($var) && isset($var[$v1]))
  164. {
  165. $var=$var[$v1];
  166. }
  167. else
  168. {
  169. $var = null;
  170. }
  171. }
  172. $nul=(!isset($this->NULL_STRING[$v])) ? ($this->NULL_STRING[""]) : ($this->NULL_STRING[$v]);
  173. $var=(!isset($var))?$nul:$var;
  174. // Commented out due to regular expression issue with '$' in replacement string.
  175. //$copy=preg_replace("/\{$v\}/","$var",$copy);
  176. // This should be faster and work better for '$'
  177. // this was periodically returning an array to string conversion error....
  178. if(!is_array($var))
  179. {
  180. $copy=str_replace("{".$v."}",$var,$copy);
  181. }
  182. }
  183. }
  184. if(isset($this->parsed_blocks[$bname]))
  185. {
  186. $this->parsed_blocks[$bname].=$copy;
  187. }
  188. else
  189. {
  190. $this->parsed_blocks[$bname]=$copy;
  191. }
  192. // reset sub-blocks
  193. if ($this->AUTORESET && (!empty($this->sub_blocks[$bname]))) {
  194. reset($this->sub_blocks[$bname]);
  195. foreach ($this->sub_blocks[$bname] as $v)
  196. $this->reset($v);
  197. }
  198. }
  199. /***[ exists ]**************************************************************/
  200. /*
  201. returns true if a block exists otherwise returns false.
  202. */
  203. function exists($bname){
  204. return (!empty($this->parsed_blocks[$bname])) || (!empty($this->blocks[$bname]));
  205. }
  206. /***[ var_exists ]**************************************************************/
  207. /*
  208. returns true if a block exists otherwise returns false.
  209. */
  210. function var_exists($bname,$vname){
  211. if(!empty($this->blocks[$bname])){
  212. return substr_count($this->blocks[$bname], '{'. $vname . '}') >0;
  213. }
  214. return false;
  215. }
  216. /***[ rparse ]**************************************************************/
  217. /*
  218. returns the parsed text for a block, including all sub-blocks.
  219. */
  220. function rparse($bname) {
  221. if (!empty($this->sub_blocks[$bname])) {
  222. reset($this->sub_blocks[$bname]);
  223. while (list($k,$v)=each($this->sub_blocks[$bname]))
  224. if (!empty($v))
  225. $this->rparse($v,$indent."\t");
  226. }
  227. $this->parse($bname);
  228. }
  229. /***[ insert_loop ]*********************************************************/
  230. /*
  231. inserts a loop ( call assign & parse )
  232. */
  233. function insert_loop($bname,$var,$value="") {
  234. $this->assign($var,$value);
  235. $this->parse($bname);
  236. }
  237. /***[ text ]****************************************************************/
  238. /*
  239. returns the parsed text for a block
  240. */
  241. function text($bname) {
  242. if(!empty($this->parsed_blocks)){
  243. return $this->parsed_blocks[isset($bname) ? $bname :$this->mainblock];
  244. }else{
  245. return '';
  246. }
  247. }
  248. /***[ out ]*****************************************************************/
  249. /*
  250. prints the parsed text
  251. */
  252. function out ($bname) {
  253. global $focus;
  254. if(isset($focus)){
  255. global $action;
  256. if($focus && is_subclass_of($focus, 'SugarBean') && !$focus->ACLAccess($action)){
  257. ACLController::displayNoAccess(true);
  258. sugar_die('');
  259. return;
  260. }}
  261. echo $this->text($bname);
  262. }
  263. /***[ reset ]***************************************************************/
  264. /*
  265. resets the parsed text
  266. */
  267. function reset ($bname) {
  268. $this->parsed_blocks[$bname]="";
  269. }
  270. /***[ parsed ]**************************************************************/
  271. /*
  272. returns true if block was parsed, false if not
  273. */
  274. function parsed ($bname) {
  275. return (!empty($this->parsed_blocks[$bname]));
  276. }
  277. /***[ SetNullString ]*******************************************************/
  278. /*
  279. sets the string to replace in case the var was not assigned
  280. */
  281. function SetNullString($str,$varname="") {
  282. $this->NULL_STRING[$varname]=$str;
  283. }
  284. /***[ SetNullBlock ]********************************************************/
  285. /*
  286. sets the string to replace in case the block was not parsed
  287. */
  288. function SetNullBlock($str,$bname="") {
  289. $this->NULL_BLOCK[$bname]=$str;
  290. }
  291. /***[ set_autoreset ]*******************************************************/
  292. /*
  293. sets AUTORESET to 1. (default is 1)
  294. if set to 1, parse() automatically resets the parsed blocks' sub blocks
  295. (for multiple level blocks)
  296. */
  297. function set_autoreset() {
  298. $this->AUTORESET=1;
  299. }
  300. /***[ clear_autoreset ]*****************************************************/
  301. /*
  302. sets AUTORESET to 0. (default is 1)
  303. if set to 1, parse() automatically resets the parsed blocks' sub blocks
  304. (for multiple level blocks)
  305. */
  306. function clear_autoreset() {
  307. $this->AUTORESET=0;
  308. }
  309. /***[ scan_globals ]********************************************************/
  310. /*
  311. scans global variables
  312. */
  313. function scan_globals() {
  314. reset($GLOBALS);
  315. while (list($k,$v)=each($GLOBALS))
  316. $GLOB[$k]=$v;
  317. $this->assign("PHP",$GLOB); /* access global variables as {PHP.HTTP_HOST} in your template! */
  318. }
  319. /******
  320. WARNING
  321. PUBLIC FUNCTIONS BELOW THIS LINE DIDN'T GET TESTED
  322. ******/
  323. /***************************************************************************/
  324. /***[ private stuff ]*******************************************************/
  325. /***************************************************************************/
  326. /***[ maketree ]************************************************************/
  327. /*
  328. generates the array containing to-be-parsed stuff:
  329. $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc.
  330. also builds the reverse parse order.
  331. */
  332. function maketree($con,$block) {
  333. $con2=explode($this->block_start_delim,$con);
  334. $level=0;
  335. $block_names=array();
  336. $blocks=array();
  337. reset($con2);
  338. while(list($k,$v)=each($con2)) {
  339. $patt="($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)";
  340. if (preg_match_all("/$patt/ims",$v,$res, PREG_SET_ORDER)) {
  341. // $res[0][1] = BEGIN or END
  342. // $res[0][2] = block name
  343. // $res[0][3] = kinda content
  344. if ($res[0][1]==$this->block_start_word) {
  345. $parent_name=implode(".",$block_names);
  346. $block_names[++$level]=$res[0][2]; /* add one level - array("main","table","row")*/
  347. $cur_block_name=implode(".",$block_names); /* make block name (main.table.row) */
  348. $this->block_parse_order[]=$cur_block_name; /* build block parsing order (reverse) */
  349. if(array_key_exists($cur_block_name, $blocks))
  350. {
  351. $blocks[$cur_block_name].=$res[0][3]; /* add contents */
  352. }
  353. else
  354. {
  355. $blocks[$cur_block_name]=$res[0][3]; /* add contents */
  356. }
  357. /* add {_BLOCK_.blockname} string to parent block */
  358. if(array_key_exists($parent_name, $blocks))
  359. {
  360. $blocks[$parent_name].="{_BLOCK_.$cur_block_name}";
  361. }
  362. else
  363. {
  364. $blocks[$parent_name]="{_BLOCK_.$cur_block_name}";
  365. }
  366. $this->sub_blocks[$parent_name][]=$cur_block_name; /* store sub block names for autoresetting and recursive parsing */
  367. $this->sub_blocks[$cur_block_name][]=""; /* store sub block names for autoresetting */
  368. } else if ($res[0][1]==$this->block_end_word) {
  369. unset($block_names[$level--]);
  370. $parent_name=implode(".",$block_names);
  371. $blocks[$parent_name].=$res[0][3]; /* add rest of block to parent block */
  372. }
  373. } else { /* no block delimiters found */
  374. $index = implode(".",$block_names);
  375. if(array_key_exists($index, $blocks))
  376. {
  377. $blocks[].=$this->block_start_delim.$v;
  378. }
  379. else
  380. {
  381. $blocks[]=$this->block_start_delim.$v;
  382. }
  383. }
  384. }
  385. return $blocks;
  386. }
  387. /***[ error stuff ]*********************************************************/
  388. /*
  389. sets and gets error
  390. */
  391. function get_error() {
  392. return ($this->ERROR=="")?0:$this->ERROR;
  393. }
  394. function set_error($str) {
  395. $this->ERROR=$str;
  396. }
  397. /***[ getfile ]*************************************************************/
  398. /*
  399. returns the contents of a file
  400. */
  401. function getfile($file) {
  402. if (!isset($file)) {
  403. $this->set_error("!isset file name!");
  404. return "";
  405. }
  406. // Pick which folder we should include from
  407. // Prefer the local directory, then try the theme directory.
  408. if (!is_file($file))
  409. $file = $this->alternate_include_directory.$file;
  410. if(is_file($file))
  411. {
  412. $file_text=file_get_contents($file);
  413. } else {
  414. $this->set_error("[$file] does not exist");
  415. $file_text="<b>__XTemplate fatal error: file [$file] does not exist__</b>";
  416. }
  417. return $file_text;
  418. }
  419. /***[ r_getfile ]***********************************************************/
  420. /*
  421. recursively gets the content of a file with {FILE "filename.tpl"} directives
  422. */
  423. function r_getfile($file) {
  424. $text=$this->getfile($file);
  425. while (preg_match($this->file_delim,$text,$res)) {
  426. $text2=$this->getfile($res[1]);
  427. $text=preg_replace("'".preg_quote($res[0])."'",$text2,$text);
  428. }
  429. return $text;
  430. }
  431. } /* end of XTemplate class. */
  432. ?>