smarty_internal_templatecompilerbase.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Smarty Template Compiler Base
  4. *
  5. * This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Main compiler class
  13. */
  14. class Smarty_Internal_TemplateCompilerBase {
  15. // hash for nocache sections
  16. private $nocache_hash = null;
  17. // suppress generation of nocache code
  18. public $suppressNocacheProcessing = false;
  19. // compile tag objects
  20. static $_tag_objects = array();
  21. // tag stack
  22. public $_tag_stack = array();
  23. // current template
  24. public $template = null;
  25. /**
  26. * Initialize compiler
  27. */
  28. public function __construct()
  29. {
  30. $this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
  31. }
  32. // abstract function doCompile($_content);
  33. /**
  34. * Methode to compile a Smarty template
  35. *
  36. * @param $template template object to compile
  37. * @return bool true if compiling succeeded, false if it failed
  38. */
  39. public function compileTemplate($template)
  40. {
  41. if (empty($template->properties['nocache_hash'])) {
  42. $template->properties['nocache_hash'] = $this->nocache_hash;
  43. } else {
  44. $this->nocache_hash = $template->properties['nocache_hash'];
  45. }
  46. /* here is where the compiling takes place. Smarty
  47. tags in the templates are replaces with PHP code,
  48. then written to compiled files. */
  49. // flag for nochache sections
  50. $this->nocache = false;
  51. $this->tag_nocache = false;
  52. // save template object in compiler class
  53. $this->template = $template;
  54. $this->smarty->_current_file = $this->template->getTemplateFilepath();
  55. // template header code
  56. $template_header = '';
  57. if (!$template->suppressHeader) {
  58. $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
  59. $template_header .= " compiled from \"" . $this->template->getTemplateFilepath() . "\" */ ?>\n";
  60. }
  61. do {
  62. // flag for aborting current and start recompile
  63. $this->abort_and_recompile = false;
  64. // get template source
  65. $_content = $template->getTemplateSource();
  66. // run prefilter if required
  67. if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
  68. $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $this->smarty, $template);
  69. }
  70. // on empty template just return header
  71. if ($_content == '') {
  72. if ($template->suppressFileDependency) {
  73. $template->compiled_template = '';
  74. } else {
  75. $template->compiled_template = $template_header . $template->createPropertyHeader();
  76. }
  77. return true;
  78. }
  79. // call compiler
  80. $_compiled_code = $this->doCompile($_content);
  81. } while ($this->abort_and_recompile);
  82. // return compiled code to template object
  83. if ($template->suppressFileDependency) {
  84. $template->compiled_template = $_compiled_code;
  85. } else {
  86. $template->compiled_template = $template_header . $template->createPropertyHeader() . $_compiled_code;
  87. }
  88. // run postfilter if required
  89. if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
  90. $template->compiled_template = Smarty_Internal_Filter_Handler::runFilter('post', $template->compiled_template, $this->smarty, $template);
  91. }
  92. }
  93. /**
  94. * Compile Tag
  95. *
  96. * This is a call back from the lexer/parser
  97. * It executes the required compile plugin for the Smarty tag
  98. *
  99. * @param string $tag tag name
  100. * @param array $args array with tag attributes
  101. * @return string compiled code
  102. */
  103. public function compileTag($tag, $args)
  104. {
  105. // $args contains the attributes parsed and compiled by the lexer/parser
  106. // assume that tag does compile into code, but creates no HTML output
  107. $this->has_code = true;
  108. $this->has_output = false;
  109. // compile the smarty tag (required compile classes to compile the tag are autoloaded)
  110. if (($_output = $this->callTagCompiler($tag, $args)) === false) {
  111. if (isset($this->smarty->template_functions[$tag])) {
  112. // template defined by {template} tag
  113. $args['name'] = "'" . $tag . "'";
  114. $_output = $this->callTagCompiler('call', $args);
  115. }
  116. }
  117. if ($_output !== false) {
  118. if ($_output !== true) {
  119. // did we get compiled code
  120. if ($this->has_code) {
  121. // Does it create output?
  122. if ($this->has_output) {
  123. $_output .= "\n";
  124. }
  125. // return compiled code
  126. return $_output;
  127. }
  128. }
  129. // tag did not produce compiled code
  130. return '';
  131. } else {
  132. // not an internal compiler tag
  133. if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
  134. // check if tag is a registered object
  135. if (isset($this->smarty->registered_objects[$tag]) && isset($args['object_methode'])) {
  136. $methode = $args['object_methode'];
  137. unset ($args['object_methode']);
  138. if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
  139. (empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
  140. return $this->callTagCompiler('private_object_function', $args, $tag, $methode);
  141. } elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
  142. return $this->callTagCompiler('private_object_block_function', $args, $tag, $methode);
  143. } else {
  144. return $this->trigger_template_error ('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
  145. }
  146. }
  147. // check if tag is registered
  148. foreach (array('compiler', 'function', 'block') as $type) {
  149. if (isset($this->smarty->registered_plugins[$type][$tag])) {
  150. // if compiler function plugin call it now
  151. if ($type == 'compiler') {
  152. if (!$this->smarty->registered_plugins[$type][$tag][1]) {
  153. $this->tag_nocache = true;
  154. }
  155. $function = $this->smarty->registered_plugins[$type][$tag][0];
  156. if (!is_array($function)) {
  157. return $function($args, $this);
  158. } else if (is_object($function[0])) {
  159. return $this->smarty->registered_plugins[$type][$tag][0][0]->$function[1]($args, $this);
  160. } else {
  161. return call_user_func_array($this->smarty->registered_plugins[$type][$tag][0], array($args, $this));
  162. }
  163. }
  164. // compile registered function or block function
  165. if ($type == 'function' || $type == 'block') {
  166. return $this->callTagCompiler('private_registered_' . $type, $args, $tag);
  167. }
  168. }
  169. }
  170. // check plugins from plugins folder
  171. foreach ($this->smarty->plugin_search_order as $plugin_type) {
  172. if ($plugin_type == 'compiler' && $this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
  173. $plugin = 'smarty_compiler_' . $tag;
  174. if (is_callable($plugin)) {
  175. return $plugin($args, $this->smarty);
  176. }
  177. if (class_exists($plugin, false)) {
  178. $plugin_object = new $plugin;
  179. if (method_exists($plugin_object, 'compile')) {
  180. return $plugin_object->compile($args, $this);
  181. }
  182. }
  183. throw new Exception("Plugin \"{$tag}\" not callable");
  184. } else {
  185. if ($function = $this->getPlugin($tag, $plugin_type)) {
  186. return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $tag, $function);
  187. }
  188. }
  189. }
  190. } else {
  191. // compile closing tag of block function
  192. $base_tag = substr($tag, 0, -5);
  193. // check if closing tag is a registered object
  194. if (isset($this->smarty->registered_objects[$base_tag]) && isset($args['object_methode'])) {
  195. $methode = $args['object_methode'];
  196. unset ($args['object_methode']);
  197. if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
  198. return $this->callTagCompiler('private_object_block_function', $args, $tag, $methode);
  199. } else {
  200. return $this->trigger_template_error ('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
  201. }
  202. }
  203. // registered block tag ?
  204. if (isset($this->smarty->registered_plugins['block'][$base_tag])) {
  205. return $this->callTagCompiler('private_registered_block', $args, $tag);
  206. }
  207. // block plugin?
  208. if ($function = $this->getPlugin($base_tag, 'block')) {
  209. return $this->callTagCompiler('private_block_plugin', $args, $tag, $function);
  210. }
  211. if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
  212. $plugin = 'smarty_compiler_' . $tag;
  213. if (is_callable($plugin)) {
  214. return $plugin($args, $this->smarty);
  215. }
  216. if (class_exists($plugin, false)) {
  217. $plugin_object = new $plugin;
  218. if (method_exists($plugin_object, 'compile')) {
  219. return $plugin_object->compile($args, $this);
  220. }
  221. }
  222. throw new Exception("Plugin \"{$tag}\" not callable");
  223. }
  224. }
  225. $this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
  226. }
  227. }
  228. /**
  229. * lazy loads internal compile plugin for tag and calls the compile methode
  230. *
  231. * compile objects cached for reuse.
  232. * class name format: Smarty_Internal_Compile_TagName
  233. * plugin filename format: Smarty_Internal_Tagname.php
  234. *
  235. * @param $tag string tag name
  236. * @param $args array with tag attributes
  237. * @param $param1 optional parameter
  238. * @param $param2 optional parameter
  239. * @param $param3 optional parameter
  240. * @return string compiled code
  241. */
  242. public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
  243. {
  244. // re-use object if already exists
  245. if (isset(self::$_tag_objects[$tag])) {
  246. // compile this tag
  247. return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
  248. }
  249. // lazy load internal compiler plugin
  250. $class_name = 'Smarty_Internal_Compile_' . $tag;
  251. if ($this->smarty->loadPlugin($class_name)) {
  252. // use plugin if found
  253. self::$_tag_objects[$tag] = new $class_name;
  254. // compile this tag
  255. return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
  256. }
  257. // no internal compile plugin for this tag
  258. return false;
  259. }
  260. /**
  261. * Check for plugins and return function name
  262. *
  263. * @param $pugin_name string name of plugin or function
  264. * @param $type string type of plugin
  265. * @return string call name of function
  266. */
  267. public function getPlugin($plugin_name, $type)
  268. {
  269. $function = null;
  270. if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
  271. if (isset($this->template->required_plugins['nocache'][$plugin_name][$type])) {
  272. $function = $this->template->required_plugins['nocache'][$plugin_name][$type]['function'];
  273. } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
  274. $this->template->required_plugins['nocache'][$plugin_name][$type] = $this->template->required_plugins['compiled'][$plugin_name][$type];
  275. $function = $this->template->required_plugins['nocache'][$plugin_name][$type]['function'];
  276. }
  277. } else {
  278. if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
  279. $function = $this->template->required_plugins['compiled'][$plugin_name][$type]['function'];
  280. } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$type])) {
  281. $this->template->required_plugins['compiled'][$plugin_name][$type] = $this->template->required_plugins['nocache'][$plugin_name][$type];
  282. $function = $this->template->required_plugins['compiled'][$plugin_name][$type]['function'];
  283. }
  284. }
  285. if (isset($function)) {
  286. if ($type == 'modifier') {
  287. $this->template->saved_modifier[$plugin_name] = true;
  288. }
  289. return $function;
  290. }
  291. // loop through plugin dirs and find the plugin
  292. $function = 'smarty_' . $type . '_' . $plugin_name;
  293. $found = false;
  294. foreach((array)$this->smarty->plugins_dir as $_plugin_dir) {
  295. $file = rtrim($_plugin_dir, '/\\') . DS . $type . '.' . $plugin_name . '.php';
  296. if (file_exists($file)) {
  297. // require_once($file);
  298. $found = true;
  299. break;
  300. }
  301. }
  302. if ($found) {
  303. if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
  304. $this->template->required_plugins['nocache'][$plugin_name][$type]['file'] = $file;
  305. $this->template->required_plugins['nocache'][$plugin_name][$type]['function'] = $function;
  306. } else {
  307. $this->template->required_plugins['compiled'][$plugin_name][$type]['file'] = $file;
  308. $this->template->required_plugins['compiled'][$plugin_name][$type]['function'] = $function;
  309. }
  310. if ($type == 'modifier') {
  311. $this->template->saved_modifier[$plugin_name] = true;
  312. }
  313. return $function;
  314. }
  315. if (is_callable($function)) {
  316. // plugin function is defined in the script
  317. return $function;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Inject inline code for nocache template sections
  323. *
  324. * This method gets the content of each template element from the parser.
  325. * If the content is compiled code and it should be not cached the code is injected
  326. * into the rendered output.
  327. *
  328. * @param string $content content of template element
  329. * @param boolean $tag_nocache true if the parser detected a nocache situation
  330. * @param boolean $is_code true if content is compiled code
  331. * @return string content
  332. */
  333. public function processNocacheCode ($content, $is_code)
  334. {
  335. // If the template is not evaluated and we have a nocache section and or a nocache tag
  336. if ($is_code && !empty($content)) {
  337. // generate replacement code
  338. if ((!$this->template->resource_object->isEvaluated || $this->template->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
  339. ($this->nocache || $this->tag_nocache || $this->template->forceNocache == 2)) {
  340. $this->template->has_nocache_code = true;
  341. $_output = str_replace("'", "\'", $content);
  342. $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>";
  343. // make sure we include modifer plugins for nocache code
  344. if (isset($this->template->saved_modifier)) {
  345. foreach ($this->template->saved_modifier as $plugin_name => $dummy) {
  346. if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
  347. $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
  348. }
  349. }
  350. $this->template->saved_modifier = null;
  351. }
  352. } else {
  353. $_output = $content;
  354. }
  355. } else {
  356. $_output = $content;
  357. }
  358. $this->suppressNocacheProcessing = false;
  359. $this->tag_nocache = false;
  360. return $_output;
  361. }
  362. /**
  363. * display compiler error messages without dying
  364. *
  365. * If parameter $args is empty it is a parser detected syntax error.
  366. * In this case the parser is called to obtain information about expected tokens.
  367. *
  368. * If parameter $args contains a string this is used as error message
  369. *
  370. * @param $args string individual error message or null
  371. */
  372. public function trigger_template_error($args = null, $line = null)
  373. {
  374. // get template source line which has error
  375. if (!isset($line)) {
  376. $line = $this->lex->line;
  377. }
  378. $match = preg_split("/\n/", $this->lex->data);
  379. $error_text = 'Syntax Error in template "' . $this->template->getTemplateFilepath() . '" on line ' . $line . ' "' . htmlspecialchars($match[$line-1]) . '" ';
  380. if (isset($args)) {
  381. // individual error message
  382. $error_text .= $args;
  383. } else {
  384. // expected token from parser
  385. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  386. $exp_token = $this->parser->yyTokenName[$token];
  387. if (isset($this->lex->smarty_token_names[$exp_token])) {
  388. // token type from lexer
  389. $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
  390. } else {
  391. // otherwise internal token name
  392. $expect[] = $this->parser->yyTokenName[$token];
  393. }
  394. }
  395. // output parser error message
  396. $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
  397. }
  398. throw new Exception($error_text);
  399. }
  400. }
  401. ?>