1 <?php
3 /**
4 * Project: Smarty: the PHP compiling template engine
5 * File: Smarty.class.php
6 * SVN: $Id: Smarty.class.php 3420 2009-12-29 20:12:11Z Uwe.Tews $
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * For questions, help, comments, discussion, etc., please join the
23 * Smarty mailing list. Send a blank e-mail to
24 * smarty-discussion-subscribe@googlegroups.com
25 *
26 * @link http://www.smarty.net/
27 * @copyright 2008 New Digital Group, Inc.
28 * @author Monte Ohrt <monte at ohrt dot com>
29 * @author Uwe Tews
30 * @package Smarty
31 * @version 3-SVN$Rev: 3286 $
32 */
34 /**
35 * define shorthand directory separator constant
36 */
37 if (!defined('DS')) {
38 define('DS', DIRECTORY_SEPARATOR);
39 }
41 /**
42 * set SMARTY_DIR to absolute path to Smarty library files.
43 * Sets SMARTY_DIR only if user application has not already defined it.
44 */
45 if (!defined('SMARTY_DIR')) {
46 define('SMARTY_DIR', dirname(__FILE__) . DS);
47 }
49 /**
50 * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
51 * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
52 */
53 if (!defined('SMARTY_SYSPLUGINS_DIR')) {
54 define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
55 }
56 if (!defined('SMARTY_PLUGINS_DIR')) {
57 define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
58 }
59 if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
60 define('SMARTY_RESOURCE_CHAR_SET', 'UTF-8');
61 }
62 if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
63 define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
64 }
66 /**
67 * define variable scopes
68 */
69 define('SMARTY_LOCAL_SCOPE', 0);
70 define('SMARTY_PARENT_SCOPE', 1);
71 define('SMARTY_ROOT_SCOPE', 2);
72 define('SMARTY_GLOBAL_SCOPE', 3);
74 /**
75 * define caching modes
76 */
77 define('SMARTY_CACHING_OFF', 0);
78 define('SMARTY_CACHING_LIFETIME_CURRENT', 1);
79 define('SMARTY_CACHING_LIFETIME_SAVED', 2);
81 /**
82 * This determines how Smarty handles "<?php ... ?>" tags in templates.
83 * possible values:
84 */
85 define('SMARTY_PHP_PASSTHRU', 0); //-> print tags as plain text
86 define('SMARTY_PHP_QUOTE', 1); //-> escape tags as entities
87 define('SMARTY_PHP_REMOVE', 2); //-> escape tags as entities
88 define('SMARTY_PHP_ALLOW', 3); //-> escape tags as entities
90 /**
91 * register the class autoloader
92 */
93 if (!defined('SMARTY_SPL_AUTOLOAD')) {
94 define('SMARTY_SPL_AUTOLOAD', 0);
95 }
97 if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
98 $registeredAutoLoadFunctions = spl_autoload_functions();
99 if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
100 spl_autoload_register();
101 }
102 } else {
103 spl_autoload_register('smartyAutoload');
104 }
106 /**
107 * This is the main Smarty class
108 */
109 class Smarty extends Smarty_Internal_Data {
110 // smarty version
111 const SMARTY_VERSION = 'Smarty3-b7';
112 // auto literal on delimiters with whitspace
113 public $auto_literal = true;
114 // display error on not assigned variables
115 public $error_unassigned = false;
116 // template directory
117 public $template_dir = null;
118 // default template handler
119 public $default_template_handler_func = null;
120 // compile directory
121 public $compile_dir = null;
122 // plugins directory
123 public $plugins_dir = null;
124 // cache directory
125 public $cache_dir = null;
126 // config directory
127 public $config_dir = null;
128 // force template compiling?
129 public $force_compile = false;
130 // check template for modifications?
131 public $compile_check = true;
132 // use sub dirs for compiled/cached files?
133 public $use_sub_dirs = false;
134 // compile_error?
135 public $compile_error = false;
136 // caching enabled
137 public $caching = false;
138 // merge compiled includea
139 public $merge_compiled_includes = false;
140 // cache lifetime
141 public $cache_lifetime = 3600;
142 // force cache file creation
143 public $force_cache = false;
144 // cache_id
145 public $cache_id = null;
146 // compile_id
147 public $compile_id = null;
148 // template delimiters
149 public $left_delimiter = "{";
150 public $right_delimiter = "}";
151 // security
152 public $security_class = 'Smarty_Security';
153 public $php_handling = SMARTY_PHP_PASSTHRU;
154 public $allow_php_tag = false;
155 public $allow_php_templates = false;
156 public $security = false;
157 public $security_policy = null;
158 public $security_handler = null;
159 public $direct_access_security = true;
160 // debug mode
161 public $debugging = false;
162 public $debugging_ctrl = 'URL';
163 public $smarty_debug_id = 'SMARTY_DEBUG';
164 public $debug_tpl = null;
165 // When set, smarty does uses this value as error_reporting-level.
166 public $error_reporting = null;
167 // config var settings
168 public $config_overwrite = true; //Controls whether variables with the same name overwrite each other.
169 public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean
170 public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file.
171 // config vars
172 public $config_vars = array();
173 // assigned tpl vars
174 public $tpl_vars = array();
175 // assigned global tpl vars
176 public $global_tpl_vars = array();
177 // dummy parent object
178 public $parent = null;
179 // global template functions
180 public $template_functions = array();
181 // resource type used if none given
182 public $default_resource_type = 'file';
183 // caching type
184 public $caching_type = 'file';
185 // internal cache resource types
186 public $cache_resource_types = array('file');
187 // config type
188 public $default_config_type = 'file';
189 // exception handler: array('ExceptionClass','ExceptionMethod');
190 public $exception_handler = null;
191 // cached template objects
192 public $template_objects = null;
193 // check If-Modified-Since headers
194 public $cache_modified_check = false;
195 // registered plugins
196 public $registered_plugins = array();
197 // plugin search order
198 public $plugin_search_order = array('function', 'block', 'compiler', 'class');
199 // registered objects
200 public $registered_objects = array();
201 // registered filters
202 public $registered_filters = array();
203 // autoload filter
204 public $autoload_filters = array();
205 // status of filter on variable output
206 public $variable_filter = true;
207 // global internal smarty vars
208 public $_smarty_vars = array();
209 // start time for execution time calculation
210 public $start_time = 0;
211 // default file permissions
212 public $_file_perms = 0644;
213 // default dir permissions
214 public $_dir_perms = 0771;
215 // smarty object reference
216 public $smarty = null;
218 /**
219 * Class constructor, initializes basic smarty properties
220 */
221 public function __construct() {
222 // self reference needed by other classes methods
223 $this->smarty = $this;
225 if (is_callable('mb_internal_encoding')) {
226 mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
227 }
228 $this->start_time = $this->_get_time();
229 // set exception handler
230 if (!empty($this->exception_handler))
231 set_exception_handler($this->exception_handler);
232 // set default dirs
233 $this->template_dir = array('.' . DS . 'templates' . DS);
234 $this->compile_dir = '.' . DS . 'templates_c' . DS;
235 $this->plugins_dir = array(SMARTY_PLUGINS_DIR);
236 $this->cache_dir = '.' . DS . 'cache' . DS;
237 $this->config_dir = '.' . DS . 'configs' . DS;
238 $this->debug_tpl = SMARTY_DIR . 'debug.tpl';
239 if (!$this->debugging && $this->debugging_ctrl == 'URL') {
240 if (isset($_SERVER['QUERY_STRING'])) {
241 $_query_string = $_SERVER['QUERY_STRING'];
242 }
243 else {
244 $_query_string = '';
245 }
246 if (false !== strpos($_query_string, $this->smarty_debug_id)) {
247 if (false !== strpos($_query_string, $this->smarty_debug_id . '=on')) {
248 // enable debugging for this browser session
249 setcookie('SMARTY_DEBUG', true);
250 $this->debugging = true;
251 }
252 elseif (false !== strpos($_query_string, $this->smarty_debug_id . '=off')) {
253 // disable debugging for this browser session
254 setcookie('SMARTY_DEBUG', false);
255 $this->debugging = false;
256 }
257 else {
258 // enable debugging for this page
259 $this->debugging = true;
260 }
261 }
262 else {
263 if (isset($_COOKIE['SMARTY_DEBUG'])) {
264 $this->debugging = true;
265 }
266 }
267 }
268 $this->assign_global('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
269 }
271 /**
272 * Class destructor
273 */
274 public function __destruct() {
275 // restore to previous exception handler, if any
276 if (!empty($this->exception_handler))
277 restore_exception_handler();
278 }
280 /**
281 * fetches a rendered Smarty template
282 *
283 * @param string $template the resource handle of the template file or template object
284 * @param mixed $cache_id cache id to be used with this template
285 * @param mixed $compile_id compile id to be used with this template
286 * @param object $ |null $parent next higher level of Smarty variables
287 * @return string rendered template output
288 */
289 public function fetch($template, $cache_id = null, $compile_id = null, $parent = null) {
290 if (is_object($cache_id)) {
291 $parent = $cache_id;
292 $cache_id = null;
293 }
294 if ($parent === null) {
295 // get default Smarty data object
296 $parent = $this;
297 }
298 // create template object if necessary
299 ($template instanceof $this->template_class)? $_template = $template :
300 $_template = $this->createTemplate ($template, $cache_id, $compile_id, $parent);
301 $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting)
302 ? $this->error_reporting : error_reporting() &~E_NOTICE);
303 // return redered template
304 if (isset($this->autoload_filters['output']) || isset($this->registered_filters['output'])) {
305 $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_template->getRenderedTemplate(), $this);
306 }
307 else {
308 $_output = $_template->getRenderedTemplate();
309 }
310 $_template->rendered_content = null;
311 error_reporting($_smarty_old_error_level);
312 return $_output;
313 }
315 /**
316 * displays a Smarty template
317 *
318 * @param string $ |object $template the resource handle of the template file or template object
319 * @param mixed $cache_id cache id to be used with this template
320 * @param mixed $compile_id compile id to be used with this template
321 * @param object $parent next higher level of Smarty variables
322 */
323 public function display($template, $cache_id = null, $compile_id = null, $parent = null) {
324 // display template
325 echo $this->fetch ($template, $cache_id, $compile_id, $parent);
326 // debug output
327 if ($this->debugging) {
328 Smarty_Internal_Debug::display_debug($this);
329 }
330 return true;
331 }
333 /**
334 * test if cache i valid
335 *
336 * @param string $ |object $template the resource handle of the template file or template object
337 * @param mixed $cache_id cache id to be used with this template
338 * @param mixed $compile_id compile id to be used with this template
339 * @return boolean cache status
340 */
341 public function is_cached($template, $cache_id = null, $compile_id = null) {
342 if (!($template instanceof $this->template_class)) {
343 $template = $this->createTemplate ($template, $cache_id, $compile_id, $this);
344 }
345 // return cache status of template
346 return $template->isCached();
347 }
349 /**
350 * Loads security class and enables security
351 */
352 public function enableSecurity() {
353 if (isset($this->security_class)) {
354 $this->security_policy = new $this->security_class;
355 $this->security_handler = new Smarty_Internal_Security_Handler($this);
356 $this->security = true;
357 }
358 else {
359 throw new Exception('Property security_class is not defined');
360 }
361 }
363 /**
364 * Set template directory
365 *
366 * @param string $ |array $template_dir folder(s) of template sorces
367 */
368 public function setTemplateDir($template_dir) {
369 $this->template_dir = (array)$template_dir;
370 return;
371 }
372 /**
373 * Adds template directory(s) to existing ones
374 *
375 * @param string $ |array $template_dir folder(s) of template sources
376 */
377 public function addTemplateDir($template_dir) {
378 $this->template_dir = array_merge((array)$this->template_dir, (array)$template_dir);
379 $this->template_dir = array_unique($this->template_dir);
380 return;
381 }
382 /**
383 * Set compile directory
384 *
385 * @param string $compile_dir folder of compiled template sources
386 */
387 public function setCompileDir($compile_dir) {
388 $this->compile_dir = $compile_dir;
389 return;
390 }
391 /**
392 * Set cache directory
393 *
394 * @param string $cache_dir folder of cache files
395 */
396 public function setCacheDir($cache_dir) {
397 $this->cache_dir = $cache_dir;
398 return;
399 }
400 /**
401 * Enable Caching
402 */
403 public function enableCaching() {
404 $this->caching = SMARTY_CACHING_LIFETIME_CURRENT;
405 return;
406 }
407 /**
408 * Set caching life time
409 *
410 * @param integer $lifetime lifetime of cached file in seconds
411 */
412 public function setCacheLifetime($lifetime) {
413 $this->cache_lifetime = $lifetime;
414 return;
415 }
416 /**
417 * Takes unknown classes and loads plugin files for them
418 * class name format: Smarty_PluginType_PluginName
419 * plugin filename format: plugintype.pluginname.php
420 *
421 * @param string $plugin_name class plugin name to load
422 * @return string|boolean filepath of loaded file or false
423 */
424 public function loadPlugin($plugin_name, $check = true) {
425 // if function or class exists, exit silently (already loaded)
426 if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false)))
427 return true;
428 // Plugin name is expected to be: Smarty_[Type]_[Name]
429 $_plugin_name = strtolower($plugin_name);
430 $_name_parts = explode('_', $_plugin_name, 3);
431 // class name must have three parts to be valid plugin
432 if (count($_name_parts) < 3 || $_name_parts[0] !== 'smarty') {
433 throw new Exception("plugin {$plugin_name} is not a valid name format");
434 return false;
435 }
436 // if type is "internal", get plugin from sysplugins
437 if ($_name_parts[1] == 'internal') {
438 $file = SMARTY_SYSPLUGINS_DIR . $_plugin_name . '.php';
439 if (file_exists($file)) {
440 require_once($file);
441 return $file;
442 }
443 else {
444 return false;
445 }
446 }
447 // plugin filename is expected to be: [type].[name].php
448 $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
449 // loop through plugin dirs and find the plugin
450 foreach((array)$this->plugins_dir as $_plugin_dir) {
451 if (strpos('/\\', substr($_plugin_dir, -1)) === false) {
452 $_plugin_dir .= DS;
453 }
454 $file = $_plugin_dir . $_plugin_filename;
455 if (file_exists($file)) {
456 require_once($file);
457 return $file;
458 }
459 }
460 // no plugin loaded
461 return false;
462 }
464 /**
465 * Sets the exception handler for Smarty.
466 *
467 * @param mixed $handler function name or array with object/method names
468 * @return string previous exception handler
469 */
470 public function setExceptionHandler($handler) {
471 $this->exception_handler = $handler;
472 return set_exception_handler($handler);
473 }
475 /**
476 * Loads cache resource.
477 *
478 * @return object of cache resource
479 */
480 public function loadCacheResource($type = null) {
481 if (!isset($type)) {
482 $type = $this->caching_type;
483 }
484 // already loaded?
485 if (isset($this->cache_resource_objects[$type])) {
486 return $this->cache_resource_objects[$type];
487 }
488 if (in_array($type, $this->cache_resource_types)) {
489 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
490 return $this->cache_resource_objects[$type] = new $cache_resource_class($this);
491 }
492 else {
493 // try plugins dir
494 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
495 if ($this->loadPlugin($cache_resource_class)) {
496 return $this->cache_resource_objects[$type] = new $cache_resource_class($this);
497 }
498 else {
499 throw new Exception("Unable to load cache resource '{$type}'");
500 }
501 }
502 }
504 /**
505 * trigger Smarty error
506 *
507 * @param string $error_msg
508 * @param integer $error_type
509 */
510 public function trigger_error($error_msg, $error_type = E_USER_WARNING) {
511 throw new Exception("Smarty error: $error_msg");
512 }
514 /**
515 * Takes unknown class methods and lazy loads sysplugin files for them
516 * class name format: Smarty_Method_MethodName
517 * plugin filename format: method.methodname.php
518 *
519 * @param string $name unknown methode name
520 * @param array $args aurgument array
521 */
522 public function __call($name, $args) {
523 $name = strtolower($name);
524 if ($name == 'smarty') {
525 throw new Exception('Please use parent::__construct() to call parent constuctor');
526 }
527 $function_name = 'smarty_method_' . $name;
528 if (!is_callable($function_name)) {
529 if (!file_exists(SMARTY_SYSPLUGINS_DIR . $function_name . '.php')) {
530 throw new Exception('Undefined Smarty method "' . $name . '"');
531 }
532 require_once(SMARTY_SYSPLUGINS_DIR . $function_name . '.php');
533 }
534 return call_user_func_array($function_name, array_merge(array($this), $args));
535 }
536 }
538 function smartyAutoload($class) {
539 $_class = strtolower($class);
540 if (substr($_class, 0, 16) === 'smarty_internal_' || $_class == 'smarty_security') {
541 include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
542 }
543 }
545 ?>