Marius Burkard
2015-12-14 1b9d2f3435f9d90443f68017cb0cd4abbe69ae52
- Added template content hook feature
4 files modified
91 ■■■■ changed files
interface/lib/classes/plugin.inc.php 31 ●●●● patch | view | raw | blame | history
interface/lib/classes/tpl.inc.php 41 ●●●● patch | view | raw | blame | history
interface/lib/classes/tpl_cache.inc.php 9 ●●●●● patch | view | raw | blame | history
interface/web/sites/templates/web_vhost_domain_edit.htm 10 ●●●● patch | view | raw | blame | history
interface/lib/classes/plugin.inc.php
@@ -107,7 +107,7 @@
        This function is called when a certian action occurs, e.g. a form gets saved or a user is logged in.
    */
    public function raiseEvent($event_name, $data) {
    public function raiseEvent($event_name, $data, $return_data = false) {
        global $app;
        if(!isset($_SESSION['s']['plugin_cache'])) {
@@ -115,20 +115,25 @@
            if($this->debug) $app->log('Loaded the plugin cache.', LOGLEVEL_DEBUG);
        }
        $result = '';
        $sub_events = explode(':', $event_name);
        if(is_array($sub_events)) {
            if(count($sub_events) == 3) {
                $tmp_event = $sub_events[2];
                $mp_event = $sub_events[2];
                if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
                $this->callPluginEvent($tmp_event, $data);
                $tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
                if($return_data == true && $tmpresult) $result .= $tmpresult;
                $tmp_event = $sub_events[0].':'.$sub_events[2];
                if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
                $this->callPluginEvent($tmp_event, $data);
                $tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
                if($return_data == true && $tmpresult) $result .= $tmpresult;
                $tmp_event = $sub_events[0].':'.$sub_events[1].':'.$sub_events[2];
                if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
                $this->callPluginEvent($tmp_event, $data);
                $tmpresult = $this->callPluginEvent($tmp_event, $data, $return_data);
                if($return_data == true && $tmpresult) $result .= $tmpresult;
                /*$sub_events = array_reverse($sub_events);
                $tmp_event = '';
@@ -140,15 +145,20 @@
                */
            } else {
                if($this->debug) $app->log("Called Event '$sub_events[0]'", LOGLEVEL_DEBUG);
                $this->callPluginEvent($sub_events[0], $data);
                $tmpresult = $this->callPluginEvent($sub_events[0], $data, $return_data);
                if($return_data == true && $tmpresult) $result .= $tmpresult;
            }
        }
        if($return_data == true) return $result;
    } // end function raiseEvent
    //* Internal function to load the plugin and call the event function in the plugin.
    private function callPluginEvent($event_name, $data) {
    private function callPluginEvent($event_name, $data, $return_data = false) {
        global $app;
        $result = '';
        //* execute the functions for the events
        if(@is_array($_SESSION['s']['plugin_cache'][$event_name])) {
@@ -175,13 +185,16 @@
                    if($this->debug) $app->log("Called method: '$function_name' in plugin '$plugin_name' for event '$event_name'", LOGLEVEL_DEBUG);
                    // call_user_method($function_name,$app->loaded_plugins[$plugin_name],$event_name,$data);
                    call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
                    $tmpresult = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
                    if($return_data == true && $tmpresult) $result .= $tmpresult;
                }
            }
        }
        if($return_data == true) return $result;
    } // end functiom callPluginEvent
interface/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
                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,28 @@
            }
        }
        /**
         * returns a string containing hook data
         * @param string $type
         * @param string $name
         * @return string hook data
         */
        private function _parseHook ($type, $name)
        {
            global $app;
            $module_name = '';
            if(strpos($name, ':') !== false) list($name, $module_name) = explode(':', $name, 2);
            $result = $app->plugin->raiseEvent('on_template_content_hook', array(
                'type' => $type,
                'name' => $name,
                'module' => $module_name
            ), true);
            if(!$result) $result = '';
            return $result;
        }
        /**
         * returns a string used for parsing in tmpl_loop statements.
@@ -1255,6 +1283,9 @@
                    return '<?php include(\''.$file.'\'); ?>';
                }
            case 'hook':
                return $this->_parseHook(@$var, @$value);
            case 'include':
                return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
interface/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'];
    }
interface/web/sites/templates/web_vhost_domain_edit.htm
@@ -13,7 +13,7 @@
</tmpl_if>
        {tmpl_hook name="begin_form"}
        <tmpl_if name="vhostdomain_type" value="domain">
            <tmpl_if name="is_admin">
                <div class="form-group">
@@ -222,26 +222,32 @@
                    {tmpl_var name='php'}
                </select></div>
            </div>
            {tmpl_hook name="begin_field" value="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="fastcgi_php_version"}
            {tmpl_var name="directive_snippets_id"}
            {tmpl_hook name="begin_field" value="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="enable_pagespeed"}
            {tmpl_hook name="begin_field" value="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="active"}
            {tmpl_hook name="end_form"}
        <input type="hidden" name="id" value="{tmpl_var name='id'}">