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

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