OSDN Git Service

Disconnect softly
[pukiwiki/pukiwiki.git] / mail.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: mail.php,v 1.5 2004/07/17 13:24:27 henoheno Exp $
6 //
7
8 // APOP/POP Before SMTP
9 function pop_before_smtp($pop_userid = '', $pop_passwd = '',
10         $pop_server = 'localhost', $pop_port = 110)
11 {
12         $pop_auth_use_apop = TRUE;      // Always try APOP, by default
13         $must_use_apop     = FALSE;     // Always try POP for APOP-disabled server
14         if (isset($GLOBALS['pop_auth_use_apop'])) {
15                 // Force APOP only, or POP only
16                 $pop_auth_use_apop = $must_use_apop = $GLOBALS['pop_auth_use_apop'];
17         }
18
19         // Compat: GLOBALS > function arguments
20         foreach(array('pop_userid', 'pop_passwd', 'pop_server', 'pop_port') as $global) {
21                 if(isset($GLOBALS[$global]) && $GLOBALS[$global] !== '')
22                         ${$global} = $GLOBALS[$global];
23         }
24
25         // Check
26         $die = '';
27         foreach(array('pop_userid', 'pop_server', 'pop_port') as $global)
28                 if(${$global} == '') $die .= "pop_before_smtp(): \$$global seems blank\n";
29         if ($die) return ($die);
30
31         // Connect
32         $errno = 0; $errstr = '';
33         $fp = @fsockopen($pop_server, $pop_port, $errno, $errstr, 30);
34         if (! $fp) return ("pop_before_smtp(): $errstr ($errno)");
35
36         // Greeting message from server, may include <challenge-string> of APOP
37         $message = fgets($fp, 1024); // 512byte max
38         if (! preg_match('/^\+OK /', $message)) {
39                 fclose($fp);
40                 return ("pop_before_smtp(): Greeting message seems invalid");
41         }
42
43         $challenge = array();
44         if ($pop_auth_use_apop &&
45            (preg_match('/<.*>/', $message, $challenge) || $must_use_apop)) {
46                 $method = 'APOP'; // APOP auth
47                 if (! isset($challenge[0])) {
48                         $response = md5(time()); // Someting worthless but variable
49                 } else {
50                         $response = md5($challenge[0] . $pop_passwd);
51                 }
52                 fputs($fp, 'APOP ' . $pop_userid . ' ' . $response . "\r\n");
53         } else {
54                 $method = 'POP'; // POP auth
55                 fputs($fp, 'USER ' . $pop_userid . "\r\n");
56                 $message = fgets($fp, 1024); // 512byte max
57                 if (! preg_match('/^\+OK /', $message)) {
58                         fclose($fp);
59                         return ("pop_before_smtp(): USER seems invalid");
60                 }
61                 fputs($fp, 'PASS ' . $pop_passwd . "\r\n");
62         }
63         $result = fgets($fp, 1024); // 512byte max, auth result
64
65         // Disconnect
66         fputs($fp, "QUIT\r\n");
67         $message = fgets($fp, 1024); // 512byte max, last "+OK"
68         fclose($fp);
69
70         if (! preg_match('/^\+OK /', $result)) {
71                 return ("pop_before_smtp(): $method authentication failed");
72         } else {
73                 return TRUE;    // Success
74         }
75 }
76 ?>