23 if (!class_exists(
'FUNC5')) require_once
'FUNC5.php';
35 static private function _ok(
$conn, &$resp, $debug = null) {
36 if (!is_resource(
$conn))
return FUNC5::trace($debug,
'invalid resource connection', 1);
40 if ($result = fgets(
$conn, self::BLEN)) {
42 if (substr($result, 0, 3) !=
'+OK') {
47 $resp[] =
'can not read';
51 }
while ($result[3] ==
'-');
56 static public function connect($host = null, $user = null, $pass = null, $port = null, $vssl = null, $tout = null, $context = null, $debug = null) {
63 if (!is_string($host)) $err[] =
'invalid host type';
67 if (!is_string($user)) $err[] =
'invalid username type';
68 else if (($user =
FUNC5::str_clear($user)) ==
'') $err[] =
'invalid username value';
69 if (!is_string($pass)) $err[] =
'invalid password type';
70 else if (($pass =
FUNC5::str_clear($pass)) ==
'') $err[] =
'invalid password value';
71 if (!(is_int($port) && $port > 0)) $err[] =
'invalid port value';
73 if (!is_string($vssl)) $err[] =
'invalid ssl version type';
75 $vssl = strtolower($vssl);
76 if (!($vssl ==
'tls' || $vssl ==
'ssl' || $vssl ==
'sslv2' || $vssl ==
'sslv3')) $err[] =
'invalid ssl version value';
79 if (!(is_int($tout) && $tout > 0)) $err[] =
'invalid timeout value';
80 if ($context != null && !is_resource($context)) $err[] =
'invalid context type';
81 if (count($err) > 0)
FUNC5::trace($debug, implode(
', ', $err));
84 $prt = ($vssl == null) ?
'tcp' : $vssl;
85 $conn = ($context == null) ? stream_socket_client($prt.
'://'.$host.
':'.$port, $errno, $errstr, $tout) : stream_socket_client($prt.
'://'.$host.
':'.$port, $errno, $errstr, $tout, STREAM_CLIENT_CONNECT, $context);
86 if (!
$conn) $_RESULT[401] = $errstr;
87 else if (!stream_set_timeout(
$conn, self::COUT)) $_RESULT[402] =
'could not set stream timeout';
88 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[403] = $resp;
98 static public function auth(
$conn = null, $user = null, $pass = null, $debug = null) {
103 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
104 if (!is_string($user)) $err[] =
'invalid username type';
105 else if (($user =
FUNC5::str_clear($user)) ==
'') $err[] =
'invalid username value';
106 if (!is_string($pass)) $err[] =
'invalid password type';
107 else if (($pass =
FUNC5::str_clear($pass)) ==
'') $err[] =
'invalid password value';
108 if (count($err) > 0)
FUNC5::trace($debug, implode(
', ', $err));
111 if (!fwrite(
$conn,
'USER '.$user.self::CRLF)) $_RESULT[404] =
'can not write';
112 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[405] = $resp;
113 else if (!fwrite(
$conn,
'PASS '.$pass.self::CRLF)) $_RESULT[405] =
'can not write';
114 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[406] = $resp;
116 $_RESULT[407] = $resp;
129 if (!fwrite(
$conn,
'QUIT'.self::CRLF)) $_RESULT[437] =
'can not write';
130 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[438] = $resp;
131 else $_RESULT[439] = $resp;
132 return @fclose(
$conn);
143 if (!fwrite(
$conn,
'NOOP'.self::CRLF)) $_RESULT[408] =
'can not write';
144 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[409] = $resp;
146 $_RESULT[410] = $resp;
160 if (!fwrite(
$conn,
'RSET'.self::CRLF)) $_RESULT[411] =
'can not write';
161 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[412] = $resp;
163 $_RESULT[413] = $resp;
177 if (!fwrite(
$conn,
'QUIT'.self::CRLF)) $_RESULT[414] =
'can not write';
178 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[415] = $resp;
180 $_RESULT[416] = $resp;
194 if (!fwrite(
$conn,
'STAT'.self::CRLF)) $_RESULT[417] =
'can not write';
195 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[418] = $resp;
197 if (count($exp = explode(
' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
198 $val1 = intval($exp[0]);
199 $val2 = intval($exp[1]);
200 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
201 $ret = array($val1 => $val2);
202 $_RESULT[421] = $resp;
203 }
else $_RESULT[420] = $resp;
204 }
else $_RESULT[419] = $resp;
210 static public function pdele(
$conn = null, $msg = null, $debug = null) {
215 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
216 if (!(is_int($msg) && $msg > 0)) $err[] =
'invalid message number';
217 if (count($err) > 0)
FUNC5::trace($debug, implode(
', ', $err));
220 if (!fwrite(
$conn,
'DELE '.$msg.self::CRLF)) $_RESULT[422] =
'can not write';
221 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[423] = $resp;
223 $_RESULT[424] = $resp;
230 static public function pretr(
$conn = null, $msg = null, $debug = null) {
235 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
236 if (!(is_int($msg) && $msg > 0)) $err[] =
'invalid message number';
237 if (count($err) > 0)
FUNC5::trace($debug, implode(
', ', $err));
240 if (!fwrite(
$conn,
'RETR '.$msg.self::CRLF)) $_RESULT[425] =
'can not write';
241 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[426] = $resp;
245 if ($res = fgets(
$conn, self::BLEN)) $ret .= $res;
247 $_RESULT[427] =
'can not read';
251 }
while ($res !=
'.'.self::CRLF);
253 $ret = substr($ret, 0, -strlen(self::CRLF.
'.'.self::CRLF));
254 $_RESULT[428] = $resp;
261 static public function plist(
$conn = null, $msg = null, $debug = null) {
266 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
267 if ($msg == null) $msg = 0;
268 if (!(is_int($msg) && $msg >= 0)) $err[] =
'invalid message number';
269 if (count($err) > 0)
FUNC5::trace($debug, implode(
', ', $err));
272 $num = ($msg > 0) ?
true :
false;
273 if (!fwrite(
$conn,
'LIST'.($num ?
' '.$msg :
'').self::CRLF)) $_RESULT[429] =
'can not write';
274 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[430] = $resp;
277 if (count($exp = explode(
' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
278 $val1 = intval($exp[0]);
279 $val2 = intval($exp[1]);
280 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
281 $ret = array($val1 => $val2);
282 $_RESULT[433] = $resp;
283 }
else $_RESULT[432] = $resp;
284 }
else $_RESULT[431] = $resp;
287 if ($res = fgets(
$conn, self::BLEN)) {
288 if (count($exp = explode(
' ', substr($res, 0, -strlen(self::CRLF)))) == 2) {
289 $val1 = intval($exp[0]);
290 $val2 = intval($exp[1]);
291 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
293 $_RESULT[436] = $resp;
295 }
else if ($res[0] !=
'.') {
296 $_RESULT[435] = $res;
301 $_RESULT[434] =
'can not read';
305 }
while ($res[0] !=
'.');
312 static public function puidl(
$conn = null, $msg = null, $debug = null) {
317 if (!is_resource(
$conn)) $err[] =
'invalid resource connection';
318 if ($msg == null) $msg = 0;
319 if (!(is_int($msg) && $msg >= 0)) $err[] =
'invalid message number';
320 if (count($err) > 0)
FUNC5::trace($debug, implode(
', ', $err));
323 $num = ($msg > 0) ?
true :
false;
324 if (!fwrite(
$conn,
'UIDL'.($num ?
' '.$msg :
'').self::CRLF)) $_RESULT[440] =
'can not write';
325 else if (!self::_ok(
$conn, $resp, $debug)) $_RESULT[441] = $resp;
328 if (count($exp = explode(
' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
329 $val1 = intval($exp[0]);
330 $val2 = trim($exp[1]);
331 if (strval($val1) === $exp[0] && $val2 !=
'') {
332 $ret = array($val1 => $val2);
333 $_RESULT[444] = $resp;
334 }
else $_RESULT[443] = $resp;
335 }
else $_RESULT[442] = $resp;
338 if ($res = fgets(
$conn, self::BLEN)) {
339 if (count($exp = explode(
' ', substr($res, 0, -strlen(self::CRLF)))) == 2) {
340 $val1 = intval($exp[0]);
341 $val2 = trim($exp[1]);
342 if (strval($val1) === $exp[0] && $val2 !=
'') {
344 $_RESULT[446] = $resp;
346 }
else if ($res[0] !=
'.') {
347 $_RESULT[445] = $res;
352 $_RESULT[434] =
'can not read';
356 }
while ($res[0] !=
'.');