public function runDeleteIncomingAccount(TBGRequest $request) { if ($account_id = $request['account_id']) { try { $account = new TBGIncomingEmailAccount($account_id); $account->delete(); return $this->renderJSON(array('message' => $this->getI18n()->__('Incoming email account deleted'))); } catch (Exception $e) { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('This is not a valid mailing account'))); } } else { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('This is not a valid mailing account'))); } }
public function processIncomingEmailAccount(TBGIncomingEmailAccount $account, $limit = 25) { $count = 0; if ($emails = $account->getUnprocessedEmails()) { try { $current_user = TBGContext::getUser(); foreach ($emails as $email) { $user = $this->getOrCreateUserFromEmailString($email->from); if ($user instanceof TBGUser) { if (TBGContext::getUser()->getID() != $user->getID()) { TBGContext::switchUserContext($user); } $message = $account->getMessage($email); $data = $message->getBodyPlain() ? $message->getBodyPlain() : strip_tags($message->getBodyHTML()); if ($data) { if (mb_detect_encoding($data, 'UTF-8', true) === false) { $data = utf8_encode($data); } $new_data = ''; foreach (explode("\n", $data) as $line) { $line = trim($line); if ($line) { $line = preg_replace('/^(_{2,}|-{2,})$/', "<hr>", $line); $new_data .= $line . "\n"; } else { $new_data .= "\n"; } } $data = nl2br($new_data, false); } // Parse the subject, and obtain the issues. $parsed_commit = TBGIssue::getIssuesFromTextByRegex(mb_decode_mimeheader($email->subject)); $issues = $parsed_commit["issues"]; // If any issues were found, add new comment to each issue. if ($issues) { foreach ($issues as $issue) { $text = preg_replace('#(^\\w.+:\\n)?(^>.*(\\n|$))+#mi', "", $data); $text = trim($text); if (!$this->processIncomingEmailCommand($text, $issue, $user) && $user->canPostComments()) { $comment = new TBGComment(); $comment->setContent($text); $comment->setPostedBy($user); $comment->setTargetID($issue->getID()); $comment->setTargetType(TBGComment::TYPE_ISSUE); $comment->save(); } } } else { if ($user->canReportIssues($account->getProject())) { $issue = new TBGIssue(); $issue->setProject($account->getProject()); $issue->setTitle(mb_decode_mimeheader($email->subject)); $issue->setDescription($data); $issue->setPostedBy($user); $issue->setIssuetype($account->getIssuetype()); $issue->save(); // Append the new issue to the list of affected issues. This // is necessary in order to process the attachments properly. $issues[] = $issue; } } // If there was at least a single affected issue, and mail // contains attachments, add those attachments to related issues. if ($issues && $message->hasAttachments()) { foreach ($message->getAttachments() as $attachment_no => $attachment) { echo 'saving attachment ' . $attachment_no; $name = $attachment['filename']; $new_filename = TBGContext::getUser()->getID() . '_' . NOW . '_' . basename($name); if (TBGSettings::getUploadStorage() == 'files') { $files_dir = TBGSettings::getUploadsLocalpath(); $filename = $files_dir . $new_filename; } else { $filename = $name; } TBGLogging::log('Creating issue attachment ' . $filename . ' from attachment ' . $attachment_no); echo 'Creating issue attachment ' . $filename . ' from attachment ' . $attachment_no; $content_type = $attachment['type'] . '/' . $attachment['subtype']; $file = new TBGFile(); $file->setRealFilename($new_filename); $file->setOriginalFilename(basename($name)); $file->setContentType($content_type); $file->setDescription($name); $file->setUploadedBy(TBGContext::getUser()); if (TBGSettings::getUploadStorage() == 'database') { $file->setContent($attachment['data']); } else { TBGLogging::log('Saving file ' . $new_filename . ' with content from attachment ' . $attachment_no); file_put_contents($new_filename, $attachment['data']); } $file->save(); // Attach file to each related issue. foreach ($issues as $issue) { $issue->attachFile($file); } } } $count++; } } } catch (Exception $e) { } if (TBGContext::getUser()->getID() != $current_user->getID()) { TBGContext::switchUserContext($current_user); } } $account->setTimeLastFetched(time()); $account->setNumberOfEmailsLastFetched($count); $account->save(); return $count; }