Пример #1
0
 /**
  * Method used to extract and associate attachments in an email
  * to the given issue.
  *
  * @param   integer $issue_id The issue ID
  * @param   mixed   $input The full body of the message or decoded email.
  * @param   boolean $internal_only Whether these files are supposed to be internal only or not
  * @param   integer $associated_note_id The note ID that these attachments should be associated with
  * @return  void
  */
 public static function extractAttachments($issue_id, $input, $internal_only = false, $associated_note_id = null)
 {
     if (!is_object($input)) {
         $input = Mime_Helper::decode($input, true, true);
     }
     // figure out who should be the 'owner' of this attachment
     $sender_email = strtolower(Mail_Helper::getEmailAddress($input->headers['from']));
     $usr_id = User::getUserIDByEmail($sender_email);
     $prj_id = Issue::getProjectID($issue_id);
     $unknown_user = false;
     if (empty($usr_id)) {
         if (CRM::hasCustomerIntegration($prj_id)) {
             // try checking if a customer technical contact has this email associated with it
             try {
                 $crm = CRM::getInstance($prj_id);
                 $contact = $crm->getContactByEmail($sender_email);
                 $usr_id = User::getUserIDByContactID($contact->getContactID());
             } catch (CRMException $e) {
                 $usr_id = null;
             }
         }
         if (empty($usr_id)) {
             // if we couldn't find a real customer by that email, set the usr_id to be the system user id,
             // and store the actual email address in the unknown_user field.
             $usr_id = APP_SYSTEM_USER_ID;
             $unknown_user = $input->headers['from'];
         }
     }
     // now for the real thing
     $attachments = Mime_Helper::getAttachments($input);
     if (count($attachments) > 0) {
         if (empty($associated_note_id)) {
             $history_log = ev_gettext('Attachment originated from an email');
         } else {
             $history_log = ev_gettext('Attachment originated from a note');
         }
         $iaf_ids = array();
         foreach ($attachments as &$attachment) {
             $attach = Workflow::shouldAttachFile($prj_id, $issue_id, $usr_id, $attachment);
             if (!$attach) {
                 continue;
             }
             $iaf_id = Attachment::addFile(0, $attachment['filename'], $attachment['filetype'], $attachment['blob']);
             if (!$iaf_id) {
                 continue;
             }
             $iaf_ids[] = $iaf_id;
         }
         if ($iaf_ids) {
             Attachment::attachFiles($issue_id, $usr_id, $iaf_ids, $internal_only, $history_log, $unknown_user, $associated_note_id);
         }
         // mark the note as having attachments (poor man's caching system)
         if ($associated_note_id != false) {
             Note::setAttachmentFlag($associated_note_id);
         }
     }
 }
Пример #2
0
 /**
  * Method used to extract and associate attachments in an email
  * to the given issue.
  *
  * @access  public
  * @param   integer $issue_id The issue ID
  * @param   string $full_email The full contents of the email
  * @param   boolean $internal_only Whether these files are supposed to be internal only or not
  * @param   integer $associated_note_id The note ID that these attachments should be associated with
  * @return  void
  */
 function extractAttachments($issue_id, $full_email, $internal_only = false, $associated_note_id = false)
 {
     // figure out who should be the 'owner' of this attachment
     $structure = Mime_Helper::decode($full_email, false, false);
     $sender_email = strtolower(Mail_API::getEmailAddress($structure->headers['from']));
     $usr_id = User::getUserIDByEmail($sender_email);
     $unknown_user = false;
     if (empty($usr_id)) {
         $prj_id = Issue::getProjectID($issue_id);
         if (Customer::hasCustomerIntegration($prj_id)) {
             // try checking if a customer technical contact has this email associated with it
             list(, $contact_id) = Customer::getCustomerIDByEmails($prj_id, array($sender_email));
             if (!empty($contact_id)) {
                 $usr_id = User::getUserIDByContactID($contact_id);
             }
         }
         if (empty($usr_id)) {
             // if we couldn't find a real customer by that email, set the usr_id to be the system user id,
             // and store the actual email address in the unknown_user field.
             $usr_id = APP_SYSTEM_USER_ID;
             $unknown_user = $structure->headers['from'];
         }
     }
     // now for the real thing
     $attachments = Mime_Helper::getAttachments($full_email);
     if (count($attachments) > 0) {
         if (empty($associated_note_id)) {
             $history_log = 'Attachment originated from an email';
         } else {
             $history_log = 'Attachment originated from a note';
         }
         $attachment_id = Attachment::add($issue_id, $usr_id, $history_log, $internal_only, $unknown_user, $associated_note_id);
         for ($i = 0; $i < count($attachments); $i++) {
             Attachment::addFile($attachment_id, $issue_id, $attachments[$i]['filename'], $attachments[$i]['filetype'], $attachments[$i]['blob']);
         }
         // mark the note as having attachments (poor man's caching system)
         if ($associated_note_id != false) {
             Note::setAttachmentFlag($associated_note_id);
         }
     }
 }