array(),
// Filters for post-init phase
'postinit' => array(),
// Filters for shutdown phase
'shutdown' => array()
);
// Init loaded filters
$loadedFilters = array();
// Load all saved filers if sql_patches is updated
if (GET_EXT_VERSION("sql_patches") >= "0.5.9") {
// Load all active filers
$result = SQL_QUERY("SELECT `filter_name`, `filter_function`, `filter_active`
FROM `"._MYSQL_PREFIX."_filters`
ORDER BY `filter_id` ASC", __FILE__, __LINE__);
// Are there entries?
if (SQL_NUMROWS($result) > 0) {
// Load all filters
while ($filterArray = SQL_FETCHARRAY($result)) {
// Mark this filter as loaded (from database)
$loadedFilters[$filterArray['filter_name']][$filterArray['filter_function']] = true;
// Set this filter
$filters[$filterArray['filter_name']][$filterArray['filter_function']] = $filterArray['filter_active'];
} // END - while
} // END - if
// Free result
SQL_FREERESULT($result);
} // END - if
// @TODO Find some more init/shutdown filter functions
// 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;
// Extend the filter function name
$filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
// Is that filter already there?
if ((isset($filters[$filterName][$filterFunction])) && (!$force)) {
// Then abort here
if (!$silentAbort) {
ADD_FATAL(sprintf(FILTER_FAILED_ALREADY_ADDED, $filterFunction, $filterName));
} // END - if
// Abort here
return false;
} // END - if
// Is the function there?
if (!function_exists($filterFunction)) {
// Then abort here
ADD_FATAL(sprintf(FILTER_FAILED_NOT_FOUND, $filterFunction, $filterName));
return false;
} // END - if
// Simply add it to the array
$filters[$filterName][$filterFunction] = "Y";
}
// "Unregisters" a filter from the given chain
function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $remove = true) {
global $filters;
// Extend the filter function name
$filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
// Is that filter there?
if ((!isset($filters[$filterName][$filterFunction])) && (!$force)) {
// Not found, so abort here
ADD_FATAL(sprintf(FILTER_FAILED_NOT_REMOVED, $filterFunction, $filterName));
return false;
} // END - if
// Shall we remove? (default, not while just showing an extension removal)
if ($remove) {
// Mark for filter removal
$filters[$filterName][$filterFunction] = "R";
} // 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;
// Is that filter chain there?
if (!isset($filters[$filterName])) {
// Then abort here (quick'N'dirty hack)
if ((!$silentAbort) && (defined('FILTER_FAILED_NO_FILTER_FOUND'))) {
// Add fatal message
ADD_FATAL(sprintf(FILTER_FAILED_NO_FILTER_FOUND, $filterName));
} // END - if
// Abort here
return false;
} // END - if
// Default return value
$returnValue = $data;
// Then run all filters
foreach ($filters[$filterName] as $filterFunction=>$active) {
// Debug message
//* DEBUG: */ echo __FUNCTION__."(".__LINE__."): name={$filterName}, func={$filterFunction}, active={$active}
\n";
// Is the filter active?
if ($active == "Y") {
// Call the filter chain
$returnValue = call_user_func_array($filterFunction, array($returnValue));
} // END - if
} // END - foreach
// Return the filtered content
return $returnValue;
}
// -----------------------------------------------------------------------------
// Generic filter functions we always need
// -----------------------------------------------------------------------------
// Filter for flushing all new filters to the database
function FILTER_FLUSH_FILTERS () {
global $filters, $link, $loadedFilters;
// Is a database link here and not in installation mode?
if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
// Abort here
ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
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 ($filters as $filterName => $filterArray) {
// Walk through all filters
foreach ($filterArray as $filterFunction => $active) {
// Is this filter loaded?
if (!isset($loadedFilters[$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
SQL_QUERY($insertSQL, __FILE__, __LINE__);
} // END - if
// Something has been removed?
if ($removed > 0) {
// Finish SQL command
$removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
// And run it
SQL_QUERY($removeSQL, __FILE__, __LINE__);
} // END - if
}
// Filter for shutting down the database link
function FILTER_SHUTDOWN_DATABASE () {
global $link;
if (is_resource($link)) {
// Close link
SQL_CLOSE($link, __FILE__, __LINE__);
} else {
// No database link
ADD_FATAL(NO_DB_LINK);
}
}
//
?>