/application/library/Thirdpart/Smarty/libs/sysplugins/smarty_resource.php

https://gitlab.com/flyhope/Hiblog · PHP · 284 lines · 106 code · 24 blank · 154 comment · 26 complexity · c3687e69c7442b07b9f1e52b8d51a37f MD5 · raw file

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