Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
62a110 1 <?php
T 2
3 /*
5a43e7 4 Copyright (c) 2007-2012, Till Brehm, projektfarm Gmbh
62a110 5 All rights reserved.
T 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 plugins {
7fe908 32
32b40d 33     var $available_events = array();
T 34     var $subscribed_events = array();
5a43e7 35     var $subscribed_actions = array();
9eff6c 36     var $debug = false;
7fe908 37
62a110 38     /*
0d0cd9 39      This function is called to load the plugins from the plugins-enabled or the plugins-core folder
62a110 40     */
7fe908 41
0d0cd9 42     function loadPlugins($type) {
7fe908 43         global $app, $conf;
0d0cd9 44
V 45         $subPath = 'plugins-enabled';
cc6568 46         //if ($type == 'core') $subPath = 'plugins-core';
7fe908 47
458767 48         $plugins_dir = $conf['rootpath'].$conf['fs_div'].$subPath.$conf['fs_div'];
5fa680 49         $tmp_plugins = array();
7fe908 50
62a110 51         if (is_dir($plugins_dir)) {
32b40d 52             if ($dh = opendir($plugins_dir)) {
5fa680 53                 //** Go trough all files in the plugin dir
62a110 54                 while (($file = readdir($dh)) !== false) {
7fe908 55                     if($file != '.' && $file != '..' && substr($file, -8, 8) == '.inc.php') {
MC 56                         $plugin_name = substr($file, 0, -8);
5fa680 57                         $tmp_plugins[$plugin_name] = $file;
62a110 58                     }
T 59                 }
5fa680 60                 //** sort the plugins by name
T 61                 ksort($tmp_plugins);
7fe908 62
5fa680 63                 //** load the plugins
T 64                 foreach($tmp_plugins as $plugin_name => $file) {
7fe908 65                     include_once $plugins_dir.$file;
MC 66                     if($this->debug) $app->log('Loading plugin: '.$plugin_name, LOGLEVEL_DEBUG);
5fa680 67                     $app->loaded_plugins[$plugin_name] = new $plugin_name;
T 68                     $app->loaded_plugins[$plugin_name]->onLoad();
69                 }
70             } else {
7fe908 71                 $app->log('Unable to open the plugins directory: '.$plugins_dir, LOGLEVEL_ERROR);
62a110 72             }
T 73         } else {
7fe908 74             $app->log('Plugins directory missing: '.$plugins_dir, LOGLEVEL_ERROR);
62a110 75         }
T 76     }
7fe908 77
62a110 78     /*
32b40d 79         This function is used by the modules to announce which events they provide
T 80     */
7fe908 81
MC 82     function announceEvents($module_name, $events) {
32b40d 83         global $app;
T 84         foreach($events as $event_name) {
85             $this->available_events[$event_name] = $module_name;
7fe908 86             if($this->debug) $app->log('Announced event: '.$event_name, LOGLEVEL_DEBUG);
32b40d 87         }
T 88     }
7fe908 89
MC 90
32b40d 91     /*
T 92      This function is called by the plugin to register for an event
62a110 93     */
7fe908 94
MC 95     function registerEvent($event_name, $plugin_name, $function_name) {
32b40d 96         global $app;
T 97         if(!isset($this->available_events[$event_name])) {
7fe908 98             $app->log("Unable to register function '$function_name' from plugin '$plugin_name' for event '$event_name'", LOGLEVEL_DEBUG);
32b40d 99         } else {
T 100             $this->subscribed_events[$event_name][] = array('plugin' => $plugin_name, 'function' => $function_name);
7fe908 101             if($this->debug)  $app->log("Registered function '$function_name' from plugin '$plugin_name' for event '$event_name'.", LOGLEVEL_DEBUG);
32b40d 102         }
62a110 103     }
7fe908 104
MC 105
106     function raiseEvent($event_name, $data) {
62a110 107         global $app;
7fe908 108
32b40d 109         // Get the subscriptions for this event
07bf51 110         $events = (isset($this->subscribed_events[$event_name]))?$this->subscribed_events[$event_name]:'';
7fe908 111         if($this->debug) $app->log('Raised event: '.$event_name, LOGLEVEL_DEBUG);
MC 112
62a110 113         if(is_array($events)) {
T 114             foreach($events as $event) {
458767 115                 $plugin_name = $event['plugin'];
J 116                 $function_name = $event['function'];
32b40d 117                 // Call the processing function of the plugin
7fe908 118                 $app->log("Calling function '$function_name' from plugin '$plugin_name' raised by event '$event_name'.", LOGLEVEL_DEBUG);
04620b 119                 // call_user_method($function_name,$app->loaded_plugins[$plugin_name],$event_name,$data);
7fe908 120                 call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
62a110 121                 unset($plugin_name);
T 122                 unset($function_name);
123             }
124         }
125         unset($event);
126         unset($events);
127     }
7fe908 128
5a43e7 129     /*
T 130      This function is called by the plugin to register for an action
131     */
7fe908 132
MC 133     function registerAction($action_name, $plugin_name, $function_name) {
5a43e7 134         global $app;
T 135         $this->subscribed_actions[$action_name][] = array('plugin' => $plugin_name, 'function' => $function_name);
ab6e69 136         if($this->debug)  $app->log("Registered function '$function_name' from plugin '$plugin_name' for action '$action_name'.", LOGLEVEL_DEBUG);
5a43e7 137     }
7fe908 138
MC 139
216ea1 140     function raiseAction($action_name, $data, $return_data = false) {
5a43e7 141         global $app;
7fe908 142
5a43e7 143         //* Get the subscriptions for this action
T 144         $actions = (isset($this->subscribed_actions[$action_name]))?$this->subscribed_actions[$action_name]:'';
7fe908 145         if($this->debug) $app->log('Raised action: '.$action_name, LOGLEVEL_DEBUG);
216ea1 146
MB 147         $result = '';
7fe908 148
5a43e7 149         if(is_array($actions)) {
T 150             foreach($actions as $action) {
151                 $plugin_name = $action['plugin'];
152                 $function_name = $action['function'];
153                 $state_out = 'ok';
154                 //* Call the processing function of the plugin
7fe908 155                 $app->log("Calling function '$function_name' from plugin '$plugin_name' raised by action '$action_name'.", LOGLEVEL_DEBUG);
MC 156                 $state = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $action_name, $data);
5a43e7 157                 //* ensure that we return the highest warning / error level if a error occured in one of the functions
216ea1 158                 if($return_data) {
MB 159                     if($state) $result .= $state;
160                 } else {
161                     if($state == 'warning' && $state_out != 'error') $state_out = 'warning';
162                     elseif($state == 'error') $state_out = 'error';
163                 }
164                 
5a43e7 165                 unset($plugin_name);
T 166                 unset($function_name);
167             }
168         }
169         unset($action);
170         unset($actions);
7fe908 171
216ea1 172         if($return_data == true) return $result;
MB 173         else return $state_out;
5a43e7 174     }
7fe908 175
62a110 176 }
T 177
458767 178 ?>