More rewrites to make use of (cached) wrapper functions
[mailer.git] / inc / filter-functions.php
index 38a1262175cebe9c8064ddffc2b91d31cce25815..d0e881ae8cd698d221e5faf3abc38f18174a3996 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Funktionen fuer Filter-System                    *
  * -------------------------------------------------------------------- *
- * $Revision:: 1185                                                   $ *
- * $Date:: 2009-10-11 04:16:39 +0200 (Sun, 11 Oct 2009)               $ *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author:: quix0r                                                   $ *
+ * $Author::                                                          $ *
  * Needs to be in all Files and every File needs "svn propset           *
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -46,40 +47,22 @@ function initFilterSystem () {
        // Is the filter already initialized?
        if (isset($GLOBALS['filter_init'])) {
                // Then abort here
-               debug_report_bug('Filter system already initialized.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
        } // END - if
 
        // Load all saved filers if sql_patches is updated
-       if (isset($GLOBALS['cache_array']['filter']['filter_name'])) {
-               // Init dummy array
-               $filterArray = array(
-                       'chains'  => array(),
-                       'loaded'  => array(),
-                       'counter' => array()
-               );
-
-               // Found in cache so rewrite the array
-               foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
-                       // Get filter function
-                       $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
-
-                       // Add the element with mapped index
-                       $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
-                       $filterArray['loaded'][$filterName][$filterFunction]  = true;
-                       $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
-               } // END - foreach
+       if ((isset($GLOBALS['cache_array']['filter']['filter_name'])) && (!isset($GLOBALS['cache_array']['filter']['chains']))) {
+               // Prepare filter array
+               prepareFilterArray();
 
                // Mark it as initialized
                $GLOBALS['filter_init'] = true;
-
-               // Remove the cache
-               $GLOBALS['cache_array']['filter'] = $filterArray;
        } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
                // Init add
                $add = '';
-               if (getExtensionVersion('sql_patches') >= '0.6.0') $add = ", `filter_counter`";
+               if (isExtensionINstalledAndNewer('sql_patches', '0.6.0')) $add = ", `filter_counter`";
 
-               // Load all active filers
+               // Load all filters
                $result = SQL_QUERY("SELECT
        `filter_name`,`filter_function`,`filter_active`".$add."
 FROM
@@ -88,7 +71,7 @@ ORDER BY
        `filter_id` ASC", __FUNCTION__, __LINE__);
 
                // Are there entries?
-               if (SQL_NUMROWS($result) > 0) {
+               if (!SQL_HASZERONUMS($result)) {
                        // Load all filters
                        while ($filterArray = SQL_FETCHARRAY($result)) {
                                // Get filter name and function
@@ -118,9 +101,9 @@ ORDER BY
 
        // Init filters
        registerFilter('init', 'LOAD_CONFIGURATION');
-       registerFilter('init', 'INIT_EXTENSIONS');
        registerFilter('init', 'INIT_RANDOMIZER');
        registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
+       registerFilter('init', 'INIT_EXTENSIONS');
        registerFilter('init', 'INIT_RANDOM_NUMBER');
        registerFilter('init', 'CHECK_SVN_REVISION');
        registerFilter('init', 'RUN_DAILY_RESET');
@@ -131,7 +114,15 @@ ORDER BY
        registerFilter('init', 'UPDATE_LOGIN_DATA');
        registerFilter('init', 'ACTIVATE_EXCHANGE');
 
-       // Login failures handler
+       // Page headers - pre-filter (normally, you want to register here)
+       registerFilter('pre_page_header', 'LOAD_PAGE_HEADER');
+
+       // Page headers - post-filter (normally, you don't want to register here)
+       // ------------------- LAST FILTER FOR THIS CHAIN! ------------------------
+       registerFilter('post_page_header', 'FINISH_PAGE_HEADER');
+       // ------------------- LAST FILTER FOR THIS CHAIN! ------------------------
+
+       // 'You are here' navigation - post filter
        registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
 
        // Filters for pre-extension-registration
@@ -163,7 +154,7 @@ ORDER BY
 
        // Compiling code
        registerFilter('compile_code', 'COMPILE_CONFIG');
-       registerFilter('compile_code', 'COMPILE_EXTENSION');
+       registerFilter('compile_code', 'COMPILE_EXPRESSION_CODE');
 
        // Generic extension update filters
        registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
@@ -184,19 +175,27 @@ ORDER BY
        registerFilter('page_footer', 'HANDLE_FATAL_ERRORS');
        registerFilter('page_footer', 'DISPLAY_COPYRIGHT');
        registerFilter('page_footer', 'DISPLAY_PARSING_TIME');
+
+       // Member login check. Always keep FETCH_USER_DATA as first entry!
+       registerFilter('member_login_check', 'FETCH_USER_DATA');
+
+       // Admin login
+       registerFilter('do_admin_login_done', 'DO_LOGIN_ADMIN');
 }
 
 // "Registers" a new filter function
 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - ENTERED!');
+
        // Extend the filter function name
        $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
 
        // Is that filter already there?
        if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
-               // Then abort here
-               if ($silentAbort === false) {
+               // In installation phase we always want to abort
+               if (($silentAbort === false) || (isInstallationPhase())) {
                        // Add fatal message
-                       addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
+                       debug_report_bug(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
                } // END - if
 
                // Abort here
@@ -213,14 +212,18 @@ function registerFilter ($filterName, $filterFunction, $silentAbort = true, $for
                } // END - if
 
                // Simply add it to the array
-               //* DEBUG: */ print __FUNCTION__.': filterName='.$filterName.',filterFunction='.$filterFunction.'<br />';
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
                $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
                $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
        } // END - if
+
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - EXIT!');
 }
 
 // "Unregisters" a filter from the given chain
-function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_run = false) {
+function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = false, $dry_run = false) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry=' . intval($dry_run) . ' - ENTERED!');
+
        // Extend the filter function name only if not loaded from database
        if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
                $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
@@ -229,27 +232,31 @@ function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_ru
        // Is that filter there?
        if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
                // Not found, so abort here
-               addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_NOT_REMOVED'), array($filterFunction, $filterName));
+               addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_NOT_REMOVED'), $filterFunction, $filterName));
                return false;
        } // END - if
 
        // Shall we remove? (default, not while just showing an extension removal)
        if ($dry_run === false) {
                // Mark for filter removal
-               //* DEBUG: */ print __FUNCTION__.': filterName='.$filterName.',filterFunction='.$filterFunction.'<br />';
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REMOVE: F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction);
                $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
        } // END - if
+
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry=' . intval($dry_run) . ' - EXIT!');
 }
 
 // "Runs" the given filters, data is optional and can be any type of data
 function runFilterChain ($filterName, $data = null) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',data[]=' . gettype($data) . ' - ENTERED!');
+
        // Is that filter chain there?
        if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
-               // We should find all these non-existing filter chains
-               //* Only for tracking: */ if ($filterName != 'sql_admin_extra_data') {
-               //* Only for tracking: */ debug_report_bug('Filter chain <strong>' . $filterName . '</strong> not found!');
-               //* Only for tracking: */ }
-               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!');
+               // Log not found filters in debug-mode
+               if (isDebugModeEnabled()) {
+                       // Log it away...
+                       logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!');
+               } // END - if
 
                // Abort here and return content
                return $data;
@@ -261,32 +268,33 @@ function runFilterChain ($filterName, $data = null) {
        // Then run all filters
        foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
                // Debug message
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Running: name={$filterName},func={$filterFunction},active={$active}");
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
 
-               // Is the filter active?
+               // Is the filter active or newly added??
                if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown','extension_remove','post_extension_run_sql'))) && ($active == 'R'))) {
                        // Is this filter there?
                        if (!function_exists($filterFunction)) {
                                // Unregister it
-                               unregisterFilter($filterName, $filterFunction);
+                               unregisterFilter(__FUNCTION__, __LINE__, $filterName, $filterFunction);
 
                                // Skip this entry
                                continue;
                        } // END - if
 
                        // Call the filter chain
-                       //* DEBUG: */ print $filterName.'/'.$filterFunction.',[]='.gettype($returnValue).'<br />';
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue));
                        $returnValue = call_user_func_array($filterFunction, array($returnValue));
 
                        // Update usage counter
                        countFilterUsage($filterName, $filterFunction);
                } elseif (isDebugModeEnabled()) {
                        // Debug message
-                       logDebugMessage(__FUNCTION__, __LINE__, "Skipped: name={$filterName},func={$filterFunction},active={$active}");
+                       logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
                }
        } // END - foreach
 
        // Return the filtered content
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',data[]=' . gettype($data) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
        return $returnValue;
 }
 
@@ -302,5 +310,35 @@ function countFilterUsage ($filterName, $filterFunction) {
        }
 }
 
+// Prepares the filter array for usage
+function prepareFilterArray () {
+       // Abort here if array is absend (e.g. not cached)
+       if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
+               // Abort silently
+               return false;
+       } // END - if
+
+       // Init dummy array
+       $filterArray = array(
+               'chains'  => array(),
+               'loaded'  => array(),
+               'counter' => array()
+       );
+
+       // Found in cache so rewrite the array
+       foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
+               // Get filter function
+               $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
+
+               // Add the element with mapped index
+               $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
+               $filterArray['loaded'][$filterName][$filterFunction]  = true;
+               $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
+       } // END - foreach
+
+       // Remove the cache
+       $GLOBALS['cache_array']['filter'] = $filterArray;
+}
+
 // [EOF]
 ?>