smarty_resource.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Smarty Resource Plugin
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Rodney Rehm
  8. */
  9. /**
  10. * Smarty Resource Plugin
  11. * Base implementation for resource plugins
  12. *
  13. * @package Smarty
  14. * @subpackage TemplateResources
  15. *
  16. * @method renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
  17. * @method populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
  18. * @method process(Smarty_Internal_Template $_smarty_tpl)
  19. */
  20. abstract class Smarty_Resource
  21. {
  22. /**
  23. * resource types provided by the core
  24. *
  25. * @var array
  26. */
  27. public static $sysplugins = array(
  28. 'file' => 'smarty_internal_resource_file.php',
  29. 'string' => 'smarty_internal_resource_string.php',
  30. 'extends' => 'smarty_internal_resource_extends.php',
  31. 'stream' => 'smarty_internal_resource_stream.php',
  32. 'eval' => 'smarty_internal_resource_eval.php',
  33. 'php' => 'smarty_internal_resource_php.php'
  34. );
  35. /**
  36. * Source is bypassing compiler
  37. *
  38. * @var boolean
  39. */
  40. public $uncompiled = false;
  41. /**
  42. * Source must be recompiled on every occasion
  43. *
  44. * @var boolean
  45. */
  46. public $recompiled = false;
  47. /**
  48. * Flag if resource does implement populateCompiledFilepath() method
  49. *
  50. * @var bool
  51. */
  52. public $hasCompiledHandler = false;
  53. /**
  54. * Load Resource Handler
  55. *
  56. * @param Smarty $smarty smarty object
  57. * @param string $type name of the resource
  58. *
  59. * @throws SmartyException
  60. * @return Smarty_Resource Resource Handler
  61. */
  62. public static function load(Smarty $smarty, $type)
  63. {
  64. // try smarty's cache
  65. if (isset($smarty->_cache[ 'resource_handlers' ][ $type ])) {
  66. return $smarty->_cache[ 'resource_handlers' ][ $type ];
  67. }
  68. // try registered resource
  69. if (isset($smarty->registered_resources[ $type ])) {
  70. return $smarty->_cache[ 'resource_handlers' ][ $type ] =
  71. $smarty->registered_resources[ $type ] instanceof Smarty_Resource ?
  72. $smarty->registered_resources[ $type ] : new Smarty_Internal_Resource_Registered();
  73. }
  74. // try sysplugins dir
  75. if (isset(self::$sysplugins[ $type ])) {
  76. $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
  77. return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
  78. }
  79. // try plugins dir
  80. $_resource_class = 'Smarty_Resource_' . ucfirst($type);
  81. if ($smarty->loadPlugin($_resource_class)) {
  82. if (class_exists($_resource_class, false)) {
  83. return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
  84. } else {
  85. $smarty->registerResource(
  86. $type,
  87. array(
  88. "smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp",
  89. "smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted"
  90. )
  91. );
  92. // give it another try, now that the resource is registered properly
  93. return self::load($smarty, $type);
  94. }
  95. }
  96. // try streams
  97. $_known_stream = stream_get_wrappers();
  98. if (in_array($type, $_known_stream)) {
  99. // is known stream
  100. if (is_object($smarty->security_policy)) {
  101. $smarty->security_policy->isTrustedStream($type);
  102. }
  103. return $smarty->_cache[ 'resource_handlers' ][ $type ] = new Smarty_Internal_Resource_Stream();
  104. }
  105. // TODO: try default_(template|config)_handler
  106. // give up
  107. throw new SmartyException("Unknown resource type '{$type}'");
  108. }
  109. /**
  110. * extract resource_type and resource_name from template_resource and config_resource
  111. *
  112. * @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
  113. *
  114. * @param string $resource_name template_resource or config_resource to parse
  115. * @param string $default_resource the default resource_type defined in $smarty
  116. *
  117. * @return array with parsed resource name and type
  118. */
  119. public static function parseResourceName($resource_name, $default_resource)
  120. {
  121. if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
  122. $type = $match[ 1 ];
  123. $name = substr($resource_name, strlen($match[ 0 ]));
  124. } else {
  125. // no resource given, use default
  126. // or single character before the colon is not a resource type, but part of the filepath
  127. $type = $default_resource;
  128. $name = $resource_name;
  129. }
  130. return array($name, $type);
  131. }
  132. /**
  133. * modify template_resource according to resource handlers specifications
  134. *
  135. * @param \Smarty_Internal_Template|\Smarty $obj Smarty instance
  136. * @param string $template_resource template_resource to extract resource handler and
  137. * name of
  138. *
  139. * @return string unique resource name
  140. * @throws \SmartyException
  141. */
  142. public static function getUniqueTemplateName($obj, $template_resource)
  143. {
  144. $smarty = $obj->_getSmartyObj();
  145. list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
  146. // TODO: optimize for Smarty's internal resource types
  147. $resource = Smarty_Resource::load($smarty, $type);
  148. // go relative to a given template?
  149. $_file_is_dotted = $name[ 0 ] === '.' && ($name[ 1 ] === '.' || $name[ 1 ] === '/');
  150. if ($obj->_isTplObj() && $_file_is_dotted
  151. && ($obj->source->type === 'file' || $obj->parent->source->type === 'extends')
  152. ) {
  153. $name = $smarty->_realpath(dirname($obj->parent->source->filepath) . DIRECTORY_SEPARATOR . $name);
  154. }
  155. return $resource->buildUniqueResourceName($smarty, $name);
  156. }
  157. /**
  158. * initialize Source Object for given resource
  159. * wrapper for backward compatibility to versions < 3.1.22
  160. * Either [$_template] or [$smarty, $template_resource] must be specified
  161. *
  162. * @param Smarty_Internal_Template $_template template object
  163. * @param Smarty $smarty smarty object
  164. * @param string $template_resource resource identifier
  165. *
  166. * @return \Smarty_Template_Source Source Object
  167. * @throws \SmartyException
  168. */
  169. public static function source(
  170. Smarty_Internal_Template $_template = null,
  171. Smarty $smarty = null,
  172. $template_resource = null
  173. ) {
  174. return Smarty_Template_Source::load($_template, $smarty, $template_resource);
  175. }
  176. /**
  177. * Load template's source into current template object
  178. *
  179. * @param Smarty_Template_Source $source source object
  180. *
  181. * @return string template source
  182. * @throws SmartyException if source cannot be loaded
  183. */
  184. abstract public function getContent(Smarty_Template_Source $source);
  185. /**
  186. * populate Source Object with meta data from Resource
  187. *
  188. * @param Smarty_Template_Source $source source object
  189. * @param Smarty_Internal_Template $_template template object
  190. */
  191. abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
  192. /**
  193. * populate Source Object with timestamp and exists from Resource
  194. *
  195. * @param Smarty_Template_Source $source source object
  196. */
  197. public function populateTimestamp(Smarty_Template_Source $source)
  198. {
  199. // intentionally left blank
  200. }
  201. /**
  202. * modify resource_name according to resource handlers specifications
  203. *
  204. * @param Smarty $smarty Smarty instance
  205. * @param string $resource_name resource_name to make unique
  206. * @param boolean $isConfig flag for config resource
  207. *
  208. * @return string unique resource name
  209. */
  210. public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
  211. {
  212. if ($isConfig) {
  213. if (!isset($smarty->_joined_config_dir)) {
  214. $smarty->getTemplateDir(null, true);
  215. }
  216. return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
  217. } else {
  218. if (!isset($smarty->_joined_template_dir)) {
  219. $smarty->getTemplateDir();
  220. }
  221. return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
  222. }
  223. }
  224. /*
  225. * Check if resource must check time stamps when when loading complied or cached templates.
  226. * Resources like 'extends' which use source components my disable timestamp checks on own resource.
  227. *
  228. * @return bool
  229. */
  230. /**
  231. * Determine basename for compiled filename
  232. *
  233. * @param Smarty_Template_Source $source source object
  234. *
  235. * @return string resource's basename
  236. */
  237. public function getBasename(Smarty_Template_Source $source)
  238. {
  239. return basename(preg_replace('![^\w]+!', '_', $source->name));
  240. }
  241. /**
  242. * @return bool
  243. */
  244. public function checkTimestamps()
  245. {
  246. return true;
  247. }
  248. }