[2] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare API - Hooks * |
---|
| 4 | * This file written by Dan Kuykendall <seek3r@phpgroupware.org> * |
---|
| 5 | * Allows applications to "hook" into each other * |
---|
| 6 | * Copyright (C) 2000, 2001 Dan Kuykendall * |
---|
| 7 | * -------------------------------------------------------------------------* |
---|
| 8 | * This library is part of the eGroupWare API * |
---|
| 9 | * http://www.egroupware.org/api * |
---|
| 10 | * ------------------------------------------------------------------------ * |
---|
| 11 | * This library is free software; you can redistribute it and/or modify it * |
---|
| 12 | * under the terms of the GNU Lesser General Public License as published by * |
---|
| 13 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
| 14 | * or any later version. * |
---|
| 15 | * This library is distributed in the hope that it will be useful, but * |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
| 18 | * See the GNU Lesser General Public License for more details. * |
---|
| 19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 21 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
| 22 | \**************************************************************************/ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | /*! |
---|
| 26 | @class hooks |
---|
| 27 | @abstract class which gives ability for applications to set and use hooks to communicate with each other |
---|
| 28 | @author Dan Kuykendall |
---|
| 29 | @copyright LGPL |
---|
| 30 | @package phpgwapi |
---|
| 31 | @access public |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | class hooks |
---|
| 35 | { |
---|
| 36 | var $found_hooks = Array(); |
---|
| 37 | var $db = ''; |
---|
| 38 | |
---|
| 39 | function hooks($db='') |
---|
| 40 | { |
---|
| 41 | $this->db = $db ? $db : $GLOBALS['phpgw']->db; // this is to allow setup to set the db |
---|
| 42 | |
---|
| 43 | $this->db->query("SELECT hook_appname, hook_location, hook_filename FROM phpgw_hooks",__LINE__,__FILE__); |
---|
| 44 | while( $this->db->next_record() ) |
---|
| 45 | { |
---|
| 46 | $this->found_hooks[$this->db->f('hook_appname')][$this->db->f('hook_location')] = $this->db->f('hook_filename'); |
---|
| 47 | } |
---|
| 48 | //echo '<pre>'; |
---|
| 49 | //print_r($this->found_hooks); |
---|
| 50 | //echo '</pre>'; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /*! |
---|
| 54 | @function process |
---|
| 55 | @abstract executes all the hooks (the user has rights to) for a given location |
---|
| 56 | @syntax process($args,$order='',$no_permission_check = False) |
---|
| 57 | @param $args location-name as string or array: |
---|
| 58 | @param $args['location'] location-name |
---|
| 59 | @param $order or $args['order'] array of appnames (as value), which should be executes first |
---|
| 60 | @param $args is passed to the hook, if its a new method-hook |
---|
| 61 | @param $no_permission_check if True execute all hooks, not only the ones a user has rights to |
---|
| 62 | @note $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo) |
---|
| 63 | @returns array with results of each hook call (with appname as key): \ |
---|
| 64 | False if no hook exists, True if old hook exists \ |
---|
| 65 | and whatever the new method-hook returns (can be True or False too!). |
---|
| 66 | */ |
---|
| 67 | function process($args, $order = '', $no_permission_check = False) |
---|
| 68 | { |
---|
| 69 | //echo "<p>hooks::process("; print_r($args); echo ")</p>\n"; |
---|
| 70 | if ($order == '') |
---|
| 71 | { |
---|
| 72 | $order = is_array($args) && isset($args['order']) ? $args['order'] : |
---|
| 73 | array($GLOBALS['phpgw_info']['flags']['currentapp']); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /* First include the ordered apps hook file */ |
---|
| 77 | foreach($order as $appname) |
---|
| 78 | { |
---|
| 79 | $results[$appname] = $this->single($args,$appname,$no_permission_check); |
---|
| 80 | |
---|
| 81 | if (!isset($results[$appname])) // happens if the method hook has no return-value |
---|
| 82 | { |
---|
| 83 | $results[$appname] = False; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /* Then add the rest */ |
---|
| 88 | if ($no_permission_check) |
---|
| 89 | { |
---|
| 90 | $apps = $GLOBALS['phpgw_info']['apps']; |
---|
| 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | $apps = $GLOBALS['phpgw_info']['user']['apps']; |
---|
| 95 | } |
---|
| 96 | settype($apps,'array'); |
---|
| 97 | foreach($apps as $app) |
---|
| 98 | { |
---|
| 99 | $appname = $app['name']; |
---|
| 100 | if (!isset($results[$appname])) |
---|
| 101 | { |
---|
| 102 | $results[$appname] = $this->single($args,$appname,$no_permission_check); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | return $results; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /*! |
---|
| 109 | @function single |
---|
| 110 | @abstract executes a single hook of a given location and application |
---|
| 111 | @syntax single($args,$appname='',$no_permission_check = False) |
---|
| 112 | @param $args location-name as string or array: |
---|
| 113 | @param $args['location'] location-name |
---|
| 114 | @param $appname or $args['appname'] name of the app, which's hook to execute, if empty the current app is used |
---|
| 115 | @param $args is passed to the hook, if its a new method-hook |
---|
| 116 | @param $no_permission_check if True execute all hooks, not only the ones a user has rights to |
---|
| 117 | @param $try_unregisterd If true, try to include old file-hook anyway (for setup) |
---|
| 118 | @note $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo) |
---|
| 119 | @returns False if no hook exists, True if an old hook exist and whatever the new method-hook returns |
---|
| 120 | */ |
---|
| 121 | function single($args, $appname = '', $no_permission_check = False,$try_unregistered = False) |
---|
| 122 | { |
---|
| 123 | //echo "<p>hooks::single("; print_r($args); echo ",'$appname','$no_permission_check','$try_unregistered')</p>\n"; |
---|
| 124 | if (is_array($args)) |
---|
| 125 | { |
---|
| 126 | $location = $args['location']; |
---|
| 127 | } |
---|
| 128 | else |
---|
| 129 | { |
---|
| 130 | $location = $args; |
---|
| 131 | } |
---|
| 132 | if (!$appname) |
---|
| 133 | { |
---|
| 134 | $appname = is_array($args) && isset($args['appname']) ? $args['appname'] : $GLOBALS['phpgw_info']['flags']['currentapp']; |
---|
| 135 | } |
---|
| 136 | $SEP = filesystem_separator(); |
---|
| 137 | |
---|
| 138 | /* First include the ordered apps hook file */ |
---|
| 139 | if (isset($this->found_hooks[$appname][$location]) || $try_unregistered) |
---|
| 140 | { |
---|
| 141 | $parts = explode('.',$method = $this->found_hooks[$appname][$location]); |
---|
| 142 | |
---|
| 143 | if (count($parts) != 3 || ($parts[1] == 'inc' && $parts[2] == 'php')) |
---|
| 144 | { |
---|
| 145 | if ($try_unregistered && empty($method)) |
---|
| 146 | { |
---|
| 147 | $method = 'hook_'.$location.'.inc.php'; |
---|
| 148 | } |
---|
| 149 | $f = PHPGW_SERVER_ROOT . $SEP . $appname . $SEP . 'inc' . $SEP . $method; |
---|
| 150 | if (file_exists($f) && |
---|
| 151 | ( $GLOBALS['phpgw_info']['user']['apps'][$appname] || (($no_permission_check || $location == 'config' || $appname == 'phpgwapi') && $appname)) ) |
---|
| 152 | { |
---|
| 153 | include($f); |
---|
| 154 | return True; |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | return False; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | else // new style method-hook |
---|
| 162 | { |
---|
| 163 | return ExecMethod($method,$args); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | { |
---|
| 168 | return False; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /*! |
---|
| 173 | @function count |
---|
| 174 | @abstract loop through the applications and count the hooks |
---|
| 175 | */ |
---|
| 176 | function count($location) |
---|
| 177 | { |
---|
| 178 | $count = 0; |
---|
| 179 | foreach($GLOBALS['phpgw_info']['user']['apps'] as $appname => $data) |
---|
| 180 | { |
---|
| 181 | if (isset($this->found_hooks[$appname][$location])) |
---|
| 182 | { |
---|
| 183 | ++$count; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | return $count; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /*! |
---|
| 190 | @function read() |
---|
| 191 | @abstract currently not being used |
---|
| 192 | */ |
---|
| 193 | function read() |
---|
| 194 | { |
---|
| 195 | //if (!is_array($this->found_hooks)) |
---|
| 196 | //{ |
---|
| 197 | $this->hooks(); |
---|
| 198 | //} |
---|
| 199 | return $this->found_hooks; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /*! |
---|
| 203 | @function register_hooks |
---|
| 204 | @abstract Register and/or de-register an application's hooks |
---|
| 205 | @syntax register_hooks($appname,$hooks='') |
---|
| 206 | @param $appname Application 'name' |
---|
| 207 | @param $hooks array with hooks to register, eg $setup_info[$app]['hooks'] or not used for only deregister the hooks |
---|
| 208 | */ |
---|
| 209 | function register_hooks($appname,$hooks='') |
---|
| 210 | { |
---|
| 211 | if(!$appname) |
---|
| 212 | { |
---|
| 213 | return False; |
---|
| 214 | } |
---|
| 215 | $db_appname = $this->db->db_addslashes($appname); |
---|
| 216 | $this->db->query("DELETE FROM phpgw_hooks WHERE hook_appname='$db_appname'",__LINE__,__FILE__); |
---|
| 217 | |
---|
| 218 | if (!is_array($hooks)) // only deregister |
---|
| 219 | { |
---|
| 220 | return True; |
---|
| 221 | } |
---|
| 222 | //echo "<p>ADDING hooks for: $appname</p>"; |
---|
| 223 | foreach($hooks as $key => $hook) |
---|
| 224 | { |
---|
| 225 | if (!is_numeric($key)) // new method-hook |
---|
| 226 | { |
---|
| 227 | $location = $key; |
---|
| 228 | $filename = $hook; |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | $location = $hook; |
---|
| 233 | $filename = "hook_$hook.inc.php"; |
---|
| 234 | } |
---|
| 235 | $this->db->query("INSERT INTO phpgw_hooks (hook_appname,hook_location,hook_filename)". |
---|
| 236 | " VALUES ('$appname','$location','$filename');"); |
---|
| 237 | } |
---|
| 238 | return True; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | /*! |
---|
| 243 | @function register_all_hooks |
---|
| 244 | @abstract Register the hooks of all applications (used by admin) |
---|
| 245 | */ |
---|
| 246 | function register_all_hooks() |
---|
| 247 | { |
---|
| 248 | $SEP = filesystem_separator(); |
---|
| 249 | |
---|
| 250 | foreach($GLOBALS['phpgw_info']['apps'] as $appname => $app) |
---|
| 251 | { |
---|
| 252 | $f = PHPGW_SERVER_ROOT . $SEP . $appname . $SEP . 'setup' . $SEP . 'setup.inc.php'; |
---|
| 253 | if(@file_exists($f)) |
---|
| 254 | { |
---|
| 255 | include($f); |
---|
| 256 | $this->register_hooks($appname,$setup_info[$appname]['hooks']); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | ?> |
---|