Function INIT_FILTER_SYSTEM() is now being executed even when there are no extensions...
[mailer.git] / inc / filters.php
index 939a15c6b6384892d57944f32280bb7d15e1f24d..0a30fc752e45697043683f813ceaea382cbfa2a6 100644 (file)
@@ -38,8 +38,8 @@ if (!defined('__SECURITY')) {
 }
 
 // Init "generic filter system"
-function INIT_FILTER_SYSTEM() {
-       global $filters, $loadedFilters;
+function INIT_FILTER_SYSTEM () {
+       global $filters, $loadedFilters, $counter;
 
        // Is the filter already initialized?
        if ((isset($filters)) && (is_array($filters))) {
@@ -58,13 +58,18 @@ function INIT_FILTER_SYSTEM() {
                'shutdown'  => array()
        );
 
-       // Init loaded filters
+       // Init loaded filters and counter
        $loadedFilters =  array();
+       $counter = array();
 
        // Load all saved filers if sql_patches is updated
        if (GET_EXT_VERSION("sql_patches") >= "0.5.9") {
+               // Init add
+               $ADD = "";
+               if (GET_EXT_VERSION("sql_patches") >= "0.6.0") $ADD = ", `filter_counter`";
+
                // Load all active filers
-               $result = SQL_QUERY("SELECT `filter_name`, `filter_function`, `filter_active`
+               $result = SQL_QUERY("SELECT `filter_name`, `filter_function`, `filter_active`".$ADD."
 FROM `"._MYSQL_PREFIX."_filters`
 ORDER BY `filter_id` ASC", __FILE__, __LINE__);
 
@@ -72,11 +77,24 @@ ORDER BY `filter_id` ASC", __FILE__, __LINE__);
                if (SQL_NUMROWS($result) > 0) {
                        // Load all filters
                        while ($filterArray = SQL_FETCHARRAY($result)) {
+                               // Get filter name and function
+                               $filterName     = $filterArray['filter_name'];
+                               $filterFunction = $filterArray['filter_function'];
+
+                               // Set counter to default
+                               $counter[$filterName][$filterFunction] = 0;
+
                                // Mark this filter as loaded (from database)
-                               $loadedFilters[$filterArray['filter_name']][$filterArray['filter_function']] = true;
+                               $loadedFilters[$filterName][$filterFunction] = true;
 
                                // Set this filter
-                               $filters[$filterArray['filter_name']][$filterArray['filter_function']] = $filterArray['filter_active'];
+                               $filters[$filterName][$filterFunction] = $filterArray['filter_active'];
+
+                               // Is the array element for counter there?
+                               if (isset($filterArray['filter_counter'])) {
+                                       // Then use this value!
+                                       $counter[$filterName][$filterFunction] = $filterArray['filter_counter'];
+                               } // END - if
                        } // END - while
                } // END - if
        
@@ -84,16 +102,39 @@ ORDER BY `filter_id` ASC", __FILE__, __LINE__);
                SQL_FREERESULT($result);
        } // END - if
 
-       // @TODO Find some more init/shutdown filter functions
+       // Init filters
+       REGISTER_FILTER('init', 'UPDATE_LOGIN_DATA');
+
+       // Login failtures handler
+       REGISTER_FILTER('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
+
+       // Filters for pre-extension-registration
+       REGISTER_FILTER('pre_extension_installed', 'RUN_SQLS');
+
+       // Filters for post-extension-registration
+       REGISTER_FILTER('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
+       REGISTER_FILTER('post_extension_installed', 'SOLVE_TASK');
+       REGISTER_FILTER('post_extension_installed', 'LOAD_INCLUDES');
+
+       // Solving tasks
+       REGISTER_FILTER('solve_task', 'SOLVE_TASK');
+
+       // Loading includes in general
+       REGISTER_FILTER('load_includes', 'LOAD_INCLUDES');
+
+       // Run SQLs
+       REGISTER_FILTER('run_sqls', 'RUN_SQLS');
+
+       // Admin ACL check
+       REGISTER_FILTER('check_admin_acl', 'CHECK_ADMIN_ACL');
 
        // Register shutdown filters
        REGISTER_FILTER('shutdown', 'FLUSH_FILTERS');
-       REGISTER_FILTER('shutdown', 'SHUTDOWN_DATABASE');
 }
 
 // "Registers" a new filter function
-function REGISTER_FILTER ($filterName, $filterFunction, $silentAbort = true, $force = false) {
-       global $filters;
+function REGISTER_FILTER ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
+       global $filters, $counter;
 
        // Extend the filter function name
        $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
@@ -116,16 +157,22 @@ function REGISTER_FILTER ($filterName, $filterFunction, $silentAbort = true, $fo
                return false;
        } // END - if
 
-       // Simply add it to the array
-       $filters[$filterName][$filterFunction] = "Y";
+       // Shall we add it?
+       if (!$dry_run) {
+               // Simply add it to the array
+               $filters[$filterName][$filterFunction] = "Y";
+               $counter[$filterName][$filterFunction] = 0;
+       } // END - if
 }
 
 // "Unregisters" a filter from the given chain
-function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $remove = true) {
-       global $filters;
+function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $dry_run = false) {
+       global $filters, $counter, $loadedFilters;
 
-       // Extend the filter function name
-       $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
+       // Extend the filter function name only if not loaded from database
+       if (!isset($loadedFilters[$filterName][$filterFunction])) {
+               $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
+       } // END - if
 
        // Is that filter there?
        if ((!isset($filters[$filterName][$filterFunction])) && (!$force)) {
@@ -135,15 +182,16 @@ function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $remov
        } // END - if
 
        // Shall we remove? (default, not while just showing an extension removal)
-       if ($remove) {
+       if (!$dry_run) {
                // Mark for filter removal
                $filters[$filterName][$filterFunction] = "R";
+               unset($counter[$filterName][$filterFunction]);
        } // END  - if
 }
 
 // "Runs" the given filters, data is optional and can be any type of data
 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
-       global $filters;
+       global $filters, $counter;
 
        // Is that filter chain there?
        if (!isset($filters[$filterName])) {
@@ -163,12 +211,24 @@ function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
        // Then run all filters
        foreach ($filters[$filterName] as $filterFunction=>$active) {
                // Debug message
-               /* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
+               //* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName},func={$filterFunction},active={$active}<br />\n";
 
                // Is the filter active?
                if ($active == "Y") {
+                       // Is this filter there?
+                       if (!function_exists($filterFunction)) {
+                               // Unregister it
+                               UNREGISTER_FILTER($filterName, $filterFunction);
+
+                               // Skip this entry
+                               continue;
+                       } // END - if
+
                        // Call the filter chain
                        $returnValue = call_user_func_array($filterFunction, array($returnValue));
+
+                       // Update usage counter
+                       $counter[$filterName][$filterFunction]++;
                } // END - if
        } // END - foreach
 
@@ -182,7 +242,10 @@ function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
 
 // Filter for flushing all new filters to the database
 function FILTER_FLUSH_FILTERS () {
-       global $filters, $link, $loadedFilters;
+       global $filters, $counter, $link, $loadedFilters, $SQLs;
+
+       // Clear all previous SQL queries
+       $SQLs = array();
 
        // Is a database link here and not in installation mode?
        if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
@@ -227,7 +290,7 @@ function FILTER_FLUSH_FILTERS () {
                $insertSQL = substr($insertSQL, 0, -1);
 
                // And run it
-               SQL_QUERY($insertSQL, __FILE__, __LINE__);
+               $SQLs[] = $insertSQL;
        } // END - if
 
        // Something has been removed?
@@ -236,22 +299,214 @@ function FILTER_FLUSH_FILTERS () {
                $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
 
                // And run it
-               SQL_QUERY($removeSQL, __FILE__, __LINE__);
+               $SQLs[] = $removeSQL;
+       } // END - if
+
+       // Shall we update usage counters (ONLY FOR DEBUGGING!)
+       if (getConfig('update_filter_usage') == "Y") {
+               // Update all counters
+               foreach ($counter as $filterName => $filterArray) {
+                       // Walk through all filters
+                       foreach ($filterArray as $filterFunction => $cnt) {
+                               // Construct and add the query
+                               $SQLs[] = sprintf("UPDATE `"._MYSQL_PREFIX."_filters` SET `filter_counter`=%s WHERE `filter_name`='%s' AND `filter_function`='%s' LIMIT 1",
+                                       bigintval($cnt),
+                                       $filterName,
+                                       $filterFunction
+                               );
+                       } // END - foreach
+               } // END - foreach
+       } // END - if
+
+       // Run the run_sqls filter in non-dry mode
+       RUN_FILTER('run_sqls', array('dry_run' => false, 'sqls' => $SQLs));
+}
+
+// Filter for calling the handler for login failtures
+function FILTER_CALL_HANDLER_LOGIN_FAILTURES ($data) {
+       // Init content
+       $content = $data;
+
+       // Handle failed logins here if not in guest
+       //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):type={$data['type']},action={$GLOBALS['action']},what={$GLOBALS['what']},lvl={$data['access_level']}<br />\n";
+       if ((($data['type'] == "what") || ($data['type'] == "action") && ((!isset($GLOBALS['what'])) || ($GLOBALS['what'] == "overview") || ($GLOBALS['what'] == getConfig('index_home')))) && ($data['access_level'] != "guest") && ((GET_EXT_VERSION("sql_patches") >= "0.4.7") || (GET_EXT_VERSION("admins") >= "0.7.0"))) {
+               // Handle failture
+               $content['content'] .= HANDLE_LOGIN_FAILTURES($data['access_level']);
+       } // END - if
+
+       // Return the content
+       return $content;
+}
+
+// Filter for redirecting to logout if sql_patches has been installed
+function FILTER_REDIRECT_TO_LOGOUT_SQL_PATCHES () {
+       // Remove this filter
+       UNREGISTER_FILTER('shutdown', __FUNCTION__);
+
+       // Is the element set?
+       if (isset($GLOBALS['ext_load_mode'])) {
+               // Redirect here
+               LOAD_URL("modules.php?module=admin&logout=1&".$GLOBALS['ext_load_mode']."=sql_patches");
+       } // END - if
+
+       // This should not happen!
+       DEBUG_LOG(__FUNCTION__, __LINE__, "Cannot auto-logout because no extension load-mode has been set.");
+}
+
+// Filter for auto-activation of a extension
+function FILTER_AUTO_ACTIVATE_EXTENSION ($data) {
+       global $EXT_ALWAYS_ACTIVE;
+
+       // Is this extension always activated?
+       if ($EXT_ALWAYS_ACTIVE == "Y") {
+               // Then activate the extension
+               //* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ext_name={$data['ext_name']}<br />\n";
+               ACTIVATE_EXTENSION($data['ext_name']);
+       } // END - if
+
+       // Return the data
+       return $data;
+}
+
+// Filter for solving task given task
+function FILTER_SOLVE_TASK ($data) {
+       // Don't solve anything if no admin!
+       if (!IS_ADMIN()) return $data;
+
+       // Is this a direct task id or array element task_id is found?
+       if (is_int($data)) {
+               // Then solve it...
+               ADMIN_SOLVE_TASK($data);
+       } elseif ((is_array($data)) && (isset($data['task_id']))) {
+               // Solve it...
+               ADMIN_SOLVE_TASK($data['task_id']);
+       }
+
+       // Return the data
+       return $data;
+}
+
+// Filter to load include files
+function FILTER_LOAD_INCLUDES ($data) {
+       global $CSS;
+
+       // Default is $data as inclusion list
+       $INC_POOL = $data;
+
+       // Is it an array?
+       if ((!isset($data)) || (!is_array($data))) {
+               // Then abort here
+               DEBUG_LOG(__FILE__, __LINE__, "INC_POOL is no array!");
+               return $data;
+       } elseif (isset($data['inc_pool'])) {
+               // Use this as new inclusion pool!
+               $INC_POOL = $data['inc_pool'];
+       }
+
+       // Check for added include files
+       if (count($INC_POOL) > 0) {
+               // Loads every include file
+               foreach ($INC_POOL as $FQFN) {
+                       require_once($FQFN);
+               } // END - foreach
+
+               // Reset array
+               if (isset($data['inc_pool'])) $data['inc_pool'] = array();
        } // END - if
+
+       // Continue with processing
+       return $data;
 }
 
-// Filter for shutting down the database link
-function FILTER_SHUTDOWN_DATABASE () {
-       global $link;
+// Filter for running SQL commands
+function FILTER_RUN_SQLS ($data) {
+       // Is the array there?
+       if ((isset($data['sqls'])) && ((!isset($data['dry_run'])) || ($data['dry_run'] == false))) {
+               // Run SQL commands
+               foreach ($data['sqls'] as $sql) {
+                       $sql = trim($sql);
+                       if (!empty($sql)) {
+                               // Do we have an "ALTER TABLE" command?
+                               if (substr(strtolower($sql), 0, 11) == "alter table") {
+                                       // Analyse the alteration command
+                                       SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
+                               } else {
+                                       // Run regular SQL command
+                                       $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
+                               }
+                       } // END - if
+               } // END - foreach
+       } // END - if
+}
+
+// Filter for updating/validating login data
+function FILTER_UPDATE_LOGIN_DATA () {
+       global $LAST;
+       if (!is_array($LAST)) $LAST = array();
+
+       // Recheck if logged in
+       if (!IS_MEMBER()) return false;
+
+       // Secure user ID
+       $GLOBALS['userid'] = bigintval(get_session('userid'));
+
+       // Extract last online time (life) and how long is auto-login valid (time)
+       $newl = time() + bigintval(get_session('lifetime'));
 
-       if (is_resource($link)) {
-               // Close link
-               SQL_CLOSE($link, __FILE__, __LINE__);
-       } else {
-               // No database link
-               ADD_FATAL(NO_DB_LINK);
+       // Load last module and last online time
+       $result = SQL_QUERY_ESC("SELECT last_module, last_online FROM `"._MYSQL_PREFIX."_user_data` WHERE userid=%s LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
+       if (SQL_NUMROWS($result) == 1) {
+               // Load last module and online time
+               list($mod, $onl) = SQL_FETCHROW($result);
+               SQL_FREERESULT($result);
+
+               // Maybe first login time?
+               if (empty($mod)) $mod = "login";
+
+               if (set_session("userid", $GLOBALS['userid'], $newl, COOKIE_PATH) && set_session("u_hash", get_session('u_hash'), $newl, COOKIE_PATH) && set_session("lifetime", bigintval(get_session('lifetime')), $newl, COOKIE_PATH)) {
+                       // This will be displayed on welcome page! :-)
+                       if (empty($LAST['module'])) {
+                               $LAST['module'] = $mod; $LAST['online'] = $onl;
+                       } // END - if
+
+                       // "what" not set?
+                       if (empty($GLOBALS['what'])) {
+                               // Fix it to default
+                               $GLOBALS['what'] = "welcome";
+                               if (getConfig('index_home') != "") $GLOBALS['what'] = getConfig('index_home');
+                       } // END - if
+
+                       // Update last module / online time
+                       SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_user_data` SET last_module='%s', last_online=UNIX_TIMESTAMP(), REMOTE_ADDR='%s' WHERE userid=%s LIMIT 1",
+                               array($GLOBALS['what'], GET_REMOTE_ADDR(), $GLOBALS['userid']), __FILE__, __LINE__);
+               }
+       }  else {
+               // Destroy session, we cannot update!
+               destroy_user_session();
        }
 }
 
+// Filter for checking admin ACL
+function FILTER_CHECK_ADMIN_ACL () {
+       // Extension not installed so it's always allowed to access everywhere!
+       $ret = true;
+
+       // Ok, Cookie-Update done
+       if (GET_EXT_VERSION("admins") >= "0.3") {
+               // Check if action GET variable was set
+               $action = SQL_ESCAPE($GLOBALS['action']);
+               if (!empty($GLOBALS['what'])) {
+                       // Get action value by what-value
+                       $action = GET_ACTION("admin", $GLOBALS['what']);
+               } // END - if
+
+               // Check for access control line of current menu entry
+               $ret = ADMINS_CHECK_ACL($action, $GLOBALS['what']);
+       } // END - if
+
+       // Return result
+       return $ret;
+}
+
 //
 ?>