1<?php
2
3/**
4 * Smarty Method ConfigLoad
5 *
6 * Smarty::configLoad() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_ConfigLoad
13{
14    /**
15     * Valid for all objects
16     *
17     * @var int
18     */
19    public $objMap = 7;
20
21    /**
22     * load a config file, optionally load just selected sections
23     *
24     * @api  Smarty::configLoad()
25     * @link http://www.smarty.net/docs/en/api.config.load.tpl
26     *
27     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
28     * @param  string                                                 $config_file filename
29     * @param  mixed                                                  $sections    array of section names, single
30     *                                                                             section or null
31     *
32     * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
33     * @throws \SmartyException
34     */
35    public function configLoad(Smarty_Internal_Data $data, $config_file, $sections = null)
36    {
37        $this->_loadConfigFile($data, $config_file, $sections, null);
38        return $data;
39    }
40
41    /**
42     * load a config file, optionally load just selected sections
43     *
44     * @api  Smarty::configLoad()
45     * @link http://www.smarty.net/docs/en/api.config.load.tpl
46     *
47     * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
48     * @param  string                                                 $config_file filename
49     * @param  mixed                                                  $sections    array of section names, single
50     *                                                                             section or null
51     * @param int                                                     $scope       scope into which config variables
52     *                                                                             shall be loaded
53     *
54     * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
55     * @throws \SmartyException
56     */
57    public function _loadConfigFile(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 0)
58    {
59        /* @var \Smarty $smarty */
60        $smarty = $data->_getSmartyObj();
61        /* @var \Smarty_Internal_Template $confObj */
62        $confObj = new Smarty_Internal_Template($config_file, $smarty, $data, null, null, null, null, true);
63        $confObj->caching = Smarty::CACHING_OFF;
64        $confObj->source->config_sections = $sections;
65        $confObj->source->scope = $scope;
66        $confObj->compiled = Smarty_Template_Compiled::load($confObj);
67        $confObj->compiled->render($confObj);
68        if ($data->_isTplObj()) {
69            $data->compiled->file_dependency[ $confObj->source->uid ] =
70                array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
71        }
72    }
73
74    /**
75     * load config variables into template object
76     *
77     * @param \Smarty_Internal_Template $tpl
78     * @param  array                    $new_config_vars
79     *
80     */
81    public function _loadConfigVars(Smarty_Internal_Template $tpl, $new_config_vars)
82    {
83        $this->_assignConfigVars($tpl->parent->config_vars, $tpl, $new_config_vars);
84        $tagScope = $tpl->source->scope;
85        if ($tagScope >= 0) {
86            if ($tagScope == Smarty::SCOPE_LOCAL) {
87                $this->_updateVarStack($tpl, $new_config_vars);
88                $tagScope = 0;
89                if (!$tpl->scope) {
90                    return;
91                }
92            }
93            if ($tpl->parent->_isTplObj() && ($tagScope || $tpl->parent->scope)) {
94                $mergedScope = $tagScope | $tpl->scope;
95                if ($mergedScope) {
96                    // update scopes
97                    foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) {
98                        $this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars);
99                        if ($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
100                            $this->_updateVarStack($tpl, $new_config_vars);
101                        }
102                    }
103                }
104            }
105        }
106    }
107
108    /**
109     * Assign all config variables in given scope
110     *
111     * @param array                     $config_vars     config variables in scope
112     * @param \Smarty_Internal_Template $tpl
113     * @param  array                    $new_config_vars loaded config variables
114     */
115    public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars)
116    {
117        // copy global config vars
118        foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
119            if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
120                $config_vars[ $variable ] = $value;
121            } else {
122                $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
123            }
124        }
125        // scan sections
126        $sections = $tpl->source->config_sections;
127        if (!empty($sections)) {
128            foreach ((array) $sections as $tpl_section) {
129                if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) {
130                    foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) {
131                        if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
132                            $config_vars[ $variable ] = $value;
133                        } else {
134                            $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
135                        }
136                    }
137                }
138            }
139        }
140    }
141
142    /**
143     * Update config variables in template local variable stack
144     *
145     * @param \Smarty_Internal_Template $tpl
146     * @param array                     $config_vars
147     */
148    public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars)
149    {
150        $i = 0;
151        while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
152            $this->_assignConfigVars($tpl->_cache[ 'varStack' ][ $i ][ 'config' ], $tpl, $config_vars);
153            $i ++;
154        }
155    }
156
157    /**
158     * gets  a config variable value
159     *
160     * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
161     * @param string                                                  $varName the name of the config variable
162     * @param bool                                                    $errorEnable
163     *
164     * @return null|string  the value of the config variable
165     */
166    public function _getConfigVariable(Smarty_Internal_Data $data, $varName, $errorEnable = true)
167    {
168        $_ptr = $data;
169        while ($_ptr !== null) {
170            if (isset($_ptr->config_vars[ $varName ])) {
171                // found it, return it
172                return $_ptr->config_vars[ $varName ];
173            }
174            // not found, try at parent
175            $_ptr = $_ptr->parent;
176        }
177        if ($data->smarty->error_unassigned && $errorEnable) {
178            // force a notice
179            $x = $$varName;
180        }
181        return null;
182    }
183}
184