From 216ea12666892397cd145cdef3c40d37b3df22a7 Mon Sep 17 00:00:00 2001
From: Marius Burkard <m.burkard@pixcept.de>
Date: Wed, 16 Dec 2015 11:03:28 -0500
Subject: [PATCH] - Changed template events (reduced php code and needed template code)

---
 server/conf/vhost.conf.master                           |    7 ++
 interface/web/sites/templates/web_vhost_domain_edit.htm |   15 ++---
 interface/lib/classes/tform_actions.inc.php             |    2 
 interface/lib/classes/tpl.inc.php                       |   39 +++++++++---
 server/lib/classes/tpl.inc.php                          |   48 +++++++++++++--
 server/lib/classes/tpl_cache.inc.php                    |    9 +-
 server/lib/classes/plugins.inc.php                      |   17 ++++-
 interface/lib/classes/tform_base.inc.php                |    6 +-
 8 files changed, 101 insertions(+), 42 deletions(-)

diff --git a/interface/lib/classes/tform_actions.inc.php b/interface/lib/classes/tform_actions.inc.php
index 8461386..7ab09ae 100644
--- a/interface/lib/classes/tform_actions.inc.php
+++ b/interface/lib/classes/tform_actions.inc.php
@@ -46,7 +46,7 @@
 		$app->tpl->newTemplate("tabbed_form.tpl.htm");
 
 		// Load table definition from file
-		$app->tform->loadFormDef($tform_def_file);
+		$app->tform->loadFormDef($tform_def_file, (isset($_SESSION['s']['module']['name']) ? $_SESSION['s']['module']['name'] : ''));
 
 		// Importing ID
 		$this->id = (isset($_REQUEST["id"]))?$app->functions->intval($_REQUEST["id"]):0;
diff --git a/interface/lib/classes/tform_base.inc.php b/interface/lib/classes/tform_base.inc.php
index 4cc98a4..2e53d27 100644
--- a/interface/lib/classes/tform_base.inc.php
+++ b/interface/lib/classes/tform_base.inc.php
@@ -126,8 +126,8 @@
 	function loadFormDef($file, $module = '') {
 		global $app, $conf;
 
-		$app->plugin->raiseEvent('on_before_formdef', $this);
 		include $file;
+		$app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$form['name'] . ':on_before_formdef', $this);
 		$this->formDef = $form;
 
 		$this->module = $module;
@@ -152,9 +152,9 @@
 		}
 		if(isset($wb_global)) unset($wb_global);
 		
-		$app->plugin->raiseEvent('on_after_formdef', $this);
-
 		$this->wordbook = $wb;
+		
+		$app->plugin->raiseEvent($_SESSION['s']['module']['name'].':'.$app->tform->formDef['name'] . ':on_after_formdef', $this);
 
 		$this->dateformat = $app->lng('conf_format_dateshort');
 		$this->datetimeformat = $app->lng('conf_format_datetime');
diff --git a/interface/lib/classes/tpl.inc.php b/interface/lib/classes/tpl.inc.php
index 3210cc0..928c215 100644
--- a/interface/lib/classes/tpl.inc.php
+++ b/interface/lib/classes/tpl.inc.php
@@ -1073,20 +1073,37 @@
 		 * @param string $name
 		 * @return string hook data
 		 */
-		private function _parseHook ($type, $name)
+		private function _parseHook ($name)
 		{
 			global $app;
 			
-			$module_name = '';
-			if(strpos($name, ':') !== false) list($module_name, $name) = explode(':', $name, 2);
+			if(!$name) return false;
 			
-			$result = $app->plugin->raiseEvent('on_template_content_hook', array(
-				'type' => $type,
-				'name' => $name,
-				'module' => $module_name
-			), true);
-			if(!$result) $result = '';
-			else $result = $this->_getData($result, false, true);
+			$module = isset($_SESSION['s']['module']['name']) ? $_SESSION['s']['module']['name'] : '';
+			$form = isset($app->tform->formDef['name']) ? $app->tform->formDef['name'] : '';
+			
+			$events = array();
+			if($module) {
+				$events[] = $module . ':' . ($form ? $form : '') . ':' . $name;
+				$events[] = $module . ':' . ($form ? $form : '') . ':on_template_content';
+			} else {
+				$events[] = $name;
+				$events[] = 'on_template_content';
+			}
+			
+			$events = array_unique($events);
+			
+			for($e = 0; $e < count($events); $e++) {
+				$tmpresult = $app->plugin->raiseEvent($events[$e], array(
+					'name' => $name,
+					'module' => $module,
+					'form' => $form
+				), true);
+				if(!$tmpresult) $tmpresult = '';
+				else $tmpresult = $this->_getData($tmpresult, false, true);
+				
+				$result .= $tmpresult;
+			}
 			
 			return $result;
 		}
@@ -1285,7 +1302,7 @@
 				}
 			
 			case 'hook':
-				return $this->_parseHook(@$var, @$value);
+				return $this->_parseHook(@$var);
 			
 			case 'include':
 				return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
diff --git a/interface/web/sites/templates/web_vhost_domain_edit.htm b/interface/web/sites/templates/web_vhost_domain_edit.htm
index d28f0b7..230c8d0 100644
--- a/interface/web/sites/templates/web_vhost_domain_edit.htm
+++ b/interface/web/sites/templates/web_vhost_domain_edit.htm
@@ -13,7 +13,7 @@
 </tmpl_if>
 
 
-        {tmpl_hook name="begin_form" value="web_vhost_domain_edit"}
+        {tmpl_hook name="begin_form"}
 		<tmpl_if name="vhostdomain_type" value="domain">
             <tmpl_if name="is_admin">
                 <div class="form-group">
@@ -222,32 +222,29 @@
                     {tmpl_var name='php'}
                 </select></div>
             </div>
-            {tmpl_hook name="begin_field" value="web_vhost_domain_edit:fastcgi_php_version"}
+            {tmpl_hook name="field_fastcgi_php_version"}
             <div class="form-group fastcgi_php_version">
                 <label for="fastcgi_php_version" class="col-sm-3 control-label">{tmpl_var name='fastcgi_php_version_txt'}</label>
                 <div class="col-sm-9"><select name="fastcgi_php_version" id="fastcgi_php_version" class="form-control">
                     {tmpl_var name='fastcgi_php_version'}
                 </select></div>
             </div>
-            {tmpl_hook name="end_field" value="web_vhost_domain_edit:fastcgi_php_version"}
-			{tmpl_var name="directive_snippets_id"}
-			{tmpl_hook name="begin_field" value="web_vhost_domain_edit:enable_pagespeed"}
+            {tmpl_var name="directive_snippets_id"}
+			{tmpl_hook name="field_enable_pagespeed"}
 			<div class="form-group nginx pagespeed">
 				<label class="col-sm-3 control-label">{tmpl_var name='enable_pagespeed_txt'}</label>
 				<div class="col-sm-9">
 					{tmpl_var name="enable_pagespeed"}
 				</div>
 			</div>
-			{tmpl_hook name="end_field" value="web_vhost_domain_edit:enable_pagespeed"}
-			{tmpl_hook name="begin_field" value="web_vhost_domain_edit:active"}
+            {tmpl_hook name="field_active"}
             <div class="form-group">
                 <label class="col-sm-3 control-label">{tmpl_var name='active_txt'}</label>
                 <div class="col-sm-9">
                     {tmpl_var name='active'}
                 </div>
             </div>
-            {tmpl_hook name="end_field" value="web_vhost_domain_edit:active"}
-			{tmpl_hook name="end_form" value="web_vhost_domain_edit"}
+			{tmpl_hook name="end_form"}
 
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
 
diff --git a/server/conf/vhost.conf.master b/server/conf/vhost.conf.master
index bdfc761..ab8430d 100644
--- a/server/conf/vhost.conf.master
+++ b/server/conf/vhost.conf.master
@@ -1,3 +1,4 @@
+<tmpl_hook name='apache2_vhost:header'>
 
 <Directory {tmpl_var name='web_basedir'}/{tmpl_var name='domain'}>
 		AllowOverride None
@@ -9,8 +10,9 @@
 		</tmpl_if>
 </Directory>
 
-<tmpl_loop name="vhosts">
+<tmpl_loop name='vhosts'>
 <VirtualHost {tmpl_var name='ip_address'}:{tmpl_var name='port'}>
+<tmpl_hook name='apache2_vhost:vhost_header'>
 <tmpl_if name='php' op='==' value='suphp'>
 		DocumentRoot <tmpl_var name='web_document_root'>
 </tmpl_else>
@@ -444,5 +446,8 @@
 		</IfModule>
 
 <tmpl_var name='apache_directives'>
+<tmpl_hook name='apache2_vhost:vhost_footer'>
 </VirtualHost>
 </tmpl_loop>
+
+<tmpl_hook name='apache2_vhost:footer'>
\ No newline at end of file
diff --git a/server/lib/classes/plugins.inc.php b/server/lib/classes/plugins.inc.php
index 4771487..9b9b143 100644
--- a/server/lib/classes/plugins.inc.php
+++ b/server/lib/classes/plugins.inc.php
@@ -73,7 +73,6 @@
 		} else {
 			$app->log('Plugins directory missing: '.$plugins_dir, LOGLEVEL_ERROR);
 		}
-
 	}
 
 	/*
@@ -138,12 +137,14 @@
 	}
 
 
-	function raiseAction($action_name, $data) {
+	function raiseAction($action_name, $data, $return_data = false) {
 		global $app;
 
 		//* Get the subscriptions for this action
 		$actions = (isset($this->subscribed_actions[$action_name]))?$this->subscribed_actions[$action_name]:'';
 		if($this->debug) $app->log('Raised action: '.$action_name, LOGLEVEL_DEBUG);
+
+		$result = '';
 
 		if(is_array($actions)) {
 			foreach($actions as $action) {
@@ -154,8 +155,13 @@
 				$app->log("Calling function '$function_name' from plugin '$plugin_name' raised by action '$action_name'.", LOGLEVEL_DEBUG);
 				$state = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $action_name, $data);
 				//* ensure that we return the highest warning / error level if a error occured in one of the functions
-				if($state == 'warning' && $state_out != 'error') $state_out = 'warning';
-				if($state == 'error') $state_out = 'error';
+				if($return_data) {
+					if($state) $result .= $state;
+				} else {
+					if($state == 'warning' && $state_out != 'error') $state_out = 'warning';
+					elseif($state == 'error') $state_out = 'error';
+				}
+				
 				unset($plugin_name);
 				unset($function_name);
 			}
@@ -163,7 +169,8 @@
 		unset($action);
 		unset($actions);
 
-		return $state_out;
+		if($return_data == true) return $result;
+		else return $state_out;
 	}
 
 }
diff --git a/server/lib/classes/tpl.inc.php b/server/lib/classes/tpl.inc.php
index 34df2a9..5e595f6 100644
--- a/server/lib/classes/tpl.inc.php
+++ b/server/lib/classes/tpl.inc.php
@@ -839,28 +839,34 @@
 		 * @access private
 		 * @return mixed data/string or boolean
 		 */
-		private function _getData ($tmplfile, $do_eval=false)
+		private function _getData ($tmplfile, $do_eval=false, $tmpl_from_string = false)
 		{
 			//* check the current file depth
 			if ($this->_includedepth > $this->OPTIONS['MAX_INCLUDES'] || $tmplfile == false) {
 				return;
 			} else {
 				if ($this->_debug){
-					array_push($this->_debugIncludedfiles, $tmplfile);
+					if($tmpl_from_string) array_push($this->_debugIncludedfiles, 'String: ' . substr($tmplfile, 0, 25) . '...');
+					else array_push($this->_debugIncludedfiles, $tmplfile);
 				}
 				if ($do_eval) {
-					array_push($this->_currentincludedir, dirname($tmplfile));
+					if($tmpl_from_string == true) array_push($this->_currentincludedir, end($this->_currentincludedir));
+					else array_push($this->_currentincludedir, dirname($tmplfile));
 					$this->_includedepth++;
 				}
 			}
 
 
-			if($this->_cache && $this->_checkCache($tmplfile)) { //* cache exists so lets use it
+			if($this->_cache && $this->_checkCache($tmplfile, $tmpl_from_string)) { //* cache exists so lets use it
 				$data = fread($fp = fopen($this->_cachefile, 'r'), filesize($this->_cachefile));
 				fclose($fp);
 			} else { //* no cache lets parse the file
-				$data = fread($fp = fopen($tmplfile, 'r'), filesize($tmplfile));
-				fclose($fp);
+				if($tmpl_from_string == true) {
+					$data = $tmplfile;
+				} else {
+					$data = fread($fp = fopen($tmplfile, 'r'), filesize($tmplfile));
+					fclose($fp);
+				}
 
 				$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
 				$regex.= 'tmpl_([\w]+)\s*';
@@ -884,7 +890,7 @@
 			}
 
 			//* now we must parse the $data and check for any <tmpl_include>'s
-			if ($this->_debug) $this->doDebugWarnings(file($tmplfile), $tmplfile);
+			if ($this->_debug && $tmpl_from_string == false) $this->doDebugWarnings(file($tmplfile), $tmplfile);
 
 			if ($do_eval) {
 				$success = @eval('?>'.$data.'<?php return 1;');
@@ -1061,6 +1067,29 @@
 			}
 		}
 
+		/**
+		 * returns a string containing hook data
+		 * @param string $type
+		 * @param string $name
+		 * @return string hook data
+		 */
+		private function _parseHook ($name)
+		{
+			global $app;
+			
+			$namespace = '';
+			if(strpos($name, ':') !== false) list($namespace, $name) = explode(':', $name, 2);
+			
+			$result = $app->plugins->raiseAction('on_template_content_hook', array(
+				'name' => $name,
+				'namespace' => $namespace,
+				'vars' => $this->_vars
+			), true);
+			if(!$result) $result = '';
+			else $result = $this->_getData($result, false, true);
+			
+			return $result;
+		}
 
 		/**
 		 * returns a string used for parsing in tmpl_loop statements.
@@ -1254,7 +1283,10 @@
 				if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
 					return '<?php include(\''.$file.'\'); ?>';
 				}
-
+			
+			case 'hook':
+				return $this->_parseHook(@$var);
+			
 			case 'include':
 				return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
 
diff --git a/server/lib/classes/tpl_cache.inc.php b/server/lib/classes/tpl_cache.inc.php
index 4bf75fa..ce8a5b1 100644
--- a/server/lib/classes/tpl_cache.inc.php
+++ b/server/lib/classes/tpl_cache.inc.php
@@ -101,8 +101,8 @@
 	 * FUNCTION: _checkCache
 	 * checks if there's a cache, if there is then it will read the cache file as the template.
 	 */
-	function _checkCache ($tmplfile) {
-		$this->_cachefile = $this->_getFilename($tmplfile);
+	function _checkCache ($tmplfile, $tmpl_from_string = false) {
+		$this->_cachefile = $this->_getFilename($tmplfile, $tmpl_from_string);
 		if ($this->_clearcache) {
 			if (file_exists($this->_cachefile)) unlink($this->_cachefile);
 			return false;
@@ -133,8 +133,9 @@
 	 * gets the full pathname for the cached file
 	 *
 	 */
-	function _getFilename($tmplfile) {
-		return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION'];
+	function _getFilename($tmplfile, $tmpl_from_string = false) {
+		if($tmpl_from_string == true) return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaRSTRING'.$tmplfile).'.'.$this->OPTIONS['CACHE_EXTENSION'];
+		else return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION'];
 	}
 
 

--
Gitblit v1.9.1