1<?php
2/**
3 * Smarty Internal Plugin Compile Include PHP
4 * Compiles the {include_php} tag
5 *
6 * @package    Smarty
7 * @subpackage Compiler
8 * @author     Uwe Tews
9 */
10
11/**
12 * Smarty Internal Plugin Compile Insert Class
13 *
14 * @package    Smarty
15 * @subpackage Compiler
16 */
17class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
18{
19    /**
20     * Attribute definition: Overwrites base class.
21     *
22     * @var array
23     * @see Smarty_Internal_CompileBase
24     */
25    public $required_attributes = array('file');
26
27    /**
28     * Attribute definition: Overwrites base class.
29     *
30     * @var array
31     * @see Smarty_Internal_CompileBase
32     */
33    public $shorttag_order = array('file');
34
35    /**
36     * Attribute definition: Overwrites base class.
37     *
38     * @var array
39     * @see Smarty_Internal_CompileBase
40     */
41    public $optional_attributes = array('once', 'assign');
42
43    /**
44     * Compiles code for the {include_php} tag
45     *
46     * @param array                                 $args     array with attributes from parser
47     * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
48     *
49     * @return string
50     * @throws \SmartyCompilerException
51     * @throws \SmartyException
52     */
53    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
54    {
55        if (!($compiler->smarty instanceof SmartyBC)) {
56            throw new SmartyException("{include_php} is deprecated, use SmartyBC class to enable");
57        }
58        // check and get attributes
59        $_attr = $this->getAttributes($compiler, $args);
60        /**
61         *
62         *
63         * @var Smarty_Internal_Template $_smarty_tpl
64         * used in evaluated code
65         */
66        $_smarty_tpl = $compiler->template;
67        $_filepath = false;
68        $_file = null;
69        eval('$_file = @' . $_attr[ 'file' ] . ';');
70        if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
71            $_filepath = $compiler->smarty->_realpath($_file, true);
72        } else {
73            if (isset($compiler->smarty->security_policy)) {
74                $_dir = $compiler->smarty->security_policy->trusted_dir;
75            } else {
76                $_dir = $compiler->smarty->trusted_dir;
77            }
78            if (!empty($_dir)) {
79                foreach ((array)$_dir as $_script_dir) {
80                    $_path = $compiler->smarty->_realpath($_script_dir . DIRECTORY_SEPARATOR . $_file, true);
81                    if (file_exists($_path)) {
82                        $_filepath = $_path;
83                        break;
84                    }
85                }
86            }
87        }
88        if ($_filepath === false) {
89            $compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", null, true);
90        }
91        if (isset($compiler->smarty->security_policy)) {
92            $compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
93        }
94        if (isset($_attr[ 'assign' ])) {
95            // output will be stored in a smarty variable instead of being displayed
96            $_assign = $_attr[ 'assign' ];
97        }
98        $_once = '_once';
99        if (isset($_attr[ 'once' ])) {
100            if ($_attr[ 'once' ] === 'false') {
101                $_once = '';
102            }
103        }
104        if (isset($_assign)) {
105            return "<?php ob_start();\ninclude{$_once} ('{$_filepath}');\n\$_smarty_tpl->assign({$_assign},ob_get_clean());\n?>";
106        } else {
107            return "<?php include{$_once} ('{$_filepath}');?>\n";
108        }
109    }
110}
111