Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mime / Message.php
blob197668a01dbfe0fcedd29265a609130de81caf81
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Mime;
12 class Message
15 protected $parts = array();
16 protected $mime = null;
18 /**
19 * Returns the list of all Zend\Mime\Part in the message
21 * @return array of \Zend\Mime\Part
23 public function getParts()
25 return $this->parts;
28 /**
29 * Sets the given array of Zend\Mime\Part as the array for the message
31 * @param array $parts
33 public function setParts($parts)
35 $this->parts = $parts;
38 /**
39 * Append a new Zend\Mime\Part to the current message
41 * @param \Zend\Mime\Part $part
43 public function addPart(Part $part)
45 /**
46 * @todo check for duplicate object handle
48 $this->parts[] = $part;
51 /**
52 * Check if message needs to be sent as multipart
53 * MIME message or if it has only one part.
55 * @return bool
57 public function isMultiPart()
59 return (count($this->parts) > 1);
62 /**
63 * Set Zend\Mime\Mime object for the message
65 * This can be used to set the boundary specifically or to use a subclass of
66 * Zend\Mime for generating the boundary.
68 * @param \Zend\Mime\Mime $mime
70 public function setMime(Mime $mime)
72 $this->mime = $mime;
75 /**
76 * Returns the Zend\Mime\Mime object in use by the message
78 * If the object was not present, it is created and returned. Can be used to
79 * determine the boundary used in this message.
81 * @return \Zend\Mime\Mime
83 public function getMime()
85 if ($this->mime === null) {
86 $this->mime = new Mime();
89 return $this->mime;
92 /**
93 * Generate MIME-compliant message from the current configuration
95 * This can be a multipart message if more than one MIME part was added. If
96 * only one part is present, the content of this part is returned. If no
97 * part had been added, an empty string is returned.
99 * Parts are separated by the mime boundary as defined in Zend\Mime\Mime. If
100 * {@link setMime()} has been called before this method, the Zend\Mime\Mime
101 * object set by this call will be used. Otherwise, a new Zend\Mime\Mime object
102 * is generated and used.
104 * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
105 * @return string
107 public function generateMessage($EOL = Mime::LINEEND)
109 if (!$this->isMultiPart()) {
110 $part = current($this->parts);
111 $body = $part->getContent($EOL);
112 } else {
113 $mime = $this->getMime();
115 $boundaryLine = $mime->boundaryLine($EOL);
116 $body = 'This is a message in Mime Format. If you see this, '
117 . "your mail reader does not support this format." . $EOL;
119 foreach (array_keys($this->parts) as $p) {
120 $body .= $boundaryLine
121 . $this->getPartHeaders($p, $EOL)
122 . $EOL
123 . $this->getPartContent($p, $EOL);
126 $body .= $mime->mimeEnd($EOL);
129 return trim($body);
133 * Get the headers of a given part as an array
135 * @param int $partnum
136 * @return array
138 public function getPartHeadersArray($partnum)
140 return $this->parts[$partnum]->getHeadersArray();
144 * Get the headers of a given part as a string
146 * @param int $partnum
147 * @param string $EOL
148 * @return string
150 public function getPartHeaders($partnum, $EOL = Mime::LINEEND)
152 return $this->parts[$partnum]->getHeaders($EOL);
156 * Get the (encoded) content of a given part as a string
158 * @param int $partnum
159 * @param string $EOL
160 * @return string
162 public function getPartContent($partnum, $EOL = Mime::LINEEND)
164 return $this->parts[$partnum]->getContent($EOL);
168 * Explode MIME multipart string into separate parts
170 * Parts consist of the header and the body of each MIME part.
172 * @param string $body
173 * @param string $boundary
174 * @throws Exception\RuntimeException
175 * @return array
177 protected static function _disassembleMime($body, $boundary)
179 $start = 0;
180 $res = array();
181 // find every mime part limiter and cut out the
182 // string before it.
183 // the part before the first boundary string is discarded:
184 $p = strpos($body, '--' . $boundary."\n", $start);
185 if ($p === false) {
186 // no parts found!
187 return array();
190 // position after first boundary line
191 $start = $p + 3 + strlen($boundary);
193 while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
194 $res[] = substr($body, $start, $p-$start);
195 $start = $p + 3 + strlen($boundary);
198 // no more parts, find end boundary
199 $p = strpos($body, '--' . $boundary . '--', $start);
200 if ($p===false) {
201 throw new Exception\RuntimeException('Not a valid Mime Message: End Missing');
204 // the remaining part also needs to be parsed:
205 $res[] = substr($body, $start, $p-$start);
206 return $res;
210 * Decodes a MIME encoded string and returns a Zend\Mime\Message object with
211 * all the MIME parts set according to the given string
213 * @param string $message
214 * @param string $boundary
215 * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
216 * @throws Exception\RuntimeException
217 * @return \Zend\Mime\Message
219 public static function createFromMessage($message, $boundary, $EOL = Mime::LINEEND)
221 $parts = Decode::splitMessageStruct($message, $boundary, $EOL);
223 $res = new static();
224 foreach ($parts as $part) {
226 // now we build a new MimePart for the current Message Part:
227 $properties = array();
228 foreach ($part['header'] as $header) {
229 /** @var \Zend\Mail\Header\HeaderInterface $header */
231 * @todo check for characterset and filename
234 $fieldName = $header->getFieldName();
235 $fieldValue = $header->getFieldValue();
236 switch (strtolower($fieldName)) {
237 case 'content-type':
238 $properties['type'] = $fieldValue;
239 break;
240 case 'content-transfer-encoding':
241 $properties['encoding'] = $fieldValue;
242 break;
243 case 'content-id':
244 $properties['id'] = trim($fieldValue,'<>');
245 break;
246 case 'content-disposition':
247 $properties['disposition'] = $fieldValue;
248 break;
249 case 'content-description':
250 $properties['description'] = $fieldValue;
251 break;
252 case 'content-location':
253 $properties['location'] = $fieldValue;
254 break;
255 case 'content-language':
256 $properties['language'] = $fieldValue;
257 break;
258 default:
259 throw new Exception\RuntimeException('Unknown header ignored for MimePart:' . $fieldName);
263 $body = $part['body'];
265 if (isset($properties['encoding'])) {
266 switch ($properties['encoding']) {
267 case 'quoted-printable':
268 $body = quoted_printable_decode($body);
269 break;
270 case 'base64':
271 $body = base64_decode($body);
272 break;
276 $newPart = new Part($body);
277 foreach ($properties as $key => $value) {
278 $newPart->$key = $value;
280 $res->addPart($newPart);
283 return $res;