IMAPMessage::headerString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace rdx\imap;
4
5
class IMAPMessage extends IMAPMessageContent implements IMAPMessagePartInterface {
6
7
	/** @var IMAPMailbox */
8
	protected $mailbox;
9
10
	/** @var int */
11
	protected $msgNumber = 1; // Body starts at 1. Header is 0.
12
13
	/** @var bool */
14
	protected $unseen = true;
15
16
	/** @var string[]  */
17
	protected $headerInfo = [];
18
19
	/** @var string */
20
	protected $subject = '';
21
22
	/** @var string */
23
	protected $subtype = '';
24
25
	public function __construct( IMAPMailbox $mailbox, $msgNumber, $unseen = null ) {
26
		$this->mailbox = $mailbox;
27
		$this->msgNumber = $msgNumber;
28
		$this->unseen = $unseen;
29
30
		if ( $unseen === null ) {
31
			$this->headerInfo = $this->mailbox()->imap()->headerinfo($this->msgNumber);
32
			$this->unseen = (bool) trim($this->headerInfo['unseen'] ?? '');
33
		}
34
	}
35
36
	/** @return bool|bool[] */
37
	protected function flags( $flags, $clear ) {
38
		$cb = [$this->mailbox()->imap(), $clear ? 'unflag' : 'flag'];
39
40
		$feedback = [];
41
		foreach ( (array) $flags AS $flag ) {
42
			$flag = '\\' . ucfirst($flag);
43
			$feedback[] = call_user_func($cb, $this->msgNumber, $flag);
44
		}
45
46
		return is_array($flags) ? $feedback : $feedback[0];
47
	}
48
49
	/** @return bool|bool[] */
50
	public function flag( $flags ) {
51
		return $this->flags($flags, false);
52
	}
53
54
	/** @return bool|bool[] */
55
	public function unflag( $flags ) {
56
		return $this->flags($flags, true);
57
	}
58
59
	/** @return int */
60
	public function utc() {
61
		return strtotime($this->header('date')[0] ?? '');
62
	}
63
64
	/** @return string */
65
	public function subject() {
66
		return $this->header('subject')[0] ?? '';
67
	}
68
69
	/** @return string */
70
	public function headerString() {
71
		return trim($this->mailbox()->imap()->fetchheader($this->msgNumber()));
72
	}
73
74
	/** @return IMAPMessagePart */
75
	public function createMessagePart( $structure, $section ) {
76
		return new IMAPMessagePart($this, $structure, $section);
77
	}
78
79
	/** @return IMAPMessagePart[] */
80
	public function parts() {
81
		if ( empty($this->parts) ) {
82
			$structure = $this->structure();
83
84
			// Possibilities:
85
			// - PLAIN
86
			// - ALTERNATIVE
87
			// - MIXED
88
			// - DELIVERY-STATUS
89
			// - RFC822
90
			// - REPORT
91
			// - HTML
92
			// - CALENDAR
93
			// - JPEG
94
95
			if ( empty($structure->parts) ) {
96
				$this->parts[] = $this->createMessagePart(
97
					$structure,
98
					[1]
99
				);
100
			}
101
			else {
102
				foreach ( $structure->parts as $n => $part ) {
103
					$this->parts[] = $this->createMessagePart(
104
						$part,
105
						[$n + 1]
106
					);
107
				}
108
			}
109
		}
110
111
		return $this->parts;
112
	}
113
114
	/** @return object */
115
	public function structure() {
116
		if ( empty($this->structure) ) {
117
			$this->structure = $this->mailbox()->imap()->fetchstructure($this->msgNumber);
118
		}
119
120
		return $this->structure;
121
	}
122
123
	/** @return string */
124
	public function subtype() {
125
		if ( empty($this->subtype) ) {
126
			$structure = $this->structure();
127
			$this->subtype = @$structure->subtype ?: '';
128
		}
129
130
		return $this->subtype;
131
	}
132
133
	/** @return int[] */
134
	public function section() {
135
		return [];
136
	}
137
138
	/** @return string */
139
	public function content() {
140
		if ( count($this->parts()) == 1 ) {
141
			return $this->part(0)->content();
142
		}
143
144
		return '';
145
	}
146
147
	/** @return string */
148
	public function decodedContent() {
149
		if ( count($this->parts()) == 1 ) {
150
			return $this->part(0)->decodedContent();
151
		}
152
153
		return '';
154
	}
155
156
	/** @return bool */
157
	public function delete() {
158
		return $this->mailbox()->imap()->delete($this->msgNumber);
159
	}
160
161
	/** @return string[] */
162
	public function simpleStructure() {
163
		$parts = [];
164
		foreach ( $this->allParts() as $part ) {
165
			$name = '';
166
167
			$name .= implode('.', $part->section()) . '. ';
168
			if ( $part->parts() ) {
169
				$name .= '*';
170
			}
171
			$name .= $part->subtype();
172
			if ( $part->parameter('disposition') ) {
173
				if ( $filename = $part->filename() ) {
174
					$name .= ' (' . $filename . ')';
175
				}
176
			}
177
			if ( $bytes = $part->parameter('bytes') ) {
178
				$name .= ' (' . number_format($bytes / 1024, 1) . 'kb)';
179
			}
180
181
			$parts[] = $name;
182
		}
183
184
		return $parts;
185
	}
186
187
	/** @return int */
188
	public function msgNumber() {
189
		return $this->msgNumber;
190
	}
191
192
	/** @return IMAPMailbox */
193
	public function mailbox() {
194
		return $this->mailbox;
195
	}
196
197
}
198