root/branches/release-32/php/lib/function.mtinclude.php @ 1554

Revision 1554, 7.6 kB (checked in by takayama, 20 months ago)

Implemented fbz:68986
* Implemented UI for session cache
* Implemented php side

  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8global $restricted_include_filenames;
9$restricted_include_filenames = array('mt-config.cgi' => 1, 'passwd' => 1);
10
11function smarty_function_mtinclude($args, &$ctx) {
12    // status: partial
13    // parameters: module, file
14    // notes: file case needs work -- search through blog site archive path, etc...
15
16    // push to ctx->vars
17    $ext_args = array();
18    while(list ($key, $val) = each($args)) {
19        if (!preg_match('/(^file$|^module$|^widget$|^blog_id$|^identifier$|^type$)/', $key)) {
20            require_once("function.mtsetvar.php");
21            smarty_function_mtsetvar(array('name' => $key, 'value' => $val), $ctx);
22            $ext_args[] = $key;
23        }
24    }
25
26    static $_include_cache = array();
27    $blog_id = $args['blog_id'];
28    $blog_id or $blog_id = $ctx->stash('blog_id');
29    $cache_id = '';
30
31    # Try to read from cache
32    global $mt;
33
34    $cache_enable = false;
35    $cacje_key = '';
36    $cache_ttl = 0;
37    $blog = $ctx->mt->db->fetch_blog($blog_id);
38
39    if ((isset($args['module']) || isset($args['widget']) || isset($args['identifier']))
40        && $blog['blog_include_cache'] == 1
41        && ((isset($args['cache']) && $args['cache'] == '1') || isset($args['key']) || isset($args['ttl'])) )
42    {
43        $tmpl_name = $args['module'];
44        $tmpl_name or $tmpl_name = $args['widget'];
45        $tmpl_name or $tmpl_name = $args['identifier'];
46        $type = $args['Widget'] ? 'widget' : 'custom';
47        if ($type == 'custom' && preg_match('/^Widget:/', $tmpl_name))
48            $type = 'widget';
49
50
51        $cache_enable = true;
52        $cache_key = isset($args['key'])
53            ? $args['key']
54            : 'blog::' . $blog_id . '::template_' . $type  . '::' . $tmpl_name;
55        $cache_ttl = isset($args['ttl']) ? $args['ttl'] : 60 * 60; # default 60 min.
56
57
58        $cache_driver = $mt->cache_driver($cache_ttl);
59        $cached_val = $cache_driver->get($cache_key, $cache_ttl);
60        if (!empty($cached_val))
61            return $cached_val;
62    }
63
64    if (isset($args['module']) && ($args['module'])) {
65        $module = $args['module'];
66        if (preg_match('/^Widget:/', $module)) {
67            $args['widget'] = preg_replace('/^Widget: ?/', '', $module);
68            unset($args['module']);
69        }
70    }
71    if (isset($args['module']) && ($args['module'])) {
72        $module = $args['module'];
73        $cache_id = 'module::' . $blog_id . '::' . $module;
74        if (isset($_include_cache[$cache_id])) {
75            $_var_compiled = $_include_cache[$cache_id];
76        } else {
77            $tmpl = $ctx->mt->db->get_template_text($ctx, $module, $blog_id, 'custom', $args['global']);
78            if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
79                $_include_cache[$cache_id] = $_var_compiled;
80            } else {
81                _clear_vars($ctx, $ext_args);
82                return $ctx->error("Error compiling template module '$module'");
83            }
84        }
85    } elseif (isset($args['widget']) && ($args['widget'])) {
86        $module = $args['widget'];
87        $cache_id = 'widget::' . $blog_id . '::' . $module;
88        if (isset($_include_cache[$cache_id])) {
89            $_var_compiled = $_include_cache[$cache_id];
90        } else {
91            $tmpl = $ctx->mt->db->get_template_text($ctx, $module, $blog_id, 'widget', $args['global']);
92            if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
93                $_include_cache[$cache_id] = $_var_compiled;
94            } else {
95                _clear_vars($ctx, $ext_args);
96                return $ctx->error("Error compiling template module '$module'");
97            }
98        }
99    } elseif (isset($args['identifier']) && ($args['identifier'])) {
100        $module = $args['identifier'];
101        $cache_id = 'identifier::' . $blog_id . '::' . $module;
102        if (isset($_include_cache[$cache_id])) {
103            $_var_compiled = $_include_cache[$cache_id];
104        } else {
105            $tmpl = $ctx->mt->db->get_template_text($ctx, $module, $blog_id, '', $args['global']);
106            if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
107                $_include_cache[$cache_id] = $_var_compiled;
108            } else {
109                _clear_vars($ctx, $ext_args);
110                return $ctx->error("Error compiling template module '$module'");
111            }
112        }
113    } elseif (isset($args['file']) && ($args['file'])) {
114        $file = $args['file'];
115        $base_filename = basename($file);
116        global $restricted_include_filenames;
117        if (array_key_exists(strtolower($base_filename), $restricted_include_filenames)) {
118            _clear_vars($ctx, $ext_args);
119            return "";
120        }
121        $cache_id = 'file::' . $blog_id . '::' . $file;
122        if (isset($_include_cache[$cache_id])) {
123            $_var_compiled = $_include_cache[$cache_id];
124        } else {
125            if (is_file($file) && is_readable($file)) {
126                $contents = @file($file);
127                $tmpl = implode('', $contents);
128            } else {
129                $blog = $ctx->stash('blog');
130                if ($blog['blog_id'] != $blog_id) {
131                    $blog = $ctx->mt->db->fetch_blog($blog_id);
132                }
133                $path = $blog['blog_site_path'];
134                if (!preg_match('!/$!', $path))
135                    $path .= '/';
136                $path .= $file;
137                if (is_file($path) && is_readable($path)) {
138                    $contents = @file($path);
139                    $tmpl = implode('', $contents);
140                } else {
141                    _clear_vars($ctx, $ext_args);
142                    return $ctx->error("Could not open file '$file'");
143                }
144            }
145            if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
146                $_include_cache[$cache_id] = $_var_compiled;
147            } else {
148                _clear_vars($ctx, $ext_args);
149                return $ctx->error("Error compiling template file '$file'");
150            }
151        }
152    } elseif (isset($args['type']) && ($args['type'])) {
153        $type = $args['type'];
154        $cache_id = 'type::' . $blog_id . '::' . $type;
155        if (isset($_include_cache[$cache_id])) {
156            $_var_compiled = $_include_cache[$cache_id];
157        } else {
158            $tmpl = $ctx->mt->db->load_special_template($ctx, null, $type, $blog_id);
159            if ($tmpl) {
160                if ($ctx->_compile_source('evaluated template', $tmpl['template_text'], $_var_compiled)) {
161                    $_include_cache[$cache_id] = $_var_compiled;
162                } else {
163                    if ($type != 'dynamic_error') {
164                        _clear_vars($ctx, $ext_args);
165                        return $ctx->error("Error compiling template module '$module'");
166                     } else {
167                        _clear_vars($ctx, $ext_args);
168                         return null;
169                     }
170                }
171            } else {
172                _clear_vars($ctx, $ext_args);
173                return null;
174            }
175        }
176    }
177
178    ob_start();
179    $ctx->_eval('?>' . $_var_compiled);
180    $_contents = ob_get_contents();
181    ob_end_clean();
182
183    _clear_vars($ctx, $ext_args);
184
185    if ($cache_enable) {
186        $cache_driver = $mt->cache_driver($cache_ttl);
187        $cache_driver->set($cache_key, $_contents, $cache_ttl);
188    }
189
190    return $_contents;
191}
192
193function _clear_vars(&$ctx, $ext_vars) {
194    # unset vars
195    $vars =& $ctx->__stash['vars'];
196    foreach ($ext_vars as $v) {
197        unset($vars[$v]);
198    }
199    $ctx->__stash['vars'] =& $vars;
200}
201?>
Note: See TracBrowser for help on using the browser.