Skip to content

Commit 1b9d2f3

Browse files
author
Marius Burkard
committed
- Added template content hook feature
1 parent 5ddadd9 commit 1b9d2f3

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

interface/lib/classes/plugin.inc.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,33 @@ public function registerEvent($event_name, $plugin_name, $function_name, $module
107107
This function is called when a certian action occurs, e.g. a form gets saved or a user is logged in.
108108
*/
109109

110-
public function raiseEvent($event_name, $data) {
110+
public function raiseEvent($event_name, $data, $return_data = false) {
111111
global $app;
112112

113113
if(!isset($_SESSION['s']['plugin_cache'])) {
114114
$this->loadPluginCache();
115115
if($this->debug) $app->log('Loaded the plugin cache.', LOGLEVEL_DEBUG);
116116
}
117-
118-
117+
118+
$result = '';
119119
$sub_events = explode(':', $event_name);
120120

121121
if(is_array($sub_events)) {
122122
if(count($sub_events) == 3) {
123-
$tmp_event = $sub_events[2];
123+
$mp_event = $sub_events[2];
124124
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
125-
$this->callPluginEvent($tmp_event, $data);
125+
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
126+
if($return_data == true && $tmpresult) $result .= $tmpresult;
127+
126128
$tmp_event = $sub_events[0].':'.$sub_events[2];
127129
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
128-
$this->callPluginEvent($tmp_event, $data);
130+
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
131+
if($return_data == true && $tmpresult) $result .= $tmpresult;
132+
129133
$tmp_event = $sub_events[0].':'.$sub_events[1].':'.$sub_events[2];
130134
if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
131-
$this->callPluginEvent($tmp_event, $data);
135+
$tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
136+
if($return_data == true && $tmpresult) $result .= $tmpresult;
132137

133138
/*$sub_events = array_reverse($sub_events);
134139
$tmp_event = '';
@@ -140,16 +145,21 @@ public function raiseEvent($event_name, $data) {
140145
*/
141146
} else {
142147
if($this->debug) $app->log("Called Event '$sub_events[0]'", LOGLEVEL_DEBUG);
143-
$this->callPluginEvent($sub_events[0], $data);
148+
$tmpresult = $this->callPluginEvent($sub_events[0], $data, $return_data);
149+
if($return_data == true && $tmpresult) $result .= $tmpresult;
144150
}
145151
}
152+
153+
if($return_data == true) return $result;
146154

147155
} // end function raiseEvent
148156

149157
//* Internal function to load the plugin and call the event function in the plugin.
150-
private function callPluginEvent($event_name, $data) {
158+
private function callPluginEvent($event_name, $data, $return_data = false) {
151159
global $app;
152160

161+
$result = '';
162+
153163
//* execute the functions for the events
154164
if(@is_array($_SESSION['s']['plugin_cache'][$event_name])) {
155165
foreach($_SESSION['s']['plugin_cache'][$event_name] as $rec) {
@@ -175,12 +185,15 @@ private function callPluginEvent($event_name, $data) {
175185
if($this->debug) $app->log("Called method: '$function_name' in plugin '$plugin_name' for event '$event_name'", LOGLEVEL_DEBUG);
176186
// call_user_method($function_name,$app->loaded_plugins[$plugin_name],$event_name,$data);
177187

178-
call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
188+
$tmpresult = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
189+
if($return_data == true && $tmpresult) $result .= $tmpresult;
179190

180191
}
181192
}
182193

183194
}
195+
196+
if($return_data == true) return $result;
184197

185198
} // end functiom callPluginEvent
186199

interface/lib/classes/tpl.inc.php

Lines changed: 39 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,28 @@ 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 ($type, $name)
1077+
{
1078+
global $app;
1079+
1080+
$module_name = '';
1081+
if(strpos($name, ':') !== false) list($name, $module_name) = explode(':', $name, 2);
1082+
1083+
$result = $app->plugin->raiseEvent('on_template_content_hook', array(
1084+
'type' => $type,
1085+
'name' => $name,
1086+
'module' => $module_name
1087+
), true);
1088+
if(!$result) $result = '';
1089+
1090+
return $result;
1091+
}
10641092

10651093
/**
10661094
* returns a string used for parsing in tmpl_loop statements.
@@ -1254,7 +1282,10 @@ private function _parseTag ($args)
12541282
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
12551283
return '<?php include(\''.$file.'\'); ?>';
12561284
}
1257-
1285+
1286+
case 'hook':
1287+
return $this->_parseHook(@$var, @$value);
1288+
12581289
case 'include':
12591290
return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
12601291

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/sites/templates/web_vhost_domain_edit.htm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h1><tmpl_var name="list_head_txt"></h1>
1313
</tmpl_if>
1414

1515

16-
16+
{tmpl_hook name="begin_form"}
1717
<tmpl_if name="vhostdomain_type" value="domain">
1818
<tmpl_if name="is_admin">
1919
<div class="form-group">
@@ -222,26 +222,32 @@ <h1><tmpl_var name="list_head_txt"></h1>
222222
{tmpl_var name='php'}
223223
</select></div>
224224
</div>
225+
{tmpl_hook name="begin_field" value="fastcgi_php_version"}
225226
<div class="form-group fastcgi_php_version">
226227
<label for="fastcgi_php_version" class="col-sm-3 control-label">{tmpl_var name='fastcgi_php_version_txt'}</label>
227228
<div class="col-sm-9"><select name="fastcgi_php_version" id="fastcgi_php_version" class="form-control">
228229
{tmpl_var name='fastcgi_php_version'}
229230
</select></div>
230231
</div>
232+
{tmpl_hook name="end_field" value="fastcgi_php_version"}
231233
{tmpl_var name="directive_snippets_id"}
234+
{tmpl_hook name="begin_field" value="enable_pagespeed"}
232235
<div class="form-group nginx pagespeed">
233236
<label class="col-sm-3 control-label">{tmpl_var name='enable_pagespeed_txt'}</label>
234237
<div class="col-sm-9">
235238
{tmpl_var name="enable_pagespeed"}
236239
</div>
237240
</div>
241+
{tmpl_hook name="end_field" value="enable_pagespeed"}
242+
{tmpl_hook name="begin_field" value="active"}
238243
<div class="form-group">
239244
<label class="col-sm-3 control-label">{tmpl_var name='active_txt'}</label>
240245
<div class="col-sm-9">
241246
{tmpl_var name='active'}
242247
</div>
243248
</div>
244-
249+
{tmpl_hook name="end_field" value="active"}
250+
{tmpl_hook name="end_form"}
245251

246252
<input type="hidden" name="id" value="{tmpl_var name='id'}">
247253

0 commit comments

Comments
 (0)