+// Enables the reset mode and runs it
+function doReset () {
+ // Enable the reset mode
+ $GLOBALS['reset_enabled'] = true;
+
+ // Run filters
+ runFilterChain('reset');
+}
+
+// Our shutdown-function
+function shutdown () {
+ // Call the filter chain 'shutdown'
+ runFilterChain('shutdown', null);
+
+ if (SQL_IS_LINK_UP()) {
+ // Close link
+ SQL_CLOSE(__FILE__, __LINE__);
+ } elseif (!isInstallationPhase()) {
+ // No database link
+ addFatalMessage(__FILE__, __LINE__, getMessage('NO_DB_LINK_SHUTDOWN'));
+ }
+
+ // Stop executing here
+ exit;
+}
+
+// Setter for userid
+function setUserId ($userid) {
+ $GLOBALS['userid'] = bigintval($userid);
+}
+
+// Getter for userid or returns zero
+function getUserId () {
+ // Default userid
+ $userid = 0;
+
+ // Is the userid set?
+ if (isUserIdSet()) {
+ // Then use it
+ $userid = $GLOBALS['userid'];
+ } // END - if
+
+ // Return it
+ return $userid;
+}
+
+// Checks ether the userid is set
+function isUserIdSet () {
+ return (isset($GLOBALS['userid']));
+}
+
+// Handle message codes from URL
+function handleCodeMessage () {
+ if (isGetRequestElementSet('code')) {
+ // Default extension is 'unknown'
+ $ext = 'unknown';
+
+ // Is extension given?
+ if (isGetRequestElementSet('ext')) $ext = getRequestElement('ext');
+
+ // Convert the 'code' parameter from URL to a human-readable message
+ $message = getMessageFromErrorCode(getRequestElement('code'));
+
+ // Load message template
+ loadTemplate('message', false, $message);
+ } // END - if
+}
+
+// Setter for extra title
+function setExtraTitle ($extraTitle) {
+ $GLOBALS['extra_title'] = $extraTitle;
+}
+
+// Getter for extra title
+function getExtraTitle () {
+ // Is the extra title set?
+ if (!isExtraTitleSet()) {
+ // No, then abort here
+ debug_report_bug('extra_title is not set!');
+ } // END - if
+
+ // Return it
+ return $GLOBALS['extra_title'];
+}
+
+// Checks if the extra title is set
+function isExtraTitleSet () {
+ return ((isset($GLOBALS['extra_title'])) && (!empty($GLOBALS['extra_title'])));
+}
+
+// Generates a 'extension foo inactive' message
+function generateExtensionInactiveMessage ($ext_name) {
+ // Is the extension empty?
+ if (empty($ext_name)) {
+ // This should not happen
+ debug_report_bug(__FUNCTION__ . ': Parameter ext is empty. This should not happen.');
+ } // END - if
+
+ // Default message
+ $message = sprintf(getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), $ext_name);
+
+ // Is an admin logged in?
+ if (isAdmin()) {
+ // Then output admin message
+ $message = sprintf(getMessage('ADMIN_EXTENSION_PROBLEM_EXT_INACTIVE'), $ext_name);
+ } // END - if
+
+ // Return prepared message
+ return $message;
+}
+
+// Generates a 'extension foo not installed' message
+function generateExtensionNotInstalledMessage ($ext_name) {
+ // Is the extension empty?
+ if (empty($ext_name)) {
+ // This should not happen
+ debug_report_bug(__FUNCTION__ . ': Parameter ext is empty. This should not happen.');
+ } // END - if
+
+ // Default message
+ $message = sprintf(getMessage('EXTENSION_PROBLEM_EXT_NOT_INSTALLED'), $ext_name);
+
+ // Is an admin logged in?
+ if (isAdmin()) {
+ // Then output admin message
+ $message = sprintf(getMessage('ADMIN_EXTENSION_PROBLEM_EXT_NOT_INSTALLED'), $ext_name);
+ } // END - if
+
+ // Return prepared message
+ return $message;
+}
+
+// Generates a message depending on if the extension is not installed or not
+// just activated
+function generateExtensionInactiveNotInstalledMessage ($ext_name) {
+ // Init message
+ $message = '';
+
+ // Is the extension not installed or just deactivated?
+ switch (isExtensionInstalled($ext_name)) {
+ case true; // Deactivated!
+ $message = generateExtensionInactiveMessage($ext_name);
+ break;
+
+ case false; // Not installed!
+ $message = generateExtensionNotInstalledMessage($ext_name);
+ break;
+
+ default: // Should not happen!
+ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid state of extension %s detected.", $ext_name));
+ $message = sprintf("Invalid state of extension %s detected.", $ext_name);
+ break;
+ } // END - switch
+
+ // Return the message
+ return $message;
+}
+
+// Reads a directory recursively by default and searches for files not matching
+// an exclusion pattern. You can now keep the exclusion pattern empty for reading
+// a whole directory.
+function getArrayFromDirectory ($baseDir, $prefix, $fileIncludeDirs = false, $addBaseDir = true, $excludeArray = array(), $extension = '.php', $excludePattern = '@(\.|\.\.)$@', $recursive = true) {
+ // Add default entries we should exclude
+ $excludeArray[] = '.';
+ $excludeArray[] = '..';
+ $excludeArray[] = '.svn';
+ $excludeArray[] = '.htaccess';
+
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "baseDir={$baseDir},prefix={$prefix} - Entered!");
+ // Init includes
+ $files = array();
+
+ // Open directory
+ $dirPointer = opendir(getConfig('PATH') . $baseDir) or app_die(__FUNCTION__, __LINE__, 'Cannot read directory ' . basename($baseDir) . '.');
+
+ // Read all entries
+ while ($baseFile = readdir($dirPointer)) {
+ // Exclude '.', '..' and entries in $excludeArray automatically
+ if (in_array($baseFile, $excludeArray, true)) {
+ // Exclude them
+ //* DEBUG: */ outputHtml('excluded=' . $baseFile . '<br />');
+ continue;
+ } // END - if
+
+ // Construct include filename and FQFN
+ $fileName = $baseDir . $baseFile;
+ $FQFN = getConfig('PATH') . $fileName;
+
+ // Remove double slashes
+ $FQFN = str_replace('//', '/', $FQFN);
+
+ // Check if the base filename matches an exclusion pattern and if the pattern is not empty
+ if ((!empty($excludePattern)) && (preg_match($excludePattern, $baseFile, $match))) {
+ // These Lines are only for debugging!!
+ //* DEBUG: */ outputHtml('baseDir:' . $baseDir . '<br />');
+ //* DEBUG: */ outputHtml('baseFile:' . $baseFile . '<br />');
+ //* DEBUG: */ outputHtml('FQFN:' . $FQFN . '<br />');
+
+ // Exclude this one
+ continue;
+ } // END - if
+
+ // Skip also files with non-matching prefix genericly
+ if (($recursive === true) && (isDirectory($FQFN))) {
+ // Is a redirectory so read it as well
+ $files = merge_array($files, getArrayFromDirectory($baseDir . $baseFile . '/', $prefix, $fileIncludeDirs, $addBaseDir, $excludeArray, $extension, $excludePattern, $recursive));
+
+ // And skip further processing
+ continue;
+ } elseif (substr($baseFile, 0, strlen($prefix)) != $prefix) {
+ // Skip this file
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Invalid prefix in file " . $baseFile . ", prefix=" . $prefix);
+ continue;
+ } elseif (!isFileReadable($FQFN)) {
+ // Not readable so skip it
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "File " . $FQFN . " is not readable!");
+ continue;
+ }
+
+ // Is the file a PHP script or other?
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "baseDir={$baseDir},prefix={$prefix},baseFile={$baseFile}");
+ if ((substr($baseFile, -4, 4) == '.php') || (($fileIncludeDirs === true) && (isDirectory($FQFN)))) {
+ // Is this a valid include file?
+ if ($extension == '.php') {
+ // Remove both for extension name
+ $extName = substr($baseFile, strlen($prefix), -4);
+
+ // Is the extension valid and active?
+ if (isExtensionNameValid($extName)) {
+ // Then add this file
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension entry ' . $baseFile . ' added.');
+ $files[] = $fileName;
+ } else {
+ // Add non-extension files as well
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Regular entry ' . $baseFile . ' added.');
+ if ($addBaseDir === true) {
+ $files[] = $fileName;
+ } else {
+ $files[] = $baseFile;
+ }
+ }
+ } else {
+ // We found .php file but should not search for them, why?
+ debug_report_bug('We should find files with extension=' . $extension . ', but we found a PHP script.');
+ }
+ } elseif (substr($baseFile, -4, 4) == $extension) {
+ // Other, generic file found
+ $files[] = $fileName;
+ }
+ } // END - while
+
+ // Close directory
+ closedir($dirPointer);
+
+ // Sort array
+ asort($files);
+
+ // Return array with include files
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '- Left!');
+ return $files;
+}
+
+// Maps a module name into a database table name
+function mapModuleToTable ($moduleName) {
+ // Map only these, still lame code...
+ switch ($moduleName) {
+ // 'index' is the guest's menu
+ case 'index': $moduleName = 'guest'; break;
+ // ... and 'login' the member's menu
+ case 'login': $moduleName = 'member'; break;
+ // Anything else will not be mapped, silently.
+ } // END - switch
+
+ // Return result
+ return $moduleName;
+}
+
+// Add SQL debug data to array for later output
+function addSqlToDebug ($result, $sqlString, $timing, $F, $L) {
+ // Already executed?
+ if (isset($GLOBALS['debug_sqls'][$F][$L][$sqlString])) {
+ // Then abort here, we don't need to profile a query twice
+ return;
+ } // END - if
+
+ // Remeber this as profiled (or not, but we don't care here)
+ $GLOBALS['debug_sqls'][$F][$L][$sqlString] = true;
+
+ // Do we have cache?
+ if (!isset($GLOBALS['debug_sql_available'])) {
+ // Check it and cache it in $GLOBALS
+ $GLOBALS['debug_sql_available'] = ((isConfigurationLoaded()) && (isExtensionInstalledAndNewer('other', '0.2.2')) && (getConfig('display_debug_sqls') == 'Y'));
+ } // END - if
+
+ // Don't execute anything here if we don't need or ext-other is missing
+ if ($GLOBALS['debug_sql_available'] === false) {
+ return;
+ } // END - if
+
+ // Generate record
+ $record = array(
+ 'num_rows' => SQL_NUMROWS($result),
+ 'affected' => SQL_AFFECTEDROWS(),
+ 'sql_str' => $sqlString,
+ 'timing' => $timing,
+ 'file' => basename($F),
+ 'line' => $L
+ );
+
+ // Add it
+ $GLOBALS['debug_sqls'][] = $record;
+}
+
+// Initializes the cache instance
+function initCacheInstance () {
+ // Load include for CacheSystem class
+ loadIncludeOnce('inc/classes/cachesystem.class.php');
+
+ // Initialize cache system only when it's needed
+ $GLOBALS['cache_instance'] = new CacheSystem();
+ if ($GLOBALS['cache_instance']->getStatus() != 'done') {
+ // Failed to initialize cache sustem
+ addFatalMessage(__FILE__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): ' . getMessage('CACHE_CANNOT_INITIALIZE'));
+ } // END - if
+}
+
+// Getter for message from array or raw message
+function getMessageFromIndexedArray ($message, $pos, $array) {
+ // Check if the requested message was found in array
+ if (isset($array[$pos])) {
+ // ... if yes then use it!
+ $ret = $array[$pos];
+ } else {
+ // ... else use default message
+ $ret = $message;
+ }
+
+ // Return result
+ return $ret;
+}
+
+// Print code with line numbers
+function linenumberCode ($code) {
+ if (!is_array($code)) $codeE = explode("\n", $code); else $codeE = $code;
+ $count_lines = count($codeE);
+
+ $r = 'Line | Code:<br />';
+ foreach($codeE as $line => $c) {
+ $r .= '<div class="line"><span class="linenum">';
+ if ($count_lines == 1) {
+ $r .= 1;
+ } else {
+ $r .= ($line == ($count_lines - 1)) ? '' : ($line+1);
+ }
+ $r .= '</span>|';
+
+ // Add code
+ $r .= '<span class="linetext">' . htmlentities($c) . '</span></div>';
+ }
+
+ return '<div class="code">' . $r . '</div>';
+}
+
+// Convert ';' to ', ' for e.g. receiver list
+function convertReceivers ($old) {
+ return str_replace(';', ', ', $old);
+}
+