|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace ZBateson\MailMimeParser; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Parses a MIME message into a \ZBateson\MailMimeParser\Message object. |
|
11
|
|
|
* |
|
12
|
|
|
* To invoke, call parse on a MailMimeParser object. |
|
13
|
|
|
* |
|
14
|
|
|
* $handle = fopen('path/to/file.txt'); |
|
15
|
|
|
* $parser = new MailMimeParser(); |
|
16
|
|
|
* $parser->parse($handle); |
|
17
|
|
|
* fclose($handle); |
|
18
|
|
|
* |
|
19
|
|
|
* @author Zaahid Bateson |
|
20
|
|
|
*/ |
|
21
|
|
|
class MailMimeParser |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \ZBateson\MailMimeParser\SimpleDi dependency injection container |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $di; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Sets up the parser. |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
3 |
|
$this->di = SimpleDi::singleton(); |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Sets the default charset used by MMP for strings returned by read |
|
38
|
|
|
* operations on text content (e.g. MessagePart::getContentResource |
|
39
|
|
|
* @param type $charset |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setDefaultCharset($charset) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Parses the passed stream handle into a ZBateson\MailMimeParser\Message |
|
48
|
|
|
* object and returns it. |
|
49
|
|
|
* |
|
50
|
|
|
* Internally, the message is first copied to a temp stream (with php://temp |
|
51
|
|
|
* which may keep it in memory or write it to disk) and its stream is used. |
|
52
|
|
|
* That way if the message is too large to hold in memory it can be written |
|
53
|
|
|
* to a temporary file if need be. |
|
54
|
|
|
* |
|
55
|
|
|
* @param resource|string $handleOrString the resource handle to the input |
|
56
|
|
|
* stream of the mime message, or a string containing a mime message |
|
57
|
|
|
* @return \ZBateson\MailMimeParser\Message |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function parse($handleOrString) |
|
60
|
|
|
{ |
|
61
|
|
|
// $tempHandle is attached to $message, and closed in its destructor |
|
62
|
2 |
|
$tempHandle = fopen('php://temp', 'r+'); |
|
63
|
2 |
|
if (is_string($handleOrString)) { |
|
64
|
1 |
|
fwrite($tempHandle, $handleOrString); |
|
65
|
1 |
|
} else { |
|
66
|
1 |
|
stream_copy_to_stream($handleOrString, $tempHandle); |
|
67
|
|
|
} |
|
68
|
2 |
|
rewind($tempHandle); |
|
69
|
2 |
|
$parser = $this->di->newMessageParser(); |
|
70
|
2 |
|
return $parser->parse($tempHandle); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.