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\Message; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Message\IMessagePart; |
10
|
|
|
use ZBateson\MailMimeParser\Message\IMimePart; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Collection of static methods that return callables for common IMultiPart |
14
|
|
|
* child filters. |
15
|
|
|
* |
16
|
|
|
* @author Zaahid Bateson |
17
|
|
|
*/ |
18
|
|
|
abstract class PartFilter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Provides an 'attachment' filter used by Message::getAttachmentPart. |
22
|
|
|
* |
23
|
|
|
* The method filters out the following types of parts: |
24
|
|
|
* - text/plain and text/html parts that do not have an 'attachment' |
25
|
|
|
* disposition |
26
|
|
|
* - any part that returns true for isMultiPart() |
27
|
|
|
* - any part that returns true for isSignaturePart() |
28
|
|
|
* |
29
|
|
|
* @return callable |
30
|
|
|
*/ |
31
|
58 |
|
public static function fromAttachmentFilter() |
32
|
|
|
{ |
33
|
|
|
return function (IMessagePart $part) { |
34
|
58 |
|
$type = strtolower($part->getContentType()); |
35
|
58 |
|
if (in_array($type, [ 'text/plain', 'text/html' ]) && strcasecmp($part->getContentDisposition(), 'inline') === 0) { |
|
|
|
|
36
|
51 |
|
return false; |
37
|
|
|
} |
38
|
58 |
|
return !(($part instanceof IMimePart) |
39
|
58 |
|
&& ($part->isMultiPart() || $part->isSignaturePart())); |
40
|
58 |
|
}; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Provides a filter that keeps parts that contain a header of $name with a |
45
|
|
|
* value that matches $value (case insensitive). |
46
|
|
|
* |
47
|
|
|
* By default signed parts are excluded. Pass FALSE to the third parameter |
48
|
|
|
* to include them. |
49
|
|
|
* |
50
|
|
|
* @param string $name The header name to look up |
51
|
|
|
* @param string $value The value to match |
52
|
|
|
* @param bool $excludeSignedParts Optional signed parts exclusion (defaults |
53
|
|
|
* to true). |
54
|
|
|
* @return callable |
55
|
|
|
*/ |
56
|
3 |
|
public static function fromHeaderValue($name, $value, $excludeSignedParts = true) |
57
|
|
|
{ |
58
|
|
|
return function(IMessagePart $part) use ($name, $value, $excludeSignedParts) { |
|
|
|
|
59
|
3 |
|
if ($part instanceof IMimePart) { |
60
|
2 |
|
if ($excludeSignedParts && $part->isSignaturePart()) { |
61
|
1 |
|
return false; |
62
|
|
|
} |
63
|
1 |
|
return strcasecmp($part->getHeaderValue($name), $value) === 0; |
|
|
|
|
64
|
|
|
} |
65
|
1 |
|
return false; |
66
|
3 |
|
}; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Includes only parts that match the passed $mimeType in the return value |
71
|
|
|
* of a call to 'getContentType()'. |
72
|
|
|
* |
73
|
|
|
* @param string $mimeType Mime type of parts to find. |
74
|
|
|
* @return callable |
75
|
|
|
*/ |
76
|
10 |
|
public static function fromContentType($mimeType) |
77
|
|
|
{ |
78
|
|
|
return function(IMessagePart $part) use ($mimeType) { |
|
|
|
|
79
|
10 |
|
return strcasecmp($part->getContentType(), $mimeType) === 0; |
80
|
10 |
|
}; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns parts matching $mimeType that do not have a Content-Disposition |
85
|
|
|
* set to 'attachment'. |
86
|
|
|
* |
87
|
|
|
* @param string $mimeType Mime type of parts to find. |
88
|
|
|
* @return callable |
89
|
|
|
*/ |
90
|
82 |
|
public static function fromInlineContentType($mimeType) |
91
|
|
|
{ |
92
|
|
|
return function(IMessagePart $part) use ($mimeType) { |
|
|
|
|
93
|
82 |
|
return strcasecmp($part->getContentType(), $mimeType) === 0 |
94
|
82 |
|
&& strcasecmp($part->getContentDisposition(), 'attachment') !== 0; |
|
|
|
|
95
|
82 |
|
}; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Finds parts with the passed disposition (matching against |
100
|
|
|
* IMessagePart::getContentDisposition()), optionally including |
101
|
|
|
* multipart parts and signed parts. |
102
|
|
|
* |
103
|
|
|
* @param string $disposition The disposition to find. |
104
|
|
|
* @param bool $includeMultipart Optionally include multipart parts by |
105
|
|
|
* passing true (defaults to false). |
106
|
|
|
* @param bool $includeSignedParts Optionally include signed parts (defaults |
107
|
|
|
* to false). |
108
|
|
|
* @return callable |
109
|
|
|
*/ |
110
|
7 |
|
public static function fromDisposition($disposition, $includeMultipart = false, $includeSignedParts = false) |
111
|
|
|
{ |
112
|
|
|
return function(IMessagePart $part) use ($disposition, $includeMultipart, $includeSignedParts) { |
|
|
|
|
113
|
7 |
|
if (($part instanceof IMimePart) && ((!$includeMultipart && $part->isMultiPart()) || (!$includeSignedParts && $part->isSignaturePart()))) { |
114
|
3 |
|
return false; |
115
|
|
|
} |
116
|
7 |
|
return strcasecmp($part->getContentDisposition(), $disposition) === 0; |
|
|
|
|
117
|
7 |
|
}; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|