/** * Test that the the get_mime_type() works correctly * */ public function testGetMimeType() { $this->assertEquals('text/plain', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.csv')); $this->assertEquals('image/gif', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.gif')); $this->assertEquals('text/html', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.html')); $this->assertEquals('image/jpeg', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.jpg')); $this->assertEquals('image/png', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.png')); $this->assertEquals('image/vnd.adobe.photoshop', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.psd')); $this->assertEquals('audio/x-wav', HTTP::get_mime_type(FRAMEWORK_DIR . '/tests/control/files/file.wav')); }
/** * Construct an HTTPResponse that will deliver a file to the client. * Caution: Since it requires $fileData to be passed as binary data (no stream support), * it's only advisable to send small files through this method. * * @static * @param $fileData * @param $fileName * @param null $mimeType * @return HTTPResponse */ public static function send_file($fileData, $fileName, $mimeType = null) { if (!$mimeType) { $mimeType = HTTP::get_mime_type($fileName); } $response = new HTTPResponse($fileData); $response->addHeader("Content-Type", "{$mimeType}; name=\"" . addslashes($fileName) . "\""); // Note a IE-only fix that inspects this header in HTTP::add_cache_headers(). $response->addHeader("Content-Disposition", "attachment; filename=\"" . addslashes($fileName) . "\""); $response->addHeader("Content-Length", strlen($fileData)); return $response; }
/** * Encode the contents of a file for emailing, including headers * * $file can be an array, in which case it expects these members: * 'filename' - the filename of the file * 'contents' - the raw binary contents of the file as a string * and can optionally include these members: * 'mimetype' - the mimetype of the file (calculated from filename if missing) * 'contentLocation' - the 'Content-Location' header value for the file * * $file can also be a string, in which case it is assumed to be the filename * * h5. contentLocation * * Content Location is one of the two methods allowed for embedding images into an html email. * It's also the simplest, and best supported. * * Assume we have an email with this in the body: * * <img src="http://example.com/image.gif" /> * * To display the image, an email viewer would have to download the image from the web every time * it is displayed. Due to privacy issues, most viewers will not display any images unless * the user clicks 'Show images in this email'. Not optimal. * * However, we can also include a copy of this image as an attached file in the email. * By giving it a contentLocation of "http://example.com/image.gif" most email viewers * will use this attached copy instead of downloading it. Better, * most viewers will show it without a 'Show images in this email' conformation. * * Here is an example of passing this information through Email.php: * * $email = new Email(); * $email->attachments[] = array( * 'filename' => BASE_PATH . "/themes/mytheme/images/header.gif", * 'contents' => file_get_contents(BASE_PATH . "/themes/mytheme/images/header.gif"), * 'mimetype' => 'image/gif', * 'contentLocation' => Director::absoluteBaseURL() . "/themes/mytheme/images/header.gif" * ); * * @param array|string $file * @param bool $destFileName * @param string $disposition * @param string $extraHeaders * @return string */ protected function encodeFileForEmail($file, $destFileName = false, $disposition = NULL, $extraHeaders = "") { if (!$file) { throw new InvalidArgumentException("Not passed a filename and/or data"); } if (is_string($file)) { $file = array('filename' => $file); $fh = fopen($file['filename'], "rb"); if ($fh) { $file['contents'] = ""; while (!feof($fh)) { $file['contents'] .= fread($fh, 10000); } fclose($fh); } } // Build headers, including content type if (!$destFileName) { $base = basename($file['filename']); } else { $base = $destFileName; } $mimeType = !empty($file['mimetype']) ? $file['mimetype'] : HTTP::get_mime_type($file['filename']); if (!$mimeType) { $mimeType = "application/unknown"; } if (empty($disposition)) { $disposition = isset($file['contentLocation']) ? 'inline' : 'attachment'; } // Encode for emailing if (substr($mimeType, 0, 4) != 'text') { $encoding = "base64"; $file['contents'] = chunk_split(base64_encode($file['contents'])); } else { // This mime type is needed, otherwise some clients will show it as an inline attachment $mimeType = 'application/octet-stream'; $encoding = "quoted-printable"; $file['contents'] = quoted_printable_encode($file['contents']); } $headers = "Content-type: {$mimeType};\n\tname=\"{$base}\"\n" . "Content-Transfer-Encoding: {$encoding}\n" . "Content-Disposition: {$disposition};\n\tfilename=\"{$base}\"\n"; if (isset($file['contentLocation'])) { $headers .= 'Content-Location: ' . $file['contentLocation'] . "\n"; } $headers .= $extraHeaders . "\n"; // Return completed packet return $headers . $file['contents']; }