+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();