1<?php
2
3/**
4 * Swift Mailer Message MIME Part
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_Mime");
14
15/**
16 * MIME Part body component for Swift Mailer
17 * @package Swift_Message
18 * @author Chris Corbyn <chris@w3style.co.uk>
19 */
20class Swift_Message_Part extends Swift_Message_Mime
21{
22  /**
23   * Constructor
24   * @param mixed The data to use in the body
25   * @param string Mime type
26   * @param string The encoding format used
27   * @param string The charset used
28   */
29  public function __construct($data=null, $type="text/plain", $encoding=null, $charset=null)
30  {
31    parent::__construct();
32
33    $this->setContentType($type);
34    $this->setEncoding($encoding);
35    $this->setCharset($charset);
36    $this->setFlowed(false);
37
38    if ($data !== null)
39    {
40      $this->setData($data);
41      if ($charset === null)
42      {
43        Swift_ClassLoader::load("Swift_Message_Encoder");
44        if (is_string($data) && Swift_Message_Encoder::instance()->isUTF8($data)) $this->setCharset("utf-8");
45        else $this->setCharset("iso-8859-1"); //The likely encoding
46      }
47    }
48  }
49  /**
50   * Get the level in the MIME hierarchy at which this section should appear.
51   * @return string
52   */
53  public function getLevel()
54  {
55    return Swift_Message_Mime::LEVEL_ALTERNATIVE;
56  }
57  /**
58   * Alias for setData()
59   * @param mixed Body
60   */
61  public function setBody($body)
62  {
63    $this->setData($body);
64  }
65  /**
66   * Alias for getData()
67   * @return mixed The document body
68   */
69  public function getBody()
70  {
71    return $this->getData();
72  }
73  /**
74   * Set the charset of the document
75   * @param string The charset used
76   */
77  public function setCharset($charset)
78  {
79    $this->headers->setAttribute("Content-Type", "charset", $charset);
80    if (($this->getEncoding() == "7bit") && (strtolower($charset) == "utf-8" || strtolower($charset) == "utf8")) $this->setEncoding("8bit");
81  }
82  /**
83   * Get the charset used in the document
84   * Returns null if none is set
85   * @return string
86   */
87  public function getCharset()
88  {
89    if ($this->headers->hasAttribute("Content-Type", "charset"))
90    {
91      return $this->headers->getAttribute("Content-Type", "charset");
92    }
93    else
94    {
95      return null;
96    }
97  }
98  /**
99   * Set the "format" attribute to flowed
100   * @param boolean On or Off
101   */
102  public function setFlowed($flowed=true)
103  {
104    $value = null;
105    if ($flowed) $value = "flowed";
106    $this->headers->setAttribute("Content-Type", "format", $value);
107  }
108  /**
109   * Pre-compilation logic
110   */
111  public function preBuild()
112  {
113    if (!($enc = $this->getEncoding())) $this->setEncoding("8bit");
114    $data = $this->getData();
115    if ($this->getCharset() === null && !$this->numChildren())
116    {
117      if (is_string($data) && Swift_Message_Encoder::instance()->isUTF8($data))
118      {
119        $this->setCharset("utf-8");
120      }
121      elseif (is_string($data) && Swift_Message_Encoder::instance()->is7BitAscii($data))
122      {
123        $this->setCharset("us-ascii");
124        if (!$enc) $this->setEncoding("7bit");
125      }
126      else $this->setCharset("iso-8859-1");
127    }
128    elseif ($this->numChildren())
129    {
130      $this->setCharset(null);
131      $this->setEncoding("7bit");
132    }
133  }
134}
135