PHP_Template.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright 2003, Paul James
  4. *
  5. * This file contains some methods from the Smarty templating engine version
  6. * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>.
  7. *
  8. * The SquirrelMail (Foowd) template implementation.
  9. * Derived from the foowd template implementation and adapted
  10. * for squirrelmail
  11. * @copyright 2005-2025 The SquirrelMail Project Team
  12. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  13. * @version $Id$
  14. * @package squirrelmail
  15. *
  16. */
  17. /**
  18. * The SquirrelMail PHP Template class. Extends the base
  19. * Template class for use with PHP template pages.
  20. *
  21. * @author Paul James
  22. * @author Monte Ohrt <monte at ispi.net>
  23. * @author Andrei Zmievski <andrei at php.net>
  24. * @author Paul Lesniewski <paul at squirrelmail.org>
  25. * @package squirrelmail
  26. *
  27. */
  28. class PHP_Template extends Template
  29. {
  30. /**
  31. * The templates values array
  32. *
  33. * @var array
  34. *
  35. */
  36. var $values = array();
  37. /**
  38. * Constructor (PHP5 style, required in some future version of PHP)
  39. *
  40. * Please do not call directly. Use Template::construct_template().
  41. *
  42. * @param string $template_id the template ID
  43. *
  44. */
  45. function __construct($template_id) {
  46. //FIXME: find a way to test that this is ONLY ever called
  47. // from parent's construct_template() method (I doubt it
  48. // is worth the trouble to parse the current stack trace)
  49. // if (???)
  50. // trigger_error('Please do not use default PHP_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
  51. parent::__construct($template_id);
  52. }
  53. /**
  54. * Constructor (PHP4 style, kept for compatibility reasons)
  55. *
  56. * Please do not call directly. Use Template::construct_template().
  57. *
  58. * @param string $template_id the template ID
  59. *
  60. */
  61. function PHP_Template($template_id) {
  62. self::__construct($template_id);
  63. }
  64. /**
  65. * Assigns values to template variables
  66. *
  67. * @param array|string $tpl_var the template variable name(s)
  68. * @param mixed $value the value to assign
  69. FIXME: Proposed idea to add a parameter here that turns variable
  70. encoding on, so that we can make sure output is always
  71. run through something like sm_encode_html_special_chars() (maybe even nl2br()?)
  72. *
  73. */
  74. function assign($tpl_var, $value = NULL) {
  75. if (is_array($tpl_var))
  76. {
  77. foreach ($tpl_var as $key => $val)
  78. {
  79. if ($key != '')
  80. $this->values[$key] = $val;
  81. }
  82. }
  83. else
  84. {
  85. if ($tpl_var != '')
  86. $this->values[$tpl_var] = $value;
  87. }
  88. }
  89. /**
  90. * Assigns values to template variables by reference
  91. *
  92. * @param string $tpl_var the template variable name
  93. * @param mixed $value the referenced value to assign
  94. FIXME: Proposed idea to add a parameter here that turns variable
  95. encoding on, so that we can make sure output is always
  96. run through something like sm_encode_html_special_chars() (maybe even nl2br()?)
  97. *
  98. */
  99. function assign_by_ref($tpl_var, &$value) {
  100. if ($tpl_var != '')
  101. $this->values[$tpl_var] = &$value;
  102. }
  103. /**
  104. * Clears the values of all assigned varaiables.
  105. *
  106. */
  107. function clear_all_assign() {
  108. $this->values = array();
  109. }
  110. /**
  111. * Returns assigned variable value(s).
  112. *
  113. * @param string $varname If given, the value of that variable
  114. * is returned, assuming it has been
  115. * previously assigned. If not specified
  116. * an array of all assigned variables is
  117. * returned. (optional)
  118. *
  119. * @return mixed Desired single variable value or list of all
  120. * assigned variable values.
  121. *
  122. */
  123. function get_template_vars($varname=NULL) {
  124. // just looking for one value
  125. //
  126. if (!empty($varname)) {
  127. if (!empty($this->values[$varname]))
  128. return $this->values[$varname];
  129. else
  130. // FIXME: this OK? What does Smarty do?
  131. return NULL;
  132. }
  133. // return all variable values
  134. //
  135. return $this->values;
  136. }
  137. /**
  138. * Appends values to template variables
  139. *
  140. * @param array|string $tpl_var the template variable name(s)
  141. * @param mixed $value the value to append
  142. * @param boolean $merge when $value is given as an array,
  143. * this indicates whether or not that
  144. * array itself should be appended as
  145. * a new template variable value or if
  146. * that array's values should be merged
  147. * into the existing array of template
  148. * variable values
  149. FIXME: Proposed idea to add a parameter here that turns variable
  150. encoding on, so that we can make sure output is always
  151. run through something like sm_encode_html_special_chars() (maybe even nl2br()?)
  152. *
  153. */
  154. function append($tpl_var, $value = NULL, $merge = FALSE)
  155. {
  156. if (is_array($tpl_var))
  157. {
  158. foreach ($tpl_var as $_key => $_val)
  159. {
  160. if ($_key != '')
  161. {
  162. if(isset($this->values[$_key]) && !is_array($this->values[$_key]))
  163. settype($this->values[$_key],'array');
  164. if($merge && is_array($_val))
  165. {
  166. // FIXME: Tentative testing seems to indicate that
  167. // this does not mirror Smarty behavior; Smarty
  168. // seems to append the full array as a new element
  169. // instead of merging, so this behavior is technically
  170. // more "correct", but Smarty seems to differ
  171. foreach($_val as $_mkey => $_mval)
  172. $this->values[$_key][$_mkey] = $_mval;
  173. }
  174. else
  175. $this->values[$_key][] = $_val;
  176. }
  177. }
  178. }
  179. else
  180. {
  181. if ($tpl_var != '' && isset($value))
  182. {
  183. if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var]))
  184. settype($this->values[$tpl_var],'array');
  185. if($merge && is_array($value))
  186. {
  187. foreach($value as $_mkey => $_mval)
  188. $this->values[$tpl_var][$_mkey] = $_mval;
  189. }
  190. else
  191. $this->values[$tpl_var][] = $value;
  192. }
  193. }
  194. }
  195. /**
  196. * Appends values to template variables by reference
  197. *
  198. * @param string $tpl_var the template variable name
  199. * @param mixed $value the referenced value to append
  200. * @param boolean $merge when $value is given as an array,
  201. * this indicates whether or not that
  202. * array itself should be appended as
  203. * a new template variable value or if
  204. * that array's values should be merged
  205. * into the existing array of template
  206. * variable values
  207. FIXME: Proposed idea to add a parameter here that turns variable
  208. encoding on, so that we can make sure output is always
  209. run through something like sm_encode_html_special_chars() (maybe even nl2br()?)
  210. *
  211. */
  212. function append_by_ref($tpl_var, &$value, $merge = FALSE)
  213. {
  214. if ($tpl_var != '' && isset($value))
  215. {
  216. if(!@is_array($this->values[$tpl_var]))
  217. settype($this->values[$tpl_var],'array');
  218. if ($merge && is_array($value))
  219. {
  220. foreach($value as $_key => $_val)
  221. $this->values[$tpl_var][$_key] = &$value[$_key];
  222. }
  223. else
  224. $this->values[$tpl_var][] = &$value;
  225. }
  226. }
  227. /**
  228. * Applys the template and generates final output destined
  229. * for the user's browser
  230. *
  231. * @param string $filepath The full file path to the template to be applied
  232. *
  233. * @return string The output for the given template
  234. *
  235. */
  236. function apply_template($filepath) {
  237. // place values array directly in scope
  238. // ($t? let's try to be more verbose please :-) )
  239. //
  240. $t = &$this->values;
  241. ob_start();
  242. include($filepath);
  243. $contents = ob_get_contents();
  244. ob_end_clean();
  245. return $contents;
  246. }
  247. }