-// 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('return is not bool (' . gettype($return) . ')');
-
- // @TODO Try to rewrite all $DATA to $content
- global $DATA;
-
- // Do we have cache?
- if (!isset($GLOBALS['template_eval'][$template])) {
- // Add more variables which you want to use in your template files
- $username = getUsername();
-
- // Make all template names lowercase
- $template = strtolower($template);
-
- // Count the template load
- incrementConfigEntry('num_templates');
-
- // Init some data
- $ret = '';
- if (empty($GLOBALS['refid'])) $GLOBALS['refid'] = 0;
-
- // Generate date/time string
- $date_time = generateDateTime(time(), '1');
-
- // Is content an array
- if (is_array($content)) $content['date_time'] = $date_time;
-
- // @DEPRECATED Try to rewrite the if() condition
- if ($template == 'member_support_form') {
- // Support request of a member
- $result = SQL_QUERY_ESC("SELECT `userid`, `gender`, `surname`, `family`, `email` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
- array(getUserId()), __FUNCTION__, __LINE__);
-
- // Is content an array?
- if (is_array($content)) {
- // Merge data
- $content = merge_array($content, SQL_FETCHARRAY($result));
-
- // Translate gender
- $content['gender'] = translateGender($content['gender']);
- } else {
- // @DEPRECATED
- // @TODO Find all templates which are using these direct variables and rewrite them.
- // @TODO After this step is done, this else-block is history
- list($gender, $surname, $family, $email) = SQL_FETCHROW($result);
-
- // Translate gender
- $gender = translateGender($gender);
- logDebugMessage(__FUNCTION__, __LINE__, sprintf("DEPRECATION-WARNING: content is not array [%s], template=%s.", gettype($content), $template));
- }
-
- // Free result
- SQL_FREERESULT($result);
- } // END - if
-
- // Base directory
- $basePath = sprintf("%stemplates/%s/html/", getConfig('PATH'), getLanguage());
- $mode = '';
-
- // Check for admin/guest/member templates
- if (substr($template, 0, 6) == 'admin_') {
- // Admin template found
- $mode = 'admin/';
- } elseif (substr($template, 0, 6) == 'guest_') {
- // Guest template found
- $mode = 'guest/';
- } elseif (substr($template, 0, 7) == 'member_') {
- // Member template found
- $mode = 'member/';
- } elseif (substr($template, 0, 8) == 'install_') {
- // Installation template found
- $mode = 'install/';
- } elseif (substr($template, 0, 4) == 'ext_') {
- // Extension template found
- $mode = 'ext/';
- } elseif (substr($template, 0, 3) == 'la_') {
- // 'Logical-area' template found
- $mode = 'la/';
- } elseif (substr($template, 0, 3) == 'js_') {
- // JavaScript template found
- $mode = 'js/';
- } elseif (substr($template, 0, 5) == 'menu_') {
- // Menu template found
- $mode = '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
- $mode = $test . '/';
- } // END - if
- }
-
- ////////////////////////
- // Generate file name //
- ////////////////////////
- $FQFN = $basePath . $mode . $template . '.tpl';
-
- if ((isWhatSet()) && ((strpos($template, '_header') > 0) || (strpos($template, '_footer') > 0)) && (($mode == 'guest/') || ($mode == 'member/') || ($mode == 'admin/'))) {
- // Select what depended header/footer template file for admin/guest/member area
- $file2 = sprintf("%s%s%s_%s.tpl",
- $basePath,
- $mode,
- $template,
- getWhat()
- );
-
- // Probe for it...
- if (isFileReadable($file2)) $FQFN = $file2;
-
- // Remove variable from memory
- unset($file2);
- } // END - if
-
- // 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)) {
- // The local file does exists so we load it. :)
- $GLOBALS['tpl_content'] = readFromFile($FQFN);
-
- // Replace ' to our own chars to preventing them being quoted
- while (strpos($GLOBALS['tpl_content'], "'") !== false) { $GLOBALS['tpl_content'] = str_replace("'", '{QUOT}', $GLOBALS['tpl_content']); }
-
- // 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 ($GLOBALS['output_mode'] == 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(smartAddSlashes($GLOBALS['tpl_content'])) . '";';
- } else {
- // Prepare eval() command
- $eval = '$ret = "' . compileCode(smartAddSlashes($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 = "' . smartAddSlashes($ret) . '";';
- } // END - if
-
- // Cache the eval() command here
- $GLOBALS['template_eval'][$template] = $eval;
-
- // Eval the code
- eval($GLOBALS['template_eval'][$template]);
- } else {
- // No file!
- $GLOBALS['template_eval'][$template] = '404';
- }
- } elseif (((isAdmin()) || ((isInstalling()) && (!isInstalled()))) && ($GLOBALS['template_eval'][$template] == '404')) {
- // Only admins shall see this warning or when installation mode is active
- $ret = '<br /><span class=\\"guest_failed\\">{--TEMPLATE_404--}</span><br />
-(' . $template . ')<br />
-<br />
-{--TEMPLATE_CONTENT--}
-<pre>' . print_r($content, true) . '</pre>
-{--TEMPLATE_DATA--}
-<pre>' . print_r($DATA, true) . '</pre>
-<br /><br />\";';
- } else {
- // Eval the code
- eval($GLOBALS['template_eval'][$template]);
- }
-
- // 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 direct
- outputHtml($ret);
- }
- } elseif (isDebugModeEnabled()) {
- // Warning, empty output!
- return 'E:' . $template . ',content=<pre>' . print_r($content, true) . '</pre>';
- }
-}
-
-// Loads an email template and compiles it
-function loadEmailTemplate ($template, $content = array(), $UID = '0') {
- global $DATA;
-
- // Our configuration is kept non-global here
- $_CONFIG = getConfigArray();
-
- // Make sure all template names are lowercase!
- $template = strtolower($template);
-
- // Default 'nickname' if extension is not installed
- $nick = '---';
-
- // Prepare IP number and User Agent
- $REMOTE_ADDR = detectRemoteAddr();
- $HTTP_USER_AGENT = detectUserAgent();
-
- // Default admin
- $ADMIN = getConfig('MAIN_TITLE');
-
- // Is the admin logged in?
- if (isAdmin()) {
- // Get admin id
- $adminId = getCurrentAdminId();
-
- // Load Admin data
- $ADMIN = getAdminEmail($adminId);
- } // END - if
-
- // Neutral email address is default
- $email = getConfig('WEBMASTER');
-
- // Expiration in a nice output format
- // NOTE: Use $content[expiration] in your templates instead of $EXPIRATION
- if (getConfig('auto_purge') == 0) {
- // Will never expire!
- $EXPIRATION = getMessage('MAIL_WILL_NEVER_EXPIRE');
- } else {
- // Create nice date string
- $EXPIRATION = createFancyTime(getConfig('auto_purge'));
- }
-
- // Is content an array?
- if (is_array($content)) {
- // Add expiration to array, $EXPIRATION is now deprecated!
- $content['expiration'] = $EXPIRATION;
- } // END - if
-
- // Load user's data
- //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):UID={$UID},template={$template},content[]=".gettype($content)."<br />");
- if (($UID > 0) && (is_array($content))) {
- // If nickname extension is installed, fetch nickname as well
- if (isExtensionActive('nickname')) {
- //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):NICKNAME!<br />");
- // Load nickname
- $result = SQL_QUERY_ESC("SELECT `surname`, `family`, `gender`, `email`, `nickname` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
- array(bigintval($UID)), __FUNCTION__, __LINE__);
- } else {
- //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):NO-NICK!<br />");
- /// Load normal data
- $result = SQL_QUERY_ESC("SELECT `surname`, `family`, `gender`, `email` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
- array(bigintval($UID)), __FUNCTION__, __LINE__);
- }
-
- // Fetch and merge data
- //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):content()=".count($content)." - PRE<br />");
- $content = merge_array($content, SQL_FETCHARRAY($result));
- //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):content()=".count($content)." - AFTER<br />");
-
- // Free result
- SQL_FREERESULT($result);
- } // END - if
-
- // Translate M to male or F to female if present
- if (isset($content['gender'])) $content['gender'] = translateGender($content['gender']);
-
- // Overwrite email from data if present
- if (isset($content['email'])) $email = $content['email'];
-
- // Store email for some functions in global data array
- $DATA['email'] = $email;
-
- // Base directory
- $basePath = sprintf("%stemplates/%s/emails/", getConfig('PATH'), getLanguage());
-
- // Check for admin/guest/member templates
- if (substr($template, 0, 6) == 'admin_') {
- // Admin template found
- $FQFN = $basePath.'admin/' . $template.'.tpl';
- } elseif (substr($template, 0, 6) == 'guest_') {
- // Guest template found
- $FQFN = $basePath.'guest/' . $template.'.tpl';
- } elseif (substr($template, 0, 7) == 'member_') {
- // Member template found
- $FQFN = $basePath.'member/' . $template.'.tpl';
- } else {
- // Test for extension
- $test = substr($template, 0, strpos($template, '_'));
- if (isExtensionNameValid($test)) {
- // Set extra path to extension's name
- $FQFN = $basePath . $test.'/' . $template.'.tpl';
- } else {
- // No special filename
- $FQFN = $basePath . $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);
- $GLOBALS['tpl_content'] = SQL_ESCAPE($GLOBALS['tpl_content']);
-
- // Run code
- $GLOBALS['tpl_content'] = "\$newContent = decodeEntities(\"".compileCode($GLOBALS['tpl_content'])."\");";
- eval($GLOBALS['tpl_content']);
- } elseif (!empty($template)) {
- // Template file not found!
- $newContent = "{--TEMPLATE_404--}: " . $template."<br />
-{--TEMPLATE_CONTENT--}
-<pre>".print_r($content, true)."</pre>
-{--TEMPLATE_DATA--}
-<pre>".print_r($DATA, true)."</pre>
-<br /><br />";
-
- // Debug mode not active? Then remove the HTML tags
- if (!isDebugModeEnabled()) $newContent = secureString($newContent);
- } else {
- // No template name supplied!
- $newContent = getMessage('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);
-
- // Compile the code and eval it
- $eval = '$newContent = "' . compileCode(smartAddSlashes($newContent)) . '";';
- eval($eval);
-
- // Return content
- return $newContent;
-}
-