From 7d49328296507c87ecf43636ed6d5db6940b0c00 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 06 Aug 2015 08:16:02 -0400
Subject: [PATCH] Code improvements and compat. with Mail_mime >= 1.9.0

---
 plugins/enigma/lib/enigma_mime_message.php |  111 +++++++++++++++++++++++++++----------------------------
 1 files changed, 54 insertions(+), 57 deletions(-)

diff --git a/plugins/enigma/lib/enigma_mime_message.php b/plugins/enigma/lib/enigma_mime_message.php
index eae8cb7..395de8f 100644
--- a/plugins/enigma/lib/enigma_mime_message.php
+++ b/plugins/enigma/lib/enigma_mime_message.php
@@ -20,11 +20,11 @@
     const PGP_SIGNED    = 1;
     const PGP_ENCRYPTED = 2;
 
-    protected $_type;
-    protected $_message;
-    protected $_body;
-    protected $_signature;
-    protected $_encrypted;
+    protected $type;
+    protected $message;
+    protected $body;
+    protected $signature;
+    protected $encrypted;
 
 
     /**
@@ -35,17 +35,16 @@
      */
     function __construct($message, $type)
     {
-        $this->_message = $message;
-        $this->_type    = $type;
+        $this->message = $message;
+        $this->type    = $type;
 
         // clone parameters
-        foreach (array_keys($this->_build_params) as $param) {
-            $this->_build_params[$param] = $message->getParam($param);
+        foreach (array_keys($this->build_params) as $param) {
+            $this->build_params[$param] = $message->getParam($param);
         }
 
         // clone headers
-        $this->_headers = $message->_headers;
-
+        $this->headers = $message->headers();
 /*
         if ($message->getParam('delay_file_io')) {
             // use common temp dir
@@ -66,7 +65,7 @@
         else {
 */
             // \r\n is must-have here
-            $this->_body = $message->get() . "\r\n";
+            $this->body = $message->get() . "\r\n";
 /*
         }
 */
@@ -77,10 +76,10 @@
      *
      * @return bool True if it is multipart, otherwise False
      */
-    function isMultipart()
+    public function isMultipart()
     {
-        return $this->_message instanceof enigma_mime_message
-            || !empty($this->_message->_parts) || $this->_message->getHTMLBody();
+        return $this->message instanceof enigma_mime_message
+            || $this->message->isMultipart() || $this->message->getHTMLBody();
     }
 
     /**
@@ -88,10 +87,10 @@
      *
      * @return string Sender address
      */
-    function getFromAddress()
+    public function getFromAddress()
     {
         // get sender address
-        $headers = $this->_message->headers();
+        $headers = $this->message->headers();
         $from    = rcube_mime::decode_address_list($headers['From'], 1, false, null, true);
         $from    = $from[1];
 
@@ -103,10 +102,10 @@
      *
      * @return array Recipients' addresses
      */
-    function getRecipients()
+    public function getRecipients()
     {
         // get sender address
-        $headers = $this->_message->headers();
+        $headers = $this->message->headers();
         $to      = rcube_mime::decode_address_list($headers['To'], null, false, null, true);
         $cc      = rcube_mime::decode_address_list($headers['Cc'], null, false, null, true);
         $bcc     = rcube_mime::decode_address_list($headers['Bcc'], null, false, null, true);
@@ -122,9 +121,9 @@
      *
      * @return string Message body
      */
-    function getOrigBody()
+    public function getOrigBody()
     {
-        $_headers = $this->_message->headers();
+        $_headers = $this->message->headers();
         $headers  = array();
 
         if ($_headers['Content-Transfer-Encoding']) {
@@ -132,7 +131,7 @@
         }
         $headers[] = 'Content-Type: ' . $_headers['Content-Type'];
 
-        return implode("\r\n", $headers) . "\r\n\r\n" . $this->_body;
+        return implode("\r\n", $headers) . "\r\n\r\n" . $this->body;
     }
 
     /**
@@ -140,9 +139,9 @@
      *
      * @param string Signature body
      */
-    function addPGPSignature($body)
+    public function addPGPSignature($body)
     {
-        $this->_signature = $body;
+        $this->signature = $body;
     }
 
     /**
@@ -150,9 +149,9 @@
      *
      * @param string Encrypted body
      */
-    function setPGPEncryptedBody($body)
+    public function setPGPEncryptedBody($body)
     {
-        $this->_encrypted = $body;
+        $this->encrypted = $body;
     }
 
     /**
@@ -166,40 +165,39 @@
      *                            without headers
      *
      * @return mixed The MIME message content string, null or PEAR error object
-     * @access public
      */
-    function get($params = null, $filename = null, $skip_head = false)
+    public function get($params = null, $filename = null, $skip_head = false)
     {
         if (isset($params)) {
             while (list($key, $value) = each($params)) {
-                $this->_build_params[$key] = $value;
+                $this->build_params[$key] = $value;
             }
         }
 
-        $this->_checkParams();
+        $this->checkParams();
 
-        if ($this->_type == self::PGP_SIGNED) {
+        if ($this->type == self::PGP_SIGNED) {
             $body   = "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)";
             $params = array(
                 'content_type' => "multipart/signed; micalg=pgp-sha1; protocol=\"application/pgp-signature\"",
-                'eol'          => $this->_build_params['eol'],
+                'eol'          => $this->build_params['eol'],
             );
 
             $message = new Mail_mimePart($body, $params);
 
-            if (!empty($this->_body)) {
-                $headers = $this->_message->headers();
+            if (!empty($this->body)) {
+                $headers = $this->message->headers();
                 $params  = array('content_type' => $headers['Content-Type']);
 
                 if ($headers['Content-Transfer-Encoding']) {
                     $params['encoding'] = $headers['Content-Transfer-Encoding'];
                 }
 
-                $message->addSubpart($this->_body, $params);
+                $message->addSubpart($this->body, $params);
             }
 
-            if (!empty($this->_signature)) {
-                $message->addSubpart($this->_signature, array(
+            if (!empty($this->signature)) {
+                $message->addSubpart($this->signature, array(
                     'filename'     => 'signature.asc',
                     'content_type' => 'application/pgp-signature',
                     'disposition'  => 'attachment',
@@ -207,11 +205,11 @@
                 ));
             }
         }
-        else if ($this->_type == self::PGP_ENCRYPTED) {
+        else if ($this->type == self::PGP_ENCRYPTED) {
             $body   = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)";
             $params = array(
                 'content_type' => "multipart/encrypted; protocol=\"application/pgp-encrypted\"",
-                'eol'          => $this->_build_params['eol'],
+                'eol'          => $this->build_params['eol'],
             );
 
             $message = new Mail_mimePart($body, $params);
@@ -221,7 +219,7 @@
                     'description'  => 'PGP/MIME version identification',
             ));
 
-            $message->addSubpart($this->_encrypted, array(
+            $message->addSubpart($this->encrypted, array(
                     'content_type' => 'application/octet-stream',
                     'description'  => 'PGP/MIME encrypted message',
                     'disposition'  => 'inline',
@@ -230,8 +228,8 @@
         }
 
         // Use saved boundary
-        if (!empty($this->_build_params['boundary'])) {
-            $boundary = $this->_build_params['boundary'];
+        if (!empty($this->build_params['boundary'])) {
+            $boundary = $this->build_params['boundary'];
         }
         else {
             $boundary = null;
@@ -241,18 +239,18 @@
         if ($filename) {
             // Append mimePart message headers and body into file
             $headers = $message->encodeToFile($filename, $boundary, $skip_head);
-            if ($this->_isError($headers)) {
+            if ($this->isError($headers)) {
                 return $headers;
             }
-            $this->_headers = array_merge($this->_headers, $headers);
+            $this->headers = array_merge($this->headers, $headers);
             return null;
         }
         else {
             $output = $message->encode($boundary, $skip_head);
-            if ($this->_isError($output)) {
+            if ($this->isError($output)) {
                 return $output;
             }
-            $this->_headers = array_merge($this->_headers, $output['headers']);
+            $this->headers = array_merge($this->headers, $output['headers']);
             return $output['body'];
         }
     }
@@ -261,20 +259,19 @@
      * Get Content-Type and Content-Transfer-Encoding headers of the message
      *
      * @return array Headers array
-     * @access private
      */
-    function _contentHeaders()
+    protected function contentHeaders()
     {
-        $this->_checkParams();
+        $this->checkParams();
 
-        $eol = !empty($this->_build_params['eol']) ? $this->_build_params['eol'] : "\r\n";
+        $eol = !empty($this->build_params['eol']) ? $this->build_params['eol'] : "\r\n";
 
         // multipart message: and boundary
-        if (!empty($this->_build_params['boundary'])) {
-            $boundary = $this->_build_params['boundary'];
+        if (!empty($this->build_params['boundary'])) {
+            $boundary = $this->build_params['boundary'];
         }
-        else if (!empty($this->_headers['Content-Type'])
-            && preg_match('/boundary="([^"]+)"/', $this->_headers['Content-Type'], $m)
+        else if (!empty($this->headers['Content-Type'])
+            && preg_match('/boundary="([^"]+)"/', $this->headers['Content-Type'], $m)
         ) {
             $boundary = $m[1];
         }
@@ -282,14 +279,14 @@
             $boundary = '=_' . md5(rand() . microtime());
         }
 
-        $this->_build_params['boundary'] = $boundary;
+        $this->build_params['boundary'] = $boundary;
 
-        if ($this->_type == self::PGP_SIGNED) {
+        if ($this->type == self::PGP_SIGNED) {
             $headers['Content-Type'] = "multipart/signed; micalg=pgp-sha1;$eol"
                 ." protocol=\"application/pgp-signature\";$eol"
                 ." boundary=\"$boundary\"";
         }
-        else if ($this->_type == self::PGP_ENCRYPTED) {
+        else if ($this->type == self::PGP_ENCRYPTED) {
             $headers['Content-Type'] = "multipart/encrypted;$eol"
                 ." protocol=\"application/pgp-encrypted\";$eol"
                 ." boundary=\"$boundary\"";

--
Gitblit v1.9.1