/framework/vendor/swift/lib/classes/Swift/Mime/Attachment.php
PHP | 143 lines | 67 code | 15 blank | 61 comment | 3 complexity | 7e4b16869435da9347b5e680811f2add MD5 | raw file
1<?php 2 3/* 4 * This file is part of SwiftMailer. 5 * (c) 2004-2009 Chris Corbyn 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11//@require 'Swift/Mime/SimpleMimeEntity.php'; 12//@require 'Swift/Mime/ContentEncoder.php'; 13//@require 'Swift/Mime/HeaderSet.php'; 14//@require 'Swift/FileStream.php'; 15//@require 'Swift/KeyCache.php'; 16 17/** 18 * An attachment, in a multipart message. 19 * @package Swift 20 * @subpackage Mime 21 * @author Chris Corbyn 22 */ 23class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity 24{ 25 26 /** Recognized MIME types */ 27 private $_mimeTypes = array(); 28 29 /** 30 * Create a new Attachment with $headers, $encoder and $cache. 31 * @param Swift_Mime_HeaderSet $headers 32 * @param Swift_Mime_ContentEncoder $encoder 33 * @param Swift_KeyCache $cache 34 * @param array $mimeTypes optional 35 */ 36 public function __construct(Swift_Mime_HeaderSet $headers, 37 Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, 38 $mimeTypes = array()) 39 { 40 parent::__construct($headers, $encoder, $cache); 41 $this->setDisposition('attachment'); 42 $this->setContentType('application/octet-stream'); 43 $this->_mimeTypes = $mimeTypes; 44 } 45 46 /** 47 * Get the nesting level used for this attachment. 48 * Always returns {@link LEVEL_MIXED}. 49 * @return int 50 */ 51 public function getNestingLevel() 52 { 53 return self::LEVEL_MIXED; 54 } 55 56 /** 57 * Get the Content-Disposition of this attachment. 58 * By default attachments have a disposition of "attachment". 59 * @return string 60 */ 61 public function getDisposition() 62 { 63 return $this->_getHeaderFieldModel('Content-Disposition'); 64 } 65 66 /** 67 * Set the Content-Disposition of this attachment. 68 * @param string $disposition 69 */ 70 public function setDisposition($disposition) 71 { 72 if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition)) 73 { 74 $this->getHeaders()->addParameterizedHeader( 75 'Content-Disposition', $disposition 76 ); 77 } 78 return $this; 79 } 80 81 /** 82 * Get the filename of this attachment when downloaded. 83 * @return string 84 */ 85 public function getFilename() 86 { 87 return $this->_getHeaderParameter('Content-Disposition', 'filename'); 88 } 89 90 /** 91 * Set the filename of this attachment. 92 * @param string $filename 93 */ 94 public function setFilename($filename) 95 { 96 $this->_setHeaderParameter('Content-Disposition', 'filename', $filename); 97 $this->_setHeaderParameter('Content-Type', 'name', $filename); 98 return $this; 99 } 100 101 /** 102 * Get the file size of this attachment. 103 * @return int 104 */ 105 public function getSize() 106 { 107 return $this->_getHeaderParameter('Content-Disposition', 'size'); 108 } 109 110 /** 111 * Set the file size of this attachment. 112 * @param int $size 113 */ 114 public function setSize($size) 115 { 116 $this->_setHeaderParameter('Content-Disposition', 'size', $size); 117 return $this; 118 } 119 120 /** 121 * Set the file that this attachment is for. 122 * @param Swift_FileStream $file 123 * @param string $contentType optional 124 */ 125 public function setFile(Swift_FileStream $file, $contentType = null) 126 { 127 $this->setFilename(basename($file->getPath())); 128 $this->setBody($file, $contentType); 129 if (!isset($contentType)) 130 { 131 $extension = strtolower(substr( 132 $file->getPath(), strrpos($file->getPath(), '.') + 1 133 )); 134 135 if (array_key_exists($extension, $this->_mimeTypes)) 136 { 137 $this->setContentType($this->_mimeTypes[$extension]); 138 } 139 } 140 return $this; 141 } 142 143}