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

Last change on this file since 18969 was 12922, checked in by mistic100, 13 years ago

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

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