Completed
Push — master ( 0a3238...3082e7 )
by Rudie
02:01
created

IMAPMessage::flag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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