class.FastTemplate.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:16k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. # bookmarker RCS version info:
  3. # $Id: class.FastTemplate.inc,v 1.2 2000/04/11 14:46:24 prenagha Exp $
  4. /*
  5. CVS Revision. 1.1.0
  6. */
  7. class FastTemplate {
  8. var $FILELIST = array(); // Holds the array of filehandles
  9. // FILELIST[HANDLE] == "fileName"
  10. var $DYNAMIC = array(); // Holds the array of dynamic
  11. // blocks, and the fileHandles they
  12. // live in.
  13. var $PARSEVARS = array(); // Holds the array of Variable
  14. // handles.
  15. // PARSEVARS[HANDLE] == "value"
  16. var $LOADED = array(); // We only want to load a template
  17. // once - when it's used.
  18. // LOADED[FILEHANDLE] == 1 if loaded
  19. // undefined if not loaded yet.
  20. var $HANDLE = array(); // Holds the handle names assigned
  21. // by a call to parse()
  22. var $ROOT = ""; // Holds path-to-templates
  23. var $WIN32 = false; // Set to true if this is a WIN32 server
  24. var $ERROR = ""; // Holds the last error message
  25. var $LAST = ""; // Holds the HANDLE to the last
  26. // template parsed by parse()
  27. var $STRICT = true; // Strict template checking.
  28. // Unresolved vars in templates will
  29. // generate a warning when found.
  30. // ************************************************************
  31. function FastTemplate ($pathToTemplates = "")
  32. {
  33. global $php_errormsg;
  34. if(!empty($pathToTemplates))
  35. {
  36. $this->set_root($pathToTemplates);
  37. }
  38. } // end (new) FastTemplate ()
  39. // ************************************************************
  40. // All templates will be loaded from this "root" directory
  41. // Can be changed in mid-process by re-calling with a new
  42. // value.
  43. function set_root ($root)
  44. {
  45. $trailer = substr($root,-1);
  46. if(!$this->WIN32)
  47. {
  48. if( (ord($trailer)) != 47 )
  49. {
  50. $root = "$root". chr(47);
  51. }
  52. if(is_dir($root))
  53. {
  54. $this->ROOT = $root;
  55. }
  56. else
  57. {
  58. $this->ROOT = "";
  59. $this->error("Specified ROOT dir [$root] is not a directory");
  60. }
  61. }
  62. else
  63. {
  64. // WIN32 box - no testing
  65. if( (ord($trailer)) != 92 )
  66. {
  67. $root = "$root" . chr(92);
  68. }
  69. $this->ROOT = $root;
  70. }
  71. } // End set_root()
  72. //  **************************************************************
  73. //  Calculates current microtime
  74. // I throw this into all my classes for benchmarking purposes
  75. // It's not used by anything in this class and can be removed
  76. // if you don't need it.
  77. function utime ()
  78. {
  79. $time = explode( " ", microtime());
  80. $usec = (double)$time[0];
  81. $sec = (double)$time[1];
  82. return $sec + $usec;
  83.     }
  84. //  **************************************************************
  85. // Strict template checking, if true sends warnings to STDOUT when
  86. // parsing a template with undefined variable references
  87. // Used for tracking down bugs-n-such. Use no_strict() to disable.
  88. function strict ()
  89. {
  90. $this->STRICT = true;
  91. }
  92. // ************************************************************
  93. // Silently discards (removes) undefined variable references
  94. // found in templates
  95. function no_strict ()
  96. {
  97. $this->STRICT = false;
  98. }
  99. // ************************************************************
  100. // A quick check of the template file before reading it.
  101. // This is -not- a reliable check, mostly due to inconsistencies
  102. // in the way PHP determines if a file is readable.
  103. function is_safe ($filename)
  104. {
  105. if(!file_exists($filename))
  106. {
  107. $this->error("[$filename] does not exist",0);
  108. return false;
  109. }
  110. return true;
  111. }
  112. // ************************************************************
  113. // Grabs a template from the root dir and 
  114. // reads it into a (potentially REALLY) big string
  115. function get_template ($template)
  116. {
  117. if(empty($this->ROOT))
  118. {
  119. $this->error("Cannot open template. Root not valid.",1);
  120. return false;
  121. }
  122. $filename = "$this->ROOT"."$template";
  123. $contents = implode("",(@file($filename)));
  124. if( (!$contents) or (empty($contents)) )
  125. {
  126. $this->error("get_template() failure: [$filename] $php_errormsg",1);
  127. }
  128. return $contents;
  129. } // end get_template
  130. // ************************************************************
  131. // Prints the warnings for unresolved variable references
  132. // in template files. Used if STRICT is true
  133. function show_unknowns ($Line)
  134. {
  135. $unknown = array();
  136. if (ereg("({[A-Z0-9_]+})",$Line,$unknown))
  137. {
  138. $UnkVar = $unknown[1];
  139. if(!(empty($UnkVar)))
  140. {
  141. @error_log("[FastTemplate] Warning: no value found for variable: $UnkVar ",0);
  142. }
  143. }
  144. } // end show_unknowns()
  145. // ************************************************************
  146. // This routine get's called by parse() and does the actual
  147. // {VAR} to VALUE conversion within the template.
  148. function parse_template ($template, $tpl_array)
  149. {
  150. while ( list ($key,$val) = each ($tpl_array) )
  151. {
  152. if (!(empty($key)))
  153. {
  154. if(gettype($val) != "string")
  155. {
  156. settype($val,"string");
  157. }
  158. // php4 doesn't like '{$' combinations.
  159. $key = '{'."$key".'}';
  160. $template = ereg_replace("$key","$val","$template");
  161. //$template = str_replace("$key","$val","$template");
  162. }
  163. }
  164. if(!$this->STRICT)
  165. {
  166. // Silently remove anything not already found
  167. $template = ereg_replace("{([A-Z0-9_]+)}","",$template);
  168. }
  169. else
  170. {
  171. // Warn about unresolved template variables
  172. if (ereg("({[A-Z0-9_]+})",$template))
  173. {
  174. $unknown = split("n",$template);
  175. while (list ($Element,$Line) = each($unknown) )
  176. {
  177. $UnkVar = $Line;
  178. if(!(empty($UnkVar)))
  179. {
  180. $this->show_unknowns($UnkVar);
  181. }
  182. }
  183. }
  184. }
  185. return $template;
  186. } // end parse_template();
  187. // ************************************************************
  188. // The meat of the whole class. The magic happens here.
  189. function parse ( $ReturnVar, $FileTags )
  190. {
  191. $append = false;
  192. $this->LAST = $ReturnVar;
  193. $this->HANDLE[$ReturnVar] = 1;
  194. if (gettype($FileTags) == "array")
  195. {
  196. unset($this->$ReturnVar); // Clear any previous data
  197. while ( list ( $key , $val ) = each ( $FileTags ) )
  198. {
  199. if ( (!isset($this->$val)) || (empty($this->$val)) )
  200. {
  201. $this->LOADED["$val"] = 1;
  202. if(isset($this->DYNAMIC["$val"]))
  203. {
  204. $this->parse_dynamic($val,$ReturnVar);
  205. }
  206. else
  207. {
  208. $fileName = $this->FILELIST["$val"];
  209. $this->$val = $this->get_template($fileName);
  210. }
  211. }
  212. // Array context implies overwrite
  213. $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS);
  214. // For recursive calls.
  215. $this->assign( array( $ReturnVar => $this->$ReturnVar ) );
  216. }
  217. } // end if FileTags is array()
  218. else
  219. {
  220. // FileTags is not an array
  221. $val = $FileTags;
  222. if( (substr($val,0,1)) == '.' )
  223. {
  224. // Append this template to a previous ReturnVar
  225. $append = true;
  226. $val = substr($val,1);
  227. }
  228. if ( (!isset($this->$val)) || (empty($this->$val)) )
  229. {
  230. $this->LOADED["$val"] = 1;
  231. if(isset($this->DYNAMIC["$val"]))
  232. {
  233. $this->parse_dynamic($val,$ReturnVar);
  234. }
  235. else
  236. {
  237. $fileName = $this->FILELIST["$val"];
  238. $this->$val = $this->get_template($fileName);
  239. }
  240. }
  241. if($append)
  242. {
  243. $this->$ReturnVar .= $this->parse_template($this->$val,$this->PARSEVARS);
  244. }
  245. else
  246. {
  247. $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS);
  248. }
  249. // For recursive calls.
  250. $this->assign(array( $ReturnVar => $this->$ReturnVar) );
  251. }
  252. return;
  253. } // End parse()
  254. // ************************************************************
  255. function FastPrint ( $template = "" )
  256. {
  257. if(empty($template))
  258. {
  259. $template = $this->LAST;
  260. }
  261. if( (!(isset($this->$template))) || (empty($this->$template)) )
  262. {
  263. $this->error("Nothing parsed, nothing printed",0);
  264. return;
  265. }
  266. else
  267. {
  268. print $this->$template;
  269. }
  270. return;
  271. }
  272. // ************************************************************
  273. function fetch ( $template = "" )
  274. {
  275. if(empty($template))
  276. {
  277. $template = $this->LAST;
  278. }
  279. if( (!(isset($this->$template))) || (empty($this->$template)) )
  280. {
  281. $this->error("Nothing parsed, nothing printed",0);
  282. return "";
  283. }
  284. return($this->$template);
  285. }
  286. // ************************************************************
  287. function define_dynamic ($Macro, $ParentName)
  288. {
  289. // A dynamic block lives inside another template file.
  290. // It will be stripped from the template when parsed
  291. // and replaced with the {$Tag}.
  292. $this->DYNAMIC["$Macro"] = $ParentName;
  293. return true;
  294. }
  295. // ************************************************************
  296. function parse_dynamic ($Macro,$MacroName)
  297. {
  298. // The file must already be in memory.
  299. $ParentTag = $this->DYNAMIC["$Macro"];
  300. if( (!$this->$ParentTag) or (empty($this->$ParentTag)) )
  301. {
  302. $fileName = $this->FILELIST[$ParentTag];
  303. $this->$ParentTag = $this->get_template($fileName);
  304. $this->LOADED[$ParentTag] = 1;
  305. }
  306. if($this->$ParentTag)
  307. {
  308. $template = $this->$ParentTag;
  309. $DataArray = split("n",$template);
  310. $newMacro = "";
  311. $newParent = "";
  312. $outside = true;
  313. $start = false;
  314. $end = false;
  315. while ( list ($lineNum,$lineData) = each ($DataArray) )
  316. {
  317. $lineTest = trim($lineData);
  318. if("<!-- BEGIN DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
  319. {
  320. $start = true;
  321. $end = false;
  322. $outside = false;
  323. }
  324. if("<!-- END DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
  325. {
  326. $start = false;
  327. $end = true;
  328. $outside = true;
  329. }
  330. if( (!$outside) and (!$start) and (!$end) )
  331. {
  332. $newMacro .= "$lineDatan"; // Restore linebreaks
  333. }
  334. if( ($outside) and (!$start) and (!$end) )
  335. {
  336. $newParent .= "$lineDatan"; // Restore linebreaks
  337. }
  338. if($end)
  339. {
  340. $newParent .= '{'."$MacroName}n";
  341. }
  342. // Next line please
  343. if($end) { $end = false; }
  344. if($start) { $start = false; }
  345. } // end While
  346. $this->$Macro = $newMacro;
  347. $this->$ParentTag = $newParent;
  348. return true;
  349. } // $ParentTag NOT loaded - MAJOR oopsie
  350. else
  351. {
  352. @error_log("ParentTag: [$ParentTag] not loaded!",0);
  353. $this->error("ParentTag: [$ParentTag] not loaded!",0);
  354. }
  355. return false;
  356. }
  357. // ************************************************************
  358. // Strips a DYNAMIC BLOCK from a template.
  359. function clear_dynamic ($Macro="")
  360. {
  361. if(empty($Macro)) { return false; }
  362. // The file must already be in memory.
  363. $ParentTag = $this->DYNAMIC["$Macro"];
  364. if( (!$this->$ParentTag) or (empty($this->$ParentTag)) )
  365. {
  366. $fileName = $this->FILELIST[$ParentTag];
  367. $this->$ParentTag = $this->get_template($fileName);
  368. $this->LOADED[$ParentTag] = 1;
  369. }
  370. if($this->$ParentTag)
  371. {
  372. $template = $this->$ParentTag;
  373. $DataArray = split("n",$template);
  374. $newParent = "";
  375. $outside = true;
  376. $start = false;
  377. $end = false;
  378. while ( list ($lineNum,$lineData) = each ($DataArray) )
  379. {
  380. $lineTest = trim($lineData);
  381. if("<!-- BEGIN DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
  382. {
  383. $start = true;
  384. $end = false;
  385. $outside = false;
  386. }
  387. if("<!-- END DYNAMIC BLOCK: $Macro -->" == "$lineTest" )
  388. {
  389. $start = false;
  390. $end = true;
  391. $outside = true;
  392. }
  393. if( ($outside) and (!$start) and (!$end) )
  394. {
  395. $newParent .= "$lineDatan"; // Restore linebreaks
  396. }
  397. // Next line please
  398. if($end) { $end = false; }
  399. if($start) { $start = false; }
  400. } // end While
  401. $this->$ParentTag = $newParent;
  402. return true;
  403. } // $ParentTag NOT loaded - MAJOR oopsie
  404. else
  405. {
  406. @error_log("ParentTag: [$ParentTag] not loaded!",0);
  407. $this->error("ParentTag: [$ParentTag] not loaded!",0);
  408. }
  409. return false;
  410. }
  411. // ************************************************************
  412. function define ($fileList)
  413. {
  414. while ( list ($FileTag,$FileName) = each ($fileList) )
  415. {
  416. $this->FILELIST["$FileTag"] = $FileName;
  417. }
  418. return true;
  419. }
  420. // ************************************************************
  421. function clear_parse ( $ReturnVar = "")
  422. {
  423. $this->clear($ReturnVar);
  424. }
  425. // ************************************************************
  426. function clear ( $ReturnVar = "" )
  427. {
  428. // Clears out hash created by call to parse()
  429. if(!empty($ReturnVar))
  430. {
  431. if( (gettype($ReturnVar)) != "array")
  432. {
  433. unset($this->$ReturnVar);
  434. return;
  435. }
  436. else
  437. {
  438. while ( list ($key,$val) = each ($ReturnVar) )
  439. {
  440. unset($this->$val);
  441. }
  442. return;
  443. }
  444. }
  445. // Empty - clear all of them
  446. while ( list ( $key,$val) = each ($this->HANDLE) )
  447. {
  448. $KEY = $key;
  449. unset($this->$KEY);
  450. }
  451. return;
  452. } // end clear()
  453. // ************************************************************
  454. function clear_all ()
  455. {
  456. $this->clear();
  457. $this->clear_assign();
  458. $this->clear_define();
  459. $this->clear_tpl();
  460. return;
  461. } // end clear_all
  462. // ************************************************************
  463. function clear_tpl ($fileHandle = "")
  464. {
  465. if(empty($this->LOADED))
  466. {
  467. // Nothing loaded, nothing to clear
  468. return true;
  469. }
  470. if(empty($fileHandle))
  471. {
  472. // Clear ALL fileHandles
  473. while ( list ($key, $val) = each ($this->LOADED) )
  474. {
  475. unset($this->$key);
  476. }
  477. unset($this->LOADED);
  478. return true;
  479. }
  480. else
  481. {
  482. if( (gettype($fileHandle)) != "array")
  483. {
  484. if( (isset($this->$fileHandle)) || (!empty($this->$fileHandle)) )
  485. {
  486. unset($this->LOADED[$fileHandle]);
  487. unset($this->$fileHandle);
  488. return true;
  489. }
  490. }
  491. else
  492. {
  493. while ( list ($Key, $Val) = each ($fileHandle) )
  494. {
  495. unset($this->LOADED[$Key]);
  496. unset($this->$Key);
  497. }
  498. return true;
  499. }
  500. }
  501. return false;
  502. } // end clear_tpl
  503. // ************************************************************
  504. function clear_define ( $FileTag = "" )
  505. {
  506. if(empty($FileTag))
  507. {
  508. unset($this->FILELIST);
  509. return;
  510. }
  511. if( (gettype($Files)) != "array")
  512. {
  513. unset($this->FILELIST[$FileTag]);
  514. return;
  515. }
  516. else
  517. {
  518. while ( list ( $Tag, $Val) = each ($FileTag) )
  519. {
  520. unset($this->FILELIST[$Tag]);
  521. }
  522. return;
  523. }
  524. }
  525. // ************************************************************
  526. // Aliased function - used for compatibility with CGI::FastTemplate
  527. function clear_parse ()
  528. {
  529. $this->clear_assign();
  530. }
  531. // ************************************************************
  532. // Clears all variables set by assign()
  533. function clear_assign ()
  534. {
  535. if(!(empty($this->PARSEVARS)))
  536. {
  537. while(list($Ref,$Val) = each ($this->PARSEVARS) )
  538. {
  539. unset($this->PARSEVARS["$Ref"]);
  540. }
  541. }
  542. }
  543. // ************************************************************
  544. function clear_href ($href)
  545. {
  546. if(!empty($href))
  547. {
  548. if( (gettype($href)) != "array")
  549. {
  550. unset($this->PARSEVARS[$href]);
  551. return;
  552. }
  553. else
  554. {
  555. while (list ($Ref,$val) = each ($href) )
  556. {
  557. unset($this->PARSEVARS[$Ref]);
  558. }
  559. return;
  560. }
  561. }
  562. else
  563. {
  564. // Empty - clear them all
  565. $this->clear_assign();
  566. }
  567. return;
  568. }
  569. // ************************************************************
  570. function assign ($tpl_array, $trailer="")
  571. {
  572. if(gettype($tpl_array) == "array")
  573. {
  574. while ( list ($key,$val) = each ($tpl_array) )
  575. {
  576. if (!(empty($key)))
  577. {
  578. // Empty values are allowed
  579. // Empty Keys are NOT
  580. $this->PARSEVARS["$key"] = $val;
  581. }
  582. }
  583. }
  584. else
  585. {
  586. // Empty values are allowed in non-array context now.
  587. if (!empty($tpl_array))
  588. {
  589. $this->PARSEVARS["$tpl_array"] = $trailer;
  590. }
  591. }
  592. }
  593. // ************************************************************
  594. // Return the value of an assigned variable.
  595. // Christian Brandel cbrandel@gmx.de
  596. function get_assigned($tpl_name = "")
  597. {
  598. if(empty($tpl_name)) { return false; }
  599. if(isset($this->PARSEVARS["$tpl_name"]))
  600. {
  601. return ($this->PARSEVARS["$tpl_name"]);
  602. }
  603. else
  604. {
  605. return false;
  606.         }
  607. }
  608. // ************************************************************
  609. function error ($errorMsg, $die = 0)
  610. {
  611. $this->ERROR = $errorMsg;
  612. echo "ERROR: $this->ERROR <BR> n";
  613. if ($die == 1)
  614. {
  615. exit;
  616. }
  617. return;
  618. } // end error()
  619. // ************************************************************
  620. // ************************************************************
  621. } // End class.FastTemplate.php3
  622. ?>