and Alc�?ve * @package phpLDAPadmin */ /** * Compares two arrays by numerically comparing their 'prority' * value. Standard `cmp-like' function. * * @param a First element to compare. * @param b Second element to compare. * * @return -1 if priority of first element is smaller than second * element priority. 1 otherwise. */ function sort_array_by_priority($a,$b) { return (($a['priority'] < $b['priority']) ? -1 : 1 ); } /** * 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() : * *
[ 'server_id' => 0, * 'dn' => 'uid=epoussa,ou=tech,o=corp,o=fr' ]* * @return true if all procedures returned true, false otherwise. */ function run_hook($hook_name,$args) { $hooks = isset($_SESSION[APPCONFIG]) ? $_SESSION[APPCONFIG]->hooks : array(); if (! count($hooks) || ! array_key_exists($hook_name,$hooks)) { return true; } $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])) { array_push($rollbacks,$hook['rollback_function']); $result = call_user_func_array($hook['hook_function'],$args); /* 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 (! is_null($result) && $result == false) { while ($rollbacks) { $rollback = array_pop($rollbacks); if ($rollback != false) { call_user_func_array($rollback,$args); } } return false; } } return true; } /** * Adds a procedure to a hook for later execution. * * @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,$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