smarty_resource.php 9.2 KB

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