Skip to content

Commit

Permalink
Extract Horde_ActiveSync_Mime::
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Sep 4, 2014
1 parent ede1c59 commit 40b255c
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 76 deletions.
88 changes: 12 additions & 76 deletions framework/ActiveSync/lib/Horde/ActiveSync/Imap/Message.php
Expand Up @@ -38,7 +38,7 @@ class Horde_ActiveSync_Imap_Message
/**
* Message structure.
*
* @var Horde_Mime_Part
* @var Horde_ActiveSync_Mime
*/
protected $_message;

Expand Down Expand Up @@ -85,13 +85,12 @@ public function __construct(
Horde_Imap_Client_Data_Fetch $data)
{
$this->_imap = $imap;
$this->_message = $data->getStructure();
$this->_message = new Horde_ActiveSync_Mime($data->getStructure());
$this->_uid = $data->getUid();
$this->_flags = $data->getFlags();
$this->_mbox = $mbox;
$this->_data = $data;
$this->_envelope = $data->getEnvelope();

}

/**
Expand Down Expand Up @@ -186,7 +185,7 @@ public function getFullMsg($stream = false)
*/
public function getStructure()
{
return $this->_message;
return $this->_message->base;
}

/**
Expand Down Expand Up @@ -812,37 +811,11 @@ public function contentTypeMap()
* @param string $mime_type The MIME type.
*
* @return boolean True if an attachment.
* @deprecated Will be removed in 3.0 (Only used in self::hasAttachments call).
*/
public function isAttachment($id, $mime_type)
{
switch ($mime_type) {
case 'text/plain':
if (!($this->_message->findBody('plain') == $id)) {
return true;
}
return false;
case 'text/html':
if (!($this->_message->findBody('html') == $id)) {
return true;
}
return false;
case 'application/pkcs7-signature':
case 'application/x-pkcs7-signature':
return false;
}

list($ptype,) = explode('/', $mime_type, 2);

switch ($ptype) {
case 'message':
return in_array($mime_type, array('message/rfc822', 'message/disposition-notification'));

case 'multipart':
return false;

default:
return true;
}
return $this->_message->isAttachment($id, $mime_type);
}

/**
Expand All @@ -852,16 +825,7 @@ public function isAttachment($id, $mime_type)
*/
public function hasiCalendar()
{
if (!$this->hasAttachments()) {
return false;
}
foreach ($this->contentTypeMap() as $id => $type) {
if ($type == 'text/calendar') {
return $this->getMimePart($id);
}
}

return false;
return $this->_message->hasiCalendar();
}

/**
Expand All @@ -871,19 +835,7 @@ public function hasiCalendar()
*/
public function hasAttachments()
{
if (isset($this->_hasAttachments)) {
return $this->_hasAttachments;
}

foreach ($this->contentTypeMap() as $id => $type) {
if ($this->isAttachment($id, $type)) {
$this->_hasAttachments = true;
return true;
}
}

$this->_hasAttachments = false;
return false;
return $this->_message->hasAttachments();
}

/**
Expand All @@ -895,30 +847,14 @@ public function hasAttachments()
* @return boolean True if message is S/MIME signed or encrypted,
* false otherwise.
*/
public function isSigned($message = null)
public function isSigned(Horde_Mime_Part $message = null)
{
if (empty($message)) {
$message = $this->_message;
}
if ($message->getType() == 'application/pkcs7-mime' ||
$message->getType() == 'application/x-pkcs7-mime') {
return true;
}

if ($message->getPrimaryType() == 'multipart') {
if ($message->getSubType() == 'signed') {
return true;
}

// Signed/encrypted part might be lower in the mime structure
foreach ($message->getParts() as $part) {
if ($this->isSigned($part)) {
return true;
}
}
if (!empty($message)) {
$message = new Horde_ActiveSync_Mime($message);
return $message->isSigned();
}

return false;
return $this->_message->isSigned();
}

}
175 changes: 175 additions & 0 deletions framework/ActiveSync/lib/Horde/ActiveSync/Mime.php
@@ -0,0 +1,175 @@
<?php
/**
* Horde_ActiveSync_Mime::
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2012-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
/**
* This class provides some base functionality for dealing with MIME objects in
* the context of ActiveSync requests.
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2012-2014 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
class Horde_ActiveSync_Mime
{

protected $_base;

protected $_hasAttachments;


public function __construct(Horde_Mime_Part $mime)
{
$this->_base = $mime;
}

public function __get($property)
{
switch ($property) {
case 'base':
return $this->_base;
}
return $this->_base->property;
}

public function __call($method, $params)
{
if (is_callable(array($this->_base, $method))) {
return call_user_func_array(array($this->_base, $method), $params);
}

throw new InvalidArgumentException();
}

/**
* Return the hasAttachments flag
*
* @return boolean
*/
public function hasAttachments()
{
if (isset($this->_hasAttachments)) {
return $this->_hasAttachments;
}

foreach ($this->_base->contentTypeMap() as $id => $type) {
if ($this->isAttachment($id, $type)) {
$this->_hasAttachments = true;
return true;
}
}
$this->_hasAttachments = false;

return false;
}

/**
* Determines if a MIME type is an attachment.
* For our purposes, an attachment is any MIME part that can be
* downloaded by itself (i.e. all the data needed to view the part is
* contained within the download data).
*
* @param string $id The MIME Id for the part we are checking.
* @param string $mime_type The MIME type.
*
* @return boolean True if an attachment.
*/
public function isAttachment($id, $mime_type)
{
switch ($mime_type) {
case 'text/plain':
if (!($this->_base->findBody('plain') == $id)) {
return true;
}
return false;
case 'text/html':
if (!($this->_base->findBody('html') == $id)) {
return true;
}
return false;
case 'application/pkcs7-signature':
case 'application/x-pkcs7-signature':
return false;
}

list($ptype,) = explode('/', $mime_type, 2);

switch ($ptype) {
case 'message':
return in_array($mime_type, array('message/rfc822', 'message/disposition-notification'));

case 'multipart':
return false;

default:
return true;
}
}

/**
* Return the MIME part of the iCalendar attachment, if available.
*
* @return mixed The mime part, if present, false otherwise.
*/
public function hasiCalendar()
{
if (!$this->hasAttachments()) {
return false;
}
foreach ($this->_base->contentTypeMap() as $id => $type) {
if ($type == 'text/calendar') {
return $this->_base->getMimePart($id);
}
}

return false;
}

/**
* Return the S/MIME status of this message (RFC2633)
*
* @return boolean True if message is S/MIME signed or encrypted,
* false otherwise.
*/
public function isSigned(Horde_Mime_Part $mime = null)
{
if (empty($mime)) {
$mime = $this->_base;
}

if ($mime->getType() == 'application/pkcs7-mime' ||
$mime->getType() == 'application/x-pkcs7-mime') {
return true;
}

if ($mime->getPrimaryType() == 'multipart') {
if ($mime->getSubType() == 'signed') {
return true;
}

// Signed/encrypted part might be lower in the mime structure
foreach ($mime->getParts() as $part) {
if ($this->isSigned($part)) {
return true;
}
}
}

return false;
}

}

0 comments on commit 40b255c

Please sign in to comment.