2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: mail.php,v 1.1 2004/08/01 01:54:35 henoheno Exp $
8 // APOP/POP Before SMTP
9 function pop_before_smtp($pop_userid = '', $pop_passwd = '',
10 $pop_server = 'localhost', $pop_port = 110)
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'];
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];
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);
32 $errno = 0; $errstr = '';
33 $fp = @fsockopen($pop_server, $pop_port, $errno, $errstr, 30);
34 if (! $fp) return ("pop_before_smtp(): $errstr ($errno)");
36 // Greeting message from server, may include <challenge-string> of APOP
37 $message = fgets($fp, 1024); // 512byte max
38 if (! preg_match('/^\+OK/', $message)) {
40 return ("pop_before_smtp(): Greeting message seems invalid");
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
50 $response = md5($challenge[0] . $pop_passwd);
52 fputs($fp, 'APOP ' . $pop_userid . ' ' . $response . "\r\n");
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)) {
59 return ("pop_before_smtp(): USER seems invalid");
61 fputs($fp, 'PASS ' . $pop_passwd . "\r\n");
64 $result = fgets($fp, 1024); // 512byte max, auth result
65 $auth = preg_match('/^\+OK/', $result);
67 fputs($fp, "STAT\r\n"); // STAT, trigger SMTP relay!
68 $message = fgets($fp, 1024); // 512byte max
72 fputs($fp, "QUIT\r\n");
73 $message = fgets($fp, 1024); // 512byte max, last "+OK"
77 return ("pop_before_smtp(): $method authentication failed");
79 return TRUE; // Success