- $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
- require($INC);
-}
-
-// Init "generic filter system"
-function initFilterSystem () {
- // Is the filter already initialized?
- if ((isset($GLOBALS['filters']['chains'])) && (is_array($GLOBALS['filters']['chains']))) {
- // Then abort here
- debug_report_bug(getMessage('FILTER_FAILED_ALREADY_INIT'));
- } // END - if
-
- // Init the filter system (just some ideas)
- $GLOBALS['filters']['chains'] = array(
- 'preinit' => array(), // Filters for pre-init phase
- 'postinit' => array(), // Filters for post-init phase
- 'shutdown' => array() // Filters for shutdown phase
- );
-
- // Init loaded filters and counter
- $GLOBALS['filters']['loaded'] = array();
- $GLOBALS['filters']['counter'] = array();
-
- // Load all saved filers if sql_patches is updated
- if (isset($GLOBALS['cache_array']['filter'])) {
- // Found in cache so rewrite the array
- $filterArray = 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
-
- // Set the array
- //die('<pre>'.print_r($filterArray, true).'</pre>');
- $GLOBALS['filters'] = $filterArray;
-
- // Remove the cache
- unset($GLOBALS['cache_array']['filter']);
- } elseif (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`".$add."
-FROM `{!_MYSQL_PREFIX!}_filters`
-ORDER BY `filter_id` ASC", __FUNCTION__, __LINE__);
-
- // Are there entries?
- 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
- $GLOBALS['filters']['counter'][$filterName][$filterFunction] = 0;
-
- // Mark this filter as loaded (from database)
- $GLOBALS['filters']['loaded'][$filterName][$filterFunction] = true;
-
- // Set this filter
- $GLOBALS['filters']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
-
- // Is the array element for counter there?
- if (isset($filterArray['filter_counter'])) {
- // Then use this value!
- $GLOBALS['filters']['counter'][$filterName][$filterFunction] = $filterArray['filter_counter'];
- } // END - if
- } // END - while
- } // END - if
-
- // Free result
- SQL_FREERESULT($result);
-
- // Init filters
- registerFilter('init', 'UPDATE_LOGIN_DATA');
- registerFilter('init', 'INIT_RANDOMIZER');
-
- // Login failures handler
- registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
-
- // Filters for pre-extension-registration
- registerFilter('pre_extension_installed', 'RUN_SQLS');
-
- // Filters for post-extension-registration
- registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
- registerFilter('post_extension_installed', 'SOLVE_TASK');
- registerFilter('post_extension_installed', 'LOAD_INCLUDES');
- registerFilter('post_extension_installed', 'REMOVE_UPDATES');
-
- // Solving tasks
- registerFilter('solve_task', 'SOLVE_TASK');
-
- // Loading includes in general
- registerFilter('load_includes', 'LOAD_INCLUDES');
-
- // Run SQLs
- registerFilter('run_sqls', 'RUN_SQLS');
-
- // Admin ACL check
- registerFilter('check_admin_acl', 'CHECK_ADMIN_ACL');
-
- // Register shutdown filters
- registerFilter('shutdown', 'FLUSH_FILTERS');
- } // END - if
-}
-
-// "Registers" a new filter function
-function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
- // Extend the filter function name
- $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
-
- // Is that filter already there?
- if ((isset($GLOBALS['filters']['chains'][$filterName][$filterFunction])) && (!$force)) {
- // Then abort here
- if (!$silentAbort) {
- addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_ALREADY_ADDED'), array($filterFunction, $filterName));
- } // END - if
-
- // Abort here
- return false;
- } // END - if
-
- // Is the function there?
- if (!function_exists($filterFunction)) {
- // Then abort here
- addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_NOT_FOUND'), array($filterFunction, $filterName));
- return false;
- } // END - if
-
- // Shall we add it?
- if (!$dry_run) {
- // Simply add it to the array
- $GLOBALS['filters']['chains'][$filterName][$filterFunction] = 'Y';
- $GLOBALS['filters']['counter'][$filterName][$filterFunction] = 0;
- } // END - if
-}
-
-// "Unregisters" a filter from the given chain
-function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_run = false) {
- // Extend the filter function name only if not loaded from database
- if (!isset($GLOBALS['filters']['loaded'][$filterName][$filterFunction])) {
- $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
- } // END - if
-
- // Is that filter there?
- if ((!isset($GLOBALS['filters']['chains'][$filterName][$filterFunction])) && (!$force)) {
- // Not found, so abort here
- addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_NOT_REMOVED'), array($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
- $GLOBALS['filters']['chains'][$filterName][$filterFunction] = 'R';
- } // END - if
-}
-
-// "Runs" the given filters, data is optional and can be any type of data
-function runFilterChain ($filterName, $data = null, $silentAbort = true) {
- // Is that filter chain there?
- if (!isset($GLOBALS['filters']['chains'][$filterName])) {
- // We should find all these non-existing filter chains
- debug_report_bug('Filter chain '.$filterName.' not found!');
- } // END - if
-
- // Default return value
- $returnValue = $data;
-
- // Then run all filters
- foreach ($GLOBALS['filters']['chains'][$filterName] as $filterFunction => $active) {
- // Debug message
- //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Running: name={$filterName},func={$filterFunction},active={$active}");
-
- // Is the filter active?
- if (($active == 'Y') || ((in_array($filterName, array('extension_remove', 'post_extension_run_sql'))) && ($active == 'R'))) {
- // Is this filter there?
- if (!function_exists($filterFunction)) {
- // Unregister it
- unregisterFilter($filterName, $filterFunction);
-
- // Skip this entry
- continue;
- } // END - if
-
- // Call the filter chain
- $returnValue = call_user_func_array($filterFunction, array($returnValue));
-
- // Update usage counter
- countFilterUsage($filterName, $filterFunction);
- } elseif (isDebugModeEnabled()) {
- // Debug message
- DEBUG_LOG(__FUNCTION__, __LINE__, "Skipped: name={$filterName},func={$filterFunction},active={$active}");
- }
- } // END - foreach
-
- // Return the filtered content
- return $returnValue;
-}
-
-// Count the filter usage
-function countFilterUsage ($filterName, $filterFunction) {
- // Is it there?
- if (isset($GLOBALS['filters']['counter'][$filterName][$filterFunction])) {
- // Yes, then increase
- $GLOBALS['filters']['counter'][$filterName][$filterFunction]++;
- } else {
- // No, then create
- $GLOBALS['filters']['counter'][$filterName][$filterFunction] = 1;
- }
-}
-
-// -----------------------------------------------------------------------------
-// Generic filter functions we always need
-// -----------------------------------------------------------------------------