1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\Imap; |
4
|
|
|
|
5
|
|
|
use Ddeboer\Imap\Exception\Exception; |
6
|
|
|
use Ddeboer\Imap\Exception\MailboxDoesNotExistException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A connection to an IMAP server that is authenticated for a user |
10
|
|
|
*/ |
11
|
|
|
class Connection |
12
|
|
|
{ |
13
|
|
|
private $server; |
14
|
|
|
private $resource; |
15
|
|
|
private $mailboxes; |
16
|
|
|
private $mailboxNames; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor |
20
|
|
|
* |
21
|
|
|
* @param resource $resource |
22
|
|
|
* @param string $server |
23
|
|
|
* |
24
|
|
|
* @throws \InvalidArgumentException |
25
|
|
|
*/ |
26
|
|
|
public function __construct($resource, $server) |
27
|
|
|
{ |
28
|
|
|
if (!is_resource($resource)) { |
29
|
|
|
throw new \InvalidArgumentException('$resource must be a resource'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->resource = $resource; |
33
|
|
|
$this->server = $server; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get a list of mailboxes (also known as folders) |
38
|
|
|
* |
39
|
|
|
* @return Mailbox[] |
40
|
|
|
*/ |
41
|
|
|
public function getMailboxes() |
42
|
|
|
{ |
43
|
|
|
if (null === $this->mailboxes) { |
44
|
|
|
foreach ($this->getMailboxNames() as $mailboxName) { |
|
|
|
|
45
|
|
|
$this->mailboxes[] = $this->getMailbox($mailboxName); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->mailboxes; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check that a mailbox with the given name exists |
54
|
|
|
* |
55
|
|
|
* @param string $name Mailbox name |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function hasMailbox($name) |
60
|
|
|
{ |
61
|
|
|
return in_array($name, $this->getMailboxNames()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a mailbox by its name |
66
|
|
|
* |
67
|
|
|
* @param string $name Mailbox name |
68
|
|
|
* |
69
|
|
|
* @return Mailbox |
70
|
|
|
* @throws MailboxDoesNotExistException If mailbox does not exist |
71
|
|
|
*/ |
72
|
|
|
public function getMailbox($name) |
73
|
|
|
{ |
74
|
|
|
if (!$this->hasMailbox($name)) { |
75
|
|
|
throw new MailboxDoesNotExistException($name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new Mailbox($this->server . imap_utf7_encode($name), $this); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Count number of messages not in any mailbox |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function count() |
87
|
|
|
{ |
88
|
|
|
return imap_num_msg($this->resource); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create mailbox |
93
|
|
|
* |
94
|
|
|
* @param $name |
95
|
|
|
* |
96
|
|
|
* @return Mailbox |
97
|
|
|
* @throws Exception |
98
|
|
|
*/ |
99
|
|
|
public function createMailbox($name) |
100
|
|
|
{ |
101
|
|
|
if (imap_createmailbox($this->resource, $this->server . $name)) { |
102
|
|
|
$this->mailboxNames = $this->mailboxes = null; |
103
|
|
|
|
104
|
|
|
return $this->getMailbox($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new Exception("Can not create '{$name}' mailbox at '{$this->server}'"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Close connection |
112
|
|
|
* |
113
|
|
|
* @param int $flag |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function close($flag = 0) |
118
|
|
|
{ |
119
|
|
|
return imap_close($this->resource, $flag); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function deleteMailbox(Mailbox $mailbox) |
123
|
|
|
{ |
124
|
|
|
if (false === imap_deletemailbox( |
125
|
|
|
$this->resource, |
126
|
|
|
$this->server . $mailbox->getName() |
127
|
|
|
)) { |
128
|
|
|
throw new Exception('Mailbox ' . $mailbox->getName() . ' could not be deleted'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->mailboxes = $this->mailboxNames = null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get IMAP resource |
136
|
|
|
* |
137
|
|
|
* @return resource |
138
|
|
|
*/ |
139
|
|
|
public function getResource() |
140
|
|
|
{ |
141
|
|
|
return $this->resource; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get mailbox names |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
private function getMailboxNames() |
150
|
|
|
{ |
151
|
|
|
if (null === $this->mailboxNames) { |
152
|
|
|
$mailboxes = imap_getmailboxes($this->resource, $this->server, '*'); |
153
|
|
|
foreach ($mailboxes as $mailbox) { |
154
|
|
|
$this->mailboxNames[] = imap_utf7_decode(str_replace($this->server, '', $mailbox->name)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->mailboxNames; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.