Skip to content

Commit dedf5fe

Browse files
author
Florian Schaal
committed
2 parents 2211cc4 + 216ea12 commit dedf5fe

File tree

13 files changed

+220
-76
lines changed

13 files changed

+220
-76
lines changed

interface/lib/classes/plugin.inc.php

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,51 @@ private function loadPluginCache() {
4343

4444

4545
if(isset($_SESSION['s']['plugin_cache'])) unset($_SESSION['s']['plugin_cache']);
46-
47-
$plugins_dir = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV;
46+
47+
$plugin_dirs = array();
48+
$plugin_dirs[] = ISPC_LIB_PATH.FS_DIV.'plugins';
49+
50+
if(is_dir(ISPC_WEB_PATH)) {
51+
if($dh = opendir(ISPC_WEB_PATH)) {
52+
while(($file = readdir($dh)) !== false) {
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';
54+
}
55+
closedir($dh);
56+
}
57+
}
58+
4859
$_SESSION['s']['plugin_cache'] = array();
4960
$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;
61+
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)) {
66+
$tmp_plugins = array();
67+
//** Go trough all files in the plugin dir
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+
}
5873
}
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();
74+
closedir($dh);
75+
//** sort the plugins by name
76+
ksort($tmp_plugins);
77+
78+
//** load the plugins
79+
foreach($tmp_plugins as $plugin_name => $file) {
80+
require $plugins_dir . FS_DIV . $file;
81+
if($this->debug) $app->log('Loading plugin: '.$plugin_name, LOGLEVEL_DEBUG);
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);
6987
}
7088
} else {
71-
$app->log('Unable to open the plugins directory: '.$plugins_dir, LOGLEVEL_ERROR);
89+
$app->log('Plugins directory missing: '.$plugins_dir, LOGLEVEL_ERROR);
7290
}
73-
} else {
74-
$app->log('Plugins directory missing: '.$plugins_dir, LOGLEVEL_ERROR);
7591
}
7692

7793
}
@@ -81,39 +97,44 @@ private function loadPluginCache() {
8197
for faster lookups without the need to load all plugins for every page.
8298
*/
8399

84-
public function registerEvent($event_name, $plugin_name, $function_name) {
100+
public function registerEvent($event_name, $plugin_name, $function_name, $module_name = '') {
85101
global $app;
86102

87-
$_SESSION['s']['plugin_cache'][$event_name][] = array('plugin' => $plugin_name, 'function' => $function_name);
103+
$_SESSION['s']['plugin_cache'][$event_name][] = array('plugin' => $plugin_name, 'function' => $function_name, 'module' => $module_name);
88104
if($this->debug) $app->log("Plugin '$plugin_name' has registered the function '$function_name' for the event '$event_name'", LOGLEVEL_DEBUG);
89105
}
90106

91107
/*
92108
This function is called when a certian action occurs, e.g. a form gets saved or a user is logged in.
93109
*/
94110

95-
public function raiseEvent($event_name, $data) {
111+
public function raiseEvent($event_name, $data, $return_data = false) {
96112
global $app;
97113

98114
if(!isset($_SESSION['s']['plugin_cache'])) {
99115
$this->loadPluginCache();
100116
if($this->debug) $app->log('Loaded the plugin cache.', LOGLEVEL_DEBUG);
101117
}
102-
103-
118+
119+
$result = '';
104120
$sub_events = explode(':', $event_name);
105121

106122
if(is_array($sub_events)) {
107123
if(count($sub_events) == 3) {
108124
$tmp_event = $sub_events[2];
109125
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
110-
$this->callPluginEvent($tmp_event, $data);
126+
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
127+
if($return_data == true && $tmpresult) $result .= $tmpresult;
128+
111129
$tmp_event = $sub_events[0].':'.$sub_events[2];
112130
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
113-
$this->callPluginEvent($tmp_event, $data);
131+
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
132+
if($return_data == true && $tmpresult) $result .= $tmpresult;
133+
114134
$tmp_event = $sub_events[0].':'.$sub_events[1].':'.$sub_events[2];
115135
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
116-
$this->callPluginEvent($tmp_event, $data);
136+
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
137+
if($return_data == true && $tmpresult) $result .= $tmpresult;
117138

118139
/*$sub_events = array_reverse($sub_events);
119140
$tmp_event = '';
@@ -125,23 +146,36 @@ public function raiseEvent($event_name, $data) {
125146
*/
126147
} else {
127148
if($this->debug) $app->log("Called Event '$sub_events[0]'", LOGLEVEL_DEBUG);
128-
$this->callPluginEvent($sub_events[0], $data);
149+
$tmpresult = $this->callPluginEvent($sub_events[0], $data, $return_data);
150+
if($return_data == true && $tmpresult) $result .= $tmpresult;
129151
}
130152
}
153+
154+
if($return_data == true) return $result;
131155

132156
} // end function raiseEvent
133157

134158
//* Internal function to load the plugin and call the event function in the plugin.
135-
private function callPluginEvent($event_name, $data) {
159+
private function callPluginEvent($event_name, $data, $return_data = false) {
136160
global $app;
137161

162+
$result = '';
163+
138164
//* execute the functions for the events
139165
if(@is_array($_SESSION['s']['plugin_cache'][$event_name])) {
140166
foreach($_SESSION['s']['plugin_cache'][$event_name] as $rec) {
141167
$plugin_name = $rec['plugin'];
142168
$function_name = $rec['function'];
143-
$plugin_file = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV.$plugin_name.'.inc.php';
144-
169+
$module_name = $rec['module'];
170+
if($module_name != '') {
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+
}
145179

146180
if(is_file($plugin_file)) {
147181
if(!isset($app->loaded_plugins[$plugin_name])) {
@@ -152,12 +186,14 @@ private function callPluginEvent($event_name, $data) {
152186
if($this->debug) $app->log("Called method: '$function_name' in plugin '$plugin_name' for event '$event_name'", LOGLEVEL_DEBUG);
153187
// call_user_method($function_name,$app->loaded_plugins[$plugin_name],$event_name,$data);
154188

155-
call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
156-
189+
$tmpresult = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
190+
if($return_data == true && $tmpresult) $result .= $tmpresult;
157191
}
158192
}
159193

160194
}
195+
196+
if($return_data == true) return $result;
161197

162198
} // end functiom callPluginEvent
163199

interface/lib/classes/tform_actions.inc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function onLoad() {
4646
$app->tpl->newTemplate("tabbed_form.tpl.htm");
4747

4848
// Load table definition from file
49-
$app->tform->loadFormDef($tform_def_file);
49+
$app->tform->loadFormDef($tform_def_file, (isset($_SESSION['s']['module']['name']) ? $_SESSION['s']['module']['name'] : ''));
5050

5151
// Importing ID
5252
$this->id = (isset($_REQUEST["id"]))?$app->functions->intval($_REQUEST["id"]):0;
@@ -594,7 +594,7 @@ function loadPlugins($next_tab) {
594594
$app->load($plugin_class);
595595
$this->plugins[$plugin_name] = new $plugin_class;
596596
$this->plugins[$plugin_name]->setOptions($plugin_name, $plugin_settings['options']);
597-
// Make the data of the form easily accessible for the plugib
597+
// Make the data of the form easily accessible for the plugin
598598
$this->plugins[$plugin_name]->form = $this;
599599
$this->plugins[$plugin_name]->onLoad();
600600
}

interface/lib/classes/tform_base.inc.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ function loadFormDef($file, $module = '') {
127127
global $app, $conf;
128128

129129
include $file;
130+
$app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$form['name'] . ':on_before_formdef', $this);
130131
$this->formDef = $form;
131132

132133
$this->module = $module;
@@ -150,8 +151,10 @@ function loadFormDef($file, $module = '') {
150151
$wb = $app->functions->array_merge($wb_global, $wb);
151152
}
152153
if(isset($wb_global)) unset($wb_global);
153-
154+
154155
$this->wordbook = $wb;
156+
157+
$app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'] . ':on_after_formdef', $this);
155158

156159
$this->dateformat = $app->lng('conf_format_dateshort');
157160
$this->datetimeformat = $app->lng('conf_format_datetime');

interface/lib/classes/tpl.inc.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -839,28 +839,34 @@ public function __construct($tmplfile = null, $options = null)
839839
* @access private
840840
* @return mixed data/string or boolean
841841
*/
842-
private function _getData ($tmplfile, $do_eval=false)
842+
private function _getData ($tmplfile, $do_eval=false, $tmpl_from_string = false)
843843
{
844844
//* check the current file depth
845845
if ($this->_includedepth > $this->OPTIONS['MAX_INCLUDES'] || $tmplfile == false) {
846846
return;
847847
} else {
848848
if ($this->_debug){
849-
array_push($this->_debugIncludedfiles, $tmplfile);
849+
if($tmpl_from_string) array_push($this->_debugIncludedfiles, 'String: ' . substr($tmplfile, 0, 25) . '...');
850+
else array_push($this->_debugIncludedfiles, $tmplfile);
850851
}
851852
if ($do_eval) {
852-
array_push($this->_currentincludedir, dirname($tmplfile));
853+
if($tmpl_from_string == true) array_push($this->_currentincludedir, end($this->_currentincludedir));
854+
else array_push($this->_currentincludedir, dirname($tmplfile));
853855
$this->_includedepth++;
854856
}
855857
}
856858

857859

858-
if($this->_cache && $this->_checkCache($tmplfile)) { //* cache exists so lets use it
860+
if($this->_cache && $this->_checkCache($tmplfile, $tmpl_from_string)) { //* cache exists so lets use it
859861
$data = fread($fp = fopen($this->_cachefile, 'r'), filesize($this->_cachefile));
860862
fclose($fp);
861863
} else { //* no cache lets parse the file
862-
$data = fread($fp = fopen($tmplfile, 'r'), filesize($tmplfile));
863-
fclose($fp);
864+
if($tmpl_from_string == true) {
865+
$data = $tmplfile;
866+
} else {
867+
$data = fread($fp = fopen($tmplfile, 'r'), filesize($tmplfile));
868+
fclose($fp);
869+
}
864870

865871
$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
866872
$regex.= 'tmpl_([\w]+)\s*';
@@ -884,7 +890,7 @@ private function _getData ($tmplfile, $do_eval=false)
884890
}
885891

886892
//* now we must parse the $data and check for any <tmpl_include>'s
887-
if ($this->_debug) $this->doDebugWarnings(file($tmplfile), $tmplfile);
893+
if ($this->_debug && $tmpl_from_string == false) $this->doDebugWarnings(file($tmplfile), $tmplfile);
888894

889895
if ($do_eval) {
890896
$success = @eval('?>'.$data.'<?php return 1;');
@@ -1061,6 +1067,46 @@ private function _parseIf($varname, $value = null, $op = null, $namespace = null
10611067
}
10621068
}
10631069

1070+
/**
1071+
* returns a string containing hook data
1072+
* @param string $type
1073+
* @param string $name
1074+
* @return string hook data
1075+
*/
1076+
private function _parseHook ($name)
1077+
{
1078+
global $app;
1079+
1080+
if(!$name) return false;
1081+
1082+
$module = isset($_SESSION['s']['module']['name']) ? $_SESSION['s']['module']['name'] : '';
1083+
$form = isset($app->tform->formDef['name']) ? $app->tform->formDef['name'] : '';
1084+
1085+
$events = array();
1086+
if($module) {
1087+
$events[] = $module . ':' . ($form ? $form : '') . ':' . $name;
1088+
$events[] = $module . ':' . ($form ? $form : '') . ':on_template_content';
1089+
} else {
1090+
$events[] = $name;
1091+
$events[] = 'on_template_content';
1092+
}
1093+
1094+
$events = array_unique($events);
1095+
1096+
for($e = 0; $e < count($events); $e++) {
1097+
$tmpresult = $app->plugin->raiseEvent($events[$e], array(
1098+
'name' => $name,
1099+
'module' => $module,
1100+
'form' => $form
1101+
), true);
1102+
if(!$tmpresult) $tmpresult = '';
1103+
else $tmpresult = $this->_getData($tmpresult, false, true);
1104+
1105+
$result .= $tmpresult;
1106+
}
1107+
1108+
return $result;
1109+
}
10641110

10651111
/**
10661112
* returns a string used for parsing in tmpl_loop statements.
@@ -1254,7 +1300,10 @@ private function _parseTag ($args)
12541300
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
12551301
return '<?php include(\''.$file.'\'); ?>';
12561302
}
1257-
1303+
1304+
case 'hook':
1305+
return $this->_parseHook(@$var);
1306+
12581307
case 'include':
12591308
return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
12601309

interface/lib/classes/tpl_cache.inc.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ function setCacheExtension($str = null) {
101101
* FUNCTION: _checkCache
102102
* checks if there's a cache, if there is then it will read the cache file as the template.
103103
*/
104-
function _checkCache ($tmplfile) {
105-
$this->_cachefile = $this->_getFilename($tmplfile);
104+
function _checkCache ($tmplfile, $tmpl_from_string = false) {
105+
$this->_cachefile = $this->_getFilename($tmplfile, $tmpl_from_string);
106106
if ($this->_clearcache) {
107107
if (file_exists($this->_cachefile)) unlink($this->_cachefile);
108108
return false;
@@ -133,8 +133,9 @@ function _checkCache ($tmplfile) {
133133
* gets the full pathname for the cached file
134134
*
135135
*/
136-
function _getFilename($tmplfile) {
137-
return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION'];
136+
function _getFilename($tmplfile, $tmpl_from_string = false) {
137+
if($tmpl_from_string == true) return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaRSTRING'.$tmplfile).'.'.$this->OPTIONS['CACHE_EXTENSION'];
138+
else return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION'];
138139
}
139140

140141

interface/web/admin/templates/directive_snippets_edit.htm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1><tmpl_var name="list_head_txt"></h1>
1616
</div>
1717
<div class="form-group">
1818
<label for="snippet" class="col-sm-3 control-label">{tmpl_var name='snippet_txt'}</label>
19-
<div class="col-sm-9"><textarea class="form-control" name="snippet" id="snippet" rows='10' cols='50'>{tmpl_var name='snippet'}</textarea></div><span class="nginx"> &nbsp; {tmpl_var name='variables_txt'}: </span><a href="javascript:void(0);" class="addPlaceholder nginx">{DOCROOT}</a><span class="nginx">, </span><a href="javascript:void(0);" class="addPlaceholder nginx">{FASTCGIPASS}</a>
19+
<div class="col-sm-9"><textarea class="form-control" name="snippet" id="snippet" rows='10' cols='50'>{tmpl_var name='snippet'}</textarea></div><span> &nbsp; {tmpl_var name='variables_txt'}: </span><a href="javascript:void(0);" class="addPlaceholder">{DOCROOT}</a>, <a href="javascript:void(0);" class="addPlaceholder">{DOCROOT_CLIENT}</a><span class="nginx">, </span><a href="javascript:void(0);" class="addPlaceholder nginx">{FASTCGIPASS}</a>
2020
</div>
2121
<div class="form-group php">
2222
<label class="col-sm-3 control-label">{tmpl_var name='required_php_snippets_txt'}</label>

0 commit comments

Comments
 (0)