1 <?php
3 /**
4 * Smarty Internal Plugin Resource PHP
5 *
6 * Implements the file system as resource for PHP templates
7 *
8 * @package Smarty
9 * @subpackage TemplateResources
10 * @author Uwe Tews
11 * @author Rodney Rehm
12 */
13 class Smarty_Internal_Resource_PHP extends Smarty_Resource_Uncompiled {
14 /**
15 * container for short_open_tag directive's value before executing PHP templates
16 * @var string
17 */
18 protected $short_open_tag;
20 /**
21 * Create a new PHP Resource
22 *
23 */
24 public function __construct()
25 {
26 $this->short_open_tag = ini_get( 'short_open_tag' );
27 }
29 /**
30 * populate Source Object with meta data from Resource
31 *
32 * @param Smarty_Template_Source $source source object
33 * @param Smarty_Internal_Template $_template template object
34 * @return void
35 */
36 public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
37 {
38 $source->filepath = $this->buildFilepath($source, $_template);
40 if ($source->filepath !== false) {
41 if (is_object($source->smarty->security_policy)) {
42 $source->smarty->security_policy->isTrustedResourceDir($source->filepath);
43 }
45 $source->uid = sha1($source->filepath);
46 if ($source->smarty->compile_check) {
47 $source->timestamp = @filemtime($source->filepath);
48 $source->exists = !!$source->timestamp;
49 }
50 }
51 }
53 /**
54 * populate Source Object with timestamp and exists from Resource
55 *
56 * @param Smarty_Template_Source $source source object
57 * @return void
58 */
59 public function populateTimestamp(Smarty_Template_Source $source)
60 {
61 $source->timestamp = @filemtime($source->filepath);
62 $source->exists = !!$source->timestamp;
63 }
65 /**
66 * Load template's source from file into current template object
67 *
68 * @param Smarty_Template_Source $source source object
69 * @return string template source
70 * @throws SmartyException if source cannot be loaded
71 */
72 public function getContent(Smarty_Template_Source $source)
73 {
74 if ($source->timestamp) {
75 return '';
76 }
77 throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
78 }
80 /**
81 * Render and output the template (without using the compiler)
82 *
83 * @param Smarty_Template_Source $source source object
84 * @param Smarty_Internal_Template $_template template object
85 * @return void
86 * @throws SmartyException if template cannot be loaded or allow_php_templates is disabled
87 */
88 public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
89 {
90 $_smarty_template = $_template;
92 if (!$source->smarty->allow_php_templates) {
93 throw new SmartyException("PHP templates are disabled");
94 }
95 if (!$source->exists) {
96 if ($_template->parent instanceof Smarty_Internal_Template) {
97 $parent_resource = " in '{$_template->parent->template_resource}'";
98 } else {
99 $parent_resource = '';
100 }
101 throw new SmartyException("Unable to load template {$source->type} '{$source->name}'{$parent_resource}");
102 }
104 // prepare variables
105 extract($_template->getTemplateVars());
107 // include PHP template with short open tags enabled
108 ini_set( 'short_open_tag', '1' );
109 include($source->filepath);
110 ini_set( 'short_open_tag', $this->short_open_tag );
111 }
112 }
114 ?>