/** * Create a new instance for an ComposerMessage for example. * * @param \GO\Base\Fs\File $file The temporary file * @return \MessageAttachment */ public function createFromTempFile(\GO\Base\Fs\File $file) { // $a['name'] = $file->name(); $a = new MessageAttachment(); $a->name = $file->name(); $a->mime = $file->mimeType(); $a->setTempFile($file); $a->size = $file->size(); return $a; }
/** * handleEmailFormInput * * This method can be used in Models and Controllers. It puts the email body * and inline (image) attachments from the client in the message, which can * then be used for storage in the database or sending emails. * * @param Array $params Must contain elements: body (string) and * * inlineAttachments (string). */ public function handleEmailFormInput($params) { if (!empty($params['subject'])) { $this->setSubject($params['subject']); } if (!empty($params['to'])) { $to = new EmailRecipients($params['to']); foreach ($to->getAddresses() as $email => $personal) { $this->addTo($email, $personal); } } if (!empty($params['cc'])) { $cc = new EmailRecipients($params['cc']); foreach ($cc->getAddresses() as $email => $personal) { $this->addCc($email, $personal); } } if (!empty($params['bcc'])) { $bcc = new EmailRecipients($params['bcc']); foreach ($bcc->getAddresses() as $email => $personal) { $this->addBcc($email, $personal); } } if (isset($params['alias_id'])) { $alias = \GO\Email\Model\Alias::model()->findByPk($params['alias_id']); $this->setFrom($alias->email, $alias->name); if (!empty($params['notification'])) { $this->setReadReceiptTo(array($alias->email => $alias->name)); } } if (isset($params['priority'])) { $this->setPriority($params['priority']); } if (isset($params['in_reply_to'])) { $headers = $this->getHeaders(); $headers->addTextHeader('In-Reply-To', $params['in_reply_to']); $headers->addTextHeader('References', $params['in_reply_to']); } if ($params['content_type'] == 'html') { $params['htmlbody'] = $this->_embedPastedImages($params['htmlbody']); //inlineAttachments is an array(array('url'=>'',tmp_file=>'relative/path/'); if (!empty($params['inlineAttachments'])) { $inlineAttachments = json_decode($params['inlineAttachments']); /* inline attachments must of course exist as a file, and also be used in * the message body */ if (count($inlineAttachments)) { foreach ($inlineAttachments as $ia) { //$tmpFile = new \GO\Base\Fs\File(\GO::config()->tmpdir.$ia['tmp_file']); if (empty($ia->tmp_file)) { continue; // Continue to the next inline attachment for processing. //throw new Exception("No temp file for inline attachment ".$ia->name); } $path = empty($ia->from_file_storage) ? \GO::config()->tmpdir . $ia->tmp_file : \GO::config()->file_storage_path . $ia->tmp_file; $tmpFile = new \GO\Base\Fs\File($path); if ($tmpFile->exists()) { //Different browsers reformat URL's to absolute or relative. So a pattern match on the filename. //$filename = rawurlencode($tmpFile->name()); $result = preg_match('/="([^"]*' . preg_quote($ia->token) . '[^"]*)"/', $params['htmlbody'], $matches); if ($result) { $img = \Swift_EmbeddedFile::fromPath($tmpFile->path()); $img->setContentType($tmpFile->mimeType()); $contentId = $this->embed($img); //$tmpFile->delete(); $params['htmlbody'] = \GO\Base\Util\String::replaceOnce($matches[1], $contentId, $params['htmlbody']); } else { //this may happen when an inline image was attached but deleted in the editor afterwards. // //throw new \Exception("Error: inline attachment could not be found in text: ".$ia->token); } } else { throw new \Exception("Error: inline attachment missing on server: " . $tmpFile->stripTempPath() . ".<br /><br />The temporary files folder is cleared on each login. Did you relogin?"); } } } } $params['htmlbody'] = $this->_fixRelativeUrls($params['htmlbody']); $htmlTop = '<html> <head> <style type="text/css"> body,p,td,div,span{ ' . \GO::config()->html_editor_font . ' }; body p{ margin:0px; } </style> </head> <body>'; $htmlBottom = '</body></html>'; $this->setHtmlAlternateBody($htmlTop . $params['htmlbody'] . $htmlBottom); } else { $this->setBody($params['plainbody'], 'text/plain'); } if (!empty($params['attachments'])) { $attachments = json_decode($params['attachments']); foreach ($attachments as $att) { $path = empty($att->from_file_storage) ? \GO::config()->tmpdir . $att->tmp_file : \GO::config()->file_storage_path . $att->tmp_file; $tmpFile = new \GO\Base\Fs\File($path); if ($tmpFile->exists()) { $file = \Swift_Attachment::fromPath($tmpFile->path()); $file->setContentType($tmpFile->mimeType()); $file->setFilename($att->fileName); $this->attach($file); //$tmpFile->delete(); } else { throw new \Exception("Error: attachment missing on server: " . $tmpFile->stripTempPath() . ".<br /><br />The temporary files folder is cleared on each login. Did you relogin?"); } } } }
public function actionPasteUploadTemporary($filename, $filetype) { $type = explode('/', $filetype); $extension = $type[1]; $_FILES['pastedFile']['name'] = $filename . '.' . $extension; $file = new GO\Base\Fs\File($_FILES['pastedFile']['tmp_name']); $file->move(GO::config()->getTempFolder(), $filename . '.' . $extension, true); $response = new \GO\Base\Data\JsonResponse(array('success' => true, 'data' => array('tmp_file' => $file->stripTempPath(), 'name' => $file->name(), 'size' => $file->size(), 'type' => $file->mimeType(), 'extension' => $file->extension(), 'human_size' => $file->humanSize(), 'from_file_storage' => false))); echo $response; }
/** * Output the right headers for outputting file data to a browser. * * @param \GO\Base\Fs\File $file Use \GO\Base\Fs\MemoryFile for outputting variables * @param boolean $inline * @param boolean $cache Cache the file for one day in the browser. * @param array $extraHeaders Key value array for extra headers */ public static function outputDownloadHeaders(\GO\Base\Fs\File $file, $inline = true, $cache = false, $extraHeaders = array()) { header('Content-Transfer-Encoding: binary'); $disposition = $inline ? 'inline' : 'attachment'; if ($cache) { header("Expires: " . date("D, j M Y G:i:s ", time() + 86400) . 'GMT'); //expires in 1 day header('Cache-Control: cache'); header('Pragma: cache'); } if (Http::isInternetExplorer()) { header('Content-Type: application/download'); header('Content-Disposition: ' . $disposition . '; filename="' . rawurlencode($file->name()) . '"'); if (!$cache) { header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); } } else { header('Content-Type: ' . $file->mimeType()); header('Content-Disposition: ' . $disposition . '; filename="' . $file->name() . '"'); if (!$cache) { header('Pragma: no-cache'); } } if ($file->exists()) { if ($file->extension() != 'zip') { //Don't set content lenght for zip files because gzip of apache will corrupt the download. http://www.heath-whyte.info/david/computers/corrupted-zip-file-downloads-with-php header('Content-Length: ' . $file->size()); } header("Last-Modified: " . gmdate("D, d M Y H:i:s", $file->mtime()) . " GMT"); header("ETag: " . $file->md5Hash()); } foreach ($extraHeaders as $header => $value) { header($header . ': ' . $value); } }