Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
interface/lib/classes/plugin.inc.php
@@ -45,12 +45,12 @@
      if(isset($_SESSION['s']['plugin_cache'])) unset($_SESSION['s']['plugin_cache']);
      
      $plugin_dirs = array();
      $plugin_dirs[] = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV;
      $plugin_dirs[] = ISPC_LIB_PATH.FS_DIV.'plugins';
      
      if(is_dir(ISPC_WEB_PATH)) {
         if($dh = opendir(ISPC_WEB_PATH)) {
            while(($file = readdir($dh)) !== false) {
               if($file !== '.' && $file !== '..' && is_dir($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';
               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';
            }
            closedir($dh);
         }
@@ -63,6 +63,7 @@
         $plugins_dir = $plugin_dirs[$d];
         if (is_dir($plugins_dir)) {
            if ($dh = opendir($plugins_dir)) {
               $tmp_plugins = array();
               //** Go trough all files in the plugin dir
               while (($file = readdir($dh)) !== false) {
                  if($file !== '.' && $file !== '..' && substr($file, -8, 8) == '.inc.php') {
@@ -76,7 +77,7 @@
               //** load the plugins
               foreach($tmp_plugins as $plugin_name => $file) {
                  include_once $plugins_dir.$file;
                  require $plugins_dir . FS_DIV . $file;
                  if($this->debug) $app->log('Loading plugin: '.$plugin_name, LOGLEVEL_DEBUG);
                  $app->loaded_plugins[$plugin_name] = new $plugin_name;
                  $app->loaded_plugins[$plugin_name]->onLoad();
@@ -107,28 +108,33 @@
      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'])) {
         $this->loadPluginCache();
         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];
            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,22 +146,27 @@
            */
         } 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])) {
         foreach($_SESSION['s']['plugin_cache'][$event_name] as $rec) {
            $plugin_name = $rec['plugin'];
            $function_name = $rec['function'];
            $module_name = $rec['function'];
            $module_name = $rec['module'];
            if($module_name != '') {
               if(strpos($module_name, '..') !== false || strpos($module_name, '/') !== false) {
                  if($this->debug) $app->log('Module name ' . $module_name . ' contains illegal characters.', LOGLEVEL_DEBUG);
@@ -175,12 +186,14 @@
               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