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']['filter_name'])) { // 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('
'.print_r($filterArray, true).''); $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'); registerFilter('init', 'DETERMINE_USERNAME'); // 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'); } // "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 === false) { 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) { // Is that filter chain there? if (!isset($GLOBALS['filters']['chains'][$filterName])) { // We should find all these non-existing filter chains DEBUG_LOG(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!'); // Abort here return false; } // 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 // ----------------------------------------------------------------------------- // Filter for flushing all new filters to the database function FILTER_FLUSH_FILTERS () { // Clear all previous SQL queries INIT_SQLS(); // Are we installing? if ((isInstalling()) || (!isInstalled())) { // Then silently skip this filter return true; } // END - if // Is a database link here and not in installation mode? if ((!SQL_IS_LINK_UP()) && (!isInstalling())) { // Abort here addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FLUSH_FAILED_NO_DATABASE')); return false; } // END - if // Is the extension sql_patches updated? if (EXT_VERSION_IS_OLDER('sql_patches', '0.5.9')) { // Abort silently here return false; } // END - if // Nothing is added/remove by default $inserted = 0; $removed = 0; // Prepare SQL queries $insertSQL = "INSERT INTO `{!_MYSQL_PREFIX!}_filters` (`filter_name`,`filter_function`,`filter_active`) VALUES"; $removeSQL = "DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_filters` WHERE"; // Write all filters to database foreach ($GLOBALS['filters']['chains'] as $filterName => $filterArray) { // Walk through all filters foreach ($filterArray as $filterFunction => $active) { // Is this filter loaded? if (!isset($GLOBALS['filters']['loaded'][$filterName][$filterFunction])) { // Add this filter (all filters are active by default) $insertSQL .= sprintf("('%s','%s','Y'),", $filterName, $filterFunction); $inserted++; } elseif ($active == "R") { // Remove this filter $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction); $removed++; } } // END - foreach } // END - foreach // Something has been added? if ($inserted > 0) { // Finish SQL command $insertSQL = substr($insertSQL, 0, -1); // And run it ADD_SQL($insertSQL); } // END - if // Something has been removed? if ($removed > 0) { // Finish SQL command $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed; // And run it ADD_SQL($removeSQL); } // END - if // Shall we update usage counters (ONLY FOR DEBUGGING!) if (getConfig('update_filter_usage') == 'Y') { // Update all counters foreach ($GLOBALS['filters']['counter'] as $filterName => $filterArray) { // Walk through all filters foreach ($filterArray as $filterFunction => $cnt) { // Construct and add the query ADD_SQL(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 runFilterChain('run_sqls'); } // Filter for calling the handler for login failures function FILTER_CALL_HANDLER_LOGIN_FAILTURES ($data) { // Init content $content = $data; // Handle failed logins here if not in guest //* DEBUG: */ print __FUNCTION__."(".__LINE__."):type={$data['type']},action={getAction()},what={getWhat()},lvl={$data['access_level']}