Хранилища Subversion ant

Редакция

Редакция 2 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 2 Редакция 3
1
<?php
1
<?php
2
/**
2
/**
3
 * Smarty plugin
3
 * Smarty plugin
4
 * @package Smarty
4
 * @package Smarty
5
 * @subpackage plugins
5
 * @subpackage plugins
6
 */
6
 */
7
7
8
/**
8
/**
9
 * Smarty {config_load} function plugin
9
 * Smarty {config_load} function plugin
10
 *
10
 *
11
 * Type:     function<br>
11
 * Type:     function<br>
12
 * Name:     config_load<br>
12
 * Name:     config_load<br>
13
 * Purpose:  load config file vars
13
 * Purpose:  load config file vars
14
 * @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
14
 * @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
15
 *       (Smarty online manual)
15
 *       (Smarty online manual)
16
 * @author Monte Ohrt <monte at ohrt dot com>
16
 * @author Monte Ohrt <monte at ohrt dot com>
17
 * @author messju mohr <messju at lammfellpuschen dot de> (added use of resources)
17
 * @author messju mohr <messju at lammfellpuschen dot de> (added use of resources)
18
 * @param array Format:
18
 * @param array Format:
19
 * <pre>
19
 * <pre>
20
 * array('file' => required config file name,
20
 * array('file' => required config file name,
21
 *       'section' => optional config file section to load
21
 *       'section' => optional config file section to load
22
 *       'scope' => local/parent/global
22
 *       'scope' => local/parent/global
23
 *       'global' => overrides scope, setting to parent if true)
23
 *       'global' => overrides scope, setting to parent if true)
24
 * </pre>
24
 * </pre>
25
 * @param Smarty
25
 * @param Smarty
26
 */
26
 */
27
function smarty_function_config_load($params, &$smarty)
27
function smarty_function_config_load($params, &$smarty)
28
{
28
{
29
        if ($smarty->debugging) {
29
        if ($smarty->debugging) {
30
            $_params = array();
30
            $_params = array();
31
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
31
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
32
            $_debug_start_time = smarty_core_get_microtime($_params, $smarty);
32
            $_debug_start_time = smarty_core_get_microtime($_params, $smarty);
33
        }
33
        }
34
34
35
        $_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
35
        $_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
36
        $_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
36
        $_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
37
        $_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
37
        $_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
38
        $_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
38
        $_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
39
39
40
        if (!isset($_file) || strlen($_file) == 0) {
40
        if (!isset($_file) || strlen($_file) == 0) {
41
            $smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
41
            $smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
42
        }
42
        }
43
43
44
        if (isset($_scope)) {
44
        if (isset($_scope)) {
45
            if ($_scope != 'local' &&
45
            if ($_scope != 'local' &&
46
                $_scope != 'parent' &&
46
                $_scope != 'parent' &&
47
                $_scope != 'global') {
47
                $_scope != 'global') {
48
                $smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
48
                $smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
49
            }
49
            }
50
        } else {
50
        } else {
51
            if ($_global) {
51
            if ($_global) {
52
                $_scope = 'parent';
52
                $_scope = 'parent';
53
            } else {
53
            } else {
54
                $_scope = 'local';
54
                $_scope = 'local';
55
            }
55
            }
56
        }
56
        }
57
57
58
        $_params = array('resource_name' => $_file,
58
        $_params = array('resource_name' => $_file,
59
                         'resource_base_path' => $smarty->config_dir,
59
                         'resource_base_path' => $smarty->config_dir,
60
                         'get_source' => false);
60
                         'get_source' => false);
61
        $smarty->_parse_resource_name($_params);
61
        $smarty->_parse_resource_name($_params);
62
        $_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
62
        $_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
63
        if (isset($_section))
63
        if (isset($_section))
64
            $_compile_file = $smarty->_get_compile_path($_file_path.'|'.$_section);
64
            $_compile_file = $smarty->_get_compile_path($_file_path.'|'.$_section);
65
        else
65
        else
66
            $_compile_file = $smarty->_get_compile_path($_file_path);
66
            $_compile_file = $smarty->_get_compile_path($_file_path);
67
67
68
        if($smarty->force_compile || !file_exists($_compile_file)) {
68
        if($smarty->force_compile || !file_exists($_compile_file)) {
69
            $_compile = true;
69
            $_compile = true;
70
        } elseif ($smarty->compile_check) {
70
        } elseif ($smarty->compile_check) {
71
            $_params = array('resource_name' => $_file,
71
            $_params = array('resource_name' => $_file,
72
                             'resource_base_path' => $smarty->config_dir,
72
                             'resource_base_path' => $smarty->config_dir,
73
                             'get_source' => false);
73
                             'get_source' => false);
74
            $_compile = $smarty->_fetch_resource_info($_params) &&
74
            $_compile = $smarty->_fetch_resource_info($_params) &&
75
                $_params['resource_timestamp'] > filemtime($_compile_file);
75
                $_params['resource_timestamp'] > filemtime($_compile_file);
76
        } else {
76
        } else {
77
            $_compile = false;
77
            $_compile = false;
78
        }
78
        }
79
79
80
        if($_compile) {
80
        if($_compile) {
81
            // compile config file
81
            // compile config file
82
            if(!is_object($smarty->_conf_obj)) {
82
            if(!is_object($smarty->_conf_obj)) {
83
                require_once SMARTY_DIR . $smarty->config_class . '.class.php';
83
                require_once SMARTY_DIR . $smarty->config_class . '.class.php';
84
                $smarty->_conf_obj = new $smarty->config_class();
84
                $smarty->_conf_obj = new $smarty->config_class();
85
                $smarty->_conf_obj->overwrite = $smarty->config_overwrite;
85
                $smarty->_conf_obj->overwrite = $smarty->config_overwrite;
86
                $smarty->_conf_obj->booleanize = $smarty->config_booleanize;
86
                $smarty->_conf_obj->booleanize = $smarty->config_booleanize;
87
                $smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
87
                $smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
88
                $smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
88
                $smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
89
            }
89
            }
90
90
91
            $_params = array('resource_name' => $_file,
91
            $_params = array('resource_name' => $_file,
92
                             'resource_base_path' => $smarty->config_dir,
92
                             'resource_base_path' => $smarty->config_dir,
93
                             $_params['get_source'] = true);
93
                             $_params['get_source'] = true);
94
            if (!$smarty->_fetch_resource_info($_params)) {
94
            if (!$smarty->_fetch_resource_info($_params)) {
95
                return;
95
                return;
96
            }
96
            }
97
            $smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
97
            $smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
98
            $_config_vars = array_merge($smarty->_conf_obj->get($_file),
98
            $_config_vars = array_merge($smarty->_conf_obj->get($_file),
99
                    $smarty->_conf_obj->get($_file, $_section));
99
                    $smarty->_conf_obj->get($_file, $_section));
100
            if(function_exists('var_export')) {
100
            if(function_exists('var_export')) {
101
                $_output = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; ?>';
101
                $_output = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; ?>';
102
            } else {
102
            } else {
103
                $_output = '<?php $_config_vars = unserialize(\'' . strtr(serialize($_config_vars),array('\''=>'\\\'', '\\'=>'\\\\')) . '\'); ?>';
103
                $_output = '<?php $_config_vars = unserialize(\'' . strtr(serialize($_config_vars),array('\''=>'\\\'', '\\'=>'\\\\')) . '\'); ?>';
104
            }
104
            }
105
            $_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
105
            $_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
106
            require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
106
            require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
107
            smarty_core_write_compiled_resource($_params, $smarty);
107
            smarty_core_write_compiled_resource($_params, $smarty);
108
        } else {
108
        } else {
109
            include($_compile_file);
109
            include($_compile_file);
110
        }
110
        }
111
111
112
        if ($smarty->caching) {
112
        if ($smarty->caching) {
113
            $smarty->_cache_info['config'][$_file] = true;
113
            $smarty->_cache_info['config'][$_file] = true;
114
        }
114
        }
115
115
116
        $smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
116
        $smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
117
        $smarty->_config[0]['files'][$_file] = true;
117
        $smarty->_config[0]['files'][$_file] = true;
118
118
119
        if ($_scope == 'parent') {
119
        if ($_scope == 'parent') {
120
                $smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
120
                $smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
121
                $smarty->_config[1]['files'][$_file] = true;
121
                $smarty->_config[1]['files'][$_file] = true;
122
        } else if ($_scope == 'global') {
122
        } else if ($_scope == 'global') {
123
            for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
123
            for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
124
                $smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
124
                $smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
125
                $smarty->_config[$i]['files'][$_file] = true;
125
                $smarty->_config[$i]['files'][$_file] = true;
126
            }
126
            }
127
        }
127
        }
128
128
129
        if ($smarty->debugging) {
129
        if ($smarty->debugging) {
130
            $_params = array();
130
            $_params = array();
131
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
131
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
132
            $smarty->_smarty_debug_info[] = array('type'      => 'config',
132
            $smarty->_smarty_debug_info[] = array('type'      => 'config',
133
                                                'filename'  => $_file.' ['.$_section.'] '.$_scope,
133
                                                'filename'  => $_file.' ['.$_section.'] '.$_scope,
134
                                                'depth'     => $smarty->_inclusion_depth,
134
                                                'depth'     => $smarty->_inclusion_depth,
135
                                                'exec_time' => smarty_core_get_microtime($_params, $smarty) - $_debug_start_time);
135
                                                'exec_time' => smarty_core_get_microtime($_params, $smarty) - $_debug_start_time);
136
        }
136
        }
137
137
138
}
138
}
139
139
140
/* vim: set expandtab: */
140
/* vim: set expandtab: */
141
141
142
?>
142
?>
143
 
143