Exemple #1
0
/**
 * Get the name of the PHP file to include for rendering a given template.
 *
 * If the format for $template is <module>::<template>, then
 * the template is loaded from the module's template directory. The
 * directory structure for storing module templates is the same as for the
 * main templates directory, only it is stored within a module's
 * directory:
 *
 *   <phorum_dir>/mods/templates/<template name>/<page>.tpl
 *
 * @param string $page
 *     The name of the temlate to compile (e.g. "header", "css",
 *     "somemod::template", etc.).
 *
 * @return string
 *     The name of the PHP file to include for rendering the template.
 */
function phorum_api_template($page)
{
    // This might for example happen if a template contains code like
    // {INCLUDE template} instead of {INCLUDE "template"}.
    if ($page === NULL || $page == "") {
        require_once PHORUM_PATH . '/include/api/error/backtrace.php';
        print "<html><head><title>Phorum Template Error</title><body>";
        print "<h1>Phorum Template Error</h1>";
        print "phorum_api_template() was called with an empty page name.<br/>";
        print "This might indicate a template problem.<br/>";
        if (function_exists('debug_print_backtrace')) {
            print "Here's a backtrace that might help finding the error:";
            print "<pre>";
            print phorum_api_error_backtrace();
            print "</pre>";
        }
        print "</body></html>";
        exit(1);
    }
    list($page, $phpfile, $tplfile) = phorum_api_template_resolve($page);
    // No template to process. This will happen in case a .php file
    // is used for defining the template instead of a .tpl file.
    if (empty($tplfile)) {
        return $phpfile;
    }
    // Compile the template if the output PHP file is not available.
    if (!file_exists($phpfile)) {
        require_once PHORUM_PATH . '/include/api/template/compile.php';
        phorum_api_template_compile($page, $tplfile, $phpfile);
    }
    return $phpfile;
}
Exemple #2
0
/**
 * @deprecated Replaced by {@link phorum_api_template_resolve()}.
 */
function phorum_get_template_file($template)
{
    return phorum_api_template_resolve($template);
}
Exemple #3
0
                 $r['cache_key'] = $mtime;
                 $module_registrations[$id]['cache_key'] = $mtime;
             }
             break;
         case "template":
             // We load the parsed template into memory. This will refresh
             // the cached template file if required. This is the easiest
             // way to make this work correctly for nested template files.
             ob_start();
             include phorum_api_template($m[2]);
             $module_registrations[$id]['content'] = ob_get_contents();
             ob_end_clean();
             // We use the mtime of the compiled template as the cache
             // key if no specific cache key was set.
             if (!isset($r['cache_key'])) {
                 list($m[2], $php, $tpl) = phorum_api_template_resolve($m[2]);
                 $mtime = @filemtime($php);
                 $r['cache_key'] = $mtime;
                 $module_registrations[$id]['cache_key'] = $mtime;
             }
             break;
         case "function":
             if (!isset($r['cache_key'])) {
                 trigger_error("javascript_register hook: module registration " . "error: \"cache_key\" field missing for source " . "\"{$r['source']}\" in module \"{$r['module']}\".");
                 exit(1);
             }
             break;
     }
 } else {
     trigger_error("javascript_register hook: module registration error: " . "illegal format for source definition \"{$r['source']}\" " . "in module \"{$r['module']}\".");
     exit(1);
Exemple #4
0
/**
 * Runs the first stage of the Phorum template processing. In this stage,
 * all (static) {include <template>} statements are recursively resolved.
 * After resolving all includes, a complete single template is constructed.
 * During this process, the function will keep track of all file
 * dependencies for the constructed template.
 *
 * @param $infile - The template file to process.
 * @param $include_depth - Current include depth (only for recursive call).
 * @param $deps - File dependencies (only for recursive call)
 * @param $include_once - Already include pages (only for recursive call)
 * @return $template - The constructed template data.
 * @return $dependencies - An array containing file dependencies for the
 *     created template data. The keys are filenames and the values are
 *     file modification times.
 */
function phorum_api_template_compile_pass1($infile, $include_depth = 0, $deps = array(), $include_once = array())
{
    $include_depth++;
    if ($include_depth > PHORUM_TEMPLATES_MAX_INCLUDE_DEPTH) {
        trigger_error("phorum_api_template_compile_pass1(): the include depth has passed " . "the maximum allowed include depth of " . PHORUM_TEMPLATES_MAX_INCLUDE_DEPTH . ". Maybe some circular " . "include loop was introduced? If not, then you can raise the " . "value for the PHORUM_TEMPLATES_MAX_INCLUDE_DEPTH definition " . "in " . htmlspecialchars(__FILE__) . ".", E_USER_ERROR);
    }
    $deps[$infile] = filemtime($infile);
    $template = phorum_api_read_file($infile);
    // Process {include [once] "page"} statements in the template.
    preg_match_all("/\\{include\\s+(.+?)\\}/is", $template, $matches);
    for ($i = 0; $i < count($matches[0]); $i++) {
        $tokens = phorum_api_template_compile_tokenize($matches[1][$i]);
        // Find out if we have a static value for the include statement.
        // Dynamic values are handled in pass 2.
        $only_once = false;
        if (strtolower($tokens[0]) == "once" && isset($tokens[1])) {
            $only_once = true;
            array_shift($tokens);
        }
        list($page, $type) = phorum_api_template_compile_val2php(NULL, $tokens[0]);
        if ($type == "variable" || $type == "constant") {
            continue;
        }
        // Since $value contains PHP code now, we have to resolve that
        // code into a real value.
        eval("\$page = {$page};");
        if ($only_once && isset($include_once[$page])) {
            $replace = '';
        } else {
            list($page, $subout, $subin) = phorum_api_template_resolve($page);
            if ($subin == NULL) {
                $replace = phorum_api_read_file($subout);
            } else {
                list($replace, $deps) = phorum_api_template_compile_pass1($subin, $include_depth, $deps, $include_once);
            }
            $include_once[$page] = true;
        }
        $template = str_replace($matches[0][$i], $replace, $template);
    }
    return array($template, $deps);
}