commit | author | age
|
da1da4
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
Copyright (c) 2010, Till Brehm, projektfarm Gmbh |
|
5 |
All rights reserved. |
|
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
class plugin { |
b1a6a5
|
32 |
|
da1da4
|
33 |
private $subscribed_events = array(); |
b67344
|
34 |
private $debug = false; |
b1a6a5
|
35 |
|
MC |
36 |
|
da1da4
|
37 |
/* |
T |
38 |
This function is called to load the plugins from the plugins folder and update the plugin cache |
|
39 |
*/ |
b1a6a5
|
40 |
|
da1da4
|
41 |
private function loadPluginCache() { |
b1a6a5
|
42 |
global $app, $conf; |
MC |
43 |
|
|
44 |
|
da1da4
|
45 |
if(isset($_SESSION['s']['plugin_cache'])) unset($_SESSION['s']['plugin_cache']); |
6b15d5
|
46 |
|
MB |
47 |
$plugin_dirs = array(); |
e797d0
|
48 |
$plugin_dirs[] = ISPC_LIB_PATH.FS_DIV.'plugins'; |
6b15d5
|
49 |
|
MB |
50 |
if(is_dir(ISPC_WEB_PATH)) { |
|
51 |
if($dh = opendir(ISPC_WEB_PATH)) { |
|
52 |
while(($file = readdir($dh)) !== false) { |
e797d0
|
53 |
if($file !== '.' && $file !== '..' && is_dir(ISPC_WEB_PATH . FS_DIV . $file) && is_dir(ISPC_WEB_PATH . FS_DIV . $file . FS_DIV . 'lib' . FS_DIV . 'plugin.d')) $plugin_dirs[] = ISPC_WEB_PATH . FS_DIV . $file . FS_DIV . 'lib' . FS_DIV . 'plugin.d'; |
6b15d5
|
54 |
} |
MB |
55 |
closedir($dh); |
|
56 |
} |
|
57 |
} |
|
58 |
|
da1da4
|
59 |
$_SESSION['s']['plugin_cache'] = array(); |
T |
60 |
$tmp_plugins = array(); |
6b15d5
|
61 |
|
MB |
62 |
for($d = 0; $d < count($plugin_dirs); $d++) { |
|
63 |
$plugins_dir = $plugin_dirs[$d]; |
|
64 |
if (is_dir($plugins_dir)) { |
|
65 |
if ($dh = opendir($plugins_dir)) { |
e797d0
|
66 |
$tmp_plugins = array(); |
6b15d5
|
67 |
//** Go trough all files in the plugin dir |
MB |
68 |
while (($file = readdir($dh)) !== false) { |
|
69 |
if($file !== '.' && $file !== '..' && substr($file, -8, 8) == '.inc.php') { |
|
70 |
$plugin_name = substr($file, 0, -8); |
|
71 |
$tmp_plugins[$plugin_name] = $file; |
|
72 |
} |
da1da4
|
73 |
} |
6b15d5
|
74 |
closedir($dh); |
MB |
75 |
//** sort the plugins by name |
|
76 |
ksort($tmp_plugins); |
b1a6a5
|
77 |
|
6b15d5
|
78 |
//** load the plugins |
MB |
79 |
foreach($tmp_plugins as $plugin_name => $file) { |
e797d0
|
80 |
require $plugins_dir . FS_DIV . $file; |
6b15d5
|
81 |
if($this->debug) $app->log('Loading plugin: '.$plugin_name, LOGLEVEL_DEBUG); |
MB |
82 |
$app->loaded_plugins[$plugin_name] = new $plugin_name; |
|
83 |
$app->loaded_plugins[$plugin_name]->onLoad(); |
|
84 |
} |
|
85 |
} else { |
|
86 |
$app->log('Unable to open the plugins directory: '.$plugins_dir, LOGLEVEL_ERROR); |
da1da4
|
87 |
} |
T |
88 |
} else { |
6b15d5
|
89 |
$app->log('Plugins directory missing: '.$plugins_dir, LOGLEVEL_ERROR); |
da1da4
|
90 |
} |
T |
91 |
} |
b1a6a5
|
92 |
|
da1da4
|
93 |
} |
b1a6a5
|
94 |
|
da1da4
|
95 |
/* |
b1a6a5
|
96 |
This function is called by the plugin to register for an event which is saved into the plugin cache |
da1da4
|
97 |
for faster lookups without the need to load all plugins for every page. |
T |
98 |
*/ |
b1a6a5
|
99 |
|
20e010
|
100 |
public function registerEvent($event_name, $plugin_name, $function_name, $module_name = '') { |
da1da4
|
101 |
global $app; |
b1a6a5
|
102 |
|
20e010
|
103 |
$_SESSION['s']['plugin_cache'][$event_name][] = array('plugin' => $plugin_name, 'function' => $function_name, 'module' => $module_name); |
b1a6a5
|
104 |
if($this->debug) $app->log("Plugin '$plugin_name' has registered the function '$function_name' for the event '$event_name'", LOGLEVEL_DEBUG); |
da1da4
|
105 |
} |
b1a6a5
|
106 |
|
da1da4
|
107 |
/* |
T |
108 |
This function is called when a certian action occurs, e.g. a form gets saved or a user is logged in. |
|
109 |
*/ |
b1a6a5
|
110 |
|
1b9d2f
|
111 |
public function raiseEvent($event_name, $data, $return_data = false) { |
da1da4
|
112 |
global $app; |
b1a6a5
|
113 |
|
da1da4
|
114 |
if(!isset($_SESSION['s']['plugin_cache'])) { |
T |
115 |
$this->loadPluginCache(); |
b1a6a5
|
116 |
if($this->debug) $app->log('Loaded the plugin cache.', LOGLEVEL_DEBUG); |
da1da4
|
117 |
} |
1b9d2f
|
118 |
|
MB |
119 |
$result = ''; |
b1a6a5
|
120 |
$sub_events = explode(':', $event_name); |
MC |
121 |
|
da1da4
|
122 |
if(is_array($sub_events)) { |
T |
123 |
if(count($sub_events) == 3) { |
860ab6
|
124 |
$tmp_event = $sub_events[2]; |
b1a6a5
|
125 |
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG); |
1b9d2f
|
126 |
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data); |
MB |
127 |
if($return_data == true && $tmpresult) $result .= $tmpresult; |
|
128 |
|
da1da4
|
129 |
$tmp_event = $sub_events[0].':'.$sub_events[2]; |
b1a6a5
|
130 |
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG); |
1b9d2f
|
131 |
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data); |
MB |
132 |
if($return_data == true && $tmpresult) $result .= $tmpresult; |
|
133 |
|
da1da4
|
134 |
$tmp_event = $sub_events[0].':'.$sub_events[1].':'.$sub_events[2]; |
b1a6a5
|
135 |
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG); |
1b9d2f
|
136 |
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data); |
MB |
137 |
if($return_data == true && $tmpresult) $result .= $tmpresult; |
b1a6a5
|
138 |
|
da1da4
|
139 |
/*$sub_events = array_reverse($sub_events); |
T |
140 |
$tmp_event = ''; |
|
141 |
foreach($sub_events as $n => $sub_event) { |
|
142 |
$tmp_event = ($n == 0)?$sub_event:$sub_event.':'.$tmp_event; |
|
143 |
if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG); |
|
144 |
$this->callPluginEvent($tmp_event,$data); |
|
145 |
} |
|
146 |
*/ |
|
147 |
} else { |
b1a6a5
|
148 |
if($this->debug) $app->log("Called Event '$sub_events[0]'", LOGLEVEL_DEBUG); |
1b9d2f
|
149 |
$tmpresult = $this->callPluginEvent($sub_events[0], $data, $return_data); |
MB |
150 |
if($return_data == true && $tmpresult) $result .= $tmpresult; |
da1da4
|
151 |
} |
T |
152 |
} |
1b9d2f
|
153 |
|
MB |
154 |
if($return_data == true) return $result; |
80e3c9
|
155 |
|
b1a6a5
|
156 |
} // end function raiseEvent |
MC |
157 |
|
|
158 |
//* Internal function to load the plugin and call the event function in the plugin. |
1b9d2f
|
159 |
private function callPluginEvent($event_name, $data, $return_data = false) { |
b1a6a5
|
160 |
global $app; |
1b9d2f
|
161 |
|
MB |
162 |
$result = ''; |
b1a6a5
|
163 |
|
MC |
164 |
//* execute the functions for the events |
8cf78b
|
165 |
if(@is_array($_SESSION['s']['plugin_cache'][$event_name])) { |
da1da4
|
166 |
foreach($_SESSION['s']['plugin_cache'][$event_name] as $rec) { |
T |
167 |
$plugin_name = $rec['plugin']; |
|
168 |
$function_name = $rec['function']; |
ad7b31
|
169 |
$module_name = $rec['module']; |
20e010
|
170 |
if($module_name != '') { |
MB |
171 |
if(strpos($module_name, '..') !== false || strpos($module_name, '/') !== false) { |
|
172 |
if($this->debug) $app->log('Module name ' . $module_name . ' contains illegal characters.', LOGLEVEL_DEBUG); |
|
173 |
continue; |
|
174 |
} |
|
175 |
$plugin_file = ISPC_WEB_PATH . FS_DIV . $module_name . FS_DIV . 'lib' . FS_DIV . 'plugin.d' . FS_DIV . $plugin_name . '.inc.php'; |
|
176 |
} else { |
|
177 |
$plugin_file = ISPC_LIB_PATH . FS_DIV . 'plugins' . FS_DIV . $plugin_name . '.inc.php'; |
|
178 |
} |
b1a6a5
|
179 |
|
da1da4
|
180 |
if(is_file($plugin_file)) { |
T |
181 |
if(!isset($app->loaded_plugins[$plugin_name])) { |
b1a6a5
|
182 |
include_once $plugin_file; |
da1da4
|
183 |
$app->loaded_plugins[$plugin_name] = new $plugin_name; |
T |
184 |
} |
b1a6a5
|
185 |
|
MC |
186 |
if($this->debug) $app->log("Called method: '$function_name' in plugin '$plugin_name' for event '$event_name'", LOGLEVEL_DEBUG); |
04620b
|
187 |
// call_user_method($function_name,$app->loaded_plugins[$plugin_name],$event_name,$data); |
80e3c9
|
188 |
|
1b9d2f
|
189 |
$tmpresult = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data); |
MB |
190 |
if($return_data == true && $tmpresult) $result .= $tmpresult; |
da1da4
|
191 |
} |
T |
192 |
} |
b1a6a5
|
193 |
|
da1da4
|
194 |
} |
1b9d2f
|
195 |
|
MB |
196 |
if($return_data == true) return $result; |
b1a6a5
|
197 |
|
MC |
198 |
} // end functiom callPluginEvent |
|
199 |
|
|
200 |
|
da1da4
|
201 |
} |
T |
202 |
|
12fcb2
|
203 |
?> |