1<?php
2
3/**
4 * Swift Mailer Embedded File (like an image or a midi file)
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
12require_once dirname(__FILE__) . "/../ClassLoader.php";
13Swift_ClassLoader::load("Swift_Message_Attachment");
14
15/**
16 * Embedded File component for Swift Mailer
17 * @package Swift_Message
18 * @author Chris Corbyn <chris@w3style.co.uk>
19 */
20class Swift_Message_EmbeddedFile extends Swift_Message_Attachment
21{
22  /**
23   * The content-id in the headers (used in <img src=...> values)
24   * @var string
25   */
26  protected $cid = null;
27
28  /**
29   * Constructor
30   * @param mixed The input source.  Can be a file or a string
31   * @param string The filename to use, optional
32   * @param string The MIME type to use, optional
33   * @param string The Content-ID to use, optional
34   * @param string The encoding format to use, optional
35   */
36  public function __construct($data=null, $name=null, $type="application/octet-stream", $cid=null, $encoding="base64")
37  {
38    parent::__construct($data, $name, $type, $encoding, "inline");
39
40    if ($cid === null)
41    {
42      $cid = self::generateFileName("swift-" . uniqid(time()) . ".");
43      $cid = urlencode($cid) . "@" . (!empty($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : "swift");
44    }
45    $this->setContentId($cid);
46
47    if ($name === null && !($data instanceof Swift_File)) $this->setFileName($cid);
48
49    $this->headers->set("Content-Description", null);
50  }
51  /**
52   * Get the level in the MIME hierarchy at which this section should appear.
53   * @return string
54   */
55  public function getLevel()
56  {
57    return Swift_Message_Mime::LEVEL_RELATED;
58  }
59  /**
60   * Set the Content-Id to use
61   * @param string The content-id
62   */
63  public function setContentId($id)
64  {
65    $id = (string) $id;
66    $this->cid = $id;
67    $this->headers->set("Content-ID", "<" . $id . ">");
68  }
69  /**
70   * Get the content-id of this file
71   * @return string
72   */
73  public function getContentId()
74  {
75    return $this->cid;
76  }
77}
78