de8d4687eceddd5a94c660b607774c6aadb4fa5e
[squirrelmail.git] / functions / plugin.php
1 <?php
2
3 /**
4 ** plugin.php
5 **
6 ** This file provides the framework for a plugin architecture.
7 **
8 ** Plugins will eventually be a way to provide added functionality
9 ** without having to patch the SquirrelMail source code. Have some
10 ** patience, though, as the these funtions might change in the near
11 ** future.
12 **
13 ** Documentation on how to write plugins might show up some time.
14 **
15 ** $Id$
16 **/
17
18
19 if (defined('plugin_php'))
20 return;
21 define('plugin_php', true);
22
23 global $plugin_general_debug, $squirrelmail_plugin_hooks;
24 $plugin_general_debug = false;
25
26 $squirrelmail_plugin_hooks = array();
27
28 // This function adds a plugin
29 function use_plugin ($name) {
30 global $plugin_general_debug;
31
32 if (file_exists('../plugins/'.$name.'/setup.php')) {
33 if ($plugin_general_debug)
34 echo "plugin: -- Loading $name/setup.php<br>\n";
35 include_once('../plugins/'.$name.'/setup.php');
36 $function = 'squirrelmail_plugin_init_'.$name;
37 if (function_exists($function))
38 {
39 if ($plugin_general_debug)
40 echo "plugin: ---- Executing $function to init plugin<br>\n";
41 $function($plugin_general_debug);
42 }
43 elseif ($plugin_general_debug)
44 echo "plugin: -- Init function $function doesn't exist.<br>\n";
45 }
46 elseif ($plugin_general_debug)
47 echo "plugin: Couldn't find $name/setup.php<br>\n";
48 }
49
50 // This function executes a hook
51 function do_hook ($name) {
52 global $squirrelmail_plugin_hooks;
53 $Data = func_get_args();
54 if (isset($squirrelmail_plugin_hooks[$name]) &&
55 is_array($squirrelmail_plugin_hooks[$name])) {
56 foreach ($squirrelmail_plugin_hooks[$name] as $id => $function) {
57 // Add something to set correct gettext domain for plugin
58 if (function_exists($function)) {
59 $function($Data);
60 }
61 }
62 }
63
64 // Variable-length argument lists have a slight problem when
65 // passing values by reference. Pity. This is a workaround.
66 return $Data;
67 }
68
69 // On startup, register all plugins configured for use
70 if (isset($plugins) && is_array($plugins))
71 foreach ($plugins as $id => $name)
72 {
73 if ($plugin_general_debug)
74 echo "plugin: Attempting load of plugin $name<br>\n";
75 use_plugin($name);
76 }
77
78 if ($plugin_general_debug)
79 {
80 echo "plugin: Hook list<br>\n";
81 foreach ($squirrelmail_plugin_hooks as $Hook => $Plugins)
82 {
83 foreach ($Plugins as $Name => $Func)
84 {
85 echo "[$Hook][$Name] = $Func<br>\n";
86 }
87 }
88 }
89
90 ?>