Merge branch '2013.03'
authorPhilipp Schüle <p.schuele@metaways.de>
Tue, 22 Oct 2013 13:47:52 +0000 (15:47 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Tue, 22 Oct 2013 13:47:52 +0000 (15:47 +0200)
1  2 
tine20/Felamimail/Controller/Message.php

@@@ -586,9 -586,16 +586,16 @@@ class Felamimail_Controller_Message ext
      protected function _getAndDecodeMessageBody(Felamimail_Model_Message $_message, $_partId, $_contentType, $_account = NULL)
      {
          $structure = $_message->getPartStructure($_partId);
-         $bodyParts = $_message->getBodyParts($structure, $_contentType);
+         if (empty($structure)) {
+             if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
+                 . ' Empty structure, could not find body parts of message ' . $_message->subject);
+             return '';
+         }
          
+         $bodyParts = $_message->getBodyParts($structure, $_contentType);
          if (empty($bodyParts)) {
+             if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
+                 . ' Could not find body parts of message ' . $_message->subject);
              return '';
          }
          
              
              if ($partStructure['contentType'] != Zend_Mime::TYPE_TEXT) {
                  $bodyCharCountBefore = strlen($body);
 -                $body = $this->_purifyBodyContent($body);
 +                $body = $this->_purifyBodyContent($body, $_message->getId());
                  $bodyCharCountAfter = strlen($body);
                  
                  if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
       * use html purifier to remove 'bad' tags/attributes from html body
       *
       * @param string $_content
 +     * @param string $messageId
       * @return string
       */
 -    protected function _purifyBodyContent($_content)
 +    protected function _purifyBodyContent($_content, $messageId)
      {
          if (!defined('HTMLPURIFIER_PREFIX')) {
              define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/../../library/HTMLPurifier'));
          if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
              . ' Current mem usage before purify: ' . memory_get_usage()/1024/1024);
          
 -        $config = HTMLPurifier_Config::createDefault();
 +        // add custom schema for passing message id to URIScheme
 +        $configSchema = HTMLPurifier_ConfigSchema::makeFromSerial();
 +        $configSchema->add('Felamimail.messageId', NULL, 'string', TRUE);
 +        $config = HTMLPurifier_Config::create(NULL, $configSchema);
          $config->set('HTML.DefinitionID', 'purify message body contents');
          $config->set('HTML.DefinitionRev', 1);
          
              'http' => true,
              'https' => true,
              'mailto' => true,
 -            'data' => true
 +            'data' => true,
 +            'cid' => true
          ));
 +        $config->set('Felamimail.messageId', $messageId);
          
          $this->_transformBodyTags($config);
          
          $uri = $config->getDefinition('URI');
          $uri->addFilter(new Felamimail_HTMLPurifier_URIFilter_TransformURI(), $config);
          
 +        // add cid uri scheme
 +        require_once(dirname(dirname(__FILE__)) . '/HTMLPurifier/URIScheme/cid.php');
 +        
          $purifier = new HTMLPurifier($config);
          $content = $purifier->purify($_content);
  
              if ($part['type'] == 'multipart') {
                  $attachments = $attachments + $this->getAttachments($message, $part['partId']);
              } else {
 +                $filename = $this->_getAttachmentFilename($part);
 +                
                  if ($part['type'] == 'text' && 
 -                    (!is_array($part['disposition']) || ($part['disposition']['type'] == Zend_Mime::DISPOSITION_INLINE && !array_key_exists("parameters", $part['disposition'])))
 +                    (! is_array($part['disposition']) || ($part['disposition']['type'] == Zend_Mime::DISPOSITION_INLINE && ! array_key_exists("parameters", $part['disposition'])))
                  ) {
 +                    if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
 +                        . ' Skipping DISPOSITION_INLINE attachment with name ' . $filename);
 +                    if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
 +                        . ' part: ' . print_r($part, TRUE));
                      continue;
                  }
                  
 -                $filename = $this->_getAttachmentFilename($part);
                  $attachmentData = array(
                      'content-type' => $part['contentType'], 
                      'filename'     => $filename,
                      'partId'       => $part['partId'],
                      'size'         => $part['size'],
 -                    'description'  => $part['description']
 +                    'description'  => $part['description'],
 +                    'cid'          => (! empty($part['id'])) ? $part['id'] : NULL,
                  );
                  
                  if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
          
          return $this->_punycodeConverter;
      }
 +
 +    /**
 +     * get resource part id
 +     * 
 +     * @param string $cid
 +     * @param string $messageId
 +     * @return array
 +     * @throws Tinebase_Exception_NotFound
 +     * 
 +     * @todo add param string $folderId?
 +     */
 +    public function getResourcePartStructure($cid, $messageId)
 +    {
 +        $message = $this->get($messageId);
 +        $this->_checkMessageAccount($message);
 +        
 +        $attachments = $this->getAttachments($messageId);
 +        
 +        foreach ($attachments as $attachment) {
 +            if ($attachment['cid'] === '<' . $cid . '>') {
 +                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
 +                    . ' Found attachment ' . $attachment['partId'] . ' with cid ' . $cid);
 +                return $attachment;
 +            }
 +        }
 +        
 +        throw new Tinebase_Exception_NotFound('Resource not found');
 +    }
  }