4 * Smarty Internal Plugin Compile Include PHP
6 * Compiles the {include_php} tag
14 * Smarty Internal Plugin Compile Insert Class
16 class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase {
17 // attribute definitions
18 public $required_attributes = array('file');
19 public $shorttag_order = array('file');
20 public $optional_attributes = array('once', 'assign');
23 * Compiles code for the {include_php} tag
25 * @param array $args array with attributes from parser
26 * @param object $compiler compiler object
27 * @return string compiled code
29 public function compile($args, $compiler)
31 if (!$compiler->smarty->allow_php_tag) {
32 throw new SmartyException("{include_php} is deprecated, set allow_php_tag = true to enable");
34 $this->compiler = $compiler;
35 // check and get attributes
36 $_attr = $this->_get_attributes($args);
40 $_smarty_tpl = $compiler->template;
42 eval('$_file = ' . $_attr['file'] . ';');
43 if (!isset($this->compiler->smarty->security_policy) && file_exists($_file)) {
46 if (isset($this->compiler->smarty->security_policy)) {
47 $_dir = $this->compiler->smarty->security_policy->trusted_dir;
49 $_dir = $this->compiler->smarty->trusted_dir;
52 foreach((array)$_dir as $_script_dir) {
53 if (strpos('/\\', substr($_script_dir, -1)) === false) {
56 if (file_exists($_script_dir . $_file)) {
57 $_filepath = $_script_dir . $_file;
63 if ($_filepath == false) {
64 $this->compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", $this->compiler->lex->taglineno);
67 if (isset($this->compiler->smarty->security_policy)) {
68 $this->compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
71 if (isset($_attr['assign'])) {
72 // output will be stored in a smarty variable instead of being displayed
73 $_assign = $_attr['assign'];
76 if (isset($_attr['once'])) {
77 if ($_attr['once'] == 'false') {
82 if (isset($_assign)) {
83 return "<?php ob_start(); include{$_once} ('{$_filepath}'); \$_smarty_tpl->assign({$_assign},ob_get_contents()); ob_end_clean();?>";
85 return "<?php include{$_once} ('{$_filepath}');?>\n";