Missing function CACHE_PURGE_ADMIN_MENU() added (stub)
[mailer.git] / inc / functions.php
index cc64df1b28519495857eed577b0e920cf0128865..533e824c60ed1327130ef6161141f9dcc89eaf06 100644 (file)
@@ -32,7 +32,7 @@
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
+if (!defined('__SECURITY')) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
        require($INC);
 }
@@ -207,17 +207,16 @@ function LOAD_TEMPLATE($template, $return=false, $content="") {
        $_CONFIG['num_templates']++;
 
        // Init some data
-       $ACTION = SQL_ESCAPE($GLOBALS['action']);
-       $WHAT = SQL_ESCAPE($GLOBALS['what']);
        $ret = "";
        if (empty($GLOBALS['refid'])) $GLOBALS['refid'] = 0;
-       $REFID = $GLOBALS['refid'];
 
-       // DEPRECATED!!!
+       // @DEPRECATED Try to remove this if() block
        if ($template == "member_support_form") {
                // Support request of a member
                $result = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
                 array($GLOBALS['userid']), __FILE__, __LINE__);
+
+               // @TODO Merge this data into $content
                list($gender, $surname, $family, $email) = SQL_FETCHROW($result);
 
                // Translate gender
@@ -899,7 +898,7 @@ function LOAD_URL($URL, $addUrlData=true) {
                print("<pre>");
                debug_print_backtrace();
                die("</pre>URL={$URL}");
-               */
+               /**/
                @header ("Location: ".str_replace("&amp;", "&", $URL));
        } else {
                // Output error message
@@ -1940,11 +1939,11 @@ function generateHash ($plainText, $salt = "") {
 
                // Generate SHA1 sum from modula of number and the prime number
                $sha1 = sha1(($a % _PRIME).$server.":".$keys.":".$data.":".date("d-m-Y (l-F-T)", time()).":".$a);
-               //* DEBUG: */ echo "SHA1=".$sha1." (".strlen($sha1).")<br>";
+               //* DEBUG: */ echo "SHA1=".$sha1." (".strlen($sha1).")<br />";
                $sha1 = scrambleString($sha1);
-               //* DEBUG: */ echo "Scrambled=".$sha1." (".strlen($sha1).")<br>";
+               //* DEBUG: */ echo "Scrambled=".$sha1." (".strlen($sha1).")<br />";
                //* DEBUG: */ $sha1b = descrambleString($sha1);
-               //* DEBUG: */ echo "Descrambled=".$sha1b." (".strlen($sha1b).")<br>";
+               //* DEBUG: */ echo "Descrambled=".$sha1b." (".strlen($sha1b).")<br />";
 
                // Generate the password salt string
                $salt = substr($sha1, 0, $_CONFIG['salt_length']);
@@ -2097,12 +2096,12 @@ function generatePassString($passHash) {
                        }
                        $mod = substr(round($mod), 0, 4);
                        $mod = str_repeat('0', 4-strlen($mod)).$mod;
-                       //* DEBUG: */ echo "*".$start."=".$mod."*<br>";
+                       //* DEBUG: */ echo "*".$start."=".$mod."*<br />";
                        $start += 4;
                        $newHash .= $mod;
                } // END - for
 
-               //* DEBUG: */ print($passHash."<br>".$newHash." (".strlen($newHash).")");
+               //* DEBUG: */ print($passHash."<br />".$newHash." (".strlen($newHash).")");
                $ret = generateHash($newHash, $_CONFIG['master_salt']);
                //* DEBUG: */ print($ret."<br />\n");
        } else {
@@ -2196,39 +2195,62 @@ function set_session ($var, $value) {
                return session_register($var);
        } elseif (!empty($value)) {
                // Update session
+               //* DEBUG: */ echo "UPDATE:".$var."=".$value."<br />\n";
                $_SESSION[$var] = $value;
-       } else {
-               // Something bad happens!
-               return false; // Hope this doesn't make so much trouble???
+               return true;
        }
 
-       // Return always true if the session variable is already set.
-       // Keept me busy for a longer while...
+       // Ignored (but valid)
        //* DEBUG: */ echo "IGNORED:".$var."=".$value."<br />\n";
        return true;
 }
 
 // Check wether a boolean constant is set
 // Taken from user comments in PHP documentation for function constant()
-function isBooleanConstantAndTrue($constname) { // : Boolean
+function isBooleanConstantAndTrue($constName) { // : Boolean
+       global $constCache;
+
+       // Failed by default
        $res = false;
-       if (defined($constname)) $res = (constant($constname) === true);
-       return($res);
+
+       // In cache?
+       if (isset($constCache[$constName])) {
+               // Use cache
+               $res = $constCache[$constName];
+       } else {
+               // Check constant
+               if (defined($constName)) $res = (constant($constName) === true);
+
+               // Set cache
+               $constCache[$constName] = $res;
+       }
+
+       // Return value
+       return $res;
 }
 
 // Check wether a session variable is set
 function isSessionVariableSet($var) {
+       //* DEBUG: */ echo __FUNCTION__.":var={$var}<br />\n";
        return (isset($_SESSION[$var]));
 }
 // Returns wether the value of the session variable or NULL if not set
 function get_session($var) {
+       global $cacheArray;
+
        // Default is not found! ;-)
        $value = null;
 
-       // Is the variable there?
-       if (isSessionVariableSet($var)) {
+       // Is the variable there or cached values?
+       if (isset($cacheArray['session'][$var])) {
+               // Get cached value (skips a lot SQL_ESCAPE() calles!
+               $value = $cacheArray['session'][$var];
+       } elseif (isSessionVariableSet($var)) {
                // Then  get it secured!
                $value = SQL_ESCAPE($_SESSION[$var]);
+
+               // Cache the value
+               $cacheArray['session'][$var] = $value;
        } // END - if
 
        // Return the value
@@ -2290,6 +2312,7 @@ function GET_DIR_AS_ARRAY ($baseDir, $prefix) {
                $file = $baseDir.$baseFile;
 
                // Is this a valid reset file?
+               //* DEBUG: */ echo __FUNCTION__.":baseDir={$baseDir},prefix={$prefix},baseFile={$baseFile}<br />\n";
                if ((is_file($file)) && (is_readable($file)) && (substr($baseFile, 0, strlen($prefix)) == $prefix) && (substr($baseFile, -4, 4) == ".php")) {
                        // Remove both for extension name
                        $extName = substr($baseFile, strlen($prefix), -4);
@@ -2301,6 +2324,9 @@ function GET_DIR_AS_ARRAY ($baseDir, $prefix) {
                        if (($extId > 0) && (EXT_IS_ACTIVE($extName))) {
                                // Then add this file
                                $INCs[] = $file;
+                       } elseif ($extId == 0) {
+                               // Add non-extension files as well
+                               $INCs[] = $file;
                        }
                } // END - if
        } // END - while
@@ -2442,6 +2468,73 @@ function REVERT_COMMA ($str) {
        // Return float
        return $float;
 }
+// Handle menu-depending failed logins and return the rendered content
+function HANDLE_LOGIN_FAILTURES ($accessLevel) {
+       // Default output is empty ;-)
+       $OUT = "";
+
+       // Is the session data set?
+       if ((isSessionVariableSet('mxchange_'.$accessLevel.'_failtures')) && (isSessionVariableSet('mxchange_'.$accessLevel.'_last_fail'))) {
+               // Ignore zero values
+               if (get_session('mxchange_'.$accessLevel.'_failtures') > 0) {
+                       // Non-guest has login failtures found, get both data and prepare it for template
+                       //* DEBUG: */ echo __FUNCTION__.":accessLevel={$accessLevel}<br />\n";
+                       $content = array(
+                               'login_failtures' => get_session('mxchange_'.$accessLevel.'_failtures'),
+                               'last_failture'   => MAKE_DATETIME(get_session('mxchange_'.$accessLevel.'_last_fail'), "2")
+                       );
+
+                       // Load template
+                       $OUT = LOAD_TEMPLATE("login_failtures", true, $content);
+               } // END - if
+
+               // Reset session data
+               set_session('mxchange_'.$accessLevel.'_failtures', "");
+               set_session('mxchange_'.$accessLevel.'_last_fail', "");
+       } // END - if
+
+       // Return rendered content
+       return $OUT;
+}
+// Rebuild cache
+function REBUILD_CACHE ($cache, $inc="") {
+       global $cacheInstance;
+
+       // Shall I remove the cache file?
+       if ((EXT_IS_ACTIVE("cache")) && (is_object($cacheInstance))) {
+               // Rebuild cache
+               if ($cacheInstance->cache_file($cache, true)) {
+                       // Destroy it
+                       $cacheInstance->cache_destroy();
+
+                       // Include file given?
+                       if (!empty($inc)) {
+                               // And rebuild it from scratch
+                               require_once(PATH."inc/loader/load_cache-".$inc.".php");
+                       } // END - if
+               } // END - if
+       } // END - if
+}
+// Purge admin menu cache
+function CACHE_PURGE_ADMIN_MENU ($id=0, $action="", $what="", $str="") {
+       global $_CONFIG, $cacheInstance;
+
+       // Is the cache extension enabled or no cache instance or admin menu cache disabled?
+       if (!EXT_IS_ACTIVE("cache")) {
+               // Cache extension not active
+               return false;
+       } elseif (!is_object($cacheInstance)) {
+               // No cache instance!
+               DEBUG_LOG(__FUNCTION__.": No cache instance found.");
+               return false;
+       } elseif ((!isset($_CONFIG['cache_admin_menu'])) || ($_CONFIG['cache_admin_menu'] == "N")) {
+               // Caching disabled (currently experiemental!)
+               return false;
+       }
+
+       // Experiemental feature!
+       trigger_error("You have to delete the admin_*.cache files by yourself at this point.");
+}
 //
 //////////////////////////////////////////////////
 //                                              //