/**
 * Insert a button with a link to the RSS XML feed.
 *
 * @return string
 */
function smarty_rssbutton()
{
    global $PIVOTX;
    // if we've disabled the Atom feed for this weblog, return nothing.
    if ($PIVOTX['weblogs']->get('', 'rss') != 1) {
        return "";
    }
    $filename = makeFeedLink("rss");
    $image = $PIVOTX['paths']['pivotx_url'] . 'pics/rssbutton.png';
    list($width, $height) = @getimagesize($PIVOTX['paths']['pivotx_path'] . 'pics/rssbutton.png');
    $alttext = __('XML: RSS Feed');
    $output = '<a href="' . $filename . '" title="' . $alttext . '" rel="nofollow" class="badge">';
    $output .= '<img src="' . $image . '" width="' . $width . '" height="' . $height . '"';
    $output .= ' alt="' . $alttext . '" class="badge" longdesc="' . $filename . '" /></a>';
    return $output;
}
    /**
     * The function that does the actual rendering of the smarty template
     *
     * @param string $template
     */
    function parseTemplate($template)
    {
        global $PIVOTX, $build, $version, $codename, $timetaken;
        $this->maintemplate = $template;
        // Extra security measures for when we're rendering 'frontside' pages:
        $PIVOTX['template']->security = true;
        if ($PIVOTX['config']->get('allow_php_in_templates') == 1) {
            $PIVOTX['template']->security_settings['PHP_TAGS'] = true;
        }
        $allowedfunctions = explode(',', "array,addslashes,trim,ltrim,rtrim,strlen,date," . "substr,strpos,md5,nl2br,strstr,strtoupper,strtolower,ucfirst,ucwords," . "count,empty,is_array,is_object,in_array,is_int,is_float,is_integer,is_numeric," . "is_string,serialize,unserialize,isset,sizeof,true,false,stripslashes," . "encode_text,safe_string,htmlentities,htmlspecialchars,html_entity_decode," . "trimtext,round,function_exists,tag_exists,intval,basename,dirname");
        $PIVOTX['template']->security_settings['IF_FUNCS'] = $allowedfunctions;
        $PIVOTX['template']->security_settings['MODIFIER_FUNCS'] = $allowedfunctions;
        // Check if we use caching..
        if ($PIVOTX['config']->get('smarty_cache')) {
            $PIVOTX['template']->caching = true;
            $PIVOTX['template']->compile_check = true;
            $PIVOTX['template']->force_compile = false;
            $code = getDefault($this->code, "");
            $cachekey = "tpl_" . $code . substr(md5($template . ',' . implode(',', $this->modifier)), 0, 10);
            // Now, let's see if the page we want is already in the cache..
            if ($PIVOTX['template']->is_cached($template, $cachekey)) {
                // It is! We can get that, and return to the calling function..
                // But first, we check for cache_before_read hooks..
                $PIVOTX['extensions']->executeHook('cache_before_read', $template);
                $this->html = $PIVOTX['template']->fetch($template, $cachekey);
                // Before we return, we check for cache_after_read hooks..
                $PIVOTX['extensions']->executeHook('cache_after_read', $this->html);
                return;
            } else {
                // Before we continue, we check for cache_missed_read hooks..
                $PIVOTX['extensions']->executeHook('cache_missed_read', $template);
            }
        } else {
            $cachekey = "";
        }
        // If we've set the hidden config option for 'always jquery', add the hook here:
        if ($PIVOTX['config']->get('always_jquery') == 1) {
            $PIVOTX['extensions']->addHook('after_parse', 'callback', 'jqueryIncludeCallback');
        }
        // Add a favicon to the page, PiovtX or user configured, unless it's set
        // to display nothing (by using '0' as user configured favicon)
        $favicon_html = "\t<link rel=\"shortcut icon\" href=\"%s\" />\n";
        $favicon = $PIVOTX['config']->get('favicon');
        if ($favicon == '0') {
            $favicon_html = '';
        } else {
            if ($favicon == '') {
                $favicon = $PIVOTX['paths']['pivotx_url'] . "pics/favicon.ico";
            }
            $favicon_html = sprintf($favicon_html, $favicon);
        }
        // Add a hook to insert the generator meta tag and possibly a favicon link
        $PIVOTX['extensions']->addHook('after_parse', 'insert_before_close_head', "\t<meta name=\"generator\" content=\"PivotX\" /><!-- version: " . strip_tags($build) . " -->\n" . $favicon_html);
        // Output the canonical link. See:
        // http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
        if ($PIVOTX['config']->get('dont_add_canonical') == 0) {
            // If we're at the site's root, regardless of _what_ page or blog it is,
            // we always return the site url..
            if ($this->modifier['action'] == "page" && $PIVOTX['config']->get('root') == "p:" . $this->modifier['uri'] || $this->modifier['action'] == "weblog" && $PIVOTX['config']->get('root') == "w:" . $this->modifier['uri'] || $this->modifier['uri'] == "" && $PIVOTX['config']->get('root') == "" && !isset($this->modifier['offset'])) {
                $link = "";
                // Also set $modifier.home, so we can check if we're at the homepage from the templates.
                $this->modifier['home'] = true;
            } else {
                $link = smarty_link(array('hrefonly' => true), $PIVOTX['template']);
            }
            if (isset($this->modifier['canonical_link'])) {
                $link = $this->modifier['canonical_link'];
            }
            // Set the canonical link..
            $canonical = sprintf("\t<link rel=\"canonical\" href=\"%s%s\" />\n", $PIVOTX['paths']['canonical_host'], empty($link) ? $PIVOTX['paths']['site_url'] : $link);
            $PIVOTX['extensions']->addHook('after_parse', 'insert_before_close_head', $canonical);
        }
        // Add a hook to insert the scheduler. Unless the hidden configuration option
        // 'dont_run_scheduler' is set.
        if ($PIVOTX['config']->get('dont_run_scheduler') == 0) {
            $PIVOTX['extensions']->addHook('after_parse', 'insert_before_close_body', "\t<div class='scheduler-wrapper'><img src='" . $PIVOTX['paths']['pivotx_url'] . "scheduler.php' alt='' width='0' height='0' /></div>\n");
        }
        // If we've enabled the XML Feeds for this weblog, insert the auto-discovery tags..
        if ($PIVOTX['weblogs']->get('', 'rss') == 1) {
            $feedtitle = $PIVOTX['config']->get('sitename') . ' &raquo; ' . $PIVOTX['weblogs']->get('', 'name');
            $feedtitle = encodeText($feedtitle);
            $autodiscovery = sprintf("\t<link rel=\"alternate\" type=\"application/rss+xml\" title=\"%s (%s)\" href=\"%s\" />\n", $feedtitle, __("RSS feed"), makeFeedLink("rss"));
            $autodiscovery .= sprintf("\t<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s (%s)\" href=\"%s\" />\n", $feedtitle, __("Atom feed"), makeFeedLink("atom"));
            if ($PIVOTX['config']->get('feed_posts_only') != 1) {
                $feedlink_params = array('content' => 'comments');
                $autodiscovery .= sprintf("\t<link rel=\"alternate\" type=\"application/rss+xml\" title=\"%s (%s)\" href=\"%s\" />\n", $feedtitle, __("RSS feed for comments"), makeFeedLink("rss", $feedlink_params));
                $autodiscovery .= sprintf("\t<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s (%s)\" href=\"%s\" />\n", $feedtitle, __("Atom feed for comments"), makeFeedLink("atom", $feedlink_params));
            }
            if ($this->modifier['category'] != "") {
                $feedcategory = $PIVOTX['categories']->getCategory($this->modifier['category']);
                if (count($feedcategory) > 0) {
                    $feedtitle = $PIVOTX['config']->get('sitename') . ' &raquo; ' . __('category') . ' ' . $feedcategory['display'];
                    $feedtitle = encodeText($feedtitle);
                    $feedlink_params = array('category' => $feedcategory['name']);
                    $autodiscovery = sprintf("\t<link rel=\"alternate\" type=\"application/rss+xml\" title=\"%s (%s)\" href=\"%s\" />\n", $feedtitle, __("RSS feed"), makeFeedLink("rss", $feedlink_params)) . $autodiscovery;
                    $autodiscovery = sprintf("\t<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s (%s)\" href=\"%s\" />\n", $feedtitle, __("Atom feed"), makeFeedLink("atom", $feedlink_params)) . $autodiscovery;
                }
            }
            // Add a hook to insert RSS and ATOM autodiscovery-tag
            $PIVOTX['extensions']->addHook('after_parse', 'insert_before_close_head', $autodiscovery);
        }
        // If we've enabled XML-RPC / the MetaWeblog API, insert the auto-discovery tags...
        if ($PIVOTX['config']->get('xmlrpc') == 1) {
            $autodiscovery = sprintf("\t<link rel=\"EditURI\" type=\"application/rsd+xml\" title=\"RSD\" href=\"%s\" />\n", makeRSDLink());
            // Add a hook to insert XML-RPC / the MetaWeblog API autodiscovery-tag
            $PIVOTX['extensions']->addHook('after_parse', 'insert_before_close_head', $autodiscovery);
        }
        // If we've enabled (non-hardened) trackback, insert the auto-discovery tags...
        if ($PIVOTX['config']->get('trackbacks') == 1 && $PIVOTX['config']->get('hardened_trackback') != 1) {
            $autodiscovery = <<<EOM
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="%url%"
dc:identifier="%url%"
dc:title="%title%"
trackback:ping="%tb-url%" />
</rdf:RDF> -->
EOM;
            $url = $PIVOTX['paths']['host'] . makeFilelink($PIVOTX['db']->entry['code'], '', '');
            if ($PIVOTX['config']->get('mod_rewrite') == 0) {
                $tb_url = $url . '&amp;trackback';
            } else {
                $tb_url = $url . '/trackback/';
            }
            $autodiscovery = str_replace("%url%", $url, $autodiscovery);
            $autodiscovery = str_replace("%title%", $PIVOTX['db']->entry['title'], $autodiscovery);
            $autodiscovery = str_replace("%tb-url%", $tb_url, $autodiscovery);
            // Add a hook to insert XML-RPC / the MetaWeblog API autodiscovery-tag
            $PIVOTX['extensions']->addHook('after_parse', 'insert_before_close_head', $autodiscovery);
        }
        // Assign some stuff to $smarty, so it's accessible from the templates.
        $PIVOTX['template']->assign('build', $build);
        $PIVOTX['template']->assign('version', $version);
        $PIVOTX['template']->assign('codename', $codename);
        $PIVOTX['template']->assign('config', $PIVOTX['config']->getConfigArray());
        $PIVOTX['template']->assign('weblogs', $PIVOTX['weblogs']->getWeblogs());
        $PIVOTX['template']->assign('paths', $PIVOTX['paths']);
        $PIVOTX['template']->assign('modifier', $this->modifier);
        $PIVOTX['template']->assign('timetaken', timeTaken());
        $PIVOTX['template']->assign('memtaken', getMem());
        $PIVOTX['template']->assign('query_count', $timetaken['query_count']);
        // If we've set the hidden config option for 'set_request_variables', set them..
        if ($PIVOTX['config']->get('set_request_variables') == 1) {
            $PIVOTX['template']->assign('get', $_GET);
            $PIVOTX['template']->assign('post', $_POST);
            $PIVOTX['template']->assign('request', $_REQUEST);
            $PIVOTX['template']->assign('server', $_SERVER);
            $PIVOTX['template']->assign('session', $_SESSION);
        }
        // Add the 'base part' of the path to the smarty variables as well
        $PIVOTX['template']->assign('templatedir', dirname($template));
        if (file_exists($PIVOTX['paths']['templates_path'] . $template)) {
            // Execute a hook, if present.
            $PIVOTX['extensions']->executeHook('during_parse', $template);
        } else {
            // hmm, template doesn't exist, so we set it to our '404' template..
            $template = "";
            // Execute a hook, if present.
            $PIVOTX['extensions']->executeHook('during_parse', $template);
            if ($template == "") {
                $template = "404.html";
                // TODO: what will be our 404 template?
            }
        }
        if ($PIVOTX['config']->get('smarty_cache')) {
            // Before we continue, we check for cache_before_write hooks..
            $PIVOTX['extensions']->executeHook('cache_before_write', $template);
        }
        $this->html = $PIVOTX['template']->fetch($template, $cachekey);
        // We're going to something really, really stupid here. If we've enabled
        // caching, and we've just written to the cache, we immediately get
        // the page from the cache again, because the copy that's in memory does
        // not have the HTML that's updated with our hooks. Thankfully this step
        // is really, really fast, because we use caching. :-)
        if ($PIVOTX['config']->get('smarty_cache')) {
            $this->html = $PIVOTX['template']->fetch($template, $cachekey);
            // Before we continue, we check for cache_after_write hooks..
            $PIVOTX['extensions']->executeHook('cache_after_write', $this->html);
        }
    }