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