source: branches/2.3/phpgwapi/inc/class.hooks.inc.php @ 370

Revision 370, 8.4 KB checked in by niltonneto, 16 years ago (diff)

Ver ocorrência #270 do Trac.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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                        //jakjr: Go search in DB, only if the array is empty.
44                        if (empty($this->found_hooks))
45                        {
46                                $this->db->query("SELECT hook_appname, hook_location, hook_filename FROM phpgw_hooks",__LINE__,__FILE__);
47                                while( $this->db->next_record() )
48                                {
49                                        $this->found_hooks[$this->db->f('hook_appname')][$this->db->f('hook_location')] = $this->db->f('hook_filename');
50                                }
51                                //echo '<pre>';
52                                //print_r($this->found_hooks);
53                                //echo '</pre>';
54                        }
55                }
56               
57                /*!
58                @function process
59                @abstract executes all the hooks (the user has rights to) for a given location
60                @syntax process($args,$order='',$no_permission_check = False)
61                @param $args location-name as string or array:
62                @param $args['location'] location-name
63                @param $order or $args['order'] array of appnames (as value), which should be executes first
64                @param $args is passed to the hook, if its a new method-hook
65                @param $no_permission_check if True execute all hooks, not only the ones a user has rights to
66                @note $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo)
67                @returns array with results of each hook call (with appname as key): \
68                        False if no hook exists, True if old hook exists \
69                        and whatever the new method-hook returns (can be True or False too!).
70                */
71                function process($args, $order = '', $no_permission_check = False)
72                {
73                        //echo "<p>hooks::process("; print_r($args); echo ")</p>\n";
74                        if ($order == '')
75                        {
76                                $order = is_array($args) && isset($args['order']) ? $args['order'] :
77                                        array($GLOBALS['phpgw_info']['flags']['currentapp']);
78                        }
79
80                        /* First include the ordered apps hook file */
81                        foreach($order as $appname)
82                        {
83                                $results[$appname] = $this->single($args,$appname,$no_permission_check);
84
85                                if (!isset($results[$appname])) // happens if the method hook has no return-value
86                                {
87                                        $results[$appname] = False;
88                                }
89                        }
90
91                        /* Then add the rest */
92                        if ($no_permission_check)
93                        {
94                                $apps = $GLOBALS['phpgw_info']['apps'];
95                        }
96                        else
97                        {
98                                $apps = $GLOBALS['phpgw_info']['user']['apps'];
99                        }
100                        settype($apps,'array');
101                        foreach($apps as $app)
102                        {
103                                $appname = $app['name'];
104                                if (!isset($results[$appname]))
105                                {
106                                        $results[$appname] = $this->single($args,$appname,$no_permission_check);
107                                }
108                        }
109                        return $results;
110                }
111
112                /*!
113                @function single
114                @abstract executes a single hook of a given location and application
115                @syntax single($args,$appname='',$no_permission_check = False)
116                @param $args location-name as string or array:
117                @param $args['location'] location-name
118                @param $appname or $args['appname'] name of the app, which's hook to execute, if empty the current app is used
119                @param $args is passed to the hook, if its a new method-hook
120                @param $no_permission_check if True execute all hooks, not only the ones a user has rights to
121                @param $try_unregisterd If true, try to include old file-hook anyway (for setup)
122                @note $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo)
123                @returns False if no hook exists, True if an old hook exist and whatever the new method-hook returns
124                */
125                function single($args, $appname = '', $no_permission_check = False,$try_unregistered = False)
126                {
127                        //echo "<p>hooks::single("; print_r($args); echo ",'$appname','$no_permission_check','$try_unregistered')</p>\n";
128                        if (is_array($args))
129                        {
130                                $location = $args['location'];
131                        }
132                        else
133                        {
134                                $location = $args;
135                        }
136                        if (!$appname)
137                        {
138                                $appname = is_array($args) && isset($args['appname']) ? $args['appname'] : $GLOBALS['phpgw_info']['flags']['currentapp'];
139                        }
140                        $SEP = filesystem_separator();
141
142                        /* First include the ordered apps hook file */
143                        if (isset($this->found_hooks[$appname][$location]) || $try_unregistered)
144                        {
145                                $parts = explode('.',$method = $this->found_hooks[$appname][$location]);
146                               
147                                if (count($parts) != 3 || ($parts[1] == 'inc' && $parts[2] == 'php'))
148                                {
149                                        if ($try_unregistered && empty($method))
150                                        {
151                                                $method = 'hook_'.$location.'.inc.php';
152                                        }
153                                        $f = PHPGW_SERVER_ROOT . $SEP . $appname . $SEP . 'inc' . $SEP . $method;
154                                        if (file_exists($f) &&
155                                                ( $GLOBALS['phpgw_info']['user']['apps'][$appname] || (($no_permission_check || $location == 'config' || $appname == 'phpgwapi') && $appname)) )
156                                        {
157                                                include($f);
158                                                return True;
159                                        }
160                                        else
161                                        {
162                                                return False;
163                                        }
164                                }
165                                else    // new style method-hook
166                                {
167                                        return ExecMethod($method,$args);
168                                }
169                        }
170                        else
171                        {
172                                return False;
173                        }
174                }
175
176                /*!
177                @function count
178                @abstract loop through the applications and count the hooks
179                */
180                function count($location)
181                {
182                        $count = 0;
183                        foreach($GLOBALS['phpgw_info']['user']['apps'] as $appname => $data)
184                        {
185                                if (isset($this->found_hooks[$appname][$location]))
186                                {
187                                                ++$count;
188                                }
189                        }
190                        return $count;
191                }
192               
193                /*!
194                @function read()
195                @abstract currently not being used
196                */
197                function read()
198                {
199                        //if (!is_array($this->found_hooks))
200                        //{
201                                $this->hooks();
202                        //}
203                        return $this->found_hooks;
204                }
205
206                /*!
207                @function register_hooks
208                @abstract Register and/or de-register an application's hooks
209                @syntax register_hooks($appname,$hooks='')
210                @param $appname Application 'name'
211                @param $hooks array with hooks to register, eg $setup_info[$app]['hooks'] or not used for only deregister the hooks
212                */
213                function register_hooks($appname,$hooks='')
214                {
215                        if(!$appname)
216                        {
217                                return False;
218                        }
219                        $db_appname = $this->db->db_addslashes($appname);
220                        $this->db->query("DELETE FROM phpgw_hooks WHERE hook_appname='$db_appname'",__LINE__,__FILE__);
221
222                        if (!is_array($hooks))  // only deregister
223                        {
224                                return True;
225                        }
226                        //echo "<p>ADDING hooks for: $appname</p>";
227                        foreach($hooks as $key => $hook)
228                        {
229                                if (!is_numeric($key))  // new method-hook
230                                {
231                                        $location = $key;
232                                        $filename = $hook;
233                                }
234                                else
235                                {
236                                        $location = $hook;
237                                        $filename = "hook_$hook.inc.php";
238                                }
239                                $this->db->query("INSERT INTO phpgw_hooks (hook_appname,hook_location,hook_filename)".
240                                        " VALUES ('$appname','$location','$filename');");
241                        }
242                        return True;
243                }
244
245               
246                /*!
247                @function register_all_hooks
248                @abstract Register the hooks of all applications (used by admin)
249                */
250                function register_all_hooks()
251                {
252                        $SEP = filesystem_separator();
253                       
254                        foreach($GLOBALS['phpgw_info']['apps'] as $appname => $app)
255                        {                       
256                                $f = PHPGW_SERVER_ROOT . $SEP . $appname . $SEP . 'setup' . $SEP . 'setup.inc.php';
257                                if(@file_exists($f))
258                                {
259                                        include($f);
260                                        $this->register_hooks($appname,$setup_info[$appname]['hooks']);
261                                }
262                        }
263                }
264        }
265?>
Note: See TracBrowser for help on using the repository browser.