moved initialization of this filter to it's own extension mode-setup.php script
[mailer.git] / inc / filter-functions.php
index f10ce83848c266df4beb6698735c40d17d964e43..de21a6fd9c69a8fe4e1d0e050fe5ccc158ced7b0 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Funktionen fuer Filter-System                    *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -48,14 +43,14 @@ function initFilterSystem () {
                reportBug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
        } // END - if
 
-       // Load all saved filers if sql_patches is updated
+       // Load all saved filers if ext-sql_patches is updated
        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;
-       } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
+               $GLOBALS['filter_init'] = TRUE;
+       } elseif ((!isInstaller()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
                // Init add
                $add = '';
                if (isExtensionINstalledAndNewer('sql_patches', '0.6.0')) {
@@ -63,7 +58,7 @@ function initFilterSystem () {
                } // END - if
 
                // Load all filters
-               $result = SQL_QUERY('SELECT
+               $result = sqlQuery('SELECT
        `filter_name`,
        `filter_function`,
        `filter_active`
@@ -74,9 +69,9 @@ ORDER BY
        `filter_id` ASC', __FUNCTION__, __LINE__);
 
                // Are there entries?
-               if (!SQL_HASZERONUMS($result)) {
+               if (!ifSqlHasZeroNumRows($result)) {
                        // Load all filters
-                       while ($filterArray = SQL_FETCHARRAY($result)) {
+                       while ($filterArray = sqlFetchArray($result)) {
                                // Get filter name and function
                                $filterName     = $filterArray['filter_name'];
                                $filterFunction = $filterArray['filter_function'];
@@ -85,7 +80,7 @@ ORDER BY
                                $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
 
                                // Mark this filter as loaded (from database)
-                               $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = true;
+                               $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = TRUE;
 
                                // Set this filter
                                $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
@@ -99,26 +94,34 @@ ORDER BY
                } // END - if
 
                // Free result
-               SQL_FREERESULT($result);
+               sqlFreeResult($result);
        }
 
        // Init filters
        registerFilter(__FUNCTION__, __LINE__, 'init', 'LOAD_CONFIGURATION');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_RANDOMIZER');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'LOAD_RUNTIME_INCLUDES');
+       registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_LANGUAGE');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_EXTENSIONS');
+       registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_SESSION');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'SET_CURRENT_DATE');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_RANDOM_NUMBER');
-       registerFilter(__FUNCTION__, __LINE__, 'init', 'CHECK_REPOSITORY_REVISION');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_HOURLY_RESET');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_DAILY_RESET');
+       registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_WEEKLY_RESET');
+       registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_MONTHLY_RESET');
+       registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_YEARLY_RESET');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'TRIGGER_SENDING_POOL');
-       registerFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_USERNAME');
+       // @TODO Remove this forced removal after a year or so
+       unregisterFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_USERNAME', TRUE);
        registerFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_WHAT_ACTION');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'COUNT_MODULE');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'UPDATE_LOGIN_DATA');
        registerFilter(__FUNCTION__, __LINE__, 'init', 'ACTIVATE_EXCHANGE');
 
+       // Post-initialization
+       registerFilter(__FUNCTION__, __LINE__, 'post_init', 'DETERMINE_USERNAME');
+
        // Page headers - pre-filter (normally, you want to register here)
        registerFilter(__FUNCTION__, __LINE__, 'pre_page_header', 'LOAD_PAGE_HEADER');
 
@@ -169,15 +172,21 @@ ORDER BY
        // Do hourly reset stuff, keep this entry first in this chain:
        registerFilter(__FUNCTION__, __LINE__, 'hourly', 'RUN_HOURLY_INCLUDES');
 
-       // Do reset stuff, keep this entry first in this chain:
-       registerFilter(__FUNCTION__, __LINE__, 'reset', 'RUN_RESET_INCLUDES');
+       // Do daily stuff, keep this entry first in this chain:
+       registerFilter(__FUNCTION__, __LINE__, 'daily', 'RUN_DAILY_INCLUDES');
+
+       // Do weekly stuff, keep this entry first in this chain:
+       registerFilter(__FUNCTION__, __LINE__, 'weekly', 'RUN_WEEKLY_INCLUDES');
+
+       // Do monthly stuff, keep this entry first in this chain:
+       registerFilter(__FUNCTION__, __LINE__, 'monthly', 'RUN_MONTHLY_INCLUDES');
+
+       // Do yearly stuff, keep this entry first in this chain:
+       registerFilter(__FUNCTION__, __LINE__, 'yearly', 'RUN_YEARLY_INCLUDES');
 
        // Remove extension
        registerFilter(__FUNCTION__, __LINE__, 'extension_remove', 'REMOVE_EXTENSION');
 
-       // Exclude some users
-       registerFilter(__FUNCTION__, __LINE__, 'exclude_users', 'HTML_INCLUDE_USERS');
-
        // Handling of fatal errors
        registerFilter(__FUNCTION__, __LINE__, 'handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
 
@@ -194,49 +203,62 @@ ORDER BY
 
        // Admin mail links
        registerFilter(__FUNCTION__, __LINE__, 'generate_admin_mail_links', 'GENERATE_POOL_MAIL_LINKS');
+
+       // Build mails
+       registerFilter(__FUNCTION__, __LINE__, 'send_build_mail', 'SEND_BUILD_MAIL');
+
+       // Handle referral banner click/view
+       registerFilter(__FUNCTION__, __LINE__, 'handle_click_php', 'HANDLE_REFERRER_BANNER_CLICK');
+       registerFilter(__FUNCTION__, __LINE__, 'handle_view_php', 'HANDLE_REFERRER_BANNER_VIEW');
+
+       // Generic filter to add hidden fields to formulars
+       registerFilter(__FUNCTION__, __LINE__, 'open_form_fields', 'ADD_INPUT_HIDDEN_SESSION_ID');
 }
 
 // "Registers" a new filter function
-function registerFilter ($F, $L, $filterName, $filterFunction, $silentAbort = true, $force = false, $isDryRun = false) {
+function registerFilter ($file, $line, $filterName, $filterFunction, $silentAbort = TRUE, $force = FALSE, $isDryRun = FALSE) {
        // Extend the filter function name
        $filterFunction = 'FILTER_' . strtoupper($filterFunction);
 
        // Debug message with FILTER_ prefix
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',F=' . basename($F) . ',L=' . $L . ' - ENTERED!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',file=' . basename($file) . ',line=' . $line . ' - ENTERED!');
 
        // Is that filter already there?
-       if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
+       if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
                // In installation phase we always want to abort
-               if (($silentAbort === false) || (isInstallationPhase())) {
+               if (($silentAbort === FALSE) || (isInstaller())) {
                        // Add fatal message
-                       reportBug(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered! F=%s,L=%s,force=%d", $filterName, $filterFunction, basename($F), $L, intval($force)));
+                       reportBug(__FUNCTION__, __LINE__, sprintf('Filter chain %s has already filter function %s registered! file=%s,L=%s,force=%d', $filterName, $filterFunction, basename($file), $line, intval($force)));
                } // END - if
 
                // Abort here
-               return false;
+               return FALSE;
        } // END - if
 
        // Shall we add it?
-       if ($isDryRun === false) {
+       if ($isDryRun === FALSE) {
                // Is the function there?
                if (!function_exists($filterFunction)) {
                        // Then abort here
-                       addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s. F=%s,L=%s,force=%d", $filterFunction, $filterName, basename($F), $L, intval($force)));
-                       return false;
+                       logDebugMessage(__FUNCTION__, __LINE__, sprintf('Filter function %s could not be added to filter chain %s. file=%s,L=%s,force=%d', $filterFunction, $filterName, basename($file), $line, intval($force)));
+                       return FALSE;
                } // END - if
 
                // Simply add it to the array
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',F=' . basename($F) . ',L=' . $L);
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',file=' . basename($file) . ',line=' . $line);
                $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 . ',force=' . intval($force) . ',F=' . basename($F) . ',L=' . $L . ' - EXIT!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',file=' . basename($file) . ',line=' . $line . ' - EXIT!');
+
+       // Worked
+       return TRUE;
 }
 
 // "Unregisters" a filter from the given chain
-function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = false, $isDryRun = false) {
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
+function unregisterFilter ($file, $line, $filterName, $filterFunction, $force = FALSE, $isDryRun = FALSE) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
 
        // Extend the filter function name only if not loaded from database
        if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
@@ -244,20 +266,23 @@ function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = false,
        } // END - if
 
        // Is that filter there?
-       if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
+       if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
                // Not found, so abort here
-               addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_NOT_REMOVED'), $filterFunction, $filterName));
-               return false;
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf('Filter function %s cannot be unregistered from filter %s.', $filterFunction, $filterName));
+               return FALSE;
        } // END - if
 
        // Shall we remove? (default, not while just showing an extension removal)
-       if ($isDryRun === false) {
+       if (($isDryRun === FALSE) && (isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction]))) {
                // Mark for filter removal
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
                $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
        } // END - if
 
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - EXIT!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - EXIT!');
+
+       // Worked
+       return TRUE;
 }
 
 // "Runs" the given filters, filterData is optional and can be any type of data
@@ -266,7 +291,7 @@ function runFilterChain ($filterName, $filterData = NULL) {
 
        // Is that filter chain there?
        if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
-               // Log not found filters in debug-mode
+               // Filter chain not found filters in debug-mode
                if (isDebugModeEnabled()) {
                        // Log it away...
                        logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' does not exist.');
@@ -280,6 +305,9 @@ function runFilterChain ($filterName, $filterData = NULL) {
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',count()=' . count($GLOBALS['cache_array']['filter']['chains'][$filterName]));
        $returnValue = $filterData;
 
+       // Continue filter chain is default
+       continueFilterChain();
+
        // Then run all filters
        foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
                // Debug message
@@ -290,11 +318,11 @@ function runFilterChain ($filterName, $filterData = NULL) {
                        // Is this filter there?
                        if (!function_exists($filterFunction)) {
                                // Should be fixed
-                               reportBug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - AUTO-UNREGISTERED!');
+                               reportBug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',active=' . $active . ' - AUTO-UNREGISTERED!');
                        } // END - if
 
                        // Call the filter chain
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue));
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue) . ' - CALLING!');
                        $returnValue = call_user_func_array($filterFunction, array($returnValue));
 
                        // Update usage counter
@@ -303,6 +331,12 @@ function runFilterChain ($filterName, $filterData = NULL) {
                        // Debug message
                        logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
                }
+
+               // Abort loop?
+               if (isFilterChainAborted()) {
+                       // Yes, then abort here
+                       break;
+               } // END - if
        } // END - foreach
 
        // Return the filtered content
@@ -327,7 +361,7 @@ function prepareFilterArray () {
        // Abort here if array is absend (e.g. not cached)
        if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
                // Abort silently
-               return false;
+               return FALSE;
        } // END - if
 
        // Init dummy array
@@ -344,7 +378,7 @@ function prepareFilterArray () {
 
                // Add the element with mapped index
                $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
-               $filterArray['loaded'][$filterName][$filterFunction]  = true;
+               $filterArray['loaded'][$filterName][$filterFunction]  = TRUE;
                $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
        } // END - foreach
 
@@ -352,16 +386,24 @@ function prepareFilterArray () {
        $GLOBALS['cache_array']['filter'] = $filterArray;
 }
 
-// Loads filter for given extension if present. This function will silently
-// ignore absent filter files.
+/**
+ * Loads filter for given extension if present. This function will silently
+ * ignore absent filter files.
+ *
+ * @param      $ext_name       Name of extension
+ * @return     void
+ */
 function loadExtensionFilters ($ext_name) {
-       // Do we have cache entry?
+       // Debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
+
+       // Is there a cache entry?
        if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
                // Default is not found
-               $GLOBALS[__FUNCTION__][$ext_name] = false;
+               $GLOBALS[__FUNCTION__][$ext_name] = FALSE;
 
                // Construct include file name
-               $incFileName = sprintf("inc/filter/%s_filter.php", $ext_name);
+               $incFileName = sprintf('inc/filter/%s_filter.php', $ext_name);
 
                // Is the include file readable?
                if (isIncludeReadable($incFileName)) {
@@ -369,12 +411,33 @@ function loadExtensionFilters ($ext_name) {
                        loadIncludeOnce($incFileName);
 
                        // Mark the file as loaded
-                       $GLOBALS[__FUNCTION__][$ext_name] = true;
+                       $GLOBALS[__FUNCTION__][$ext_name] = TRUE;
                } elseif (isDebugModeEnabled()) {
                        // Log missing file
-                       logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');
+                       //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');
                }
        } // END - if
+
+       // Debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',result=' . intval($GLOBALS[__FUNCTION__][$ext_name]) . ' - EXIT!');
+}
+
+// Checks whether the filter chain has been aborted
+function isFilterChainAborted () {
+       // Determine it
+       return ((isset($GLOBALS['filter_chain_interrupted'])) && ($GLOBALS['filter_chain_interrupted'] === TRUE));
+}
+
+// Interrupts the filter chain by enabling flag 'filter_chain_aborted'
+function interruptFilterChain () {
+       // Set it
+       $GLOBALS['filter_chain_interrupted'] = TRUE;
+}
+
+// Continues the filter chain by disabling flag 'filter_chain_aborted'
+function continueFilterChain () {
+       // Set it
+       $GLOBALS['filter_chain_interrupted'] = FALSE;
 }
 
 // [EOF]