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