Example #1
0
 public function processIncomingEmailAccount(IncomingEmailAccount $account)
 {
     $count = 0;
     if ($emails = $account->getUnprocessedEmails()) {
         try {
             $current_user = framework\Context::getUser();
             foreach ($emails as $email) {
                 $user = $this->getOrCreateUserFromEmailString($email->from);
                 if ($user instanceof User) {
                     if (framework\Context::getUser()->getID() != $user->getID()) {
                         framework\Context::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 = Issue::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->canPostComments()) {
                                 $comment = new Comment();
                                 $comment->setContent($text);
                                 $comment->setPostedBy($user);
                                 $comment->setTargetID($issue->getID());
                                 $comment->setTargetType(Comment::TYPE_ISSUE);
                                 $comment->save();
                             }
                         }
                     } else {
                         if ($user->canReportIssues($account->getProject())) {
                             $issue = new Issue();
                             $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 = framework\Context::getUser()->getID() . '_' . NOW . '_' . basename($name);
                             if (framework\Settings::getUploadStorage() == 'files') {
                                 $files_dir = framework\Settings::getUploadsLocalpath();
                                 $filename = $files_dir . $new_filename;
                             } else {
                                 $filename = $name;
                             }
                             Logging::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 File();
                             $file->setRealFilename($new_filename);
                             $file->setOriginalFilename(basename($name));
                             $file->setContentType($content_type);
                             $file->setDescription($name);
                             $file->setUploadedBy(framework\Context::getUser());
                             if (framework\Settings::getUploadStorage() == 'database') {
                                 $file->setContent($attachment['data']);
                             } else {
                                 Logging::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 (framework\Context::getUser()->getID() != $current_user->getID()) {
             framework\Context::switchUserContext($current_user);
         }
     }
     $account->setTimeLastFetched(time());
     $account->setNumberOfEmailsLastFetched($count);
     $account->save();
     return $count;
 }
 public function createActivityImageByFileInfoAndActivityId(array $fileInfo, $activityId)
 {
     $file = new File();
     $file->setOriginalFilename(basename($fileInfo['name']));
     $file->setType($fileInfo['type']);
     $fileFormat = $file->getImageFormat();
     if (is_null($fileFormat) || '' == $fileFormat) {
         $fileFormat = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
     }
     $fileBaseName = md5(time()) . '_' . $fileFormat;
     $filename = 'ac_' . $fileInfo['member_id'] . '_' . $fileBaseName;
     $file->setName($filename);
     $file->setFilesize($fileInfo['size']);
     $bin = new FileBin();
     $bin->setBin($fileInfo['binary']);
     $file->setFileBin($bin);
     $file->save();
     $activityImage = new ActivityImage();
     $activityImage->setActivityDataId($activityId);
     $activityImage->setFileId($file->getId());
     $activityImage->setUri($this->getActivityImageUriByfileInfoAndFilename($fileInfo, $filename));
     $activityImage->setMimeType($file->type);
     $activityImage->save();
     $this->createUploadImageFileByFileInfoAndSaveFileName($fileInfo, $filename);
     return $activityImage;
 }
 public function executeUpload(sfWebRequest $request)
 {
     // for apiKey check
     $memberId = $this->getUser()->getMember();
     if ('1' === $request->getParameter('forceHtml')) {
         // workaround for some browsers
         $this->getResponse()->setContentType('text/html');
     }
     if (!$_FILES) {
         return $this->renderJSON(array('status' => 'error', 'message' => 'null file'));
     }
     if (!$_FILES['upfile']) {
         return $this->renderJSON(array('status' => 'error', 'message' => 'null file'));
     }
     $filename = basename($_FILES['upfile']['name']);
     if (!$filename) {
         return $this->renderJSON(array('status' => 'error', 'message' => 'null file'));
     }
     $tmpFileName = $request->getParameter('changedname');
     if ($filename != $tmpFileName) {
         $separates = $separates = explode('.', $filename);
         $cnt = count($separates);
         $fname = '';
         $ext = '';
         if (1 == $cnt) {
             $fname = $value;
         } else {
             $fname = join('', array_slice($separates, 0, $cnt - 1));
             $ext = '.' . $separates[$cnt - 1];
         }
         if ('' == $fname) {
             $filename = $tmpFileName;
         }
     }
     $filename = preg_replace('/\\|\\/|\\*|:|\\?|\\&|\'|\\"|>|<|undefined|\\|/', '-', urldecode($filename));
     $communityId = (int) $request->getParameter('community_id');
     if (1 <= (int) $communityId) {
         $community = Doctrine::getTable('Community')->find($communityId);
         if (!$community->isPrivilegeBelong($this->getUser()->getMember()->getId())) {
             return $this->renderJSON(array('status' => 'error', 'message' => 'you are not this community member.'));
         }
         $dirname = '/c' . $communityId;
     } else {
         $dirname = '/m' . $this->getUser()->getMember()->getId();
     }
     //validate $filepath
     if (!preg_match('/^\\/[mc][0-9]+/', $dirname)) {
         return $this->renderJSON(array('status' => 'error', 'message' => 'file path error. ' . $dirname));
     }
     $f = new File();
     $f->setOriginalFilename($filename);
     $f->setType($_FILES['upfile']['type']);
     $f->setName($dirname . '/' . time() . $filename);
     $f->setFilesize($_FILES['upfile']['size']);
     if ($stream = fopen($_FILES['upfile']['tmp_name'], 'r')) {
         $bin = new FileBin();
         $bin->setBin(stream_get_contents($stream));
         $f->setFileBin($bin);
         $f->save();
         $response = true;
     } else {
         //file open error
         $response = false;
     }
     if (true === $response) {
         return $this->renderJSON(array('status' => 'success', 'message' => 'file up success ' . $response, 'file' => $f->toArray(false)));
     } else {
         return $this->renderJSON(array('status' => 'error', 'message' => 'file upload error'));
     }
 }
 protected function getImportedPluginFile($filename, $filepath)
 {
     $file = new File();
     $file->setType('application/x-gzip');
     $file->setOriginalFilename($filename);
     $file->setName(strtr($filename, '.', '_'));
     $bin = new FileBin();
     $bin->setBin(file_get_contents($filepath));
     $file->setFileBin($bin);
     $file->save();
     $this->uploadToS3($file);
     return $file;
 }