]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_resource_php.php
Changes in api
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_resource_php.php
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_Resource_Uncompiled
13 {
14     /**
15      * container for short_open_tag directive's value before executing PHP templates
16      *
17      * @var string
18      */
19     protected $short_open_tag;
20
21     /**
22      * Create a new PHP Resource
23
24      */
25     public function __construct()
26     {
27         $this->short_open_tag = ini_get('short_open_tag');
28     }
29
30     /**
31      * populate Source Object with meta data from Resource
32      *
33      * @param  Smarty_Template_Source   $source    source object
34      * @param  Smarty_Internal_Template $_template template object
35      *
36      * @return void
37      */
38     public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
39     {
40         $source->filepath = $this->buildFilepath($source, $_template);
41
42         if ($source->filepath !== false) {
43             if (is_object($source->smarty->security_policy)) {
44                 $source->smarty->security_policy->isTrustedResourceDir($source->filepath);
45             }
46
47             $source->uid = sha1($source->filepath);
48             if ($source->smarty->compile_check) {
49                 $source->timestamp = @filemtime($source->filepath);
50                 $source->exists = !!$source->timestamp;
51             }
52         }
53     }
54
55     /**
56      * populate Source Object with timestamp and exists from Resource
57      *
58      * @param  Smarty_Template_Source $source source object
59      *
60      * @return void
61      */
62     public function populateTimestamp(Smarty_Template_Source $source)
63     {
64         $source->timestamp = @filemtime($source->filepath);
65         $source->exists = !!$source->timestamp;
66     }
67
68     /**
69      * Load template's source from file into current template object
70      *
71      * @param  Smarty_Template_Source $source source object
72      *
73      * @return string                 template source
74      * @throws SmartyException        if source cannot be loaded
75      */
76     public function getContent(Smarty_Template_Source $source)
77     {
78         if ($source->timestamp) {
79             return '';
80         }
81         throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
82     }
83
84     /**
85      * Render and output the template (without using the compiler)
86      *
87      * @param  Smarty_Template_Source   $source    source object
88      * @param  Smarty_Internal_Template $_template template object
89      *
90      * @return void
91      * @throws SmartyException          if template cannot be loaded or allow_php_templates is disabled
92      */
93     public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
94     {
95         if (!$source->smarty->allow_php_templates) {
96             throw new SmartyException("PHP templates are disabled");
97         }
98         if (!$source->exists) {
99             if ($_template->parent instanceof Smarty_Internal_Template) {
100                 $parent_resource = " in '{$_template->parent->template_resource}'";
101             } else {
102                 $parent_resource = '';
103             }
104             throw new SmartyException("Unable to load template {$source->type} '{$source->name}'{$parent_resource}");
105         }
106
107         // prepare variables
108         extract($_template->getTemplateVars());
109
110         // include PHP template with short open tags enabled
111         ini_set('short_open_tag', '1');
112         /** @var Smarty_Internal_Template $_smarty_template
113          * used in included file
114          */
115         $_smarty_template = $_template;
116         include($source->filepath);
117         ini_set('short_open_tag', $this->short_open_tag);
118     }
119 }