forked from mazaika/imap_driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap_driver.php
166 lines (128 loc) · 4.32 KB
/
imap_driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/** This class can do certain imap commands, including custom commands, search etc.
* Class imap_driver
*/
class imap_driver
{
private $command_counter = "00000001";
private $fp;
public $error;
public $last_response = array();
public $last_endline = "";
private $full_debug = false;
public function init($host, $port)
{
if (!($this->fp = fsockopen($host, $port, $errno, $errstr, 15))) {
$this->error = "Could not connect to host ($errno) $errstr";
return false;
}
if (!stream_set_timeout($this->fp, 15)) {
$this->error = "Could not set timeout";
return false;
}
$line = fgets($this->fp);
if ($this->full_debug) {
echo $line;
}
return true;
}
public function login($login, $pwd)
{
$this->command("LOGIN $login $pwd");
if (preg_match('~^OK~', $this->last_endline)) {
return true;
} else {
$this->error = join(', ', $this->last_response);
$this->close();
return false;
}
}
public function select_folder($folder)
{
$this->command("SELECT $folder");
if (preg_match('~^OK~', $this->last_endline)) {
return true;
} else {
$this->error = join(', ', $this->last_response);
$this->close();
return false;
}
}
public function get_uids_by_search($criteria)
{
$this->command("SEARCH $criteria");
if (preg_match('~^OK~', $this->last_endline) && is_array($this->last_response) && count($this->last_response) == 1) {
$splitted_response = explode(' ', $this->last_response[0]);
$uids = array();
foreach ($splitted_response as $item) {
if (preg_match('~^\d+$~', $item)) {
$uids[] = $item;
}
}
return $uids;
} else {
$this->error = join(', ', $this->last_response);
$this->close();
return false;
}
}
public function get_headers_from_uid($uid)
{
$this->command("FETCH $uid BODY.PEEK[HEADER]");
if (preg_match('~^OK~', $this->last_endline)) {
array_shift($this->last_response); // skip first line
$headers = array();
$prev_match = '';
foreach ($this->last_response as $item) {
if (preg_match('~^([a-z][a-z0-9-_]+):~is', $item, $match)) {
$header_name = strtolower($match[1]);
$prev_match = $header_name;
$headers[$header_name] = trim(substr($item, strlen($header_name) + 1));
} else {
$headers[$prev_match] .= " " . $item;
}
}
return $headers;
} else {
$this->error = join(', ', $this->last_response);
$this->close();
return false;
}
}
private function command($command)
{
$this->last_response = array();
$this->last_endline = "";
if ($this->full_debug) {
echo "$this->command_counter $command\r\n";
}
fwrite($this->fp, "$this->command_counter $command\r\n");
while ($line = fgets($this->fp)) {
$line = trim($line); // do not combine with the line above in while loop, because sometimes valid response maybe \n
if ($this->full_debug) {
echo "Line: [$line]\n";
}
$line_arr = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
if (count($line_arr) > 0) {
$code = array_shift($line_arr);
if (strtoupper($code) == $this->command_counter) {
$this->last_endline = join(' ', $line_arr);
break;
} else {
$this->last_response[] = $line;
}
} else {
$this->last_response[] = $line;
}
}
$this->increment_counter();
}
private function increment_counter()
{
$this->command_counter = sprintf('%08d', intval($this->command_counter) + 1);
}
public function close()
{
fclose($this->fp);
}
}