2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2005-11-13 07:58:38 +01:00
|
|
|
/**************************************************************************\
|
|
|
|
* eGroupWare API - Hooks *
|
|
|
|
* This file written by Dan Kuykendall <seek3r@phpgroupware.org> *
|
|
|
|
* Allows applications to "hook" into each other *
|
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall *
|
|
|
|
* New method hooks and docu are written by <RalfBecker@outdoor-training.de>*
|
|
|
|
* -------------------------------------------------------------------------*
|
|
|
|
* This library is part of the eGroupWare API *
|
|
|
|
* http://www.egroupware.org/api *
|
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
2003-08-28 16:31:11 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* class which gives ability for applications to set and use hooks to communicate with each other
|
|
|
|
*
|
|
|
|
* Hooks need to be declared in the app's setup.inc.php file and they have to be registered
|
|
|
|
* (copied into the database) by
|
|
|
|
* - installing or updating the app via setup or
|
|
|
|
* - running Admin >> register all hooks
|
|
|
|
* As the hooks-class can get cached in the session (session-type PHP_RESTORE), you also have to log
|
|
|
|
* out and in again, that your changes take effect.
|
|
|
|
*
|
|
|
|
* Hooks can either have two formats
|
|
|
|
* - new method hooks (prefered), which are methods of a class. You can pass parameters to the call and
|
|
|
|
* they can return values. They get declared in setup.inc.php as:
|
|
|
|
* $setup_info['appname']['hooks']['location'] = 'app.class.method';
|
|
|
|
* - old type, which are included files. Values can only be passed by global values and they cant return anything.
|
|
|
|
* Old declaration in setup.inc.php:
|
|
|
|
* $setup_info['appname']['hooks'][] = 'location';
|
|
|
|
*
|
|
|
|
* @author Dan Kuykendall
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de> new method hooks
|
|
|
|
* @license LGPL
|
|
|
|
* @package phpgwapi
|
|
|
|
* @access public
|
|
|
|
*/
|
2002-01-02 15:33:05 +01:00
|
|
|
|
2001-05-06 15:40:04 +02:00
|
|
|
class hooks
|
|
|
|
{
|
2002-01-02 15:33:05 +01:00
|
|
|
var $found_hooks = Array();
|
2005-11-13 07:58:38 +01:00
|
|
|
var $db;
|
|
|
|
var $table = 'egw_hooks';
|
2003-04-28 00:26:04 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* constructor, reads and caches the complete hooks table
|
|
|
|
*
|
|
|
|
* @param object $db=null database class, if null we use $GLOBALS['egw']->db
|
|
|
|
*/
|
|
|
|
function hooks($db=null)
|
2001-05-06 15:40:04 +02:00
|
|
|
{
|
2005-07-23 13:39:20 +02:00
|
|
|
$this->db = $db ? $db : $GLOBALS['egw']->db; // this is to allow setup to set the db
|
2003-04-28 00:26:04 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
$this->db->select($this->table,'hook_appname,hook_location,hook_filename',false,__LINE__,__FILE__);
|
2003-04-28 00:26:04 +02:00
|
|
|
while( $this->db->next_record() )
|
2001-05-14 05:54:40 +02:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$this->found_hooks[$this->db->f('hook_appname')][$this->db->f('hook_location')] = $this->db->f('hook_filename');
|
2001-05-14 05:54:40 +02:00
|
|
|
}
|
2002-01-02 15:33:05 +01:00
|
|
|
//echo '<pre>';
|
|
|
|
//print_r($this->found_hooks);
|
|
|
|
//echo '</pre>';
|
2001-05-06 15:40:04 +02:00
|
|
|
}
|
2003-08-28 16:31:11 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* executes all the hooks (the user has rights to) for a given location
|
|
|
|
*
|
|
|
|
* @param string/array $args location-name as string or array with keys location, order and
|
|
|
|
* further data to be passed to the hook, if its a new method-hook
|
|
|
|
* @param array $order appnames (as value), which should be executes first
|
|
|
|
* @param boolean $no_permission_check if True execute all hooks, not only the ones a user has rights to
|
|
|
|
* $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo)
|
|
|
|
* @return array with results of each hook call (with appname as key) and value:
|
|
|
|
* - False if no hook exists,
|
|
|
|
* - True if old hook exists and
|
|
|
|
* - whatever the new method-hook returns (can be True or False too!).
|
|
|
|
*/
|
2003-04-28 00:26:04 +02:00
|
|
|
function process($args, $order = '', $no_permission_check = False)
|
2001-05-06 15:40:04 +02:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
//echo "<p>hooks::process("; print_r($args); echo ")</p>\n";
|
2002-01-02 15:33:05 +01:00
|
|
|
if ($order == '')
|
2001-05-06 15:40:04 +02:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$order = is_array($args) && isset($args['order']) ? $args['order'] :
|
2005-07-23 13:39:20 +02:00
|
|
|
array($GLOBALS['egw_info']['flags']['currentapp']);
|
2001-05-06 15:40:04 +02:00
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2002-01-02 15:33:05 +01:00
|
|
|
/* First include the ordered apps hook file */
|
2003-04-28 00:26:04 +02:00
|
|
|
foreach($order as $appname)
|
2001-05-06 15:40:04 +02:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$results[$appname] = $this->single($args,$appname,$no_permission_check);
|
2003-05-24 16:25:13 +02:00
|
|
|
|
2003-11-19 16:16:37 +01:00
|
|
|
if (!isset($results[$appname])) // happens if the method hook has no return-value
|
2003-05-24 16:25:13 +02:00
|
|
|
{
|
|
|
|
$results[$appname] = False;
|
|
|
|
}
|
2002-01-02 15:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then add the rest */
|
|
|
|
if ($no_permission_check)
|
|
|
|
{
|
2005-07-23 13:39:20 +02:00
|
|
|
$apps = $GLOBALS['egw_info']['apps'];
|
2002-01-02 15:33:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-07-23 13:39:20 +02:00
|
|
|
$apps = $GLOBALS['egw_info']['user']['apps'];
|
2003-04-28 00:26:04 +02:00
|
|
|
}
|
2004-02-03 07:06:54 +01:00
|
|
|
settype($apps,'array');
|
2003-04-28 00:26:04 +02:00
|
|
|
foreach($apps as $app)
|
|
|
|
{
|
|
|
|
$appname = $app['name'];
|
|
|
|
if (!isset($results[$appname]))
|
2002-01-02 15:33:05 +01:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$results[$appname] = $this->single($args,$appname,$no_permission_check);
|
2001-05-06 15:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
2003-04-28 00:26:04 +02:00
|
|
|
return $results;
|
2001-05-06 15:40:04 +02:00
|
|
|
}
|
2002-01-02 15:33:05 +01:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* executes a single hook of a given location and application
|
|
|
|
*
|
|
|
|
* @param string/array $args location-name as string or array with keys location, appname and
|
|
|
|
* further data to be passed to the hook, if its a new method-hook
|
|
|
|
* @param string $appname name of the app, which's hook to execute, if empty the current app is used
|
|
|
|
* @param boolean $no_permission_check if True execute all hooks, not only the ones a user has rights to
|
|
|
|
* $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo)
|
|
|
|
* @param boolean $try_unregisterd If true, try to include old file-hook anyway (for setup)
|
|
|
|
* @return mixed False if no hook exists, True if old hook exists and whatever the new method-hook returns (can be True or False too!).
|
|
|
|
*/
|
2003-04-28 10:24:45 +02:00
|
|
|
function single($args, $appname = '', $no_permission_check = False,$try_unregistered = False)
|
2002-01-02 15:33:05 +01:00
|
|
|
{
|
2003-04-28 10:24:45 +02:00
|
|
|
//echo "<p>hooks::single("; print_r($args); echo ",'$appname','$no_permission_check','$try_unregistered')</p>\n";
|
2003-04-28 00:26:04 +02:00
|
|
|
if (is_array($args))
|
2002-01-02 15:33:05 +01:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$location = $args['location'];
|
2002-01-02 15:33:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$location = $args;
|
2002-01-02 15:33:05 +01:00
|
|
|
}
|
2003-04-28 00:26:04 +02:00
|
|
|
if (!$appname)
|
2002-02-10 20:34:11 +01:00
|
|
|
{
|
2005-07-23 13:39:20 +02:00
|
|
|
$appname = is_array($args) && isset($args['appname']) ? $args['appname'] : $GLOBALS['egw_info']['flags']['currentapp'];
|
2002-02-10 20:34:11 +01:00
|
|
|
}
|
2003-08-28 16:31:11 +02:00
|
|
|
$SEP = filesystem_separator();
|
2002-02-10 20:34:11 +01:00
|
|
|
|
2003-04-28 00:26:04 +02:00
|
|
|
/* First include the ordered apps hook file */
|
2003-04-28 10:24:45 +02:00
|
|
|
if (isset($this->found_hooks[$appname][$location]) || $try_unregistered)
|
2002-02-10 20:34:11 +01:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
$parts = explode('.',$method = $this->found_hooks[$appname][$location]);
|
|
|
|
|
|
|
|
if (count($parts) != 3 || ($parts[1] == 'inc' && $parts[2] == 'php'))
|
2002-02-10 20:34:11 +01:00
|
|
|
{
|
2003-11-19 16:16:37 +01:00
|
|
|
if ($try_unregistered && empty($method))
|
2003-04-28 10:24:45 +02:00
|
|
|
{
|
|
|
|
$method = 'hook_'.$location.'.inc.php';
|
|
|
|
}
|
2005-07-23 13:39:20 +02:00
|
|
|
$f = EGW_SERVER_ROOT . $SEP . $appname . $SEP . 'inc' . $SEP . $method;
|
2003-04-28 00:26:04 +02:00
|
|
|
if (file_exists($f) &&
|
2005-07-23 13:39:20 +02:00
|
|
|
( $GLOBALS['egw_info']['user']['apps'][$appname] || (($no_permission_check || $location == 'config' || $appname == 'phpgwapi') && $appname)) )
|
2003-04-28 00:26:04 +02:00
|
|
|
{
|
|
|
|
include($f);
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2002-02-10 20:34:11 +01:00
|
|
|
}
|
2003-04-28 00:26:04 +02:00
|
|
|
else // new style method-hook
|
2002-02-10 20:34:11 +01:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
return ExecMethod($method,$args);
|
2002-02-10 20:34:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
return False;
|
2002-02-10 20:34:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* loop through the applications and count the hooks
|
|
|
|
*
|
|
|
|
* @param string $location location-name
|
|
|
|
* @return int the number of found hooks
|
|
|
|
*/
|
2002-01-02 15:33:05 +01:00
|
|
|
function count($location)
|
|
|
|
{
|
|
|
|
$count = 0;
|
2005-07-23 13:39:20 +02:00
|
|
|
foreach($GLOBALS['egw_info']['user']['apps'] as $appname => $data)
|
2002-01-02 15:33:05 +01:00
|
|
|
{
|
2003-04-28 00:26:04 +02:00
|
|
|
if (isset($this->found_hooks[$appname][$location]))
|
2002-01-02 15:33:05 +01:00
|
|
|
{
|
2003-08-28 16:31:11 +02:00
|
|
|
++$count;
|
2002-01-02 15:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
2003-04-28 00:26:04 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* @deprecated currently not being used
|
|
|
|
*/
|
2002-01-02 15:33:05 +01:00
|
|
|
function read()
|
|
|
|
{
|
|
|
|
//if (!is_array($this->found_hooks))
|
|
|
|
//{
|
|
|
|
$this->hooks();
|
|
|
|
//}
|
|
|
|
return $this->found_hooks;
|
|
|
|
}
|
2003-04-28 00:26:04 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* Register and/or de-register an application's hooks
|
|
|
|
*
|
|
|
|
* First all existing hooks of $appname get deleted in the db and then the given ones get registered.
|
|
|
|
*
|
|
|
|
* @param string $appname Application 'name'
|
|
|
|
* @param array $hooks=null hooks to register, eg $setup_info[$app]['hooks'] or not used for only deregister the hooks
|
|
|
|
* @return boolean false on error, true otherwise
|
|
|
|
*/
|
|
|
|
function register_hooks($appname,$hooks=null)
|
2003-04-28 00:26:04 +02:00
|
|
|
{
|
|
|
|
if(!$appname)
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2005-11-13 07:58:38 +01:00
|
|
|
$this->db->delete($this->table,array('hook_appname' => $appname),__LINE__,__FILE__);
|
2003-04-28 00:26:04 +02:00
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
if (!is_array($hooks) || !count($hooks)) // only deregister
|
2003-04-28 00:26:04 +02:00
|
|
|
{
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
//echo "<p>ADDING hooks for: $appname</p>";
|
|
|
|
foreach($hooks as $key => $hook)
|
|
|
|
{
|
2003-11-19 16:16:37 +01:00
|
|
|
if (!is_numeric($key)) // new method-hook
|
2003-04-28 00:26:04 +02:00
|
|
|
{
|
|
|
|
$location = $key;
|
|
|
|
$filename = $hook;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$location = $hook;
|
|
|
|
$filename = "hook_$hook.inc.php";
|
|
|
|
}
|
2005-11-13 07:58:38 +01:00
|
|
|
$this->db->insert($this->table,array(
|
|
|
|
'hook_filename' => $filename,
|
|
|
|
),array(
|
|
|
|
'hook_appname' => $appname,
|
|
|
|
'hook_location' => $location,
|
|
|
|
),__LINE__,__FILE__);
|
2003-04-28 00:26:04 +02:00
|
|
|
}
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-13 07:58:38 +01:00
|
|
|
/**
|
|
|
|
* Register the hooks of all applications (used by admin)
|
|
|
|
*/
|
2003-04-28 00:26:04 +02:00
|
|
|
function register_all_hooks()
|
|
|
|
{
|
2003-08-28 16:31:11 +02:00
|
|
|
$SEP = filesystem_separator();
|
|
|
|
|
2005-07-23 13:39:20 +02:00
|
|
|
foreach($GLOBALS['egw_info']['apps'] as $appname => $app)
|
2003-04-28 00:26:04 +02:00
|
|
|
{
|
2005-07-23 13:39:20 +02:00
|
|
|
$f = EGW_SERVER_ROOT . $SEP . $appname . $SEP . 'setup' . $SEP . 'setup.inc.php';
|
2003-04-28 00:26:04 +02:00
|
|
|
if(@file_exists($f))
|
|
|
|
{
|
|
|
|
include($f);
|
|
|
|
$this->register_hooks($appname,$setup_info[$appname]['hooks']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-05-06 15:40:04 +02:00
|
|
|
}
|