Completed
Push — master ( 070c71...411399 )
by Rudie
02:07
created

IMAPMessage::header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace rdx\imap;
4
5
class IMAPMessage implements IMAPMessagePartInterface {
6
7
	protected $mailbox; // typeof IMAPMailbox
8
9
	protected $msgNumber = 1; // starts at 1, not 0
10
	protected $unseen = true;
11
12
	protected $headers = [];
13
	protected $structure; // typeof stdClass
14
15
	protected $subject = '';
16
	protected $parts = [];
17
	protected $parameters = [];
18
	protected $plainBody;
19
	protected $HTMLBody;
20
	protected $attachments = []; // typeof Array<IMAPMessageAttachment>
21
22
	public function __construct( IMAPMailbox $mailbox, $msgNumber, $unseen = null ) {
23
		$this->mailbox = $mailbox;
24
		$this->msgNumber = $msgNumber;
25
		$this->unseen = $unseen;
26
27
		if ( $unseen === null ) {
28
			$this->unseen = (bool) trim($this->header('unseen'));
29
		}
30
	}
31
32
	protected function flags( $flags, $clear ) {
33
		$cb = $clear ? 'imap_clearflag_full' : 'imap_setflag_full';
34
35
		$feedback = [];
36
		foreach ( (array)$flags AS $flag ) {
37
			$flag = '\\' . ucfirst($flag);
38
			$feedback[] = $cb($this->mailbox()->imap(), (string)$this->msgNumber, $flag);
39
		}
40
41
		return is_array($flags) ? $feedback : $feedback[0];
42
	}
43
44
	public function flag( $flags ) {
45
		return $this->flags($flags, false);
46
	}
47
48
	public function unflag( $flags ) {
49
		return $this->flags($flags, true);
50
	}
51
52
	public function utc() {
53
		return strtotime($this->header('date'));
54
	}
55
56
	public function subject() {
57
		if ( empty($this->subject) ) {
58
			$subject = imap_utf8($this->header('subject'));
59
			$this->subject = trim($subject);
60
		}
61
62
		return $this->subject;
63
	}
64
65
	public function headers() {
66
		if ( empty($this->headers) ) {
67
			$headers = imap_headerinfo($this->mailbox()->imap(), $this->msgNumber);
68
			foreach ( $headers as $name => $value ) {
69
				$this->headers[ strtolower($name) ] = $value;
70
			}
71
		}
72
73
		return $this->headers;
74
	}
75
76
	public function header( $name ) {
77
		$headers = $this->headers();
78
		return @$headers[ strtolower($name) ];
79
	}
80
81
	public function parts() {
82
		if ( empty($this->parts) ) {
83
			$structure = $this->structure();
84
85
			// Possibilities:
86
			// - PLAIN
87
			// - ALTERNATIVE
88
			// - MIXED
89
			// - DELIVERY-STATUS
90
			// - RFC822
91
			// - REPORT
92
			// - HTML
93
			// - CALENDAR
94
			// - JPEG
95
96
			if ( empty($structure->parts) ) {
97
				$this->parts[] = new IMAPMessagePart(
98
					$this,
99
					$structure,
100
					[1]
101
				);
102
			}
103
			else {
104
				foreach ($structure->parts as $n => $part) {
105
					$this->parts[] = new IMAPMessagePart(
106
						$this,
107
						$part,
108
						[$n+1]
109
					);
110
				}
111
			}
112
		}
113
114
		return $this->parts;
115
	}
116
117 View Code Duplication
	public function allParts( $withContainers = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
		$parts = [];
119
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
120
			foreach ( $message->parts() as $part ) {
121
				if ( $part->parts() ) {
122
					if ( $withContainers ) {
123
						$parts[] = $part;
124
					}
125
126
					$iterate($part);
127
				}
128
				else {
129
					$parts[] = $part;
130
				}
131
			}
132
		};
133
134
		$iterate($this);
135
		return $parts;
136
	}
137
138
	public function part( $index ) {
139
		$parts = $this->parts();
140
		return @$parts[$index];
141
	}
142
143
	public function structure() {
144
		if ( empty($this->structure) ) {
145
			$this->structure = imap_fetchstructure($this->mailbox()->imap(), $this->msgNumber);
146
		}
147
148
		return $this->structure;
149
	}
150
151
	public function content() {
152
		if ( count($this->parts()) == 1 ) {
153
			return $this->part(0)->content();
154
		}
155
156
		return '';
157
	}
158
159 View Code Duplication
	public function parameters() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
		if ( empty($this->parameters) ) {
161
			$structure = $this->structure();
162
163
			$this->parameters['bytes'] = @$structure->bytes;
164
165
			foreach ((array) @$structure->parameters as $param) {
166
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
167
			}
168
			foreach ((array) @$structure->dparameters as $param) {
169
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
170
			}
171
		}
172
173
		return $this->parameters;
174
	}
175
176
	public function simpleStructure() {
177
		$parts = [];
178
		foreach ( $this->allParts(true) as $part ) {
179
			$name = '';
180
181
			$name .= implode('.', $part->section()) . '. ';
182
			if ( $part->parts() ) {
183
				$name .= '*';
184
			}
185
			$name .= $part->subtype();
186
			if ( $bytes = $part->parameter('bytes') ) {
187
				$name .= ' (' . $bytes . ')';
188
			}
189
190
			$parts[] = $name;
191
		}
192
193
		return $parts;
194
	}
195
196
	public function msgNumber() {
197
		return $this->msgNumber;
198
	}
199
200
	public function mailbox() {
201
		return $this->mailbox;
202
	}
203
204
}
205