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

Last change on this file since 1604 was 1604, checked in by rvelices, 17 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | branch        : BSF (Best So Far)
7// | file          : $Id: functions_plugins.inc.php 1604 2006-11-10 01:10:42Z rvelices $
8// | last update   : $Date: 2006-11-10 01:10:42 +0000 (Fri, 10 Nov 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1604 $
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
155
156function trigger_action($event, $data=null)
157{
158  global $pwg_event_handlers;
159  if ($event!='pre_trigger_event'
160    and $event!='post_trigger_event'
161    and $event!='trigger_action')
162  {// special case for debugging - avoid recursive calls
163    trigger_action('trigger_action',
164        array('event'=>$event, 'data'=>$data) );
165  }
166
167  if ( !isset($pwg_event_handlers[$event]) )
168  {
169    return;
170  }
171  $args = array_slice(func_get_args(), 2);
172
173  foreach ($pwg_event_handlers[$event] as $priority => $handlers)
174  {
175    if ( !is_null($handlers) )
176    {
177      foreach($handlers as $handler)
178      {
179        $all_args = array_merge( array($data), $args);
180        $function_name = $handler['function'];
181        $accepted_args = $handler['accepted_args'];
182
183        if ( $accepted_args == 1 )
184          $the_args = array($data);
185        elseif ( $accepted_args > 1 )
186          $the_args = array_slice($all_args, 0, $accepted_args);
187        elseif ( $accepted_args == 0 )
188          $the_args = NULL;
189        else
190          $the_args = $all_args;
191
192        call_user_func_array($function_name, $the_args);
193      }
194    }
195  }
196}
197
198
199/* Returns an array of plugins defined in the database
200 * @param string $state optional filter on this state
201 * @param string $id optional returns only data about given plugin
202*/
203function get_db_plugins($state='', $id='')
204{
205  $query = '
206SELECT * FROM '.PLUGINS_TABLE;
207  if (!empty($state) or !empty($id) )
208  {
209    $query .= '
210WHERE 1=1';
211    if (!empty($state))
212    {
213      $query .= '
214  AND state="'.$state.'"';
215    }
216    if (!empty($id))
217    {
218      $query .= '
219  AND id="'.$id.'"';
220    }
221  }
222
223  $result = pwg_query($query);
224  $plugins = array();
225  while ($row = mysql_fetch_array($result))
226  {
227    array_push($plugins, $row);
228  }
229  return $plugins;
230}
231
232
233function load_plugin($plugin)
234{
235  $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php';
236  if ( file_exists($file_name) )
237  {
238    include_once( $file_name );
239  }
240}
241
242/*loads all the plugins on startup*/
243function load_plugins()
244{
245  global $conf;
246  if ($conf['disable_plugins'])
247  {
248    return;
249  }
250
251  $plugins = get_db_plugins('active');
252  foreach( $plugins as $plugin)
253  {// include main from a function to avoid using same function context
254    load_plugin($plugin);
255  }
256  trigger_action('plugins_loaded');
257}
258?>
Note: See TracBrowser for help on using the repository browser.