protected function initMailPart(IncomingMail $mail, $partStructure, $partNum)
 {
     $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, FT_UID) : imap_body($this->getImapStream(), $mail->id, FT_UID);
     if ($partStructure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($partStructure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($partStructure->encoding == 3) {
         $data = imap_base64($data);
     } elseif ($partStructure->encoding == 4) {
         $data = imap_qprint($data);
     }
     $params = array();
     if (!empty($partStructure->parameters)) {
         foreach ($partStructure->parameters as $param) {
             $params[strtolower($param->attribute)] = $param->value;
         }
     }
     if (!empty($partStructure->dparameters)) {
         foreach ($partStructure->dparameters as $param) {
             $paramName = strtolower(preg_match('~^(.*?)\\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
             if (isset($params[$paramName])) {
                 $params[$paramName] .= $param->value;
             } else {
                 $params[$paramName] = $param->value;
             }
         }
     }
     if (!empty($params['charset'])) {
         $data = iconv(strtoupper($params['charset']), $this->serverEncoding . '//IGNORE', $data);
     }
     // attachments
     $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachmentId) {
         if (empty($params['filename']) && empty($params['name'])) {
             $fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
         } else {
             $fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding);
             $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding);
         }
         $attachment = new IncomingMailAttachment();
         $attachment->id = $attachmentId;
         $attachment->name = $fileName;
         if ($this->attachmentsDir) {
             $replace = array('/\\s/' => '_', '/[^0-9a-zA-Z_\\.]/' => '', '/_+/' => '_', '/(^_)|(_$)/' => '');
             $fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName));
             $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . $fileSysName;
             file_put_contents($attachment->filePath, $data);
         }
         $mail->addAttachment($attachment);
     } elseif ($partStructure->type == 0 && $data) {
         if (strtolower($partStructure->subtype) == 'plain') {
             $mail->textPlain .= $data;
         } else {
             $mail->textHtml .= $data;
         }
     } elseif ($partStructure->type == 2 && $data) {
         $mail->textPlain .= trim($data);
     }
     if (!empty($partStructure->parts)) {
         foreach ($partStructure->parts as $subPartNum => $subPartStructure) {
             if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') {
                 $this->initMailPart($mail, $subPartStructure, $partNum);
             } else {
                 $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1));
             }
         }
     }
 }
 protected function initMailPart(IncomingMail $mail, $partStructure, $partNum)
 {
     $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, FT_UID) : imap_body($this->getImapStream(), $mail->id, FT_UID);
     if ($partStructure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($partStructure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($partStructure->encoding == 3) {
         $data = preg_replace('~[^a-zA-Z0-9+=/]+~s', '', $data);
         $data = imap_base64($data);
     } elseif ($partStructure->encoding == 4) {
         $data = quoted_printable_decode($data);
     }
     $params = array();
     if ($partStructure->type == 2 and $partStructure->encoding == 0 and $partStructure->disposition == 'ATTACHMENT') {
         $mail_attachment_head = imap_rfc822_parse_headers($data);
         if (!empty($mail_attachment_head)) {
             $file_name = preg_replace('/[^A-Za-z0-9\\-]/', ' ', $mail_attachment_head->subject);
             $params['filename'] = $file_name . '.eml';
             $params['name'] = $file_name . '.eml';
         }
         $data = imap_utf8($data);
     }
     if (!empty($partStructure->parameters)) {
         foreach ($partStructure->parameters as $param) {
             $params[strtolower($param->attribute)] = $param->value;
         }
     }
     if (!empty($partStructure->dparameters)) {
         foreach ($partStructure->dparameters as $param) {
             $paramName = strtolower(preg_match('~^(.*?)\\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
             if (isset($params[$paramName])) {
                 $params[$paramName] .= $param->value;
             } else {
                 $params[$paramName] = $param->value;
             }
         }
     }
     $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachmentId) {
         if (empty($params['filename']) && empty($params['name'])) {
             $fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
         } else {
             $fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding);
             $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding);
         }
         $attachment = new IncomingMailAttachment();
         $attachment->id = $attachmentId;
         $attachment->name = $fileName;
         $attachment->disposition = $partStructure->disposition;
         $attachment->wordpresdir = wp_upload_bits($fileName, null, $data);
         $mail->addAttachment($attachment);
     } else {
         if (!empty($params['charset'])) {
             $data = $this->convertStringEncoding($data, $params['charset'], $this->serverEncoding);
         }
         if ($partStructure->type == 0 && $data) {
             if (strtolower($partStructure->subtype) == 'plain') {
                 $mail->textPlain .= $data;
             } else {
                 $mail->textHtml .= $data;
             }
         } elseif ($partStructure->type == 2 && $data) {
             $mail->textPlain .= trim($data);
         }
     }
     if (!empty($partStructure->parts)) {
         foreach ($partStructure->parts as $subPartNum => $subPartStructure) {
             if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') {
                 //Processing this .msg attachment above.
                 //$this->initMailPart( $mail, $subPartStructure, $partNum );
             } else {
                 //Process anything else
                 $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1));
             }
         }
     }
 }
 /**
  * Conflict incoming mail
  * 
  * @param void
  * @return void
  */
 function conflict()
 {
     if ($this->active_mail->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     require_once INCOMING_MAIL_MODULE_PATH . '/models/IncomingMailImporter.class.php';
     $mail_data = $this->request->post('mail');
     if (!is_foreachable($mail_data)) {
         flash_error(incoming_mail_module_get_status_description($this->active_mail->getState()));
         $mail_data = array('subject' => $this->active_mail->getSubject(), 'body' => $this->active_mail->getBody(), 'created_by_id' => $this->active_mail->getCreatedById(), 'project_id' => $this->active_mail->getProjectId());
     }
     // if
     if ($this->request->isSubmitted()) {
         $this->active_mail->setSubject(array_var($mail_data, 'subject'));
         $this->active_mail->setBody(array_var($mail_data, 'body'));
         $creator_id = array_var($mail_data, 'created_by_id');
         if ($creator_id && $creator_id != 'original_author') {
             $creator = Users::findById($creator_id);
             if (instance_of($creator, 'User')) {
                 $this->active_mail->setCreatedBy($creator);
             }
             // if
         }
         // if
         $this->active_mail->setCreatedById(array_var($mail_data, 'created_by_id'));
         $this->active_mail->setObjectType(array_var($mail_data, 'object_type'));
         if (array_var($mail_data, 'object_type') == 'comment') {
             $this->active_mail->setParentId(array_var($mail_data, 'parent_id'));
         }
         // if
         // import email
         if (instance_of($importing_result = IncomingMailImporter::importPendingEmail($this->active_mail, $creator_id == 'original_author'), 'ProjectObject')) {
             // we have successfully imported email
             $this->active_mail->delete();
             if ($this->request->isAsyncCall()) {
                 $this->renderText(lang('<p>Conflict Solved Successfully!</p><p>View created <a href=":url">:object</a>.</p>', array('object' => $this->active_mail->getObjectType(), 'url' => $importing_result->getViewUrl())));
             } else {
                 flash_success('Conflict Solved Successfully!');
                 $this->redirectTo('incoming_mail');
             }
             // if
         } else {
             if ($this->request->isAsyncCall()) {
                 $this->httpError(HTTP_ERR_INVALID_PROPERTIES, null, false, 2);
             } else {
                 flash_error($importing_result->getMessage());
             }
             // if
         }
         // if
     }
     // if
     $user = $this->active_mail->getCreatedBy();
     if (instance_of($user, 'User')) {
         $this->smarty->assign('object_user', $user);
     } else {
         $this->smarty->assign('object_user', $this->logged_user);
     }
     // if
     $this->smarty->assign(array('async' => $this->request->isAsyncCall(), 'form_url' => $this->active_mail->getImportUrl() . ($this->request->isAsyncCall() ? '?skip_layout=1&async=1' : ''), 'status_message' => incoming_mail_module_get_status_description($this->active_mail->getState()), 'mail_data' => $mail_data, 'project' => $this->active_mail->getProject()));
     $flash =& Flash::instance();
     $flash->init();
     js_assign('additional_fields_url', assemble_url('incoming_mail_additional_form_fields'));
 }
 /**
  * Attach files from incoming mail to $project_object
  *
  * @param IncomingMail $incoming_mail
  * @param ProjectObject $project_object
  * @return null
  */
 function attachFilesToProjectObject(&$incoming_mail, &$project_object)
 {
     $attachments = $incoming_mail->getAttachments();
     $formated_attachments = array();
     if (is_foreachable($attachments)) {
         foreach ($attachments as $attachment) {
             $formated_attachments[] = array('path' => INCOMING_MAIL_ATTACHMENTS_FOLDER . '/' . $attachment->getTemporaryFilename(), 'filename' => $attachment->getOriginalFilename(), 'type' => strtolower($attachment->getContentType()));
         }
         // foreach
         attach_from_array($formated_attachments, $project_object);
     }
     // if
 }