source: trunk/include/functions_plugins.inc.php @ 5196

Last change on this file since 5196 was 5196, checked in by plg, 15 years ago

increase copyright year to 2010

File size: 8.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/*
25Events and event handlers are the core of Piwigo plugin management.
26Plugins are addons that are found in plugins subdirectory. If activated, PWG
27will include the index.php of each plugin.
28Events are triggered by PWG core code. Plugins (or even PWG itself) can
29register their functions to handle these events. An event is identified by a
30string.
31*/
32
33define('PHPWG_PLUGINS_PATH', PHPWG_ROOT_PATH.'plugins/');
34
35define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50);
36
37/* Register a event handler.
38 * @param string $event the name of the event to listen to
39 * @param mixed $func the function that will handle the event
40 * @param int $priority optional priority (greater priority will
41 * be executed at last)
42*/
43function add_event_handler($event, $func,
44    $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1)
45{
46  global $pwg_event_handlers;
47
48  if ( isset($pwg_event_handlers[$event][$priority]) )
49  {
50    foreach($pwg_event_handlers[$event][$priority] as $handler)
51    {
52      if ( $handler['function'] == $func )
53      {
54        return false;
55      }
56    }
57  }
58
59  $pwg_event_handlers[$event][$priority][] =
60    array(
61      'function'=>$func,
62      'accepted_args'=>$accepted_args);
63  ksort( $pwg_event_handlers[$event] );
64  return true;
65}
66
67/* Register a event handler.
68 * @param string $event the name of the event to listen to
69 * @param mixed $func the function that needs removal
70 * @param int $priority optional priority (greater priority will
71 * be executed at last)
72*/
73function remove_event_handler($event, $func,
74   $priority=EVENT_HANDLER_PRIORITY_NEUTRAL)
75{
76  global $pwg_event_handlers;
77
78  if (!isset( $pwg_event_handlers[$event][$priority] ) )
79  {
80    return false;
81  }
82  for ($i=0; $i<count($pwg_event_handlers[$event][$priority]); $i++)
83  {
84    if ($pwg_event_handlers[$event][$priority][$i]['function']==$func)
85    {
86      unset($pwg_event_handlers[$event][$priority][$i]);
87      $pwg_event_handlers[$event][$priority] =
88        array_values($pwg_event_handlers[$event][$priority]);
89
90      if ( empty($pwg_event_handlers[$event][$priority]) )
91      {
92        unset( $pwg_event_handlers[$event][$priority] );
93        if (empty( $pwg_event_handlers[$event] ) )
94        {
95          unset( $pwg_event_handlers[$event] );
96        }
97      }
98      return true;
99    }
100  }
101  return false;
102}
103
104/* Triggers an event and calls all registered event handlers
105 * @param string $event name of the event
106 * @param mixed $data data to pass to handlers
107*/
108function trigger_event($event, $data=null)
109{
110  global $pwg_event_handlers;
111
112  if ( isset($pwg_event_handlers['trigger']) )
113  {// just for debugging
114    trigger_action('trigger',
115        array('type'=>'event', 'event'=>$event, 'data'=>$data) );
116  }
117
118  if ( !isset($pwg_event_handlers[$event]) )
119  {
120    return $data;
121  }
122  $args = array_slice(func_get_args(), 2);
123
124  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
125  {
126    foreach($handlers as $handler)
127    {
128      $all_args = array_merge( array($data), $args);
129      $function_name = $handler['function'];
130      $accepted_args = $handler['accepted_args'];
131
132      if ( $accepted_args == 1 )
133        $the_args = array($data);
134      elseif ( $accepted_args > 1 )
135        $the_args = array_slice($all_args, 0, $accepted_args);
136      elseif ( $accepted_args == 0 )
137        $the_args = NULL;
138      else
139        $the_args = $all_args;
140
141      $data = call_user_func_array($function_name, $the_args);
142    }
143  }
144  trigger_action('trigger',
145       array('type'=>'post_event', 'event'=>$event, 'data'=>$data) );
146  return $data;
147}
148
149function trigger_action($event, $data=null)
150{
151  global $pwg_event_handlers;
152  if ( isset($pwg_event_handlers['trigger']) and $event!='trigger' )
153  {// special case for debugging - avoid recursive calls
154    trigger_action('trigger',
155        array('type'=>'action', 'event'=>$event, 'data'=>$data) );
156  }
157
158  if ( !isset($pwg_event_handlers[$event]) )
159  {
160    return;
161  }
162  $args = array_slice(func_get_args(), 2);
163
164  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
165  {
166    foreach($handlers as $handler)
167    {
168      $all_args = array_merge( array($data), $args);
169      $function_name = $handler['function'];
170      $accepted_args = $handler['accepted_args'];
171
172      if ( $accepted_args == 1 )
173        $the_args = array($data);
174      elseif ( $accepted_args > 1 )
175        $the_args = array_slice($all_args, 0, $accepted_args);
176      elseif ( $accepted_args == 0 )
177        $the_args = NULL;
178      else
179        $the_args = $all_args;
180
181      call_user_func_array($function_name, $the_args);
182    }
183  }
184}
185
186/** Saves some data with the associated plugim id. It can be retrieved later (
187 * during this script lifetime) using get_plugin_data
188 * @param string plugin_id
189 * @param mixed data
190 * returns true on success, false otherwise
191 */
192function set_plugin_data($plugin_id, &$data)
193{
194  global $pwg_loaded_plugins;
195  if ( isset($pwg_loaded_plugins[$plugin_id]) )
196  {
197    $pwg_loaded_plugins[$plugin_id]['plugin_data'] = &$data;
198    return true;
199  }
200  return false;
201}
202
203/** Retrieves plugin data saved previously with set_plugin_data
204 * @param string plugin_id
205 */
206function &get_plugin_data($plugin_id)
207{
208  global $pwg_loaded_plugins;
209  if ( isset($pwg_loaded_plugins[$plugin_id]) )
210  {
211    return $pwg_loaded_plugins[$plugin_id]['plugin_data'];
212  }
213  return null;
214}
215
216/* Returns an array of plugins defined in the database
217 * @param string $state optional filter on this state
218 * @param string $id optional returns only data about given plugin
219*/
220function get_db_plugins($state='', $id='')
221{
222  $query = '
223SELECT * FROM '.PLUGINS_TABLE;
224  $clauses = array();
225  if (!empty($state))
226  {
227    $clauses[] = 'state=\''.$state.'\'';
228  }
229  if (!empty($id))
230  {
231    $clauses[] = 'id="'.$id.'"';
232  }
233  if (count($clauses))
234  {
235      $query .= '
236  WHERE '. implode(' AND ', $clauses);
237  }
238
239  $result = pwg_query($query);
240  $plugins = array();
241  while ($row = pwg_db_fetch_assoc($result))
242  {
243    array_push($plugins, $row);
244  }
245  return $plugins;
246}
247
248
249function load_plugin($plugin)
250{
251  $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php';
252  if ( file_exists($file_name) )
253  {
254    global $pwg_loaded_plugins;
255    $pwg_loaded_plugins[ $plugin['id'] ] = $plugin;
256    include_once( $file_name );
257  }
258}
259
260/*loads all the plugins on startup*/
261function load_plugins()
262{
263  global $conf, $pwg_loaded_plugins;
264  $pwg_loaded_plugins = array();
265  if ($conf['enable_plugins'])
266  {
267    $plugins = get_db_plugins('active');
268    foreach( $plugins as $plugin)
269    {// include main from a function to avoid using same function context
270      load_plugin($plugin);
271    }
272    trigger_action('plugins_loaded');
273  }
274}
275?>
Note: See TracBrowser for help on using the repository browser.