1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\Imap; |
4
|
|
|
|
5
|
|
|
use Ddeboer\Imap\Exception\AuthenticationFailedException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An IMAP server |
9
|
|
|
*/ |
10
|
|
|
class Server |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string Internet domain name or bracketed IP address of server |
14
|
|
|
*/ |
15
|
|
|
private $hostname; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int TCP port number |
19
|
|
|
*/ |
20
|
|
|
private $port; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string Optional flags |
24
|
|
|
*/ |
25
|
|
|
private $flags; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $connection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $parameters; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
* |
40
|
|
|
* @param string $hostname Internet domain name or bracketed IP address |
41
|
|
|
* of server |
42
|
|
|
* @param int $port TCP port number |
43
|
|
|
* @param string $flags Optional flags |
44
|
|
|
* @param array $parameters Connection parameters |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
$hostname, |
48
|
|
|
$port = 993, |
49
|
|
|
$flags = '/imap/ssl/validate-cert', |
50
|
|
|
$parameters = array() |
51
|
|
|
) { |
52
|
|
|
if (!function_exists('imap_open')) { |
53
|
|
|
throw new \RuntimeException('IMAP extension must be enabled'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->hostname = $hostname; |
57
|
|
|
$this->port = $port; |
58
|
|
|
$this->flags = $flags ? '/' . ltrim($flags, '/') : ''; |
59
|
|
|
$this->parameters = $parameters; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Authenticate connection |
64
|
|
|
* |
65
|
|
|
* @param string $username Username |
66
|
|
|
* @param string $password Password |
67
|
|
|
* |
68
|
|
|
* @return Connection |
69
|
|
|
* @throws AuthenticationFailedException |
70
|
|
|
*/ |
71
|
|
|
public function authenticate($username, $password) |
72
|
|
|
{ |
73
|
|
|
// Wrap imap_open, which gives notices instead of exceptions |
74
|
|
|
set_error_handler( |
75
|
|
|
function ($nr, $message) use ($username) { |
76
|
|
|
throw new AuthenticationFailedException($username, $message); |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$resource = imap_open( |
81
|
|
|
$this->getServerString(), |
82
|
|
|
$username, |
83
|
|
|
$password, |
84
|
|
|
null, |
85
|
|
|
1, |
86
|
|
|
$this->parameters |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
if (false === $resource) { |
90
|
|
|
throw new AuthenticationFailedException($username); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
restore_error_handler(); |
94
|
|
|
|
95
|
|
|
$check = imap_check($resource); |
96
|
|
|
$mailbox = $check->Mailbox; |
97
|
|
|
$this->connection = substr($mailbox, 0, strpos($mailbox, '}')+1); |
98
|
|
|
|
99
|
|
|
// These are necessary to get rid of PHP throwing IMAP errors |
100
|
|
|
imap_errors(); |
101
|
|
|
imap_alerts(); |
102
|
|
|
|
103
|
|
|
return new Connection($resource, $this->connection); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Glues hostname, port and flags and returns result |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private function getServerString() |
112
|
|
|
{ |
113
|
|
|
return sprintf( |
114
|
|
|
'{%s:%s%s}', |
115
|
|
|
$this->hostname, |
116
|
|
|
$this->port, |
117
|
|
|
$this->flags |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|