1<?php
2
3/**
4 * Swift Mailer Message Attachment
5 * Please read the LICENSE file
6 * @copyright Chris Corbyn <chris@w3style.co.uk>
7 * @author Chris Corbyn <chris@w3style.co.uk>
8 * @package Swift_Message
9 * @license GNU Lesser General Public License
10 */
11
12
13/**
14 * Attachment component for Swift Mailer
15 * @package Swift_Message
16 * @author Chris Corbyn <chris@w3style.co.uk>
17 */
18class Swift_Message_Attachment extends Swift_Message_Mime
19{
20  /**
21   * A numeric counter, incremented by 1 when a filename is made.
22   * @var int
23   */
24  protected static $fileId = 0;
25
26  /**
27   * Constructor
28   * @param mixed The data to use in the body
29   * @param string Mime type
30   * @param string The encoding format used
31   * @param string The charset used
32   */
33  public function __construct($data=null, $name=null, $type="application/octet-stream", $encoding="base64", $disposition="attachment")
34  {
35    parent::__construct();
36
37    $this->setContentType($type);
38    $this->setEncoding($encoding);
39    $this->setDescription($name);
40    $this->setDisposition($disposition);
41    $this->setFileName($name);
42
43    if ($data !== null) $this->setData($data, ($name === null));
44  }
45  /**
46   * Get a unique filename (just a sequence)
47   * @param string the prefix for the filename
48   * @return string
49   */
50  public static function generateFileName($prefix="file")
51  {
52    return $prefix . (self::$fileId++);
53  }
54  /**
55   * Get the level in the MIME hierarchy at which this section should appear.
56   * @return string
57   */
58  public function getLevel()
59  {
60    return Swift_Message_Mime::LEVEL_MIXED;
61  }
62  /**
63   * Overrides setData() in MIME so that a filename can be set
64   * @param mixed The data to set for the body
65   * @param boolean If the stream is a file, should it's filename be used?
66   * @throws Swift_FileException If the stream cannot be read
67   */
68  public function setData($data, $read_filename=true)
69  {
70    parent::setData($data);
71    if ($read_filename && ($data instanceof Swift_file))
72    {
73      $this->setFileName($data->getFileName());
74    }
75  }
76  /**
77   * Set the name (and description) used to identify the file
78   * This method overrides any value previously set with setDescription()
79   * @param string The filename including it's extension if any
80   * @throws Swift_Message_MimeException If some required headers have been deliberately removed
81   */
82  public function setFileName($name)
83  {
84    $this->headers->setAttribute("Content-Type", "name", $name);
85    $this->setDescription($name);
86    if ($this->headers->has("Content-Disposition"))
87    {
88      $this->headers->setAttribute("Content-Disposition", "filename", $name);
89    }
90  }
91  /**
92   * Get the filename of this attachment
93   * @return string
94   * @throws Swift_Message_MimeException If some vital headers have been removed
95   */
96  public function getFileName()
97  {
98    if ($this->headers->hasAttribute("Content-Type", "name"))
99    {
100      return $this->headers->getAttribute("Content-Type", "name");
101    }
102    else return null;
103  }
104  /**
105   * Set the Content-Description header
106   * @param string The description in the header (filename usually!)
107   */
108  public function setDescription($desc)
109  {
110    $this->headers->set("Content-Description", $desc);
111  }
112  /**
113   * Return the description in the headers
114   * @return string
115   */
116  public function getDescription()
117  {
118    if ($this->headers->has("Content-Description"))
119    {
120      return $this->headers->get("Content-Description");
121    }
122    else return null;
123  }
124  /**
125   * Set the disposition of the attachment (usually inline or attachment)
126   * @param string The value to use in the Content-Disposition field
127   */
128  public function setDisposition($disposition)
129  {
130    $this->headers->set("Content-Disposition", $disposition);
131  }
132  /**
133   * Get the disposition used in the attachment (usually inline or attachment)
134   * @return string
135   */
136  public function getDisposition()
137  {
138    if ($this->headers->has("Content-Disposition"))
139    {
140      return $this->headers->get("Content-Disposition");
141    }
142    else return null;
143  }
144  /**
145   * Execute needed logic prior to building
146   */
147  public function preBuild()
148  {
149    if ($this->getFileName() === null)
150    {
151      if ($this->getData() instanceof Swift_File)
152      {
153        $this->setFileName($this->getData()->getFileName());
154      }
155      else
156      {
157        $this->setFileName(self::generateFileName("file.att."));
158      }
159    }
160  }
161}
162