MIL001_m.inc
6 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/*
* pop3-class.php By TOMO (2002/07/06)
*
* [Version] : 1.1.0 (2002/07/06)
* [URL] : http://www.spencernetwork.org/
* [E-MAIL] : groove@spencernetwork.org
*
* pop3-class.php is free but without any warranty.
* use this script at your own risk.
*
*/
/* Interface
[ Constractor ]
pop3(string server, string user, string pass [, int port = 110])
[ Property ]
bool apop
bool debug
int time_out
string server
int port
string user
string pass
[ Method ]
bool open(void)
bool close(void)
array get_stat(void)
array get_list(int num = 0)
array get_uidl(int num = 0)
bool dele(int num)
string top(int num, int line)
string head(int num)
string retr(int num)
bool noop(void)
bool rset(void)
*/
class pop3
{
/* Public variables */
var $apop; // use APOP
var $debug; // print debug message
var $time_out; // fsockopen() time-out
var $server; // pop3 server
var $port; // pop3 port number
var $user; // login user
var $pass; // login password
/* Private variables */
var $_sock;
function pop3($server, $user, $pass, $port = 110)
{
$this->apop = FALSE;
$this->debug = FALSE;
$this->time_out = 30;
$this->server = $server;
$this->port = $port;
$this->user = $user;
$this->pass = $pass;
$this->_sock = FALSE;
}
/* Public functions */
function open()
{
if ($this->_sock) $this->close();
$this->_debug("Trying to ".$this->server.":".$this->port." ...\n");
$this->_sock = @fsockopen($this->server, $this->port, $errno, $errstr, $this->time_out);
if (!$this->_sock) {
$this->_debug("Error: Could not connect to ".$this->server."\n");
$this->_debug("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
if (!$this->_ok($resp)) {
$this->_debug("Error: Could not connect to ".$this->server."\n");
return FALSE;
}
if (ereg("<.+>", $resp, $match)) {
$time_stamp = $match[0];
} else {
$time_stamp = '';
}
$this->_debug("Connected to ".$this->server."\n");
if (!$this->_login($time_stamp)) {
$this->_debug("Error: Login failed\n");
return FALSE;
}
$this->_debug("Login succeeded\n");
return TRUE;
}
function get_stat()
{
if (!$this->_putcmd('STAT')) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
list($num, $octet) = explode(' ', $resp);
return array($num, $octet);
}
function get_list($num = 0)
{
return $this->_get_list('LIST', $num);
}
function dele($num)
{
if (!$this->_putcmd('DELE', $num)) {
return FALSE;
}
return $this->_ok($resp);
}
function get_uidl($num = 0)
{
return $this->_get_list('UIDL', $num);
}
function top($num, $line)
{
if (!$this->_putcmd('TOP', $num.' '.$line)) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
return $this->_getresp();
}
function head($num)
{
return $this->top($num, 0);
}
function retr($num)
{
if (!$this->_putcmd('RETR', $num)) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
return $this->_getresp();
}
function noop()
{
if (!$this->_putcmd('NOOP')) {
return FALSE;
}
return $this->_ok($resp);
}
function rset()
{
if (!$this->_putcmd('RSET')) {
return FALSE;
}
return $this->_ok($resp);
}
function close()
{
if (!$this->_sock) {
$this->_debug("Error: Socket is not opened\n");
return FALSE;
}
if (!$this->_putcmd('QUIT')) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
$ret = fclose($this->_sock);
if ($ret) {
$this->_debug("Disconnected from remote host\n");
return TRUE;
} else {
$this->_debug("Error: Could not disconnect from remote host\n");
return FALSE;
}
}
/* Private Functions */
function _login($time_stamp)
{
if ($this->apop) {
return $this->_login_apop($time_stamp);
} else {
return $this->_login_pop();
}
}
function _login_pop()
{
if (!$this->_putcmd('USER', $this->user)) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
if (!$this->_putcmd('PASS', $this->pass)) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
return TRUE;
}
function _login_apop($time_stamp)
{
$digest = md5($time_stamp.$this->pass);
if (!$this->_putcmd('APOP', $this->user." ".$digest)) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
return TRUE;
}
function _putcmd($cmd, $arg = '')
{
if (!$this->_sock) {
$this->_debug("Error: Socket is not opened\n");
return FALSE;
}
if ($arg != '') {
$cmd = $cmd.' '.$arg;
}
if (!fputs($this->_sock, $cmd."\r\n")) {
$this->_debug("Error: Could not put command $cmd\n");
return FALSE;
}
$this->_debug('> '.$cmd."\n");
return TRUE;
}
function _ok(&$resp)
{
if (!$this->_sock) {
$this->_debug("Error: Socket is not opened\n");
return FALSE;
}
$line = trim(fgets($this->_sock, 512));
$this->_debug('< ' . $line . "\n");
$tmp = split(' ', $line, 2);
if (isset($tmp[1])) {
$resp = $tmp[1];
} else {
$resp = '';
}
if (substr($line, 0, 1) != '+') {
return FALSE;
}
return TRUE;
}
function _getresp()
{
if (!$this->_sock) {
$this->_debug("Error: Socket is not opened\n");
return FALSE;
}
$response = '';
$line = '';
while ($line != ".\r\n") {
$line = ereg_replace("^\\.\\.", '.', $line);
$response .= $line;
$line = fgets($this->_sock, 512);
}
$this->_debug(str_replace("\r\n", "\n", $response));
return $response;
}
function _debug($message)
{
if ($this->debug) {
echo $message;
}
}
function _get_list($cmd, $num = 0)
{
if ($num < 1) {
$arg = '';
} else {
$arg = $num;
}
if (!$this->_putcmd($cmd, $arg)) {
return FALSE;
}
if (!$this->_ok($resp)) {
return FALSE;
}
if ($num < 1) {
$resp = trim($this->_getresp());
}
if ($resp == '') {
return array('');
}
$data = explode("\r\n", $resp );
reset($data);
while (list($key, $value) = each($data)) {
list($no, $uid) = explode(' ', $value);
$list[$no] = $uid;
}
return $list;
}
}
?>