1: <?php
2:
3: /**
4: * Smarty Internal Plugin Resource PHP
5: * Implements the file system as resource for PHP templates
6: *
7: * @package Smarty
8: * @subpackage TemplateResources
9: * @author Uwe Tews
10: * @author Rodney Rehm
11: */
12: class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
13: {
14: /**
15: * Flag that it's an uncompiled resource
16: *
17: * @var bool
18: */
19: public $uncompiled = true;
20:
21: /**
22: * Resource does implement populateCompiledFilepath() method
23: *
24: * @var bool
25: */
26: public $hasCompiledHandler = true;
27:
28: /**
29: * container for short_open_tag directive's value before executing PHP templates
30: *
31: * @var string
32: */
33: protected $short_open_tag;
34:
35: /**
36: * Create a new PHP Resource
37: */
38: public function __construct()
39: {
40: $this->short_open_tag = function_exists('ini_get') ? ini_get('short_open_tag') : 1;
41: }
42:
43: /**
44: * Load template's source from file into current template object
45: *
46: * @param Smarty_Template_Source $source source object
47: *
48: * @return string template source
49: * @throws SmartyException if source cannot be loaded
50: */
51: public function getContent(Smarty_Template_Source $source)
52: {
53: if ($source->exists) {
54: return '';
55: }
56: throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
57: }
58:
59: /**
60: * populate compiled object with compiled filepath
61: *
62: * @param Smarty_Template_Compiled $compiled compiled object
63: * @param Smarty_Internal_Template $_template template object (is ignored)
64: */
65: public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
66: {
67: $compiled->filepath = $_template->source->filepath;
68: $compiled->timestamp = $_template->source->timestamp;
69: $compiled->exists = $_template->source->exists;
70: $compiled->file_dependency[ $_template->source->uid ] =
71: array(
72: $compiled->filepath,
73: $compiled->timestamp,
74: $_template->source->type,
75: );
76: }
77:
78: /**
79: * Render and output the template (without using the compiler)
80: *
81: * @param Smarty_Template_Source $source source object
82: * @param Smarty_Internal_Template $_template template object
83: *
84: * @return void
85: * @throws SmartyException if template cannot be loaded or allow_php_templates is disabled
86: */
87: public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
88: {
89: if (!$source->smarty->allow_php_templates) {
90: throw new SmartyException('PHP templates are disabled');
91: }
92: if (!$source->exists) {
93: throw new SmartyException(
94: "Unable to load template '{$source->type}:{$source->name}'" .
95: ($_template->_isSubTpl() ? " in '{$_template->parent->template_resource}'" : '')
96: );
97: }
98: // prepare variables
99: extract($_template->getTemplateVars());
100: // include PHP template with short open tags enabled
101: if (function_exists('ini_set')) {
102: ini_set('short_open_tag', '1');
103: }
104: /**
105: *
106: *
107: * @var Smarty_Internal_Template $_smarty_template
108: * used in included file
109: */
110: $_smarty_template = $_template;
111: include $source->filepath;
112: if (function_exists('ini_set')) {
113: ini_set('short_open_tag', $this->short_open_tag);
114: }
115: }
116: }
117: