Skip to content

Commit da1da41

Browse files
committed
FS#1064 - Add support for event based plugins to the web frontend.
1 parent 7bd0de0 commit da1da41

File tree

7 files changed

+268
-89
lines changed

7 files changed

+268
-89
lines changed

interface/lib/app.inc.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct()
6565
if(empty($_SESSION['s']['language'])) $_SESSION['s']['language'] = $conf['language'];
6666
}
6767

68-
$this->uses('auth');
68+
$this->uses('auth,plugin');
6969
}
7070

7171
public function uses($classes)
@@ -98,7 +98,15 @@ public function load($files)
9898
/** Priority values are: 0 = DEBUG, 1 = WARNING, 2 = ERROR */
9999
public function log($msg, $priority = 0)
100100
{
101+
global $conf;
101102
if($priority >= $this->_conf['log_priority']) {
103+
// $server_id = $conf["server_id"];
104+
$server_id = 0;
105+
$priority = intval($priority);
106+
$tstamp = time();
107+
$msg = $this->db->quote('[INTERFACE]: '.$msg);
108+
$this->db->query("INSERT INTO sys_log (server_id,datalog_id,loglevel,tstamp,message) VALUES ($server_id,0,$priority,$tstamp,'$msg')");
109+
/*
102110
if (is_writable($this->_conf['log_file'])) {
103111
if (!$fp = fopen ($this->_conf['log_file'], 'a')) {
104112
$this->error('Unable to open logfile.');
@@ -110,6 +118,7 @@ public function log($msg, $priority = 0)
110118
} else {
111119
$this->error('Unable to write to logfile.');
112120
}
121+
*/
113122
}
114123
}
115124

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
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 {
32+
33+
private $subscribed_events = array();
34+
private $debug = true;
35+
36+
37+
/*
38+
This function is called to load the plugins from the plugins folder and update the plugin cache
39+
*/
40+
41+
private function loadPluginCache() {
42+
global $app,$conf;
43+
44+
45+
if(isset($_SESSION['s']['plugin_cache'])) unset($_SESSION['s']['plugin_cache']);
46+
47+
$plugins_dir = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV;
48+
$_SESSION['s']['plugin_cache'] = array();
49+
$tmp_plugins = array();
50+
51+
if (is_dir($plugins_dir)) {
52+
if ($dh = opendir($plugins_dir)) {
53+
//** Go trough all files in the plugin dir
54+
while (($file = readdir($dh)) !== false) {
55+
if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
56+
$plugin_name = substr($file,0,-8);
57+
$tmp_plugins[$plugin_name] = $file;
58+
}
59+
}
60+
//** sort the plugins by name
61+
ksort($tmp_plugins);
62+
63+
//** load the plugins
64+
foreach($tmp_plugins as $plugin_name => $file) {
65+
include_once($plugins_dir.$file);
66+
if($this->debug) $app->log("Loading Plugin: $plugin_name",LOGLEVEL_DEBUG);
67+
$app->loaded_plugins[$plugin_name] = new $plugin_name;
68+
$app->loaded_plugins[$plugin_name]->onLoad();
69+
}
70+
} else {
71+
$app->log("Unable to open the plugin directory: $plugins_dir",LOGLEVEL_ERROR);
72+
}
73+
} else {
74+
$app->log("Plugin directory missing: $plugins_dir",LOGLEVEL_ERROR);
75+
}
76+
77+
}
78+
79+
/*
80+
This function is called by the plugin to register for an event which is saved into the plugin cache
81+
for faster lookups without the need to load all plugins for every page.
82+
*/
83+
84+
public function registerEvent($event_name,$plugin_name,$function_name) {
85+
global $app;
86+
87+
$_SESSION['s']['plugin_cache'][$event_name][] = array('plugin' => $plugin_name, 'function' => $function_name);
88+
if($this->debug) $app->log("Plugin '$plugin_name' has registered the function '$function_name' for the event '$event_name'",LOGLEVEL_DEBUG);
89+
90+
}
91+
92+
/*
93+
This function is called when a certian action occurs, e.g. a form gets saved or a user is logged in.
94+
*/
95+
96+
public function raiseEvent($event_name,$data) {
97+
global $app;
98+
99+
if(!isset($_SESSION['s']['plugin_cache'])) {
100+
$this->loadPluginCache();
101+
if($this->debug) $app->log("Loaded the plugin cache.",LOGLEVEL_DEBUG);
102+
}
103+
104+
105+
$sub_events = explode(':',$event_name);
106+
107+
if(is_array($sub_events)) {
108+
if(count($sub_events) == 3) {
109+
$tmp_event = $sub_events[2];
110+
if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG);
111+
$this->callPluginEvent($tmp_event,$data);
112+
$tmp_event = $sub_events[0].':'.$sub_events[2];
113+
if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG);
114+
$this->callPluginEvent($tmp_event,$data);
115+
$tmp_event = $sub_events[0].':'.$sub_events[1].':'.$sub_events[2];
116+
if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG);
117+
$this->callPluginEvent($tmp_event,$data);
118+
119+
/*$sub_events = array_reverse($sub_events);
120+
$tmp_event = '';
121+
foreach($sub_events as $n => $sub_event) {
122+
$tmp_event = ($n == 0)?$sub_event:$sub_event.':'.$tmp_event;
123+
if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG);
124+
$this->callPluginEvent($tmp_event,$data);
125+
}
126+
*/
127+
} else {
128+
if($this->debug) $app->log("Called Event '$sub_events[0]'",LOGLEVEL_DEBUG);
129+
$this->callPluginEvent($sub_events[0],$data);
130+
}
131+
}
132+
133+
} // end function raiseEvent
134+
135+
//* Internal function to load the plugin and call the event function in the plugin.
136+
private function callPluginEvent($event_name,$data) {
137+
global $app;
138+
139+
//* execute the functions for the events
140+
if(is_array($_SESSION['s']['plugin_cache'][$event_name])) {
141+
foreach($_SESSION['s']['plugin_cache'][$event_name] as $rec) {
142+
$plugin_name = $rec['plugin'];
143+
$function_name = $rec['function'];
144+
$plugin_file = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV.$plugin_name.'.inc.php';
145+
if(is_file($plugin_file)) {
146+
if(!isset($app->loaded_plugins[$plugin_name])) {
147+
include_once($plugin_file);
148+
$app->loaded_plugins[$plugin_name] = new $plugin_name;
149+
}
150+
if($this->debug) $app->log("Called method: '$function_name' in plugin '$plugin_name' for event '$event_name'",LOGLEVEL_DEBUG);
151+
call_user_method($function_name,$app->loaded_plugins[$plugin_name],$event_name,$data);
152+
}
153+
}
154+
155+
}
156+
} // end functiom callPluginEvent
157+
158+
159+
}
160+
161+
?>

interface/lib/classes/tform_actions.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function onUpdate() {
118118
}
119119

120120
$this->onAfterUpdate();
121+
$app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'].':'.'on_after_update',$this);
121122

122123
// Write data history (sys_datalog)
123124
if($app->tform->formDef['db_history'] == 'yes') {
@@ -195,6 +196,7 @@ function onInsert() {
195196
}
196197

197198
$this->onAfterInsert();
199+
$app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'].':'.'on_after_insert',$this);
198200

199201
// Write data history (sys_datalog)
200202
if($app->tform->formDef['db_history'] == 'yes') {

interface/lib/config.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
$conf['app_version'] = ISPC_APP_VERSION;
9898
$conf['app_link'] = 'http://www.howtoforge.com/forums/showthread.php?t=26988';
9999
$conf['modules_available'] = 'admin,mail,sites,monitor,client,dns,help';
100-
$conf["server_id"] = "{server_id}";
100+
$conf["server_id"] = "1";
101101

102102

103103
//** Interface

interface/web/login/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ public function render() {
149149
include_once($_SESSION['s']['user']['startmodule'].'/lib/module.conf.php');
150150
$_SESSION['s']['module'] = $module;
151151
}
152+
153+
$app->plugin->raiseEvent('login',$this);
154+
152155
echo 'HEADER_REDIRECT:'.$_SESSION['s']['module']['startpage'];
153156

154157
exit;

interface/web/login/logout.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
exit;
6060
}
6161

62+
$app->plugin->raiseEvent('logout',true);
63+
6264
$_SESSION["s"]["user"] = null;
6365
$_SESSION["s"]["module"] = null;
6466
$_SESSION['s_old'] = null;

0 commit comments

Comments
 (0)