OSDN Git Service

BugTrack2/9: Remove all #freeze convert plugin
[pukiwiki/pukiwiki.git] / lib / mail.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: mail.php,v 1.1 2004/08/01 01:54:35 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
64         $result = fgets($fp, 1024); // 512byte max, auth result
65         $auth   = preg_match('/^\+OK/', $result);
66         if ($auth) {
67                 fputs($fp, "STAT\r\n"); // STAT, trigger SMTP relay!
68                 $message = fgets($fp, 1024); // 512byte max
69         }
70
71         // Disconnect
72         fputs($fp, "QUIT\r\n");
73         $message = fgets($fp, 1024); // 512byte max, last "+OK"
74         fclose($fp);
75
76         if (! $auth) {
77                 return ("pop_before_smtp(): $method authentication failed");
78         } else {
79                 return TRUE;    // Success
80         }
81 }
82 ?>