+// Getter for member id or returns zero
+function getMemberId () {
+ // Default member id
+ $memberid = '0';
+
+ // Is the member id set?
+ if (isMemberIdSet()) {
+ // Then use it
+ $memberid = $GLOBALS['member_id'];
+ } // END - if
+
+ // Return it
+ return $memberid;
+}
+
+// Checks ether the member id is set
+function isMemberIdSet () {
+ return (isset($GLOBALS['member_id']));
+}
+
+// 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(__FUNCTION__, __LINE__, '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'])));
+}
+
+// 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, $suffix = '') {
+ // 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(getPath() . $baseDir) or debug_report_bug(__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: */ debugOutput('excluded=' . $baseFile);
+ continue;
+ } // END - if
+
+ // Construct include filename and FQFN
+ $fileName = $baseDir . $baseFile;
+ $FQFN = getPath() . $fileName;
+
+ // Remove double slashes
+ $FQFN = str_replace('//', '/', $FQFN);
+
+ // Check if the base filenname 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: */ debugOutput('baseDir:' . $baseDir);
+ //* DEBUG: */ debugOutput('baseFile:' . $baseFile);
+ //* DEBUG: */ debugOutput('FQFN:' . $FQFN);
+
+ // 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 ((!empty($suffix)) && (substr($baseFile, -(strlen($suffix . $extension)), (strlen($suffix . $extension))) != $suffix . $extension)) {
+ // Skip wrong suffix as well
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Invalid suffix in file ' . $baseFile . ', suffix=' . $suffix);
+ 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(__FUNCTION__, __LINE__, '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
+ sort($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()) && (isDisplayDebugSqlEnabled()));
+ } // 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(__FUNCTION__, __LINE__, '(<font color="#0000aa">' . __LINE__ . '</font>): {--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;
+}
+
+// Convert ';' to ', ' for e.g. receiver list
+function convertReceivers ($old) {
+ return str_replace(';', ', ', $old);
+}
+
+// Get a module from filename and access level
+function getModuleFromFileName ($file, $accessLevel) {
+ // Default is 'invalid';
+ $modCheck = 'invalid';
+
+ // @TODO This is still very static, rewrite it somehow
+ switch ($accessLevel) {
+ case 'admin':
+ $modCheck = 'admin';
+ break;
+
+ case 'sponsor':
+ case 'guest':
+ case 'member':
+ $modCheck = getModule();
+ break;
+
+ default: // Unsupported file name / access level
+ debug_report_bug(__FUNCTION__, __LINE__, 'Unsupported file name=' . basename($file) . '/access level=' . $accessLevel);
+ break;
+ }
+
+ // Return result
+ return $modCheck;
+}
+
+// Encodes an URL for adding session id, etc.
+function encodeUrl ($url, $outputMode = '0') {
+ // Do we have already have a PHPSESSID inside or view.php is called? Then abort here
+ if ((strpos($url, session_name()) !== false) || (getScriptOutputMode() == -3)) return $url;
+
+ // Do we have a valid session?
+ if (((!isset($GLOBALS['valid_session'])) || ($GLOBALS['valid_session'] === false) || (!isset($_COOKIE[session_name()]))) && (isSpider() === false)) {
+ // Invalid session
+ // Determine right seperator
+ $seperator = '&';
+ if (strpos($url, '?') === false) {
+ // No question mark
+ $seperator = '?';
+ } elseif ((getScriptOutputMode() != '0') || ($outputMode != '0')) {
+ // Non-HTML mode
+ $seperator = '&';
+ }
+
+ // Add it to URL
+ if (session_id() != '') {
+ $url .= $seperator . session_name() . '=' . session_id();
+ } // END - if
+ } // END - if
+
+ // Add {?URL?} ?
+ if ((substr($url, 0, strlen(getUrl())) != getUrl()) && (substr($url, 0, 7) != '{?URL?}') && (substr($url, 0, 7) != 'http://') && (substr($url, 0, 8) != 'https://')) {
+ // Add it
+ $url = '{?URL?}/' . $url;
+ } // END - if
+
+ // Return the URL
+ return $url;
+}
+
+// Simple check for spider
+function isSpider () {
+ // Get the UA
+ $userAgent = strtolower(detectUserAgent(true));
+
+ // It should not be empty, if so it is better a spider/bot
+ if (empty($userAgent)) return true;
+
+ // Is it a spider?
+ return ((strpos($userAgent, 'spider') !== false) || (strpos($userAgent, 'slurp') !== false) || (strpos($userAgent, 'bot') !== false) || (strpos($userAgent, 'archiver') !== false));
+}
+
+// Function to search for the last modified file
+function searchDirsRecursive ($dir, &$last_changed, $lookFor = 'Date') {
+ // Get dir as array
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'dir=' . $dir);
+ // Does it match what we are looking for? (We skip a lot files already!)
+ // RegexPattern to exclude ., .., .revision, .svn, debug.log or .cache in the filenames
+ $excludePattern = '@(\.revision|\.svn|debug\.log|\.cache|config\.php)$@';
+
+ $ds = getArrayFromDirectory($dir, '', false, true, array(), '.php', $excludePattern);
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count(ds)='.count($ds));
+
+ // Walk through all entries
+ foreach ($ds as $d) {
+ // Generate proper FQFN
+ $FQFN = str_replace('//', '/', getPath() . $dir . '/' . $d);
+
+ // Is it a file and readable?
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'dir=' . $dir . ',d=' . $d);
+ if (isFileReadable($FQFN)) {
+ // $FQFN is a readable file so extract the requested data from it
+ $check = extractRevisionInfoFromFile($FQFN, $lookFor);
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'File: ' . $d . ' found. check=' . $check);
+
+ // Is the file more recent?
+ if ((!isset($last_changed[$lookFor])) || ($last_changed[$lookFor] < $check)) {
+ // This file is newer as the file before
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'NEWER!');
+ $last_changed['path_name'] = $FQFN;
+ $last_changed[$lookFor] = $check;
+ } // END - if
+ } else {
+ // Not readable
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'File: ' . $d . ' not readable or directory.');
+ }
+ } // END - foreach