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