From 50b04366ee7472272e2576d17c609e1d26345221 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 19 Feb 2015 04:25:38 -0500
Subject: [PATCH] Merge branch 'master' of github.com:roundcube/roundcubemail

---
 program/lib/Roundcube/rcube_plugin_api.php |   55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/program/lib/Roundcube/rcube_plugin_api.php b/program/lib/Roundcube/rcube_plugin_api.php
index b3a3298..8fd3253 100644
--- a/program/lib/Roundcube/rcube_plugin_api.php
+++ b/program/lib/Roundcube/rcube_plugin_api.php
@@ -34,6 +34,8 @@
     public $dir;
     public $url = 'plugins/';
     public $task = '';
+    public $initialized = false;
+
     public $output;
     public $handlers              = array();
     public $allowed_prefs         = array();
@@ -85,12 +87,20 @@
     {
         $this->task   = $task;
         $this->output = $app->output;
-
         // register an internal hook
         $this->register_hook('template_container', array($this, 'template_container_hook'));
-
         // maybe also register a shudown function which triggers
         // shutdown functions of all plugin objects
+
+        foreach ($this->plugins as $plugin) {
+            // ... task, request type and framed mode
+            if (!$this->filter($plugin)) {
+                $plugin->init();
+            }
+        }
+
+        // we have finished initializing all plugins
+        $this->initialized = true;
     }
 
     /**
@@ -171,13 +181,21 @@
                 // check inheritance...
                 if (is_subclass_of($plugin, 'rcube_plugin')) {
                     // ... task, request type and framed mode
-                    if (($force || !$plugin->task || preg_match('/^('.$plugin->task.')$/i', $this->task))
-                        && (!$plugin->noajax || (is_object($this->output) && $this->output->type == 'html'))
-                        && (!$plugin->noframe || empty($_REQUEST['_framed']))
-                    ) {
-                        $plugin->init();
-                        $this->plugins[$plugin_name] = $plugin;
+
+                    // call onload method on plugin if it exists.
+                    // this is useful if you want to be called early in the boot process
+                    if (method_exists($plugin, 'onload')) {
+                        $plugin->onload();
                     }
+
+                    // init a plugin only if $force is set or if we're called after initialization
+                    if (($force || $this->initialized)
+                        && !$this->filter($plugin))
+                    {
+                        $plugin->init();
+                    }
+
+                    $this->plugins[$plugin_name] = $plugin;
 
                     if (!empty($plugin->allowed_prefs)) {
                         $this->allowed_prefs = array_merge($this->allowed_prefs, $plugin->allowed_prefs);
@@ -200,6 +218,19 @@
         }
 
         return false;
+    }
+
+    /**
+     * check if we should prevent this plugin from initialising
+     *
+     * @param $plugin
+     * @return bool
+     */
+    private function filter($plugin)
+    {
+        return (($plugin->noajax  && !(is_object($this->output) && $this->output->type == 'html') )
+             || ($plugin->task && !preg_match('/^('.$plugin->task.')$/i', $this->task))
+             || ($plugin->noframe && !empty($_REQUEST['_framed']))) ? true : false;
     }
 
     /**
@@ -374,9 +405,11 @@
      */
     public function unregister_hook($hook, $callback)
     {
-        $callback_id = array_search($callback, $this->handlers[$hook]);
+        $callback_id = array_search($callback, (array) $this->handlers[$hook]);
         if ($callback_id !== false) {
-            unset($this->handlers[$hook][$callback_id]);
+            // array_splice() removes the element and re-indexes keys
+            // that is required by the 'for' loop in exec_hook() below
+            array_splice($this->handlers[$hook], $callback_id, 1);
         }
     }
 
@@ -395,7 +428,7 @@
             $args = array('arg' => $args);
         }
 
-        // TODO: avoid recusion by checking in_array($hook, $this->exec_stack) ?
+        // TODO: avoid recursion by checking in_array($hook, $this->exec_stack) ?
 
         $args += array('abort' => false);
         array_push($this->exec_stack, $hook);

--
Gitblit v1.9.1