1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 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 | /** |
---|
25 | * @package functions\plugins |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | /** base directory of plugins */ |
---|
30 | define('PHPWG_PLUGINS_PATH', PHPWG_ROOT_PATH.'plugins/'); |
---|
31 | /** default priority for plugins handlers */ |
---|
32 | define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50); |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * Used to declare maintenance methods of a plugin. |
---|
37 | */ |
---|
38 | abstract class PluginMaintain |
---|
39 | { |
---|
40 | /** @var string $plugin_id */ |
---|
41 | protected $plugin_id; |
---|
42 | |
---|
43 | /** |
---|
44 | * @param string $id |
---|
45 | */ |
---|
46 | function __construct($id) |
---|
47 | { |
---|
48 | $this->plugin_id = $id; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * @param string $plugin_version |
---|
53 | * @param array &$errors - used to return error messages |
---|
54 | */ |
---|
55 | abstract function install($plugin_version, &$errors=array()); |
---|
56 | |
---|
57 | /** |
---|
58 | * @param string $plugin_version |
---|
59 | * @param array &$errors - used to return error messages |
---|
60 | */ |
---|
61 | abstract function activate($plugin_version, &$errors=array()); |
---|
62 | |
---|
63 | abstract function deactivate(); |
---|
64 | |
---|
65 | abstract function uninstall(); |
---|
66 | |
---|
67 | /** |
---|
68 | * Tests if the plugin needs to be updated and call an update function |
---|
69 | * |
---|
70 | * @param string $version version exposed by the plugin (potentially new) |
---|
71 | * @param string $on_update name of a method to call when an update is needed |
---|
72 | * it receives the previous version as first parameter |
---|
73 | */ |
---|
74 | function autoUpdate($version, $on_update=null) |
---|
75 | { |
---|
76 | global $pwg_loaded_plugins; |
---|
77 | |
---|
78 | $current_version = $pwg_loaded_plugins[$this->plugin_id]['version']; |
---|
79 | |
---|
80 | if ( $version == 'auto' or $current_version == 'auto' |
---|
81 | or safe_version_compare($current_version, $version, '<') |
---|
82 | ) |
---|
83 | { |
---|
84 | if (!empty($on_update)) |
---|
85 | { |
---|
86 | call_user_func(array(&$this, $on_update), $current_version); |
---|
87 | } |
---|
88 | |
---|
89 | if ($version != 'auto') |
---|
90 | { |
---|
91 | $query = ' |
---|
92 | UPDATE '. PLUGINS_TABLE .' |
---|
93 | SET version = "'. $version .'" |
---|
94 | WHERE id = "'. $this->plugin_id .'" |
---|
95 | ;'; |
---|
96 | pwg_query($query); |
---|
97 | |
---|
98 | $pwg_loaded_plugins[$this->plugin_id]['version'] = $version; |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Used to declare maintenance methods of a theme. |
---|
106 | */ |
---|
107 | abstract class ThemeMaintain |
---|
108 | { |
---|
109 | /** @var string $theme_id */ |
---|
110 | protected $theme_id; |
---|
111 | |
---|
112 | /** |
---|
113 | * @param string $id |
---|
114 | */ |
---|
115 | function __construct($id) |
---|
116 | { |
---|
117 | $this->theme_id = $id; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * @param string $theme_version |
---|
122 | * @param array &$errors - used to return error messages |
---|
123 | */ |
---|
124 | abstract function activate($theme_version, &$errors=array()); |
---|
125 | |
---|
126 | abstract function deactivate(); |
---|
127 | |
---|
128 | abstract function delete(); |
---|
129 | |
---|
130 | /** |
---|
131 | * Tests if the theme needs to be updated and call an update function |
---|
132 | * |
---|
133 | * @param string $version version exposed by the theme (potentially new) |
---|
134 | * @param string $on_update name of a method to call when an update is needed |
---|
135 | * it receives the previous version as first parameter |
---|
136 | */ |
---|
137 | function autoUpdate($version, $on_update=null) |
---|
138 | { |
---|
139 | $query = ' |
---|
140 | SELECT version |
---|
141 | FROM '. THEMES_TABLE .' |
---|
142 | WHERE id = "'. $this->theme_id .'" |
---|
143 | ;'; |
---|
144 | list($current_version) = pwg_db_fetch_row(pwg_query($query)); |
---|
145 | |
---|
146 | if ( $version == 'auto' or $current_version == 'auto' |
---|
147 | or safe_version_compare($current_version, $version, '<') |
---|
148 | ) |
---|
149 | { |
---|
150 | if (!empty($on_update)) |
---|
151 | { |
---|
152 | call_user_func(array(&$this, $on_update), $current_version); |
---|
153 | } |
---|
154 | |
---|
155 | if ($version != 'auto') |
---|
156 | { |
---|
157 | $query = ' |
---|
158 | UPDATE '. THEMES_TABLE .' |
---|
159 | SET version = "'. $version .'" |
---|
160 | WHERE id = "'. $this->theme_id .'" |
---|
161 | ;'; |
---|
162 | pwg_query($query); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | * Register an event handler. |
---|
171 | * |
---|
172 | * @param string $event the name of the event to listen to |
---|
173 | * @param Callable $func the callback function |
---|
174 | * @param int $priority greater priority will be executed at last |
---|
175 | */ |
---|
176 | function add_event_handler($event, $func, |
---|
177 | $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1) |
---|
178 | { |
---|
179 | global $pwg_event_handlers; |
---|
180 | |
---|
181 | if ( isset($pwg_event_handlers[$event][$priority]) ) |
---|
182 | { |
---|
183 | foreach($pwg_event_handlers[$event][$priority] as $handler) |
---|
184 | { |
---|
185 | if ( $handler['function'] == $func ) |
---|
186 | { |
---|
187 | return false; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | $pwg_event_handlers[$event][$priority][] = |
---|
193 | array( |
---|
194 | 'function'=>$func, |
---|
195 | 'accepted_args'=>$accepted_args); |
---|
196 | ksort( $pwg_event_handlers[$event] ); |
---|
197 | return true; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Removes an event handler. |
---|
202 | * @see add_event_handler() |
---|
203 | * |
---|
204 | * @param string $event |
---|
205 | * @param Callable $func |
---|
206 | * @param int $priority |
---|
207 | */ |
---|
208 | function remove_event_handler($event, $func, |
---|
209 | $priority=EVENT_HANDLER_PRIORITY_NEUTRAL) |
---|
210 | { |
---|
211 | global $pwg_event_handlers; |
---|
212 | |
---|
213 | if (!isset( $pwg_event_handlers[$event][$priority] ) ) |
---|
214 | { |
---|
215 | return false; |
---|
216 | } |
---|
217 | for ($i=0; $i<count($pwg_event_handlers[$event][$priority]); $i++) |
---|
218 | { |
---|
219 | if ($pwg_event_handlers[$event][$priority][$i]['function']==$func) |
---|
220 | { |
---|
221 | unset($pwg_event_handlers[$event][$priority][$i]); |
---|
222 | $pwg_event_handlers[$event][$priority] = |
---|
223 | array_values($pwg_event_handlers[$event][$priority]); |
---|
224 | |
---|
225 | if ( empty($pwg_event_handlers[$event][$priority]) ) |
---|
226 | { |
---|
227 | unset( $pwg_event_handlers[$event][$priority] ); |
---|
228 | if (empty( $pwg_event_handlers[$event] ) ) |
---|
229 | { |
---|
230 | unset( $pwg_event_handlers[$event] ); |
---|
231 | } |
---|
232 | } |
---|
233 | return true; |
---|
234 | } |
---|
235 | } |
---|
236 | return false; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Triggers a modifier event and calls all registered event handlers. |
---|
241 | * trigger_change() is used as a modifier: it allows to transmit _$data_ |
---|
242 | * through all handlers, thus each handler MUST return a value, |
---|
243 | * optional _$args_ are not transmitted. |
---|
244 | * |
---|
245 | * @since 2.6 |
---|
246 | * @todo remove trigger_event() |
---|
247 | * |
---|
248 | * @param string $event |
---|
249 | * @param mixed $data data to transmit to all handlers |
---|
250 | * @param mixed $args,... optional arguments |
---|
251 | * @return mixed $data |
---|
252 | */ |
---|
253 | function trigger_change($event, $data=null) |
---|
254 | { |
---|
255 | $args = func_get_args(); |
---|
256 | return call_user_func_array('trigger_event', $args); |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * @deprecated 2.6 |
---|
261 | * @see trigger_change |
---|
262 | */ |
---|
263 | function trigger_event($event, $data=null) |
---|
264 | { |
---|
265 | global $pwg_event_handlers; |
---|
266 | |
---|
267 | if ( isset($pwg_event_handlers['trigger']) ) |
---|
268 | {// just for debugging |
---|
269 | trigger_action('trigger', |
---|
270 | array('type'=>'event', 'event'=>$event, 'data'=>$data) ); |
---|
271 | } |
---|
272 | |
---|
273 | if ( !isset($pwg_event_handlers[$event]) ) |
---|
274 | { |
---|
275 | return $data; |
---|
276 | } |
---|
277 | $args = func_get_args(); |
---|
278 | |
---|
279 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
280 | { |
---|
281 | foreach($handlers as $handler) |
---|
282 | { |
---|
283 | $function_name = $handler['function']; |
---|
284 | $accepted_args = $handler['accepted_args']; |
---|
285 | $args[1] = $data; |
---|
286 | $data = call_user_func_array($function_name, array_slice($args,1,$accepted_args) ); |
---|
287 | } |
---|
288 | } |
---|
289 | trigger_action('trigger', |
---|
290 | array('type'=>'post_event', 'event'=>$event, 'data'=>$data) ); |
---|
291 | return $data; |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * Triggers a notifier event and calls all registered event handlers. |
---|
296 | * trigger_notify() is only used as a notifier, no modification of data is possible |
---|
297 | * |
---|
298 | * @since 2.6 |
---|
299 | * @todo remove trigger_action() |
---|
300 | * |
---|
301 | * @param string $event |
---|
302 | * @param mixed $args,... optional arguments |
---|
303 | */ |
---|
304 | function trigger_notify($event) |
---|
305 | { |
---|
306 | $args = func_get_args(); |
---|
307 | return call_user_func_array('trigger_action', $args); |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * @deprecated 2.6 |
---|
312 | * @see trigger_notify |
---|
313 | */ |
---|
314 | function trigger_action($event) |
---|
315 | { |
---|
316 | global $pwg_event_handlers; |
---|
317 | if ( isset($pwg_event_handlers['trigger']) and $event!='trigger' ) |
---|
318 | {// special case for debugging - avoid recursive calls |
---|
319 | trigger_action('trigger', |
---|
320 | array('type'=>'action', 'event'=>$event, 'data'=>null) ); |
---|
321 | } |
---|
322 | |
---|
323 | if ( !isset($pwg_event_handlers[$event]) ) |
---|
324 | { |
---|
325 | return; |
---|
326 | } |
---|
327 | $args = func_get_args(); |
---|
328 | |
---|
329 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
330 | { |
---|
331 | foreach($handlers as $handler) |
---|
332 | { |
---|
333 | $function_name = $handler['function']; |
---|
334 | $accepted_args = $handler['accepted_args']; |
---|
335 | |
---|
336 | call_user_func_array($function_name, array_slice($args,1,$accepted_args) ); |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | /** |
---|
342 | * Saves some data with the associated plugin id, data are only available |
---|
343 | * during script lifetime. |
---|
344 | * @depracted 2.6 |
---|
345 | * |
---|
346 | * @param string $plugin_id |
---|
347 | * @param mixed &$data |
---|
348 | * @return bool |
---|
349 | */ |
---|
350 | function set_plugin_data($plugin_id, &$data) |
---|
351 | { |
---|
352 | global $pwg_loaded_plugins; |
---|
353 | if ( isset($pwg_loaded_plugins[$plugin_id]) ) |
---|
354 | { |
---|
355 | $pwg_loaded_plugins[$plugin_id]['plugin_data'] = &$data; |
---|
356 | return true; |
---|
357 | } |
---|
358 | return false; |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | * Retrieves plugin data saved previously with set_plugin_data. |
---|
363 | * @see set_plugin_data() |
---|
364 | * @depracted 2.6 |
---|
365 | * |
---|
366 | * @param string $plugin_id |
---|
367 | * @return mixed |
---|
368 | */ |
---|
369 | function &get_plugin_data($plugin_id) |
---|
370 | { |
---|
371 | global $pwg_loaded_plugins; |
---|
372 | if ( isset($pwg_loaded_plugins[$plugin_id]['plugin_data']) ) |
---|
373 | { |
---|
374 | return $pwg_loaded_plugins[$plugin_id]['plugin_data']; |
---|
375 | } |
---|
376 | return null; |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * Returns an array of plugins defined in the database. |
---|
381 | * |
---|
382 | * @param string $state optional filter |
---|
383 | * @param string $id returns only data about given plugin |
---|
384 | * @return array |
---|
385 | */ |
---|
386 | function get_db_plugins($state='', $id='') |
---|
387 | { |
---|
388 | $query = ' |
---|
389 | SELECT * FROM '.PLUGINS_TABLE; |
---|
390 | $clauses = array(); |
---|
391 | if (!empty($state)) |
---|
392 | { |
---|
393 | $clauses[] = 'state=\''.$state.'\''; |
---|
394 | } |
---|
395 | if (!empty($id)) |
---|
396 | { |
---|
397 | $clauses[] = 'id="'.$id.'"'; |
---|
398 | } |
---|
399 | if (count($clauses)) |
---|
400 | { |
---|
401 | $query .= ' |
---|
402 | WHERE '. implode(' AND ', $clauses); |
---|
403 | } |
---|
404 | |
---|
405 | $result = pwg_query($query); |
---|
406 | $plugins = array(); |
---|
407 | while ($row = pwg_db_fetch_assoc($result)) |
---|
408 | { |
---|
409 | $plugins[] = $row; |
---|
410 | } |
---|
411 | return $plugins; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * Loads a plugin, it includes the main.inc.php file and updates _$pwg_loaded_plugins_. |
---|
416 | * |
---|
417 | * @param string $plugin |
---|
418 | */ |
---|
419 | function load_plugin($plugin) |
---|
420 | { |
---|
421 | $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php'; |
---|
422 | if ( file_exists($file_name) ) |
---|
423 | { |
---|
424 | global $pwg_loaded_plugins; |
---|
425 | $pwg_loaded_plugins[ $plugin['id'] ] = $plugin; |
---|
426 | include_once( $file_name ); |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | /** |
---|
431 | * Loads all the registered plugins. |
---|
432 | */ |
---|
433 | function load_plugins() |
---|
434 | { |
---|
435 | global $conf, $pwg_loaded_plugins; |
---|
436 | $pwg_loaded_plugins = array(); |
---|
437 | if ($conf['enable_plugins']) |
---|
438 | { |
---|
439 | $plugins = get_db_plugins('active'); |
---|
440 | foreach( $plugins as $plugin) |
---|
441 | {// include main from a function to avoid using same function context |
---|
442 | load_plugin($plugin); |
---|
443 | } |
---|
444 | trigger_action('plugins_loaded'); |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | ?> |
---|