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

Last change on this file since 2038 was 1900, checked in by rub, 18 years ago

Apply property svn:eol-style Value: LF

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