function smarty_function_mtwidgetmanager($args, &$ctx)
{
    $blog_id = $args['blog_id'];
    $blog_id or $blog_id = $ctx->stash('blog_id');
    $blog_id or $blog_id = 0;
    $widgetmanager = $args['name'];
    // Should we try to load is there's only one?
    if (!$widgetmanager) {
        return;
    }
    $tmpl = $ctx->mt->db()->get_template_text($ctx, $widgetmanager, $blog_id, 'widgetset', $args['global']);
    if (!isset($tmpl) || !$tmpl) {
        return '';
    }
    if (isset($tmpl) && $tmpl) {
        // push to ctx->vars
        $ext_args = array();
        while (list($key, $val) = each($args)) {
            if (!preg_match('/(^name$|^blog_id$)/', $key)) {
                require_once "function.mtsetvar.php";
                smarty_function_mtsetvar(array('name' => $key, 'value' => $val), $ctx);
                $ext_args[] = $key;
            }
        }
        $contents = '';
        if (preg_match_all('/widget\\=\\"([^"]+)\\"/', $tmpl, $matches)) {
            foreach ($matches[1] as $widget) {
                $contents .= $ctx->tag('include', array('widget' => $widget, 'blog_id' => $blog_id));
            }
        }
        _clear_vars($ctx, $ext_args);
        return $contents;
    }
    return '';
}
function smarty_function_mtsubcatsrecurse($args, &$ctx)
{
    $localvars = array('subCatsDepth', 'category', 'subCatIsFirst', 'subCatIsLast', 'subFolderHead', 'subFolderFoot');
    $fn = $ctx->stash('subCatTokens');
    #if (!method_exists($ctx,$fn)) {
    #    return $ctx->error("Called SubCatsRecurse outside of SubCategories tag!");
    #}
    $cat = $ctx->stash('category');
    # Get the depth info
    $max_depth = $args['max_depth'];
    $depth = $ctx->stash('subCatsDepth') or 0;
    # Get the sorting info
    $sort_method = $ctx->stash('subCatsSortMethod');
    $sort_order = $ctx->stash('subCatsSortOrder');
    $sort_by = $ctx->stash('subCatsSortBy');
    $sort_by or $sort_by = 'user_custom';
    # Get the class info
    $class = 'category';
    if (isset($args['class'])) {
        $class = $args['class'];
    }
    $cats = $ctx->mt->db()->fetch_categories(array('blog_id' => $ctx->stash('blog_id'), 'category_id' => $cat->category_id, 'children' => 1, 'show_empty' => 1, 'class' => $class, 'sort_order' => $sort_order, 'sort_by' => $sort_by));
    #$cats = sort_cats($ctx, $sort_method, $sort_order, $child_cats);
    if (!$cats) {
        return '';
        #$ctx->error("No sub categories!");
    }
    $count = 0;
    $res = '';
    require_once "function.mtsetvar.php";
    $ctx->localize($localvars);
    $ctx->stash('subCatsDepth', $depth + 1);
    while ($c = array_shift($cats)) {
        smarty_function_mtsetvar(array('name' => '__depth__', 'value' => $depth + 1), $ctx);
        $ctx->stash('category', $c);
        $ctx->stash('subCatIsFirst', !$count);
        $ctx->stash('subCatIsLast', !count($cats));
        $ctx->stash('subFolderHead', !$count);
        $ctx->stash('subFolderFoot', !count($cats));
        ob_start();
        $fn($ctx, array());
        #call_user_method($fn, $ctx, $ctx, array());
        $res .= ob_get_contents();
        ob_end_clean();
        $count++;
    }
    $ctx->restore($localvars);
    return $res;
}
function smarty_function_mtsetglobalvar($args, &$ctx)
{
    require_once 'function.mtsetvar.php';
    smarty_function_mtsetvar($args, $ctx);
    $global_vars = $ctx->stash('__get_hash_var_global_vars');
    if (!isset($global_vars)) {
        $global_vars = array();
    }
    if (isset($args['name'])) {
        $name = $args['name'];
    }
    if ($name) {
        $global_vars[$name] = $ctx->__stash['vars'][$name];
    }
    $ctx->stash('__get_hash_var_global_vars', $global_vars);
}
function smarty_function_mtwidgetmanager($args, &$ctx)
{
    $blog_id = $args['blog_id'];
    $blog_id or $blog_id = $ctx->stash('blog_id');
    $blog_id or $blog_id = 0;
    $widgetmanager = $args['name'];
    // Should we try to load is there's only one?
    if (!$widgetmanager) {
        return;
    }
    $tmpl = $ctx->mt->db->get_template_text($ctx, $widgetmanager, $blog_id, 'widgetset', $args['global']);
    if (!isset($tmpl) || !$tmpl) {
        # TODO: doing save_widgetset should write template text
        # error status for now to see if there is any pattern
        # that requires to consturct includes from template meta (modulesets).
        return $ctx->error($ctx->mt->translate('Error: widgetset [_1] is empty.', $widgetmanager));
    }
    if (isset($tmpl) && $tmpl) {
        // push to ctx->vars
        $ext_args = array();
        while (list($key, $val) = each($args)) {
            if (!preg_match('/(^name$|^blog_id$)/', $key)) {
                require_once "function.mtsetvar.php";
                smarty_function_mtsetvar(array('name' => $key, 'value' => $val), $ctx);
                $ext_args[] = $key;
            }
        }
        if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) {
            ob_start();
            $ctx->_eval('?>' . $_var_compiled);
            $_contents = ob_get_contents();
            ob_end_clean();
            _clear_vars($ctx, $ext_args);
            return $_contents;
        } else {
            _clear_vars($ctx, $ext_args);
            return $ctx->error($ctx->mt->translate('Error compiling widgetset [_1]', $widgetmanager));
        }
    }
    return '';
}
function smarty_function_mtmbsetvar($args, &$ctx)
{
    if (isset($args['name'])) {
        $name = $args['name'];
    }
    if (!$name) {
        return '';
    }
    if (isset($args['value'])) {
        $value = $args['value'];
    }
    if (!$value) {
        return '';
    }
    if (strpos($name, '$') === 0) {
        $name = preg_replace('/^\\$/', '', $name);
        $_name = $ctx->__stash['vars'][$name];
        if (!$_name) {
            $_name = $ctx->__stash['vars'][strtolower($name)];
        }
        $name = $_name;
    }
    if (strpos($value, '$') === 0) {
        $value = preg_replace('/^\\$/', '', $value);
        $_value = $ctx->__stash['vars'][$value];
        if (!$_value) {
            $_value = $ctx->__stash['vars'][strtolower($value)];
        }
        $value = $_value;
    }
    $ctx->__stash['vars'][$name] = $value;
    $ctx->__stash['vars'][strtolower($name)] = $value;
    $args['name'] = $name;
    $args['value'] = $value;
    require_once 'function.mtsetvar.php';
    smarty_function_mtsetvar($args, $ctx);
}
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;
}
Esempio n. 7
0
function smarty_function_mtvar($args, &$ctx)
{
    // status: complete
    // parameters: name
    if (array_key_exists('value', $args) && !array_key_exists('op', $args)) {
        require_once "function.mtsetvar.php";
        return smarty_function_mtsetvar($args, $ctx);
    }
    require_once "MTUtil.php";
    $vars =& $ctx->__stash['vars'];
    $value = '';
    $name = $args['name'];
    $name or $name = $args['var'];
    if (preg_match('/^(config|request)\\.(.+)$/i', $name, $m)) {
        if (strtolower($m[1]) == 'config') {
            if (!preg_match('/password/i', $m[2])) {
                global $mt;
                return $mt->config[strtolower($m[2])];
            }
        } elseif (strtolower($m[1]) == 'request') {
            return $_REQUEST[$m[2]];
        }
    }
    if (!$name) {
        return '';
    }
    if (preg_match('/^(\\w+)\\((.+)\\)$/', $name, $matches)) {
        $func = $matches[1];
        $name = $matches[2];
    } else {
        if (array_key_exists('function', $args)) {
            $func = $args['function'];
        }
    }
    # pick off any {...} or [...] from the name.
    if (preg_match('/^(.+)([\\[\\{])(.+)[\\]\\}]$/', $name, $matches)) {
        $name = $matches[1];
        $br = $matches[2];
        $ref = $matches[3];
        if (preg_match('/^\\\\\\$(.+)/', $ref, $ref_matches)) {
            $ref = $vars[$ref_matches[1]];
            if (!isset($ref)) {
                $ref = chr(0);
            }
        }
        $br == '[' ? $index = $ref : ($key = $ref);
    } else {
        if (array_key_exists('index', $args)) {
            $index = $args['index'];
        } else {
            if (array_key_exists('key', $args)) {
                $key = $args['key'];
            }
        }
    }
    if (preg_match('/^\\$/', $name)) {
        $name = $vars[$name];
        if (!isset($name)) {
            return $ctx->error($ctx->mt->translate("You used a [_1] tag without a valid name attribute.", "<MT{$tag}>"));
        }
    }
    if (isset($vars[$name])) {
        $value = $vars[$name];
    }
    if (!is_array($value) && preg_match('/^smarty_fun_[a-f0-9]+$/', $value)) {
        if (function_exists($value)) {
            ob_start();
            $value($ctx, array());
            $value = ob_get_contents();
            ob_end_clean();
        } else {
            $value = '';
        }
    }
    $return_val = $value;
    if (isset($name)) {
        if (is_hash($value)) {
            if (isset($key)) {
                if (isset($func)) {
                    if ('delete' == strtolower($func)) {
                        $return_val = $value[$key];
                        unset($value[$key]);
                        $vars[$name] = $value;
                    } else {
                        return $ctx->error($ctx->mt->translate("'[_1]' is not a valid function for a hash.", $func));
                    }
                } else {
                    if ($key != chr(0)) {
                        $return_val = $value[$key];
                    } else {
                        unset($value);
                    }
                }
            } elseif (isset($func)) {
                if ('count' == strtolower($func)) {
                    $return_val = count(array_keys($value));
                } else {
                    return $ctx->error($ctx->mt->translate("'[_1]' is not a valid function for a hash.", $func));
                }
            } else {
                if (array_key_exists('to_json', $args) && $args['to_json']) {
                    if (function_exists('json_encode')) {
                        $return_val = json_encode($value);
                    } else {
                        $return_val = '';
                    }
                }
            }
        } elseif (is_array($value)) {
            if (isset($index)) {
                if (is_numeric($index)) {
                    $return_val = $value[$index];
                } else {
                    unset($value);
                    # fall through to any 'default'
                }
            } elseif (isset($func)) {
                $func = strtolower($func);
                if ('pop' == $func) {
                    $return_val = array_pop($value);
                    $vars[$name] = $value;
                } elseif ('shift' == $func) {
                    $return_val = array_shift($value);
                    $vars[$name] = $value;
                } elseif ('count' == $func) {
                    $return_val = count($value);
                } else {
                    return $ctx->error($ctx->mt->translate("'[_1]' is not a valid function for an array.", $func));
                }
            } else {
                if (array_key_exists('to_json', $args) && $args['to_json']) {
                    if (function_exists('json_encode')) {
                        $return_val = json_encode($value);
                    } else {
                        $return_val = '';
                    }
                }
            }
        }
        if (array_key_exists('op', $args)) {
            $op = $args['op'];
            $rvalue = $args['value'];
            if ($op && isset($value) && !is_array($value)) {
                $return_val = _math_operation($op, $value, $rvalue);
                if (!isset($return_val)) {
                    return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $value, $op, $rvalue));
                }
            }
        }
    }
    if ($return_val == '') {
        if (isset($args['default'])) {
            $return_val = $args['default'];
        }
    }
    if (isset($args['escape'])) {
        $esc = strtolower($args['escape']);
        if ($esc == 'js') {
            $return_val = encode_js($return_val);
        } elseif ($esc == 'html') {
            if (version_compare(phpversion(), '4.3.0', '>=')) {
                global $mt;
                $charset = $mt->config('PublishCharset');
                $return_val = htmlentities($return_val, ENT_COMPAT, $charset);
            } else {
                $return_val = htmlentities($return_val, ENT_COMPAT);
            }
        } elseif ($esc == 'url') {
            $return_val = urlencode($return_val);
            $return_val = preg_replace('/\\+/', '%20', $return_val);
        }
    }
    return $return_val;
}