Marius Cramer
2013-11-14 b1a6a5a3991cec5cd08873b01376e45d0b247f18
interface/lib/classes/plugin.inc.php
@@ -29,92 +29,92 @@
*/
class plugin {
   private $subscribed_events = array();
   private $debug = false;
   /*
    This function is called to load the plugins from the plugins folder and update the plugin cache
   */
   private function loadPluginCache() {
      global $app,$conf;
      global $app, $conf;
      if(isset($_SESSION['s']['plugin_cache'])) unset($_SESSION['s']['plugin_cache']);
      $plugins_dir = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV;
      $_SESSION['s']['plugin_cache'] = array();
      $tmp_plugins = array();
      if (is_dir($plugins_dir)) {
         if ($dh = opendir($plugins_dir)) {
            //** Go trough all files in the plugin dir
            while (($file = readdir($dh)) !== false) {
               if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
                  $plugin_name = substr($file,0,-8);
               if($file != '.' && $file != '..' && substr($file, -8, 8) == '.inc.php') {
                  $plugin_name = substr($file, 0, -8);
                  $tmp_plugins[$plugin_name] = $file;
               }
            }
            //** sort the plugins by name
            ksort($tmp_plugins);
            //** load the plugins
            foreach($tmp_plugins as $plugin_name => $file) {
               include_once($plugins_dir.$file);
               if($this->debug) $app->log('Loading plugin: '.$plugin_name,LOGLEVEL_DEBUG);
               include_once $plugins_dir.$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();
            }
         } else {
            $app->log('Unable to open the plugins directory: '.$plugins_dir,LOGLEVEL_ERROR);
            $app->log('Unable to open the plugins directory: '.$plugins_dir, LOGLEVEL_ERROR);
         }
      } else {
         $app->log('Plugins directory missing: '.$plugins_dir,LOGLEVEL_ERROR);
         $app->log('Plugins directory missing: '.$plugins_dir, LOGLEVEL_ERROR);
      }
   }
   /*
    This function is called by the plugin to register for an event which is saved into the plugin cache
    This function is called by the plugin to register for an event which is saved into the plugin cache
    for faster lookups without the need to load all plugins for every page.
   */
   public function registerEvent($event_name,$plugin_name,$function_name) {
   public function registerEvent($event_name, $plugin_name, $function_name) {
      global $app;
      $_SESSION['s']['plugin_cache'][$event_name][] = array('plugin' => $plugin_name, 'function' => $function_name);
      if($this->debug) $app->log("Plugin '$plugin_name' has registered the function '$function_name' for the event '$event_name'",LOGLEVEL_DEBUG);
      if($this->debug) $app->log("Plugin '$plugin_name' has registered the function '$function_name' for the event '$event_name'", LOGLEVEL_DEBUG);
   }
   /*
      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) {
      global $app;
      if(!isset($_SESSION['s']['plugin_cache'])) {
         $this->loadPluginCache();
         if($this->debug) $app->log('Loaded the plugin cache.',LOGLEVEL_DEBUG);
         if($this->debug) $app->log('Loaded the plugin cache.', LOGLEVEL_DEBUG);
      }
      $sub_events = explode(':',$event_name);
      $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);
            if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
            $this->callPluginEvent($tmp_event, $data);
            $tmp_event = $sub_events[0].':'.$sub_events[2];
            if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG);
            $this->callPluginEvent($tmp_event,$data);
            if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
            $this->callPluginEvent($tmp_event, $data);
            $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);
            if($this->debug) $app->log("Called Event '$tmp_event'", LOGLEVEL_DEBUG);
            $this->callPluginEvent($tmp_event, $data);
            /*$sub_events = array_reverse($sub_events);
            $tmp_event = '';
            foreach($sub_events as $n => $sub_event) {
@@ -124,44 +124,44 @@
            }
            */
         } else {
            if($this->debug) $app->log("Called Event '$sub_events[0]'",LOGLEVEL_DEBUG);
            $this->callPluginEvent($sub_events[0],$data);
            if($this->debug) $app->log("Called Event '$sub_events[0]'", LOGLEVEL_DEBUG);
            $this->callPluginEvent($sub_events[0], $data);
         }
      }
    } // end function raiseEvent
    //* Internal function to load the plugin and call the event function in the plugin.
    private function callPluginEvent($event_name,$data) {
       global $app;
       //* execute the functions for the events
   } // end function raiseEvent
   //* Internal function to load the plugin and call the event function in the plugin.
   private function callPluginEvent($event_name, $data) {
      global $app;
      //* 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'];
            $plugin_file = ISPC_LIB_PATH.FS_DIV.'plugins'.FS_DIV.$plugin_name.'.inc.php';
            if(is_file($plugin_file)) {
               if(!isset($app->loaded_plugins[$plugin_name])) {
                  include_once($plugin_file);
                  include_once $plugin_file;
                  $app->loaded_plugins[$plugin_name] = new $plugin_name;
               }
               if($this->debug) $app->log("Called method: '$function_name' in plugin '$plugin_name' for event '$event_name'",LOGLEVEL_DEBUG);
               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);
               call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
            }
         }
      }
    } // end functiom callPluginEvent
   } // end functiom callPluginEvent
}
?>