| 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 | | |
|---|
| | 30 | // When the module name starts by 'Widget', it converts to 'Widget' from 'Module'. |
|---|
| 71 | | if (isset($args['module']) && ($args['module'])) { |
|---|
| 72 | | $module = $args['module']; |
|---|
| 73 | | $cache_id = 'module::' . $blog_id . '::' . $module; |
|---|
| | 38 | |
|---|
| | 39 | // Fetch template meta data |
|---|
| | 40 | $load_type = null; |
|---|
| | 41 | $load_name = null; |
|---|
| | 42 | if (isset($args['module'])) { |
|---|
| | 43 | $load_type = 'custom'; |
|---|
| | 44 | $load_name = $args['module']; |
|---|
| | 45 | } elseif (isset($args['widget'])) { |
|---|
| | 46 | $load_type = 'widget'; |
|---|
| | 47 | $load_name = $args['widget']; |
|---|
| | 48 | } elseif (isset($args['identifier'])) { |
|---|
| | 49 | $load_type = 'identifier'; |
|---|
| | 50 | $load_name = $args['identifier']; |
|---|
| | 51 | } |
|---|
| | 52 | |
|---|
| | 53 | $tmpl_meta = array(); |
|---|
| | 54 | if (!empty($load_type)) { |
|---|
| | 55 | $is_global = isset($args['global']) && $args['global'] ? 1 : 0; |
|---|
| | 56 | $tmpl_meta = $ctx->mt->db->fetch_template_meta($load_type, $load_name, $blog_id, $is_global); |
|---|
| | 57 | } |
|---|
| | 58 | |
|---|
| | 59 | # Convert to phrase of PHP Include |
|---|
| | 60 | $ssi_enable = false; |
|---|
| | 61 | $include_file = ''; |
|---|
| | 62 | if (!empty($load_type) && |
|---|
| | 63 | isset($blog) && $blog['blog_include_system'] == 'php' && |
|---|
| | 64 | ((isset($args['ssi']) && $args['ssi']) || (isset($tmpl_meta['include_with_ssi']) && $tmpl_meta['include_with_ssi']))) { |
|---|
| | 65 | |
|---|
| | 66 | $ssi_enable = true; |
|---|
| | 67 | |
|---|
| | 68 | // Generates include path using Key |
|---|
| | 69 | $base_path = ''; |
|---|
| | 70 | if (isset($args['key'])) { |
|---|
| | 71 | $base_path = $args['key']; |
|---|
| | 72 | } elseif(isset($args['cache_key'])) { |
|---|
| | 73 | $base_path or $base_path = $args['cache_key']; |
|---|
| | 74 | } |
|---|
| | 75 | $include_path_array = _include_path($base_path); |
|---|
| | 76 | |
|---|
| | 77 | require_once('MTUtil.php'); |
|---|
| | 78 | $filename = dirify($tmpl_meta['template_name']); |
|---|
| | 79 | $filename or $filename = 'template_' . $tmpl_meta['template_id']; |
|---|
| | 80 | $filename .= '.'.$blog['blog_file_extension']; |
|---|
| | 81 | |
|---|
| | 82 | $include_path = $blog['blog_site_path']; |
|---|
| | 83 | if (substr($include_path, strlen($include_path) - 1, 1) != DIRECTORY_SEPARATOR) |
|---|
| | 84 | $include_path .= DIRECTORY_SEPARATOR; |
|---|
| | 85 | foreach ($include_path_array as $p) { |
|---|
| | 86 | $include_path .= $p . DIRECTORY_SEPARATOR; |
|---|
| | 87 | } |
|---|
| | 88 | $include_file = $include_path . $filename; |
|---|
| | 89 | } |
|---|
| | 90 | |
|---|
| | 91 | # Try to read from cache |
|---|
| | 92 | $cache_enable = false; |
|---|
| | 93 | $cache_id = ''; |
|---|
| | 94 | $cacje_key = ''; |
|---|
| | 95 | $cache_ttl = 0; |
|---|
| | 96 | if (!empty($load_type) && |
|---|
| | 97 | isset($blog) && $blog['blog_include_cache'] == 1 && |
|---|
| | 98 | ((isset($tmpl_meta['cache_expire_type']) && ($tmpl_meta['cache_expire_type'] == '1' || $tmpl_meta['cache_expire_type'] == '2')) || |
|---|
| | 99 | ((isset($args['cache']) && $args['cache'] == '1') || isset($args['key']) || isset($args['cache_key']) || isset($args['ttl'])))) |
|---|
| | 100 | { |
|---|
| | 101 | global $mt; |
|---|
| | 102 | $cache_enable = true; |
|---|
| | 103 | $cache_key = isset($args['key']) |
|---|
| | 104 | ? $args['key'] |
|---|
| | 105 | : isset($args['cache_key']) |
|---|
| | 106 | ? $args['cache_key'] |
|---|
| | 107 | : 'blog::' . $blog_id . '::template_' . $load_type . '::' . $load_name; |
|---|
| | 108 | |
|---|
| | 109 | if (isset($args['ttl'])) |
|---|
| | 110 | $cache_ttl = $args['ttl']; |
|---|
| | 111 | elseif (isset($tmpl_meta['cache_expire_type']) && $tmpl_meta['cache_expire_type'] == '1') |
|---|
| | 112 | $cache_ttl = $tmpl_meta['cache_expire_interval']; |
|---|
| | 113 | else |
|---|
| | 114 | $cache_ttl = 60 * 60; # default 60 min. |
|---|
| | 115 | |
|---|
| | 116 | if (isset($tmpl_meta['cache_expire_type']) && $tmpl_meta['cache_expire_type'] == '2') { |
|---|
| | 117 | $expire_types = preg_split('/,/', $tmpl_meta['cache_expire_event'], -1, PREG_SPLIT_NO_EMPTY); |
|---|
| | 118 | if (!empty($expire_types)) { |
|---|
| | 119 | $latest = $ctx->mt->db->get_latest_touch($blog_id, $expire_types); |
|---|
| | 120 | if ($latest) { |
|---|
| | 121 | if ($ssi_enable) { |
|---|
| | 122 | $file_stat = stat($include_file); |
|---|
| | 123 | if ($file_stat) { |
|---|
| | 124 | $file_stamp = gmdate("Y-m-d H:i:s", $file_stat[9]); |
|---|
| | 125 | if (strtotime($latest) > strtotime($file_stamp)) |
|---|
| | 126 | $cache_ttl = 1; |
|---|
| | 127 | } |
|---|
| | 128 | } else { |
|---|
| | 129 | $cache_ttl = time() - strtotime($latest); |
|---|
| | 130 | } |
|---|
| | 131 | } |
|---|
| | 132 | } |
|---|
| | 133 | } |
|---|
| | 134 | |
|---|
| | 135 | if ($cache_ttl == 0 || (time() - strtotime($tmpl_meta['template_modified_on']) < $cache_ttl)) { |
|---|
| | 136 | $cache_ttl = time() - strtotime($tmpl_meta['template_modified_on']); |
|---|
| | 137 | } |
|---|
| | 138 | |
|---|
| | 139 | $cache_driver = $mt->cache_driver($cache_ttl); |
|---|
| | 140 | $cached_val = $cache_driver->get($cache_key, $cache_ttl); |
|---|
| | 141 | if (!empty($cached_val)) { |
|---|
| | 142 | _clear_vars($ctx, $ext_args); |
|---|
| | 143 | if ($ssi_enable) { |
|---|
| | 144 | if (file_exists($include_file) && is_readable($include_file)) { |
|---|
| | 145 | $content = file_get_contents($include_file); |
|---|
| | 146 | if ($content) |
|---|
| | 147 | return $content; |
|---|
| | 148 | } |
|---|
| | 149 | } else { |
|---|
| | 150 | return $cached_val; |
|---|
| | 151 | } |
|---|
| | 152 | } |
|---|
| | 153 | } |
|---|
| | 154 | |
|---|
| | 155 | # Compile template |
|---|
| | 156 | static $_include_cache = array(); |
|---|
| | 157 | $_var_compiled = ''; |
|---|
| | 158 | |
|---|
| | 159 | if (!empty($load_type)) { |
|---|
| | 160 | $cache_id = $load_type . '::' . $blog_id . '::' . $load_name; |
|---|
| 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 | | } |
|---|
| | 169 | $_include_cache[$cache_id] = $_var_compiled; |
|---|
| 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 { |
|---|
| | 177 | $tmpl = _get_template_from_file($ctx, $file, $blog_id); |
|---|
| | 178 | if (!$ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { |
|---|
| | 236 | } |
|---|
| | 237 | |
|---|
| | 238 | function _get_template_from_file ($ctx, $file, $blog_id) { |
|---|
| | 239 | $base_filename = basename($file); |
|---|
| | 240 | global $restricted_include_filenames; |
|---|
| | 241 | if (array_key_exists(strtolower($base_filename), $restricted_include_filenames)) { |
|---|
| | 242 | return ''; |
|---|
| | 243 | } |
|---|
| | 244 | if (is_file($file) && is_readable($file)) { |
|---|
| | 245 | $contents = @file($file); |
|---|
| | 246 | $tmpl = implode('', $contents); |
|---|
| | 247 | } else { |
|---|
| | 248 | $blog = $ctx->stash('blog'); |
|---|
| | 249 | if ($blog['blog_id'] != $blog_id) { |
|---|
| | 250 | $blog = $ctx->mt->db->fetch_blog($blog_id); |
|---|
| | 251 | } |
|---|
| | 252 | $path = $blog['blog_site_path']; |
|---|
| | 253 | if (!preg_match('!/$!', $path)) |
|---|
| | 254 | $path .= '/'; |
|---|
| | 255 | $path .= $file; |
|---|
| | 256 | if (is_file($path) && is_readable($path)) { |
|---|
| | 257 | $contents = @file($path); |
|---|
| | 258 | $tmpl = implode('', $contents); |
|---|
| | 259 | } else { |
|---|
| | 260 | return false; |
|---|
| | 261 | } |
|---|
| | 262 | } |
|---|
| | 263 | |
|---|
| | 264 | return $tmpl; |
|---|