Latest SANDPIT - MERGE from CVS (MERGE-GIT)
This commit is contained in:
106
lib/hooks.php
106
lib/hooks.php
@@ -1,12 +1,9 @@
|
||||
<?php
|
||||
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/hooks.php,v 1.10.2.3 2008/11/28 04:44:54 wurley Exp $
|
||||
// $Header$
|
||||
|
||||
/**
|
||||
* Functions related to hooks management.
|
||||
*
|
||||
* @author Benjamin Drieu <benjamin.drieu@fr.alcove.com> and Alc<6C>?ve
|
||||
* @package phpLDAPadmin
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
@@ -21,6 +18,9 @@
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA
|
||||
*
|
||||
* @author Benjamin Drieu <benjamin.drieu@fr.alcove.com> and Alc<6C>?ve
|
||||
* @package phpLDAPadmin
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -40,10 +40,9 @@ function sort_array_by_priority($a,$b) {
|
||||
/**
|
||||
* Runs procedures attached to a hook.
|
||||
*
|
||||
* @param hook_name Name of hook to run.
|
||||
* @param args Array of optional arguments set by
|
||||
* phpldapadmin. It is normally in a form known
|
||||
* by call_user_func_array() :
|
||||
* @param hook_name Name of hook to run.
|
||||
* @param args Array of optional arguments set by phpldapadmin. It is normally in a form known by call_user_func_array() :
|
||||
*
|
||||
* <pre>[ 'server_id' => 0,
|
||||
* 'dn' => 'uid=epoussa,ou=tech,o=corp,o=fr' ]</pre>
|
||||
*
|
||||
@@ -52,39 +51,53 @@ function sort_array_by_priority($a,$b) {
|
||||
function run_hook($hook_name,$args) {
|
||||
$hooks = isset($_SESSION[APPCONFIG]) ? $_SESSION[APPCONFIG]->hooks : array();
|
||||
|
||||
syslog_debug("Running hook $hook_name.");
|
||||
if (DEBUG_ENABLED)
|
||||
debug_log('Running HOOK (%s,%s)',257,__FILE__,__LINE__,__METHOD__,
|
||||
$hook_name,$args);
|
||||
|
||||
if (! array_key_exists($hook_name,$hooks)) {
|
||||
syslog_notice("Hook '$hook_name' not defined !\n");
|
||||
if (DEBUG_ENABLED)
|
||||
debug_log('Returning, HOOK not defined (%s)',257,__FILE__,__LINE__,__METHOD__,
|
||||
$hook_name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unset($rollbacks);
|
||||
$rollbacks = array ();
|
||||
$rollbacks = array();
|
||||
reset($hooks[$hook_name]);
|
||||
|
||||
/* Execution of procedures attached is done using a numeric order
|
||||
* since all procedures have been attached to the hook with a
|
||||
* numerical weight. */
|
||||
while (list($key,$hook) = each($hooks[$hook_name])) {
|
||||
if (DEBUG_ENABLED)
|
||||
debug_log('Calling HOOK Function (%s)(%s)',257,__FILE__,__LINE__,__METHOD__,
|
||||
$hook['hook_function'],$args);
|
||||
|
||||
array_push($rollbacks,$hook['rollback_function']);
|
||||
syslog_debug("Calling ".$hook['hook_function']."\n");
|
||||
|
||||
$result = call_user_func_array($hook['hook_function'],$args);
|
||||
syslog_notice("Called ".$hook['hook_function']."\n");
|
||||
if (DEBUG_ENABLED)
|
||||
debug_log('Called HOOK Function (%s)',257,__FILE__,__LINE__,__METHOD__,
|
||||
$hook['hook_function']);
|
||||
|
||||
/* If a procedure fails, its optional rollback is executed with
|
||||
/* If a procedure fails (identified by a false return), its optional rollback is executed with
|
||||
* the same arguments. After that, all rollbacks from
|
||||
* previously executed procedures are executed in the reverse
|
||||
* order. */
|
||||
if ($result != true) {
|
||||
syslog_debug("Function ".$hook['hook_function']." returned $result\n");
|
||||
if (! is_null($result) && $result == false) {
|
||||
if (DEBUG_ENABLED)
|
||||
debug_log('HOOK Function [%s] return (%s)',257,__FILE__,__LINE__,__METHOD__,
|
||||
$hook['hook_function'],$result);
|
||||
|
||||
while ($rollbacks) {
|
||||
$rollback = array_pop($rollbacks);
|
||||
|
||||
if ($rollback != false) {
|
||||
syslog_debug("Executing rollback $rollback\n");
|
||||
if (DEBUG_ENABLED)
|
||||
debug_log('HOOK Function Rollback (%s)',257,__FILE__,__LINE__,__METHOD__,
|
||||
$rollback);
|
||||
|
||||
call_user_func_array($rollback,$args);
|
||||
}
|
||||
}
|
||||
@@ -99,19 +112,26 @@ function run_hook($hook_name,$args) {
|
||||
/**
|
||||
* Adds a procedure to a hook for later execution.
|
||||
*
|
||||
* @param hook_name Name of the hook.
|
||||
* @param priority Numeric priority. Lowest means
|
||||
* procedure will be executed before.
|
||||
* @param hook_function Name of the php function called upon
|
||||
* hook trigger.
|
||||
* @param rollback_function Name of the php rollback function
|
||||
* called upon failure.
|
||||
* @param hook_name Name of the hook.
|
||||
* @param hook_function Name of the php function called upon hook trigger.
|
||||
* @param priority Numeric priority. Lowest means procedure will be executed before.
|
||||
* @param rollback_function Name of the php rollback function called upon failure.
|
||||
*/
|
||||
function add_hook($hook_name,$priority,$hook_function,$rollback_function) {
|
||||
function add_hook($hook_name,$hook_function,$priority=0,$rollback_function=null) {
|
||||
# First, see if the hook function exists.
|
||||
if (! function_exists($hook_function)) {
|
||||
system_message(array(
|
||||
'title'=>_('Hook function does not exist'),
|
||||
'body'=>sprintf('Hook name: %s<br/>Hook function: %s',$hook_name,$hook_function),
|
||||
'type'=>'warn'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! array_key_exists($hook_name,$_SESSION[APPCONFIG]->hooks))
|
||||
$_SESSION[APPCONFIG]->hooks[$hook_name] = array();
|
||||
|
||||
remove_hook($hook_name,-1,$hook_function,'');
|
||||
remove_hook($hook_name,$hook_function,-1,null);
|
||||
|
||||
array_push($_SESSION[APPCONFIG]->hooks[$hook_name],array(
|
||||
'priority' => $priority,
|
||||
@@ -124,19 +144,13 @@ function add_hook($hook_name,$priority,$hook_function,$rollback_function) {
|
||||
/**
|
||||
* Removes a procedure from a hook, based on a filter.
|
||||
*
|
||||
* @param hook_name Name of the hook.
|
||||
* @param priority Numeric priority. If set, all
|
||||
* procedures of that priority will be
|
||||
* removed.
|
||||
* @param hook_function Name of the procedure function. If
|
||||
* set, all procedures that call this
|
||||
* function will be removed.
|
||||
* @param rollback_function Name of the php rollback function
|
||||
* called upon failure. If set, all
|
||||
* procedures that call this function
|
||||
* as a rollback will be removed.
|
||||
* @param hook_name Name of the hook.
|
||||
* @param priority Numeric priority. If set, all procedures of that priority will be removed.
|
||||
* @param hook_function Name of the procedure function. If set, all procedures that call this function will be removed.
|
||||
* @param rollback_function Name of the php rollback function called upon failure. If set, all
|
||||
* procedures that call this function as a rollback will be removed.
|
||||
*/
|
||||
function remove_hook($hook_name,$priority,$hook_function,$rollback_function) {
|
||||
function remove_hook($hook_name,$hook_function,$priority,$rollback_function) {
|
||||
if (array_key_exists($hook_name,$_SESSION[APPCONFIG]->hooks)) {
|
||||
reset($_SESSION[APPCONFIG]->hooks[$hook_name]);
|
||||
|
||||
@@ -161,17 +175,19 @@ function clear_hooks($hook_name) {
|
||||
unset($_SESSION[APPCONFIG]->hooks[$hook_name]);
|
||||
}
|
||||
|
||||
$hooks = array();
|
||||
|
||||
# Evaluating user-made hooks
|
||||
if (is_dir(HOOKSDIR.'functions')) {
|
||||
$dir = dir(HOOKSDIR.'functions');
|
||||
$hooks['dir'] = dir(HOOKSDIR.'functions');
|
||||
|
||||
while (false !== ($hookfile = $dir->read())) {
|
||||
$filename = sprintf('%s/%s/%s',HOOKSDIR,'functions',$hookfile);
|
||||
while ($hooks['file'] = $hooks['dir']->read()) {
|
||||
$script = sprintf('%s/%s/%s',HOOKSDIR,'functions',$hooks['file']);
|
||||
|
||||
if (is_file($filename) and eregi('php[0-9]?$',$hookfile))
|
||||
require_once "$filename";
|
||||
if (is_file($script) && preg_match('/php[0-9]?$/',$hooks['file']))
|
||||
require_once $script;
|
||||
}
|
||||
|
||||
$dir -> close();
|
||||
$hooks['dir']->close();
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user