/**
  * Private helper method
  *
  * @param   &peer.mail.MimePart[] parts
  * @param   array p structure parts as retrieved from cclient lib
  * @param   string id default '' part id
  */
 protected function _recurseparts(&$parts, $p, $id = '')
 {
     static $types = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'unknown');
     for ($i = 0, $s = sizeof($p); $i < $s; $i++) {
         $pid = sprintf('%s%d', $id, $i + 1);
         if (empty($p[$i]->parts)) {
             $part = new MimePart(NULL, NULL, $this->_lookupattr(@$p[$i]->parameters, 'CHARSET'), $this->_lookupattr(@$p[$i]->dparameters, 'NAME'));
         } else {
             $part = new MultiPart();
         }
         $part->setContentType($types[$p[$i]->type] . '/' . strtolower($p[$i]->subtype));
         $part->setDisposition($p[$i]->ifdisposition ? MIME_DISPOSITION_ATTACHMENT : MIME_DISPOSITION_INLINE);
         if (FALSE !== ($f = $this->_lookupattr($p[$i]->dparameters, 'FILENAME'))) {
             $part->setFilename($f);
         }
         $part->id = $pid;
         // We can retrieve the body here since the message has been read anyway
         if (!empty($p[$i]->parts)) {
             if ($p[$i]->ifsubtype) {
                 switch ($p[$i]->subtype) {
                     case 'MIXED':
                         $pid = substr($pid, 0, -2);
                         break;
                     default:
                         // Nothing
                 }
             }
             // Recurse through parts
             $this->_recurseparts($part->parts, $p[$i]->parts, $pid . '.');
             // Multipart -> part.0 are the headers
             $part->parts[0]->setHeaderString($this->folder->getMessagePart($this->uid, $pid . '.0'));
         } else {
             $part->body = $this->folder->getMessagePart($this->uid, $pid);
         }
         $part->folder = $this->folder;
         $parts[] = $part;
     }
 }
/**
 * Send an iMIP message since they look like a non-local user.
 *  
 * @param string $method The METHOD parameter from the iTIP
 * @param string $to_email The e-mail address we're going to send to
 * @param vCalendar $vcal The iTIP part of the message.
 */
function doImipMessage($method, $to_email, vCalendar $itip)
{
    global $c, $request;
    header('Debug: Sending iMIP ' . $method . ' message to ' . $to_email);
    $mime = new MultiPart();
    $mime->addPart($itip->Render(), 'text/calendar; charset=UTF-8; method=' . $method);
    $friendly_part = isset($c->iMIP->template[$method]) ? $c->iMIP->template[$method] : <<<EOTEMPLATE
This is a meeting ##METHOD## which your e-mail program should be able to
import into your calendar.  Alternatively you could save the attachment
and load that into your calendar instead.
EOTEMPLATE;
    $components = $itip->GetComponents('VTIMEZONE', false);
    $replaceable = array('METHOD', 'DTSTART', 'DTEND', 'SUMMARY', 'DESCRIPTION', 'URL');
    foreach ($replaceable as $pname) {
        $search = '##' . $pname . '##';
        if (strstr($friendly_part, $search) !== false) {
            $property = $itip->GetProperty($pname);
            if (empty($property)) {
                $property = $components[0]->GetProperty($pname);
            }
            if (empty($property)) {
                $replace = '';
            } else {
                switch ($pname) {
                    case 'DTSTART':
                    case 'DTEND':
                        $when = new RepeatRuleDateTime($property);
                        $replace = $when->format('c');
                        break;
                    default:
                        $replace = $property->GetValue();
                }
            }
            $friendly_part = str_replace($search, $replace, $friendly_part);
        }
    }
    $mime->addPart($friendly_part, 'text/plain');
    $email = new EMail();
    $email->SetFrom($request->principal->email());
    $email->AddTo($to_email);
    $email->SetSubject($components[0]->GetPValue('SUMMARY'));
    $email->SetBody($mime->getMimeParts());
    if (isset($c->iMIP->pretend_email)) {
        $email->Pretend($mime->getMimeHeaders());
    } else {
        if (!isset($c->iMIP->send_email) || !$c->iMIP->send_email) {
            $email->PretendLog($mime->getMimeHeaders());
        } else {
            $email->Send($mime->getMimeHeaders());
        }
    }
}