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