Use injector to get IMP_Contents singleton instance
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Apr 2010 07:38:15 +0000 (01:38 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Apr 2010 20:03:53 +0000 (14:03 -0600)
23 files changed:
imp/compose-dimp.php
imp/compose-mimp.php
imp/compose.php
imp/lib/Ajax/Application.php
imp/lib/Application.php
imp/lib/Compose.php
imp/lib/Contents.php
imp/lib/Filter.php
imp/lib/Injector/Binder/Contents.php [new file with mode: 0644]
imp/lib/Injector/Factory/Contents.php [new file with mode: 0644]
imp/lib/Mailbox.php
imp/lib/Message.php
imp/lib/Mime/Viewer/Partial.php
imp/lib/Spam.php
imp/lib/Ui/Compose.php
imp/lib/Views/ShowMessage.php
imp/message-mimp.php
imp/message.php
imp/pgp.php
imp/saveimage.php
imp/smime.php
imp/thread.php
imp/view.php

index 7f44336..2c02791 100644 (file)
@@ -71,7 +71,7 @@ if (in_array($vars->type, array('reply', 'reply_all', 'reply_auto', 'reply_list'
     }
 
     try {
-        $imp_contents = IMP_Contents::singleton($vars->uid . IMP::IDX_SEP . $vars->folder);
+        $imp_contents = $injector->getInstance('IMP_Contents')->getOb($vars->folder, $vars->uid);
     } catch (Horde_Exception $e) {
         $notification->push(_("Requested message not found."), 'horde.error');
         $vars->uid = $vars->folder = null;
@@ -143,7 +143,7 @@ case 'forward_redirect':
 
 case 'resume':
     try {
-        $result = $imp_compose->resumeDraft($vars->uid . IMP::IDX_SEP . $vars->folder);
+        $result = $imp_compose->resumeDraft($vars->folder, $vars->uid);
 
         if ($result['mode'] == 'html') {
             $show_editor = true;
index 2c9f54b..74304d2 100644 (file)
@@ -104,7 +104,7 @@ switch ($vars->a) {
 // 'd' = draft
 case 'd':
     try {
-        $result = $imp_compose->resumeDraft($imp_mbox['uid'] . IMP::IDX_SEP . $imp_mbox['thismailbox']);
+        $result = $imp_compose->resumeDraft($imp_mbox['thismailbox'], $imp_mbox['uid']);
 
         $msg = $result['msg'];
         $header = array_merge($header, $result['header']);
index 5eb8160..5999fba 100644 (file)
@@ -234,7 +234,7 @@ case 'mailto_link':
 
 case 'draft':
     try {
-        $result = $imp_compose->resumeDraft($uid . IMP::IDX_SEP . $thismailbox);
+        $result = $imp_compose->resumeDraft($thismailbox, $uid);
 
         if (!is_null($rtemode)) {
             $rtemode = ($result['mode'] == 'html');
index d1211b0..abfa4a8 100644 (file)
@@ -1537,7 +1537,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
         if (!($imp_contents = $imp_compose->getContentsOb())) {
             $indices = $GLOBALS['imp_imap']->ob()->utils->fromSequenceString($this->_vars->uid);
             $i = each($indices);
-            $imp_contents = IMP_Contents::singleton(reset($i['value']) . IMP::IDX_SEP . $i['key']);
+            $imp_contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($i['key'], reset($i['value']));
         }
 
         return array($imp_compose, $imp_contents);
index 77608d3..ba8e502 100644 (file)
@@ -93,6 +93,7 @@ class IMP_Application extends Horde_Registry_Application
     {
         /* Add IMP-specific binders. */
         $binders = array(
+            'IMP_Contents' => new IMP_Injector_Binder_Contents(),
             'IMP_Crypt_Pgp' => new IMP_Injector_Binder_Pgp(),
             'IMP_Crypt_Smime' => new IMP_Injector_Binder_Smime(),
             'IMP_Folder' => new IMP_Injector_Binder_Folder(),
index 097aec1..e302089 100644 (file)
@@ -356,8 +356,8 @@ class IMP_Compose
     /**
      * Resumes a previously saved draft message.
      *
-     * @param string $uid  The IMAP message mailbox/index. The uid should
-     *                     be in IMP::parseIndicesList() format #1.
+     * @param string $mailbox  Draft mailbox.
+     * @param integer $uid     Message UID.
      *
      * @return mixed  An array with the following keys:
      * <pre>
@@ -368,11 +368,11 @@ class IMP_Compose
      * </pre>
      * @throws IMP_Compose_Exception
      */
-    public function resumeDraft($uid)
+    public function resumeDraft($mailbox, $uid)
     {
         try {
-            $contents = IMP_Contents::singleton($uid);
-        } catch (Horde_Exception $e) {
+            $contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mailbox, $uid);
+        } catch (IMP_Exception $e) {
             throw new IMP_Compose_Exception($e);
         }
 
@@ -436,15 +436,15 @@ class IMP_Compose
                     // even though the server is the same. UIDVALIDITY should
                     // catch any true server/backend changes.
                     ($GLOBALS['imp_imap']->checkUidvalidity($imap_url['mailbox']) == $imap_url['uidvalidity']) &&
-                    IMP_Contents::singleton($imap_url['uid'] . IMP::IDX_SEP . $imap_url['mailbox'])) {
+                    $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($imap_url['uid'] . IMP::IDX_SEP . $imap_url['mailbox'])) {
                     $this->_metadata['mailbox'] = $imap_url['mailbox'];
                     $this->_metadata['reply_type'] = $reply_type;
                     $this->_metadata['uid'] = $imap_url['uid'];
                 }
-            } catch (Horde_Exception $e) {}
+            } catch (Exception $e) {}
         }
 
-        $this->_metadata['draft_uid_resume'] = $uid;
+        $this->_metadata['draft_uid_resume'] = $uid . IMP::IDX_SEP . $mailbox;
         $this->_modified = true;
 
         return array(
@@ -1702,7 +1702,7 @@ class IMP_Compose
         foreach ($msgList as $mbox => $indicesList) {
             foreach ($indicesList as $idx) {
                 ++$attached;
-                $contents = IMP_Contents::singleton($idx . IMP::IDX_SEP . $mbox);
+                $contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mbox, $idx);
                 $headerob = $contents->getHeaderOb();
 
                 $part = new Horde_Mime_Part();
@@ -2713,7 +2713,7 @@ class IMP_Compose
     public function getContentsOb()
     {
         return $this->getMetadata('reply_type')
-            ? IMP_Contents::singleton($this->getMetadata('uid') . IMP::IDX_SEP . $this->getMetadata('mailbox'))
+            ? $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($this->getMetadata('mailbox'), $this->getMetadata('uid'))
             : null;
     }
 
index bb47e04..b480645 100644 (file)
@@ -44,21 +44,6 @@ class IMP_Contents
     public $lastBodyPartDecode = null;
 
     /**
-     * Singleton instances
-     *
-     * @var array
-     */
-    static protected $_instances = array();
-
-    /**
-     * Internal counter for ensuring Horde_Mime_Part objects passed to
-     * singleton() are given unique objects.
-     *
-     * @var integer
-     */
-    static protected $_mimepartid = 1;
-
-    /**
      * The IMAP UID of the message.
      *
      * @var integer
@@ -94,39 +79,13 @@ class IMP_Contents
     protected $_embedded = array();
 
     /**
-     * Attempts to return a reference to a concrete IMP_Contents instance.
-     * If an IMP_Contents object is currently stored in the local cache,
-     * recreate that object.  Else, create a new instance.
-     * Ensures that only one IMP_Contents instance for any given message is
-     * available at any one time.
-     *
-     * @param mixed $in  Either a UID string (UID . IMP::IDX_SEP . Mailbox) or
-     *                   a Horde_Mime_Part object.
-     *
-     * @return IMP_Contents  The IMP_Contents object.
-     * @throws Horde_Exception
-     */
-    static public function singleton($in)
-    {
-        $sig = ($in instanceof Horde_Mime_Part)
-            ? 'horde_mime_part_' . self::$_mimepartid++
-            : $in;
-
-        if (empty(self::$_instances[$sig])) {
-            self::$_instances[$sig] = new self($in);
-        }
-
-        return self::$_instances[$sig];
-    }
-
-    /**
      * Constructor.
      *
      * @param mixed $in  Either a UID string (UID . IMP::IDX_SEP . Mailbox) or
      *                   a Horde_Mime_Part object.
-     * @throws Horde_Exception
+     * @throws IMP_Exception
      */
-    protected function __construct($in)
+    public function __construct($in)
     {
         if ($in instanceof Horde_Mime_Part) {
             $this->_message = $in;
@@ -139,11 +98,11 @@ class IMP_Contents
                     Horde_Imap_Client::FETCH_STRUCTURE => array('parse' => true)
                 ), array('ids' => array($this->_uid)));
             } catch (Horde_Imap_Client_Exception $e) {
-                throw new Horde_Exception('Error displaying message.');
+                throw new IMP_Exception('Error displaying message.');
             }
 
             if (!isset($ret[$this->_uid]['structure'])) {
-                throw new Horde_Exception('Error displaying message.');
+                throw new IMP_Exception('Error displaying message.');
             }
 
             $this->_message = $ret[$this->_uid]['structure'];
@@ -704,7 +663,7 @@ class IMP_Contents
         if (($mask && self::SUMMARY_IMAGE_SAVE) &&
             $GLOBALS['registry']->hasMethod('images/selectGalleries') &&
             ($mime_part->getPrimaryType() == 'image')) {
-            $part['img_save'] = Horde::link('#', _("Save Image in Gallery"), 'saveImgAtc', null, Horde::popupJs(Horde::applicationUrl('saveimage.php'), array('params' => array('uid' => ($this->_uid . IMP::IDX_SEP . $this->_mailbox), 'id' => $id), 'height' => 200, 'width' => 450)) . 'return false;') . '</a>';
+            $part['img_save'] = Horde::link('#', _("Save Image in Gallery"), 'saveImgAtc', null, Horde::popupJs(Horde::applicationUrl('saveimage.php'), array('params' => array('mbox' => $this->_mailbox, 'uid' => $this->_uid, 'id' => $id), 'height' => 200, 'width' => 450)) . 'return false;') . '</a>';
         }
 
         /* Add print link? */
index 6eab2e3..11fdaad 100644 (file)
@@ -113,7 +113,7 @@ class IMP_Filter
             $GLOBALS['imp_imap']->checkUidvalidity($mbox);
 
             foreach ($msgIndices as $idx) {
-                $contents = IMP_Contents::singleton($idx . IMP::IDX_SEP . $mbox);
+                $contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mbox, $idx);
                 $hdr = $contents->getHeaderOb();
                 $addr[] = Horde_Mime_Address::bareAddress($hdr->getValue('from'));
             }
diff --git a/imp/lib/Injector/Binder/Contents.php b/imp/lib/Injector/Binder/Contents.php
new file mode 100644 (file)
index 0000000..e81aec8
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Binder for IMP_Contents::.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package IMP
+ */
+class IMP_Injector_Binder_Contents implements Horde_Injector_Binder
+{
+    /**
+     */
+    public function create(Horde_Injector $injector)
+    {
+        return new IMP_Injector_Factory_Contents($injector);
+    }
+
+    /**
+     */
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+
+}
diff --git a/imp/lib/Injector/Factory/Contents.php b/imp/lib/Injector/Factory/Contents.php
new file mode 100644 (file)
index 0000000..e606e8c
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+/**
+ * A Horde_Injector:: based IMP_Contents:: factory.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  IMP
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @link     http://pear.horde.org/index.php?package=IMP
+ */
+
+/**
+ * A Horde_Injector:: based IMP_Contents:: factory.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @category Horde
+ * @package  IMP
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @link     http://pear.horde.org/index.php?package=IMP
+ */
+class IMP_Injector_Factory_Contents
+{
+    /**
+     * Instances.
+     *
+     * @var array
+     */
+    private $_instances = array();
+
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_injector = $injector;
+    }
+
+    /**
+     * Return the Horde_Editor:: instance.
+     *
+     * @param string $mailbox  The mailbox name.
+     * @param integer $uid     The message UID.
+     *
+     * @return IMP_Contents  The singleton contents instance.
+     * @throws IMP_Exception
+     */
+    public function getOb($mailbox, $uid)
+    {
+        $uid = $uid . IMP::IDX_SEP . $mailbox;
+        if (!isset($this->_instances[$uid])) {
+            $this->_instances[$uid] = new IMP_Contents($uid);
+        }
+        return $this->_instances[$uid];
+    }
+
+}
index 8413e8f..19b472b 100644 (file)
@@ -264,13 +264,13 @@ class IMP_Mailbox
                           !in_array('\\seen', $v['flags'])))) {
                         if (empty($preview_info[$k])) {
                             try {
-                                $imp_contents = IMP_Contents::singleton($k . IMP::IDX_SEP . $mbox);
+                                $imp_contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mbox, $k);
                                 $prev = $imp_contents->generatePreview();
                                 $preview_info[$k] = array('IMPpreview' => $prev['text'], 'IMPpreviewc' => $prev['cut']);
                                 if (!is_null($cache)) {
                                     $tostore[$k] = $preview_info[$k];
                                 }
-                            } catch (Horde_Exception $e) {
+                            } catch (Exception $e) {
                                 $preview_info[$k] = array('IMPpreview' => '', 'IMPpreviewc' => false);
                             }
                         }
index 6f54f63..c25ad27 100644 (file)
@@ -312,7 +312,7 @@ class IMP_Message
         foreach ($msgList as $folder => $msgIndices) {
             foreach ($msgIndices as $index) {
                 /* Fetch the message contents. */
-                $imp_contents = IMP_Contents::singleton($index . IMP::IDX_SEP . $folder);
+                $imp_contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($folder, $index);
 
                 /* Fetch the message headers. */
                 $imp_headers = $imp_contents->getHeaderOb();
@@ -475,7 +475,7 @@ class IMP_Message
         $GLOBALS['imp_imap']->checkUidvalidity($mbox);
 
         /* Get a local copy of the message. */
-        $contents = IMP_Contents::singleton($index . IMP::IDX_SEP . $mbox);
+        $contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mbox, $index);
 
         /* Loop through all to-be-stripped mime parts. */
         if (is_null($partid)) {
index 2805fc8..67b9926 100644 (file)
@@ -108,7 +108,7 @@ class IMP_Horde_Mime_Viewer_Partial extends Horde_Mime_Viewer_Driver
             if ($val == $number) {
                 $parts[$number] = $this->_mimepart->getContents();
             } else {
-                $ic = IMP_Contents::singleton($val . IMP::IDX_SEP . $mbox);
+                $ic = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mbox, $val);
                 $parts[$ic->getMIMEMessage()->getContentTypeParameter('number')] = $ic->getBody();
             }
         }
index fa948f1..2378e73 100644 (file)
@@ -39,7 +39,7 @@ class IMP_Spam
         foreach ($msgList as $mbox => $msgIndices) {
             try {
                 $GLOBALS['imp_imap']->checkUidvalidity($mbox);
-            } catch (Horde_Exception $e) {
+            } catch (IMP_Exception $e) {
                 continue;
             }
 
@@ -47,8 +47,8 @@ class IMP_Spam
                 /* Fetch the raw message contents (headers and complete
                  * body). */
                 try {
-                    $imp_contents = IMP_Contents::singleton($idx . IMP::IDX_SEP . $mbox);
-                } catch (Horde_Exception $e) {
+                    $imp_contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mbox, $idx);
+                } catch (IMP_Exception $e) {
                     continue;
                 }
 
index d9ae6e1..c6c6f57 100644 (file)
@@ -191,8 +191,8 @@ class IMP_Ui_Compose
     {
         if (!empty($uid)) {
             try {
-                return IMP_Contents::singleton($uid . IMP::IDX_SEP . $mailbox);
-            } catch (Horde_Exception $e) {
+                return $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mailbox, $uid);
+            } catch (IMP_Exception $e) {
                 $GLOBALS['notification']->push(_("Could not retrieve the message from the mail server."), 'horde.error');
             }
         }
index 74b5e42..edf008e 100644 (file)
@@ -118,8 +118,8 @@ class IMP_Views_ShowMessage
 
         /* Parse MIME info and create the body of the message. */
         try {
-            $imp_contents = IMP_Contents::singleton($uid . IMP::IDX_SEP . $mailbox);
-        } catch (Horde_Exception $e) {
+            $imp_contents = $GLOBALS['injector']->getInstance('IMP_Contents')->getOb($mailbox, $uid);
+        } catch (IMP_Exception $e) {
             $result['error'] = $error_msg;
             $result['errortype'] = 'horde.error';
             return $result;
index 944ded4..fa231c7 100644 (file)
@@ -120,8 +120,8 @@ $use_pop = ($_SESSION['imp']['protocol'] == 'pop');
 
 /* Parse the message. */
 try {
-    $imp_contents = IMP_Contents::singleton($uid . IMP::IDX_SEP . $mailbox_name);
-} catch (Horde_Exception $e) {
+    $imp_contents = $injector->getInstance('IMP_Contents')->getOb($mailbox_name, $uid);
+} catch (IMP_Exception $e) {
     header('Location: ' . IMP::generateIMPUrl('mailbox-mimp.php', $mailbox_name)->setRaw(true)->add('a', 'm'));
     exit;
 }
index f31d054..6da7b52 100644 (file)
@@ -188,8 +188,8 @@ $uid = $index_array['uid'];
 
 /* Parse the message. */
 try {
-    $imp_contents = IMP_Contents::singleton($uid . IMP::IDX_SEP . $mailbox_name);
-} catch (Horde_Exception $e) {
+    $imp_contents = $injector->getInstance('IMP_Contents')->getOb($mailbox_name, $uid);
+} catch (IMP_Exception $e) {
     $imp_mailbox->removeMsgs(true);
     _returnToMailbox(null, 'message_missing');
     require IMP_BASE . '/mailbox.php';
index 1402b5e..a45334b 100644 (file)
@@ -141,7 +141,7 @@ case 'info_personal_private_key':
 
 case 'save_attachment_public_key':
     /* Retrieve the key from the message. */
-    $contents = IMP_Contents::singleton($vars->uid . IMP::IDX_SEP . $vars->mailbox);
+    $contents = $injector->getInstance('IMP_Contents')->getOb($vars->mailbox, $vars->uid);
     $mime_part = $contents->getMIMEPart($vars->mime_id);
     if (empty($mime_part)) {
         throw new IMP_Exception('Cannot retrieve public key from message.');
index 311427f..6364dd4 100644 (file)
 require_once dirname(__FILE__) . '/lib/Application.php';
 Horde_Registry::appInit('imp');
 
-$id = Horde_Util::getFormData('id');
-$uid = Horde_Util::getFormData('uid');
+$vars = Horde_Variables::getDefaultVariables();
 
 /* Run through the action handlers. */
-switch (Horde_Util::getFormData('actionID')) {
+switch ($vars->actionID) {
 case 'save_image':
-    $contents = IMP_Contents::singleton($uid);
-    $mime_part = $contents->getMIMEPart($id);
+    $contents = $injector->getInstance('IMP_Contents')->getOb($vars->mbox, $vars->uid);
+    $mime_part = $contents->getMIMEPart($vars->id);
     $image_data = array(
         'data' => $mime_part->getContents(),
         'description' => $mime_part->getDescription(true),
@@ -29,7 +28,7 @@ case 'save_image':
         'type' => $mime_part->getType()
     );
     try {
-        $registry->call('images/saveImage', array(null, Horde_Util::getFormData('gallery'), $image_data));
+        $registry->call('images/saveImage', array(null, $vars->gallery, $image_data));
     } catch (Horde_Exception $e) {
         $notification->push($e);
         break;
@@ -47,8 +46,8 @@ if (!$registry->hasMethod('images/selectGalleries') ||
 $t = $injector->createInstance('Horde_Template');
 $t->setOption('gettext', true);
 $t->set('action', Horde::applicationUrl('saveimage.php'));
-$t->set('id', htmlspecialchars($id));
-$t->set('uid', htmlspecialchars($uid));
+$t->set('id', htmlspecialchars($vars->id));
+$t->set('uid', htmlspecialchars($vars->uid));
 $t->set('image_img', Horde::img('mime/image.png', _("Image")));
 
 /* Build the list of galleries. */
index 4be7867..b54cd6e 100644 (file)
@@ -89,7 +89,7 @@ case 'process_import_personal_certs':
 
 case 'save_attachment_public_key':
     /* Retrieve the key from the message. */
-    $contents = IMP_Contents::singleton($vars->uid . IMP::IDX_SEP . $vars->mailbox);
+    $contents = $injector->getInstance('IMP_Contents')->getOb($vars->mailbox, $vars->uid);
     $mime_part = $contents->getMIMEPart($vars->mime_id);
     if (empty($mime_part)) {
         throw new IMP_Exception('Cannot retrieve public key from message.');
index de4be45..2d818e1 100644 (file)
@@ -88,7 +88,7 @@ foreach ($loop_array as $mbox => $idxlist) {
 
         /* Get the body of the message. */
         $curr_msg = $curr_tree = array();
-        $contents = IMP_Contents::singleton($idx . IMP::IDX_SEP . $mbox);
+        $contents = $injector->getInstance('IMP_Contents')->getOb($mbox, $idx);
         $mime_id = $contents->findBody();
         if ($contents->canDisplay($mime_id, IMP_Contents::RENDER_INLINE)) {
             $ret = $contents->renderMIMEPart($mime_id, IMP_Contents::RENDER_INLINE);
index 5c9fd5c..7f0d651 100644 (file)
@@ -61,12 +61,12 @@ if ($vars->actionID == 'compose_attach_preview') {
 
     /* Create a dummy IMP_Contents() object so we can use the view code below.
      * Then use the 'view_attach' handler to output. */
-    $contents = IMP_Contents::singleton($mime);
+    $contents = new IMP_Contents($mime);
 } else {
     if (!$vars->uid || !$vars->mailbox) {
         exit;
     }
-    $contents = IMP_Contents::singleton($vars->uid . IMP::IDX_SEP . $vars->mailbox);
+    $contents = $injector->getInstance('IMP_Contents')->getOb($vars->mailbox, $vars->uid);
 }
 
 /* Run through action handlers */