You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_resource.php 9.4KB

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