-// Load a template file and return it's content (only it's name; do not use ' or ")
-function loadTemplate ($template, $return = false, $content = array()) {
- // @TODO Remove this sanity-check if all is fine
- if (!is_bool($return)) debug_report_bug(__FUNCTION__, __LINE__, 'return is not bool (' . gettype($return) . ')');
-
- // @TODO Try to rewrite all $DATA to $content
- global $DATA;
-
- // Do we have cache?
- if ((isDebuggingTemplateCache() === false) && (isTemplateCached($template))) {
- // Evaluate the cache
- eval(readTemplateCache($template));
- } elseif (!isset($GLOBALS['template_eval'][$template])) {
- // Make all template names lowercase
- $template = strtolower($template);
-
- // Init some data
- $ret = '';
- if (empty($GLOBALS['refid'])) $GLOBALS['refid'] = '0';
-
- // Base directory
- $basePath = sprintf("%stemplates/%s/html/", getConfig('PATH'), getLanguage());
- $extraPath = detectExtraTemplatePath($template);;
-
- ////////////////////////
- // Generate file name //
- ////////////////////////
- $FQFN = $basePath . $extraPath . $template . '.tpl';
-
- // Does the special template exists?
- if (!isFileReadable($FQFN)) {
- // Reset to default template
- $FQFN = $basePath . $template . '.tpl';
- } // END - if
-
- // Now does the final template exists?
- if (isFileReadable($FQFN)) {
- // Count the template load
- incrementConfigEntry('num_templates');
-
- // The local file does exists so we load it. :)
- $GLOBALS['tpl_content'] = readFromFile($FQFN);
-
- // Do we have to compile the code?
- $ret = '';
- if ((strpos($GLOBALS['tpl_content'], '$') !== false) || (strpos($GLOBALS['tpl_content'], '{--') !== false) || (strpos($GLOBALS['tpl_content'], '{?') !== false) || (strpos($GLOBALS['tpl_content'], '{%') !== false)) {
- // Normal HTML output?
- if (getOutputMode() == '0') {
- // Add surrounding HTML comments to help finding bugs faster
- $ret = '<!-- Template ' . $template . " - Start -->\n" . $GLOBALS['tpl_content'] . '<!-- Template ' . $template . " - End -->\n";
-
- // Prepare eval() command
- $eval = '$ret = "' . compileCode(escapeQuotes($ret)) . '";';
- } elseif (substr($template, 0, 3) == 'js_') {
- // JavaScripts don't like entities and timings
- $eval = '$ret = decodeEntities("' . compileRawCode(escapeJavaScriptQuotes($GLOBALS['tpl_content'])) . '");';
- } else {
- // Prepare eval() command, other output doesn't like entities, maybe
- $eval = '$ret = decodeEntities("' . compileRawCode(escapeQuotes($GLOBALS['tpl_content'])) . '");';
- }
- } else {
- // Add surrounding HTML comments to help finding bugs faster
- $ret = '<!-- Template ' . $template . " - Start -->\n" . $GLOBALS['tpl_content'] . '<!-- Template ' . $template . " - End -->\n";
- $eval = '$ret = "' . compileRawCode(escapeQuotes($ret)) . '";';
- } // END - if
-
- // Cache the eval() command here
- $GLOBALS['template_eval'][$template] = $eval;
- } elseif ((isAdmin()) || ((isInstalling()) && (!isInstalled()))) {
- // Only admins shall see this warning or when installation mode is active
- $ret = '<div class="para">
- <span class="guest_failed">{--TEMPLATE_404--}</span>
-</div>
-<div class="para">
- (' . $template . ')
-</div>
-<div class="para">
- {--TEMPLATE_CONTENT--}
- <pre>' . print_r($content, true) . '</pre>
- {--TEMPLATE_DATA--}
- <pre>' . print_r($DATA, true) . '</pre>
-</div>';
- } else {
- // No file!
- $GLOBALS['template_eval'][$template] = '404';
- }
- }
-
- // Code set?
- if ((isset($GLOBALS['template_eval'][$template])) && ($GLOBALS['template_eval'][$template] != '404')) {
- // Eval the code
- eval($GLOBALS['template_eval'][$template]);
- } // END - if
-
- // Do we have some content to output or return?
- if (!empty($ret)) {
- // Not empty so let's put it out! ;)
- if ($return === true) {
- // Return the HTML code
- return $ret;
- } else {
- // Output directly
- outputHtml($ret);
- }
- } elseif (isDebugModeEnabled()) {
- // Warning, empty output!
- return 'E:' . $template . ',content=<pre>' . print_r($content, true) . '</pre>';
- }
-}
-
-// Detects the extra template path from given template name
-function detectExtraTemplatePath ($template) {
- // Default is empty
- $extraPath = '';
-
- // Do we have cache?
- if (!isset($GLOBALS['extra_path'][$template])) {
- // Check for admin/guest/member/etc. templates
- if (substr($template, 0, 6) == 'admin_') {
- // Admin template found
- $extraPath = 'admin/';
- } elseif (substr($template, 0, 6) == 'guest_') {
- // Guest template found
- $extraPath = 'guest/';
- } elseif (substr($template, 0, 7) == 'member_') {
- // Member template found
- $extraPath = 'member/';
- } elseif (substr($template, 0, 7) == 'select_') {
- // Selection template found
- $extraPath = 'select/';
- } elseif (substr($template, 0, 8) == 'install_') {
- // Installation template found
- $extraPath = 'install/';
- } elseif (substr($template, 0, 4) == 'ext_') {
- // Extension template found
- $extraPath = 'ext/';
- } elseif (substr($template, 0, 3) == 'la_') {
- // 'Logical-area' template found
- $extraPath = 'la/';
- } elseif (substr($template, 0, 3) == 'js_') {
- // JavaScript template found
- $extraPath = 'js/';
- } elseif (substr($template, 0, 5) == 'menu_') {
- // Menu template found
- $extraPath = 'menu/';
- } else {
- // Test for extension
- $test = substr($template, 0, strpos($template, '_'));
-
- // Probe for valid extension name
- if (isExtensionNameValid($test)) {
- // Set extra path to extension's name
- $extraPath = $test . '/';
- } // END - if
- }
-
- // Store it in cache
- $GLOBALS['extra_path'][$template] = $extraPath;
- } // END - if
-
- // Return result
- return $GLOBALS['extra_path'][$template];
-}
-
-// Loads an email template and compiles it
-function loadEmailTemplate ($template, $content = array(), $userid = '0') {
- global $DATA;
-
- // Make sure all template names are lowercase!
- $template = strtolower($template);
-
- // Is content an array?
- if (is_array($content)) {
- // Add expiration to array
- if ((isConfigEntrySet('auto_purge')) && (getConfig('auto_purge') == '0')) {
- // Will never expire!
- $content['expiration'] = '{--MAIL_WILL_NEVER_EXPIRE--}';
- } elseif (isConfigEntrySet('auto_purge')) {
- // Create nice date string
- $content['expiration'] = createFancyTime(getConfig('auto_purge'));
- } else {
- // Missing entry
- $content['expiration'] = '{--MAIL_NO_CONFIG_AUTO_PURGE--}';
- }
- } // END - if
-
- // Load user's data
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "UID={$userid},template={$template},content[]=".gettype($content));
- if (($userid > 0) && (is_array($content))) {
- // If nickname extension is installed, fetch nickname as well
- if ((isExtensionActive('nickname')) && (isNicknameUsed($userid))) {
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "NICKNAME!<br />");
- // Load by nickname
- fetchUserData($userid, 'nickname');
- } else {
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "NO-NICK!<br />");
- /// Load by userid
- fetchUserData($userid);
- }
-
- // Merge data if valid
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "content()=".count($content)." - PRE<br />");
- if (isUserDataValid()) {
- $content = merge_array($content, getUserDataArray());
- } // END - if
- //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "content()=".count($content)." - AFTER<br />");
- } // END - if
-
- // Overwrite email from data if present
- if (isset($content['email'])) $email = $content['email'];
-
- // Store email for some functions in global $DATA array
- // @TODO Do only use $content, not $DATA or raw variables
- $DATA['email'] = $email;
-
- // Base directory
- $basePath = sprintf("%stemplates/%s/emails/", getConfig('PATH'), getLanguage());
-
- // Detect extra path
- $extraPath = detectExtraTemplatePath($template);
-
- // Generate full FQFN
- $FQFN = $basePath . $extraPath . $template . '.tpl';
-
- // Does the special template exists?
- if (!isFileReadable($FQFN)) {
- // Reset to default template
- $FQFN = $basePath . $template . '.tpl';
- } // END - if
-
- // Now does the final template exists?
- $newContent = '';
- if (isFileReadable($FQFN)) {
- // The local file does exists so we load it. :)
- $GLOBALS['tpl_content'] = readFromFile($FQFN);
-
- // Run code
- $GLOBALS['tpl_content'] = '$newContent = decodeEntities("' . compileRawCode(escapeQuotes($GLOBALS['tpl_content'])) . '");';
- eval($GLOBALS['tpl_content']);
- } elseif (!empty($template)) {
- // Template file not found!
- $newContent = '<div class="para">
- {--TEMPLATE_404--}: ' . $template . '
-</div>
-<div class="para">
- {--TEMPLATE_CONTENT--}
- <pre>' . print_r($content, true) . '</pre>
- {--TEMPLATE_DATA--}
- <pre>' . print_r($DATA, true) . '</pre>
-</div>';
-
- // Debug mode not active? Then remove the HTML tags
- if (!isDebugModeEnabled()) $newContent = secureString($newContent);
- } else {
- // No template name supplied!
- $newContent = '{--NO_TEMPLATE_SUPPLIED--}';
- }
-
- // Is there some content?
- if (empty($newContent)) {
- // Compiling failed
- $newContent = "Compiler error for template " . $template . " !\nUncompiled content:\n" . $GLOBALS['tpl_content'];
-
- // Add last error if the required function exists
- if (function_exists('error_get_last')) $newContent .= "\n--------------------------------------\nDebug:\n".print_r(error_get_last(), true)."--------------------------------------\nPlease don't alter these informations!\nThanx.";
- } // END - if
-
- // Remove content and data
- unset($content);
- unset($DATA);
-
- // Return content
- return $newContent;
-}
-