Beispiel #1
0
/**
 * Get a tempcoded version of a normal XHTML template. It is perhaps the most common ocPortal function to load up templates using do_template, and then attach them together either as parameters to each other, or via the tempcode attach method.
 *
 * @param  ID_TEXT			The codename of the template being loaded
 * @param  ?array				A map of parameters for the template (key to value) (NULL: no parameters)
 * @param  ?LANGUAGE_NAME 	The language to load the template in (templates can embed language references) (NULL: users own language)
 * @param  boolean			Whether to not produce a stack dump if the template is missing
 * @param  ?ID_TEXT			Alternate template to use if the primary one does not exist (NULL: none)
 * @param  string				File type suffix of template file (e.g. .tpl)
 * @param  string				Subdirectory type to look in
 * @set    templates css
 * @param  ?ID_TEXT			Theme to use (NULL: current theme)
 * @return tempcode			The tempcode for this template
 */
function do_template($codename, $parameters = NULL, $lang = NULL, $light_error = false, $fallback = NULL, $suffix = '.tpl', $type = 'templates', $theme = NULL)
{
    if (!isset($lang) || $lang == '') {
        global $USER_LANG_CACHED;
        $lang = isset($USER_LANG_CACHED) ? $USER_LANG_CACHED : (function_exists('user_lang') ? user_lang() : 'EN');
    }
    if ($GLOBALS['SEMI_DEBUG_MODE']) {
        if ($codename == strtolower($codename) && $type != 'css' && $codename != 'tempcode_test' && $codename != 'handle_conflict_resolution') {
            fatal_exit('Template names should be in upper case, and the files should be stored in upper case.');
        }
        if (substr($codename, -7) == '_SCREEN' || substr($codename, -8) == '_OVERLAY' || $codename == 'POOR_XHTML_WRAPPER' || $codename == 'OCF_WRAPPER') {
            $GLOBALS['SCREEN_TEMPLATE_CALLED'] = $codename;
        }
    }
    global $TEMPLATE_PREVIEW_OP, $RECORD_TEMPLATES_USED, $RECORDED_TEMPLATES_USED, $FILE_ARRAY, $MEM_CACHE, $KEEP_MARKERS, $SHOW_EDIT_LINKS, $XHTML_SPIT_OUT, $CACHE_TEMPLATES, $FORUM_DRIVER, $POSSIBLY_IN_SAFE_MODE, $CACHED_THEME, $CACHED_FOUND, $LOADED_TPL_CACHE;
    $special_treatment = ($KEEP_MARKERS || $SHOW_EDIT_LINKS) && is_null($XHTML_SPIT_OUT);
    if ($RECORD_TEMPLATES_USED) {
        $RECORDED_TEMPLATES_USED[] = $codename;
    }
    // Variables we'll need
    if (!isset($theme)) {
        $theme = isset($CACHED_THEME) ? $CACHED_THEME : (isset($FORUM_DRIVER) && is_object($FORUM_DRIVER) && method_exists($FORUM_DRIVER, 'get_theme') ? filter_naughty($FORUM_DRIVER->get_theme()) : 'default');
    }
    $prefix_default = get_file_base() . '/themes/';
    $prefix = $theme == 'default' ? $prefix_default : get_custom_file_base() . '/themes/';
    // Is it structurally cached on disk yet?
    if (!isset($CACHED_FOUND[$codename][$lang][$theme][$suffix][$type])) {
        $loaded_this_once = false;
    } else {
        $loaded_this_once = true;
    }
    $_data = false;
    if ($CACHE_TEMPLATES && !$TEMPLATE_PREVIEW_OP && (!$POSSIBLY_IN_SAFE_MODE || !in_safe_mode())) {
        $tcp_path = $prefix . $theme . '/templates_cached/' . $lang . '/' . $codename . $suffix . '.tcp';
        if ($loaded_this_once) {
            if (isset($LOADED_TPL_CACHE[$codename][$theme])) {
                $_data = $LOADED_TPL_CACHE[$codename][$theme];
            } else {
                $_data = new ocp_tempcode();
                $test = $_data->from_assembly_executed($tcp_path, array($codename, $codename, $lang, $theme, $suffix, $type, $fallback));
                if (!$test) {
                    $_data = false;
                }
                // failed
            }
        } else {
            global $SITE_INFO;
            $support_smart_decaching = !isset($SITE_INFO['disable_smart_decaching']) || $SITE_INFO['disable_smart_decaching'] == '0';
            if ($support_smart_decaching) {
                if (!isset($CACHED_FOUND[$codename][$lang][$theme][$suffix][$type])) {
                    $found = find_template_place($codename, $lang, $theme, $suffix, $type);
                    $CACHED_FOUND[$codename][$lang][$theme][$suffix][$type] = $found;
                } else {
                    $found = $CACHED_FOUND[$codename][$lang][$theme][$suffix][$type];
                }
                if (!is_null($found)) {
                    if (isset($GLOBALS['CURRENT_SHARE_USER'])) {
                        $file_path = get_custom_file_base() . '/themes/' . $found[0] . $found[1] . $codename . $suffix;
                        if (!is_file($file_path)) {
                            $file_path = get_file_base() . '/themes/' . $found[0] . $found[1] . $codename . $suffix;
                        }
                    } else {
                        $file_path = ($theme == 'default' && $suffix != '.css' ? get_file_base() : get_custom_file_base()) . '/themes/' . $found[0] . $found[1] . $codename . $suffix;
                    }
                    $tcp_time = @filemtime($tcp_path);
                } else {
                    $tcp_time = false;
                }
            }
            if (!$support_smart_decaching || $tcp_time !== false && is_file($file_path) && $found !== NULL) {
                if (!$support_smart_decaching || filemtime($file_path) < $tcp_time) {
                    $_data = new ocp_tempcode();
                    $test = $_data->from_assembly_executed($tcp_path, array($codename, $codename, $lang, $theme, $suffix, $type, $fallback));
                    if (!$test) {
                        $_data = false;
                    }
                    // failed
                }
            }
        }
    }
    if ($_data === false) {
        if (!isset($CACHED_FOUND[$codename][$lang][$theme][$suffix][$type])) {
            $found = find_template_place($codename, $lang, $theme, $suffix, $type);
            $CACHED_FOUND[$codename][$lang][$theme][$suffix][$type] = $found;
        } else {
            $found = $CACHED_FOUND[$codename][$lang][$theme][$suffix][$type];
        }
        unset($CACHED_FOUND[$codename][$lang][$theme][$suffix][$type]);
        if (is_null($found)) {
            if (is_null($fallback)) {
                if ($light_error) {
                    return paragraph(do_lang_tempcode('MISSING_TEMPLATE_FILE', escape_html($codename)), '34rwefwfdee');
                }
                fatal_exit(do_lang_tempcode('MISSING_TEMPLATE_FILE', escape_html($codename)));
            } else {
                $result = do_template($fallback, $parameters, $lang);
                return $result;
            }
        } else {
            require_code('tempcode_compiler');
            $_data = _do_template($found[0], $found[1], $codename, $codename, $lang, $suffix, $theme);
        }
    }
    if ($loaded_this_once) {
        // On 3rd load (and onwards) it will be fully cached
        $LOADED_TPL_CACHE[$codename][$theme] = $_data;
    }
    if (!isset($parameters)) {
        $out = new ocp_tempcode();
        $out->codename = $codename;
        $out->code_to_preexecute = $_data->code_to_preexecute;
        $out->preprocessable_bits = $_data->preprocessable_bits;
        $out->seq_parts = $_data->seq_parts;
        foreach ($out->seq_parts as $i => $bit) {
            if ($bit[1] != array()) {
                $out->seq_parts[$i][1] = array();
            }
        }
        return $out;
    }
    $ret = $_data->bind($parameters, $codename);
    if ($special_treatment) {
        $ret->codename = '(mixed)';
        // Stop optimisation that assumes the codename represents the sole content of it
    }
    if ($special_treatment) {
        if ($KEEP_MARKERS) {
            $__data = new ocp_tempcode();
            $__data->attach('<!-- START-TEMPLATE=' . escape_html($codename) . ' -->');
            $__data->attach($ret);
            $__data->attach('<!-- END-TEMPLATE=' . escape_html($codename) . ' -->');
            $ret = $__data;
        }
        if ($SHOW_EDIT_LINKS && $codename != 'PARAM_INFO') {
            $edit_url = build_url(array('page' => 'admin_themes', 'type' => '_edit_templates', 'theme' => $theme, 'f0file' => $codename), 'adminzone');
            $parameters2 = array();
            foreach ($parameters as $k => $v) {
                if (is_array($v)) {
                    $parameters2[$k] = '(array)';
                } elseif (!is_object($v)) {
                    $parameters2[$k] = $v;
                } else {
                    $parameters2[$k] = $v->evaluate();
                    if (strlen($parameters2[$k]) > 100) {
                        $parameters2[$k] = substr($parameters2[$k], 0, 100) . '...';
                    }
                }
            }
            $param_info = do_template('PARAM_INFO', array('MAP' => $parameters2));
            $SHOW_EDIT_LINKS = false;
            $ret = do_template('TEMPLATE_EDIT_LINK', array('_GUID' => '511ae911d31a5b237a4371ff22fc78fd', 'PARAM_INFO' => $param_info, 'CONTENTS' => $ret, 'CODENAME' => $codename, 'EDIT_URL' => $edit_url));
            $SHOW_EDIT_LINKS = true;
        }
    }
    return $ret;
}