From 20740a9650b24711b22d2597c1cff4d69d574b84 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 04 Feb 2015 04:21:00 -0500
Subject: [PATCH] Fix error in exec_hook() in case some hook handler was unregistered before

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

diff --git a/program/lib/Roundcube/rcube_plugin_api.php b/program/lib/Roundcube/rcube_plugin_api.php
index 346e832..23732ef 100644
--- a/program/lib/Roundcube/rcube_plugin_api.php
+++ b/program/lib/Roundcube/rcube_plugin_api.php
@@ -374,9 +374,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);
         }
     }
 
@@ -400,8 +402,9 @@
         $args += array('abort' => false);
         array_push($this->exec_stack, $hook);
 
-        foreach ((array)$this->handlers[$hook] as $callback) {
-            $ret = call_user_func($callback, $args);
+        // Use for loop here, so handlers added in the hook will be executed too
+        for ($i = 0; $i < count($this->handlers[$hook]); $i++) {
+            $ret = call_user_func($this->handlers[$hook][$i], $args);
             if ($ret && is_array($ret)) {
                 $args = $ret + $args;
             }

--
Gitblit v1.9.1