function smarty_function_mtinclude($args, &$ctx)
{
    // status: partial
    // parameters: module, file
    // notes: file case needs work -- search through blog site archive path, etc...
    // push to ctx->vars
    $ext_args = array();
    while (list($key, $val) = each($args)) {
        if (!preg_match('/(^file$|^module$|^widget$|^blog_id$|^identifier$|^type$)/', $key)) {
            require_once "function.mtsetvar.php";
            smarty_function_mtsetvar(array('name' => $key, 'value' => $val), $ctx);
            $ext_args[] = $key;
        }
    }
    $blog_id = $args['blog_id'];
    $blog_id or $blog_id = $ctx->stash('blog_id');
    if ($args['local']) {
        $blog_id = $ctx->stash('local_blog_id');
    }
    $blog = $ctx->mt->db()->fetch_blog($blog_id);
    // When the module name starts by 'Widget', it converts to 'Widget' from 'Module'.
    if (isset($args['module']) && $args['module']) {
        $module = $args['module'];
        if (preg_match('/^Widget:/', $module)) {
            $args['widget'] = preg_replace('/^Widget: ?/', '', $module);
            unset($args['module']);
        }
    }
    // Fetch template meta data
    $load_type = null;
    $load_name = null;
    if (isset($args['module'])) {
        $load_type = 'custom';
        $load_name = $args['module'];
    } elseif (isset($args['widget'])) {
        $load_type = 'widget';
        $load_name = $args['widget'];
    } elseif (isset($args['identifier'])) {
        $load_type = 'identifier';
        $load_name = $args['identifier'];
    }
    $tmpl_meta = null;
    if (!empty($load_type)) {
        $is_global = isset($args['global']) && $args['global'] ? 1 : 0;
        if (isset($args['parent']) && $args['parent']) {
            if (isset($args['global']) && $args['global']) {
                return $ctx->error($ctx->mt->translate("'parent' modifier cannot be used with '[_1]'", 'global'));
            }
            if (isset($args['local']) && $args['local']) {
                return $ctx->error($ctx->mt->translate("'parent' modifier cannot be used with '[_1]'", 'global'));
            }
            $local_blog = $ctx->mt->db()->fetch_blog($ctx->stash('local_blog_id'));
            if ($local_blog->is_blog()) {
                $website = $local_blog->website();
                $blog_id = $website->id;
            } else {
                $blog_id = $local_blog->id;
            }
        }
        $tmpl_meta = $ctx->mt->db()->fetch_template_meta($load_type, $load_name, $blog_id, $is_global);
    }
    # Convert to phrase of PHP Include
    require_once 'MTUtil.php';
    $ssi_enable = false;
    $include_file = '';
    if (!empty($load_type) && isset($blog) && $blog->blog_include_system == 'php' && (isset($args['ssi']) && $args['ssi'] || $tmpl_meta->include_with_ssi)) {
        $ssi_enable = true;
        // Generates include path using Key
        $base_path = '';
        if (isset($args['key'])) {
            $base_path = $args['key'];
        } elseif (isset($args['cache_key'])) {
            $base_path or $base_path = $args['cache_key'];
        }
        $include_path_array = _include_path($base_path);
        $filename = dirify($tmpl_meta->template_name);
        $filename or $filename = 'template_' . $tmpl_meta->template_id;
        $filename .= '.' . $blog->blog_file_extension;
        $include_path = $blog->site_path();
        if (substr($include_path, strlen($include_path) - 1, 1) != DIRECTORY_SEPARATOR) {
            $include_path .= DIRECTORY_SEPARATOR;
        }
        foreach ($include_path_array as $p) {
            $include_path .= $p . DIRECTORY_SEPARATOR;
        }
        $include_file = $include_path . $filename;
    }
    # Try to read from cache
    $cache_enable = false;
    $cache_id = '';
    $cache_key = '';
    $cache_ttl = 0;
    $cache_expire_type = $tmpl_meta->cache_expire_type;
    if (!empty($load_type) && isset($blog) && $blog->blog_include_cache == 1 && ($cache_expire_type == '1' || $cache_expire_type == '2') || (isset($args['cache']) && $args['cache'] == '1' || isset($args['key']) || isset($args['cache_key']) || isset($args['ttl']))) {
        $cache_blog_id = isset($args['global']) && $args['global'] == 1 ? 0 : $blog_id;
        $mt = MT::get_instance();
        $cache_enable = true;
        $cache_key = isset($args['key']) ? $args['key'] : (isset($args['cache_key']) ? $args['cache_key'] : md5('blog::' . $cache_blog_id . '::template_' . $load_type . '::' . $load_name));
        if (isset($args['ttl'])) {
            $cache_ttl = $args['ttl'];
        } elseif (isset($cache_expire_type) && $cache_expire_type == '1') {
            $cache_ttl = $tmpl_meta->cache_expire_interval;
        } else {
            $cache_ttl = 60 * 60;
        }
        # default 60 min.
        if (isset($cache_expire_type) && $cache_expire_type == '2') {
            $expire_types = preg_split('/,/', $tmpl_meta->cache_expire_event, -1, PREG_SPLIT_NO_EMPTY);
            if (!empty($expire_types)) {
                $latest = $ctx->mt->db()->get_latest_touch($blog_id, $expire_types);
                if ($latest) {
                    $latest_touch = $latest->modified_on;
                    if ($ssi_enable) {
                        $file_stat = stat($include_file);
                        if ($file_stat) {
                            $file_stamp = gmdate("Y-m-d H:i:s", $file_stat[9]);
                            if (datetime_to_timestamp($latest_touch) > datetime_to_timestamp($file_stamp)) {
                                $cache_ttl = 1;
                            }
                        }
                    } else {
                        $cache_ttl = time() - datetime_to_timestamp($latest_touch, 'gmt');
                    }
                }
            }
        }
        $elapsed_time = time() - offset_time(datetime_to_timestamp($tmpl_meta->template_modified_on, 'gmt'), $blog, '-');
        if ($cache_ttl == 0 || $elapsed_time < $cache_ttl) {
            $cache_ttl = $elapsed_time;
        }
        $cache_driver = $mt->cache_driver($cache_ttl);
        $cached_val = $cache_driver->get($cache_key, $cache_ttl);
        if (!empty($cached_val)) {
            _clear_vars($ctx, $ext_args);
            if ($ssi_enable) {
                if (file_exists($include_file) && is_readable($include_file)) {
                    $content = file_get_contents($include_file);
                    if ($content) {
                        return $content;
                    }
                }
            } else {
                return $cached_val;
            }
        }
    }
    if ($ssi_enable && !$cache_enable) {
        if (file_exists($include_file) && is_readable($include_file)) {
            $content = file_get_contents($include_file);
            if ($content) {
                return $content;
            }
        }
    }
    # Compile template
    static $_include_cache = array();
    $_var_compiled = '';
    if (!empty($load_type)) {
        $cache_blog_id = isset($args['global']) && $args['global'] == 1 ? 0 : $blog_id;
        $cache_id = $load_type . '::' . $cache_blog_id . '::' . $load_name;
        if (isset($_include_cache[$cache_id])) {
            $_var_compiled = $_include_cache[$cache_id];
        } else {
            $tmpl = $ctx->mt->db()->get_template_text($ctx, $load_name, $blog_id, $load_type, $args['global']);
            if (!$ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
                _clear_vars($ctx, $ext_args);
                return $ctx->error("Error compiling template module '{$module}'");
            }
            $_include_cache[$cache_id] = $_var_compiled;
        }
    } elseif (isset($args['file']) && $args['file']) {
        $mt = MT::get_instance();
        if (!$mt->config('AllowFileInclude')) {
            return $ctx->error('File include is disabled by "AllowFileInclude" config directive.');
        }
        $file = $args['file'];
        $cache_id = 'file::' . $blog_id . '::' . $file;
        if (isset($_include_cache[$cache_id])) {
            $_var_compiled = $_include_cache[$cache_id];
        } else {
            $tmpl = _get_template_from_file($ctx, $file, $blog_id);
            if (!$ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
                _clear_vars($ctx, $ext_args);
                return $ctx->error("Error compiling template file '{$file}'");
            }
            $_include_cache[$cache_id] = $_var_compiled;
        }
    } elseif (isset($args['type']) && $args['type']) {
        $type = $args['type'];
        $cache_id = 'type::' . $blog_id . '::' . $type;
        if (isset($_include_cache[$cache_id])) {
            $_var_compiled = $_include_cache[$cache_id];
        } else {
            $tmpl = $ctx->mt->db()->load_special_template($ctx, null, $type, $blog_id);
            if ($tmpl) {
                if ($ctx->_compile_source('evaluated template', $tmpl->template_text, $_var_compiled)) {
                    $_include_cache[$cache_id] = $_var_compiled;
                } else {
                    if ($type != 'dynamic_error') {
                        _clear_vars($ctx, $ext_args);
                        return $ctx->error("Error compiling template module '{$module}'");
                    } else {
                        _clear_vars($ctx, $ext_args);
                        return null;
                    }
                }
            } else {
                _clear_vars($ctx, $ext_args);
                return null;
            }
        }
    }
    ob_start();
    $ctx->_eval('?>' . $_var_compiled);
    $_contents = ob_get_contents();
    ob_end_clean();
    _clear_vars($ctx, $ext_args);
    if ($cache_enable) {
        $cache_driver = $mt->cache_driver($cache_ttl);
        $cache_driver->set($cache_key, $_contents, $cache_ttl);
    }
    if ($ssi_enable) {
        $include_dir = dirname($include_file);
        if (!file_exists($include_dir) && !is_dir($include_dir)) {
            mkpath($include_dir, 0777);
        }
        if (is_writable($include_dir)) {
            if ($h_file = fopen($include_file, 'w')) {
                fwrite($h_file, $_contents);
                fclose($h_file);
            }
        }
    }
    return $_contents;
}
示例#2
0
    function _hdlr_date($args, &$ctx)
    {
        $ts = null;
        if (isset($args['ts'])) {
            $ts = $args['ts'];
        }
        $ts or $ts = $ctx->stash('current_timestamp');
        $ts = preg_replace('![^0-9]!', '', $ts);
        $blog = $ctx->stash('blog');
        if ($ts == '') {
            $t = time();
            if ($args['utc']) {
                $ts = gmtime($t);
            } else {
                $ts = offset_time_list($t, $blog);
            }
            $ts = sprintf("%04d%02d%02d%02d%02d%02d", $ts[5] + 1900, $ts[4] + 1, $ts[3], $ts[2], $ts[1], $ts[0]);
        }
        if (isset($args['utc'])) {
            if (!is_object($blog)) {
                $blog = $ctx->mt->db()->fetch_blog($blog);
            }
            preg_match('/(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)/', $ts, $matches);
            list($all, $y, $mo, $d, $h, $m, $s) = $matches;
            $so = $blog->blog_server_offset;
            $timelocal = mktime($h, $m, $s, $mo, $d, $y);
            $localtime = localtime($timelocal);
            if ($localtime[8]) {
                $so += 1;
            }
            $partial_hour_offset = 60 * abs($so - intval($so));
            $four_digit_offset = sprintf('%s%02d%02d', $so < 0 ? '-' : '+', abs($so), $partial_hour_offset);
            $ts = gmdate('YmdHis', strtotime("{$y}-{$mo}-{$d} {$h}:{$m}:{$s} {$four_digit_offset}"));
        }
        if (isset($args['format_name'])) {
            if ($format = $args['format_name']) {
                $tz = 'Z';
                if (!$args['utc']) {
                    $blog = $ctx->stash('blog');
                    if (!is_object($blog)) {
                        $blog = $ctx->mt->db()->fetch_blog($blog);
                    }
                    $so = $blog->blog_server_offset;
                    $partial_hour_offset = 60 * abs($so - intval($so));
                    if ($format == 'rfc822') {
                        $tz = sprintf("%s%02d%02d", $so < 0 ? '-' : '+', abs($so), $partial_hour_offset);
                    } elseif ($format == 'iso8601') {
                        $tz = sprintf("%s%02d:%02d", $so < 0 ? '-' : '+', abs($so), $partial_hour_offset);
                    }
                }
                if ($format == 'rfc822') {
                    $args['format'] = '%a, %d %b %Y %H:%M:%S ' . $tz;
                    $args['language'] = 'en';
                } elseif ($format == 'iso8601') {
                    $args['format'] = '%Y-%m-%dT%H:%M:%S' . $tz;
                }
            }
        }
        if (!isset($args['format'])) {
            $args['format'] = null;
        }
        require_once "MTUtil.php";
        $fds = format_ts($args['format'], $ts, $blog, isset($args['language']) ? $args['language'] : null);
        if (isset($args['relative'])) {
            if ($args['relative'] == 'js') {
                preg_match('/(\\d\\d\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)/', $ts, $match);
                list($xx, $y, $mo, $d, $h, $m, $s) = $match;
                $mo--;
                $js = <<<EOT
<script type="text/javascript">
/* <![CDATA[ */
document.write(mtRelativeDate(new Date({$y},{$mo},{$d},{$h},{$m},{$s}), '{$fds}'));
/* ]]> */
</script><noscript>{$fds}</noscript>
EOT;
                return $js;
            } else {
                preg_match('/(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)/', $ts, $matches);
                list($all, $y, $mo, $d, $h, $m, $s) = $matches;
                $unix_ts = offset_time(gmmktime($h, $m, $s, $mo, $d, $y), $blog, '-');
                $now_ts = time();
                $relative = $this->relative_date($unix_ts, $now_ts, $args['relative'], $blog);
                if (is_array($relative)) {
                    return format_ts($relate['format'], $ts, $blog, isset($args['language']) ? $args['language'] : null);
                } elseif ($relative) {
                    return $relative;
                }
            }
        }
        return $fds;
    }
示例#3
0
 function view($blog_id = null)
 {
     require_once "MTUtil.php";
     $blog_id or $blog_id = $this->blog_id;
     $ctx =& $this->context();
     $this->init_plugins();
     $ctx->caching = $this->caching;
     // Some defaults...
     $mtdb =& $this->db();
     $ctx->mt->db =& $mtdb;
     // Set up our customer error handler
     set_error_handler(array(&$this, 'error_handler'));
     // User-specified request through request variable
     $path = $this->request;
     // Apache request
     if (!$path && $_SERVER['REQUEST_URI']) {
         $path = $_SERVER['REQUEST_URI'];
         // strip off any query string...
         $path = preg_replace('/\\?.*/', '', $path);
         // strip any duplicated slashes...
         $path = preg_replace('!/+!', '/', $path);
     }
     // IIS request by error document...
     if (preg_match('/IIS/', $_SERVER['SERVER_SOFTWARE'])) {
         // assume 404 handler
         if (preg_match('/^\\d+;(.*)$/', $_SERVER['QUERY_STRING'], $matches)) {
             $path = $matches[1];
             $path = preg_replace('!^http://[^/]+!', '', $path);
             if (preg_match('/\\?(.+)?/', $path, $matches)) {
                 $_SERVER['QUERY_STRING'] = $matches[1];
                 $path = preg_replace('/\\?.*$/', '', $path);
             }
         }
     }
     // now set the path so it may be queried
     $path = preg_replace('/\\\\/', '\\\\\\\\', $path);
     $this->request = $path;
     // When we are invoked as an ErrorDocument, the parameters are
     // in the environment variables REDIRECT_*
     if (isset($_SERVER['REDIRECT_QUERY_STRING'])) {
         // todo: populate $_GET and QUERY_STRING with REDIRECT_QUERY_STRING
         $_SERVER['QUERY_STRING'] = getenv('REDIRECT_QUERY_STRING');
     }
     if (preg_match('/\\.(\\w+)$/', $path, $matches)) {
         $req_ext = strtolower($matches[1]);
     }
     $this->blog_id = $blog_id;
     $data =& $this->resolve_url($path);
     if (!$data) {
         // 404!
         $this->http_error = 404;
         header("HTTP/1.1 404 Not found");
         return $ctx->error($this->translate("Page not found - [_1]", $path), E_USER_ERROR);
     }
     $info =& $data['fileinfo'];
     $fi_path = $info['fileinfo_url'];
     $fid = $info['fileinfo_id'];
     $at = $info['fileinfo_archive_type'];
     $ts = $info['fileinfo_startdate'];
     $tpl_id = $info['fileinfo_template_id'];
     $cat = $info['fileinfo_category_id'];
     $auth = $info['fileinfo_author_id'];
     $entry_id = $info['fileinfo_entry_id'];
     $blog_id = $info['fileinfo_blog_id'];
     $blog =& $data['blog'];
     if ($at == 'index') {
         $at = null;
         $ctx->stash('index_archive', true);
     } else {
         $ctx->stash('index_archive', false);
     }
     $tts = $data['template']['template_modified_on'];
     if ($tts) {
         $tts = offset_time(datetime_to_timestamp($tts), $blog);
     }
     $ctx->stash('template_timestamp', $tts);
     $ctx->stash('template_created_on', $data['template']['template_created_on']);
     $page_layout = $data['blog']['blog_page_layout'];
     $columns = get_page_column($page_layout);
     $vars =& $ctx->__stash['vars'];
     $vars['page_columns'] = $columns;
     $vars['page_layout'] = $page_layout;
     if (isset($data['template']['template_identifier'])) {
         $vars[$data['template']['template_identifier']] = 1;
     }
     $this->configure_paths($blog['blog_site_path']);
     // start populating our stash
     $ctx->stash('blog_id', $blog_id);
     $ctx->stash('local_blog_id', $blog_id);
     $ctx->stash('blog', $blog);
     $ctx->stash('build_template_id', $tpl_id);
     // conditional get support...
     if ($this->caching) {
         $this->cache_modified_check = true;
     }
     if ($this->conditional) {
         $last_ts = $blog['blog_children_modified_on'];
         $last_modified = $ctx->_hdlr_date(array('ts' => $last_ts, 'format' => '%a, %d %b %Y %H:%M:%S GMT', 'language' => 'en', 'utc' => 1), $ctx);
         $this->doConditionalGet($last_modified);
     }
     $cache_id = $blog_id . ';' . $fi_path . ';' . $_SERVER['QUERY_STRING'];
     if (!$ctx->is_cached('mt:' . $tpl_id, $cache_id)) {
         if (isset($at) && $at != 'Category') {
             global $_archivers;
             require_once "archive_lib.php";
             global $_archivers;
             if (!isset($_archivers[$at])) {
                 // 404
                 $this->http_errr = 404;
                 header("HTTP/1.1 404 Not Found");
                 return $ctx->error($this->translate("Page not found - [_1]", $at), E_USER_ERROR);
             }
             $archiver = $_archivers[$at];
             $archiver->template_params($ctx);
         }
         if ($cat) {
             $archive_category = $mtdb->fetch_category($cat);
             $ctx->stash('category', $archive_category);
             $ctx->stash('archive_category', $archive_category);
         }
         if ($auth) {
             $archive_author = $mtdb->fetch_author($auth);
             $ctx->stash('author', $archive_author);
             $ctx->stash('archive_author', $archive_author);
         }
         if (isset($at)) {
             if ($at != 'Category' && isset($ts)) {
                 list($ts_start, $ts_end) = $archiver->get_range($ctx, $ts);
                 $ctx->stash('current_timestamp', $ts_start);
                 $ctx->stash('current_timestamp_end', $ts_end);
             }
             $ctx->stash('current_archive_type', $at);
         }
         if (isset($entry_id) && $entry_id && ($at == 'Individual' || $at == 'Page')) {
             if ($at == 'Individual') {
                 $entry =& $mtdb->fetch_entry($entry_id);
             } elseif ($at == 'Page') {
                 $entry =& $mtdb->fetch_page($entry_id);
             }
             $ctx->stash('entry', $entry);
             $ctx->stash('current_timestamp', $entry['entry_authored_on']);
         }
         if ($at == 'Category') {
             $vars =& $ctx->__stash['vars'];
             $vars['archive_class'] = "category-archive";
             $vars['category_archive'] = 1;
             $vars['archive_template'] = 1;
             $vars['archive_listing'] = 1;
             $vars['module_category_archives'] = 1;
         }
     }
     $output = $ctx->fetch('mt:' . $tpl_id, $cache_id);
     $this->http_error = 200;
     header("HTTP/1.1 200 OK");
     // content-type header-- need to supplement with charset
     $content_type = $ctx->stash('content_type');
     if (!isset($content_type)) {
         $content_type = $this->mime_types['__default__'];
         if ($req_ext && isset($this->mime_types[$req_ext])) {
             $content_type = $this->mime_types[$req_ext];
         }
     }
     $charset = $this->config('PublishCharset');
     if (isset($charset)) {
         if (!preg_match('/charset=/', $content_type)) {
             $content_type .= '; charset=' . $charset;
         }
     }
     header("Content-Type: {$content_type}");
     // finally, issue output
     $output = preg_replace('/^\\s*/', '', $output);
     echo $output;
     // if warnings found, show it.
     if (!empty($this->warning)) {
         $this->_dump($this->warning);
     }
     if ($this->debugging) {
         $this->log("Queries: " . $mtdb->num_queries);
         $this->log("Queries executed:");
         $queries = $mtdb->savedqueries;
         foreach ($queries as $q) {
             $this->log($q);
         }
         $this->log_dump();
     }
     restore_error_handler();
 }
示例#4
0
function offset_time_list($ts, $blog = null, $dir = null)
{
    return gmtime(offset_time($ts, $blog, $dir));
}