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

Last change on this file since 2339 was 2339, checked in by rub, 16 years ago

Change some PhpWebGallery to Piwigo.
Not all PhpWebGallery has been translated!

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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  // just for debugging
113  trigger_action('pre_trigger_event',
114        array('event'=>$event, 'data'=>$data) );
115
116  if ( !isset($pwg_event_handlers[$event]) )
117  {
118    trigger_action('post_trigger_event',
119        array('event'=>$event, 'data'=>$data) );
120    return $data;
121  }
122  $args = array_slice(func_get_args(), 2);
123
124  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
125  {
126    if ( !is_null($handlers) )
127    {
128      foreach($handlers as $handler)
129      {
130        $all_args = array_merge( array($data), $args);
131        $function_name = $handler['function'];
132        $accepted_args = $handler['accepted_args'];
133
134        if ( $accepted_args == 1 )
135          $the_args = array($data);
136        elseif ( $accepted_args > 1 )
137          $the_args = array_slice($all_args, 0, $accepted_args);
138        elseif ( $accepted_args == 0 )
139          $the_args = NULL;
140        else
141          $the_args = $all_args;
142
143        $data = call_user_func_array($function_name, $the_args);
144      }
145    }
146  }
147  trigger_action('post_trigger_event',
148        array('event'=>$event, 'data'=>$data) );
149  return $data;
150}
151
152function trigger_action($event, $data=null)
153{
154  global $pwg_event_handlers;
155  if ($event!='pre_trigger_event'
156    and $event!='post_trigger_event'
157    and $event!='trigger_action')
158  {// special case for debugging - avoid recursive calls
159    trigger_action('trigger_action',
160        array('event'=>$event, 'data'=>$data) );
161  }
162
163  if ( !isset($pwg_event_handlers[$event]) )
164  {
165    return;
166  }
167  $args = array_slice(func_get_args(), 2);
168
169  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
170  {
171    if ( !is_null($handlers) )
172    {
173      foreach($handlers as $handler)
174      {
175        $all_args = array_merge( array($data), $args);
176        $function_name = $handler['function'];
177        $accepted_args = $handler['accepted_args'];
178
179        if ( $accepted_args == 1 )
180          $the_args = array($data);
181        elseif ( $accepted_args > 1 )
182          $the_args = array_slice($all_args, 0, $accepted_args);
183        elseif ( $accepted_args == 0 )
184          $the_args = NULL;
185        else
186          $the_args = $all_args;
187
188        call_user_func_array($function_name, $the_args);
189      }
190    }
191  }
192}
193
194/** Saves some data with the associated plugim id. It can be retrieved later (
195 * during this script lifetime) using get_plugin_data
196 * @param string plugin_id
197 * @param mixed data
198 * returns true on success, false otherwise
199 */
200function set_plugin_data($plugin_id, &$data)
201{
202  global $pwg_loaded_plugins;
203  if ( isset($pwg_loaded_plugins[$plugin_id]) )
204  {
205    $pwg_loaded_plugins[$plugin_id]['plugin_data'] = &$data;
206    return true;
207  }
208  return false;
209}
210
211/** Retrieves plugin data saved previously with set_plugin_data
212 * @param string plugin_id
213 */
214function &get_plugin_data($plugin_id)
215{
216  global $pwg_loaded_plugins;
217  if ( isset($pwg_loaded_plugins[$plugin_id]) )
218  {
219    return $pwg_loaded_plugins[$plugin_id]['plugin_data'];
220  }
221  return null;
222}
223
224/* Returns an array of plugins defined in the database
225 * @param string $state optional filter on this state
226 * @param string $id optional returns only data about given plugin
227*/
228function get_db_plugins($state='', $id='')
229{
230  $query = '
231SELECT * FROM '.PLUGINS_TABLE;
232  if (!empty($state) or !empty($id) )
233  {
234    $query .= '
235WHERE 1=1';
236    if (!empty($state))
237    {
238      $query .= '
239  AND state="'.$state.'"';
240    }
241    if (!empty($id))
242    {
243      $query .= '
244  AND id="'.$id.'"';
245    }
246  }
247
248  $result = pwg_query($query);
249  $plugins = array();
250  while ($row = mysql_fetch_array($result))
251  {
252    array_push($plugins, $row);
253  }
254  return $plugins;
255}
256
257
258function load_plugin($plugin)
259{
260  $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php';
261  if ( file_exists($file_name) )
262  {
263    global $pwg_loaded_plugins;
264    $pwg_loaded_plugins[ $plugin['id'] ] = $plugin;
265    include_once( $file_name );
266  }
267}
268
269/*loads all the plugins on startup*/
270function load_plugins()
271{
272  global $conf, $pwg_loaded_plugins;
273  $pwg_loaded_plugins = array();
274  if ($conf['enable_plugins'])
275  {
276    $plugins = get_db_plugins('active');
277    foreach( $plugins as $plugin)
278    {// include main from a function to avoid using same function context
279      load_plugin($plugin);
280    }
281    trigger_action('plugins_loaded');
282  }
283}
284?>
Note: See TracBrowser for help on using the repository browser.