1 <?php |
|
2 /** |
|
3 * Smarty Internal Extension |
|
4 * This file contains the Smarty template extension to create a code frame |
|
5 * |
|
6 * @package Smarty |
|
7 * @subpackage Template |
|
8 * @author Uwe Tews |
|
9 */ |
|
10 |
|
11 /** |
|
12 * Class Smarty_Internal_Extension_CodeFrame |
|
13 * Create code frame for compiled and cached templates |
|
14 */ |
|
15 class Smarty_Internal_Extension_CodeFrame { |
|
16 /** |
|
17 * Create code frame for compiled and cached templates |
|
18 * |
|
19 * @param Smarty_Internal_Template $_template |
|
20 * @param string $content optional template content |
|
21 * @param bool $cache flag for cache file |
|
22 * |
|
23 * @return string |
|
24 */ |
|
25 public static function create(Smarty_Internal_Template $_template, $content = '', $cache = false) { |
|
26 // build property code |
|
27 $_template->properties['has_nocache_code'] = $_template->has_nocache_code || !empty($_template->required_plugins['nocache']); |
|
28 $_template->properties['version'] = Smarty::SMARTY_VERSION; |
|
29 if (!isset($_template->properties['unifunc'])) { |
|
30 $_template->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); |
|
31 } |
|
32 $properties = $_template->properties; |
|
33 if (!$cache) { |
|
34 unset($properties['tpl_function']); |
|
35 if (!empty($_template->compiler->templateProperties)) { |
|
36 $properties['tpl_function'] = $_template->compiler->templateProperties['tpl_function']; |
|
37 } |
|
38 } |
|
39 $output = "<?php\n"; |
|
40 $output .= "/*%%SmartyHeaderCode:{$_template->properties['nocache_hash']}%%*/\n"; |
|
41 if ($_template->smarty->direct_access_security) { |
|
42 $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n"; |
|
43 } |
|
44 $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ");\n"; |
|
45 $output .= "/*/%%SmartyHeaderCode%%*/\n"; |
|
46 $output .= "if (\$_valid && !is_callable('{$_template->properties['unifunc']}')) {\n"; |
|
47 $output .= "function {$_template->properties['unifunc']} (\$_smarty_tpl) {\n"; |
|
48 // include code for plugins |
|
49 if (!$cache) { |
|
50 if (!empty($_template->required_plugins['compiled'])) { |
|
51 foreach ($_template->required_plugins['compiled'] as $tmp) { |
|
52 foreach ($tmp as $data) { |
|
53 $file = addslashes($data['file']); |
|
54 if (is_Array($data['function'])) { |
|
55 $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n"; |
|
56 } else { |
|
57 $output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n"; |
|
58 } |
|
59 } |
|
60 } |
|
61 } |
|
62 if (!empty($_template->required_plugins['nocache'])) { |
|
63 $_template->has_nocache_code = true; |
|
64 $output .= "echo '/*%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; "; |
|
65 foreach ($_template->required_plugins['nocache'] as $tmp) { |
|
66 foreach ($tmp as $data) { |
|
67 $file = addslashes($data['file']); |
|
68 if (is_Array($data['function'])) { |
|
69 $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n"); |
|
70 } else { |
|
71 $output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n"); |
|
72 } |
|
73 } |
|
74 } |
|
75 $output .= "?>/*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/';\n"; |
|
76 } |
|
77 } |
|
78 $output .= "?>\n"; |
|
79 $output = self::appendCode($output, $content); |
|
80 return self::appendCode($output, "<?php }\n}\n?>"); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Create code frame of compiled template function |
|
85 * |
|
86 * @param \Smarty_Internal_Template $_template |
|
87 * @param string $content |
|
88 * |
|
89 * @return string |
|
90 */ |
|
91 public static function createFunctionFrame(Smarty_Internal_Template $_template, $content = '') { |
|
92 if (!isset($_template->properties['unifunc'])) { |
|
93 $_template->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); |
|
94 } |
|
95 $output = "<?php\n"; |
|
96 $output .= "/*%%SmartyHeaderCode:{$_template->properties['nocache_hash']}%%*/\n"; |
|
97 $output .= "if (\$_valid && !is_callable('{$_template->properties['unifunc']}')) {\n"; |
|
98 $output .= "function {$_template->properties['unifunc']} (\$_smarty_tpl) {\n"; |
|
99 $output .= "?>\n" . $content; |
|
100 $output .= "<?php\n"; |
|
101 $output .= "/*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/\n"; |
|
102 $output .= "}\n}\n?>"; |
|
103 return $output; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Append code segments and remove unneeded ?> <?php transitions |
|
108 * |
|
109 * @param string $left |
|
110 * @param string $right |
|
111 * |
|
112 * @return string |
|
113 */ |
|
114 public static function appendCode($left, $right) { |
|
115 if (preg_match('/\s*\?>$/', $left) && preg_match('/^<\?php\s+/', $right)) { |
|
116 $left = preg_replace('/\s*\?>$/', "\n", $left); |
|
117 $left .= preg_replace('/^<\?php\s+/', '', $right); |
|
118 } else { |
|
119 $left .= $right; |
|
120 } |
|
121 return $left; |
|
122 } |
|
123 } |
|