Xataface Email Module 0.3
Email/Mailmerge Module for Xataface
lib/XPM/PHP5/POP35.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  *                                                                                         *
00005  *  XPertMailer is a PHP Mail Class that can send and read messages in MIME format.        *
00006  *  This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/)     *
00007  *  Copyright (C) 2007 Tanase Laurentiu Iulian                                             *
00008  *                                                                                         *
00009  *  This library is free software; you can redistribute it and/or modify it under the      *
00010  *  terms of the GNU Lesser General Public License as published by the Free Software       *
00011  *  Foundation; either version 2.1 of the License, or (at your option) any later version.  *
00012  *                                                                                         *
00013  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A        *
00015  *  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.        *
00016  *                                                                                         *
00017  *  You should have received a copy of the GNU Lesser General Public License along with    *
00018  *  this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  *  Fifth Floor, Boston, MA 02110-1301, USA                                                *
00020  *                                                                                         *
00021  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00022 
00023 if (!class_exists('FUNC5')) require_once 'FUNC5.php';
00024 
00025 $_RESULT = array();
00026 
00027 class POP35 {
00028 
00029         const CRLF = "\r\n";
00030         const PORT = 110;
00031         const TOUT = 30;
00032         const COUT = 5;
00033         const BLEN = 1024;
00034 
00035         static private function _ok($conn, &$resp, $debug = null) {
00036                 if (!is_resource($conn)) return FUNC5::trace($debug, 'invalid resource connection', 1);
00037                 else {
00038                         $ret = true;
00039                         do {
00040                                 if ($result = fgets($conn, self::BLEN)) {
00041                                         $resp[] = $result;
00042                                         if (substr($result, 0, 3) != '+OK') {
00043                                                 $ret = false;
00044                                                 break;
00045                                         }
00046                                 } else {
00047                                         $resp[] = 'can not read';
00048                                         $ret = false;
00049                                         break;
00050                                 }
00051                         } while ($result[3] == '-');
00052                         return $ret;
00053                 }
00054         }
00055 
00056         static public function connect($host = null, $user = null, $pass = null, $port = null, $vssl = null, $tout = null, $context = null, $debug = null) {
00057                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00058                 global $_RESULT;
00059                 $_RESULT = array();
00060                 if ($port == null) $port = self::PORT;
00061                 if ($tout == null) $tout = self::TOUT;
00062                 $err = array();
00063                 if (!is_string($host)) $err[] = 'invalid host type';
00064                 else {
00065                         if (!(trim($host) != '' && (FUNC5::is_ipv4($host) || FUNC5::is_hostname($host, true, $debug)))) $err[] = 'invalid host value';
00066                 }
00067                 if (!is_string($user)) $err[] = 'invalid username type';
00068                 else if (($user = FUNC5::str_clear($user)) == '') $err[] = 'invalid username value';
00069                 if (!is_string($pass)) $err[] = 'invalid password type';
00070                 else if (($pass = FUNC5::str_clear($pass)) == '') $err[] = 'invalid password value';
00071                 if (!(is_int($port) && $port > 0)) $err[] = 'invalid port value';
00072                 if ($vssl != null) {
00073                         if (!is_string($vssl)) $err[] = 'invalid ssl version type';
00074                         else {
00075                                 $vssl = strtolower($vssl);
00076                                 if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) $err[] = 'invalid ssl version value';
00077                         }
00078                 }
00079                 if (!(is_int($tout) && $tout > 0)) $err[] = 'invalid timeout value';
00080                 if ($context != null && !is_resource($context)) $err[] = 'invalid context type';
00081                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00082                 else {
00083                         $ret = false;
00084                         $prt = ($vssl == null) ? 'tcp' : $vssl;
00085                         $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);
00086                         if (!$conn) $_RESULT[401] = $errstr;
00087                         else if (!stream_set_timeout($conn, self::COUT)) $_RESULT[402] = 'could not set stream timeout';
00088                         else if (!self::_ok($conn, $resp, $debug)) $_RESULT[403] = $resp;
00089                         else $ret = self::auth($conn, $user, $pass, $debug);
00090                         if (!$ret) {
00091                                 if (is_resource($conn)) @fclose($conn);
00092                                 $conn = false;
00093                         }
00094                         return $conn;
00095                 }
00096         }
00097 
00098         static public function auth($conn = null, $user = null, $pass = null, $debug = null) {
00099                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00100                 global $_RESULT;
00101                 $_RESULT = array();
00102                 $err = array();
00103                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00104                 if (!is_string($user)) $err[] = 'invalid username type';
00105                 else if (($user = FUNC5::str_clear($user)) == '') $err[] = 'invalid username value';
00106                 if (!is_string($pass)) $err[] = 'invalid password type';
00107                 else if (($pass = FUNC5::str_clear($pass)) == '') $err[] = 'invalid password value';
00108                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00109                 else {
00110                         $ret = false;
00111                         if (!fwrite($conn, 'USER '.$user.self::CRLF)) $_RESULT[404] = 'can not write';
00112                         else if (!self::_ok($conn, $resp, $debug)) $_RESULT[405] = $resp;
00113                         else if (!fwrite($conn, 'PASS '.$pass.self::CRLF)) $_RESULT[405] = 'can not write';
00114                         else if (!self::_ok($conn, $resp, $debug)) $_RESULT[406] = $resp;
00115                         else {
00116                                 $_RESULT[407] = $resp;
00117                                 $ret = true;
00118                         }
00119                         return $ret;
00120                 }
00121         }
00122 
00123         static public function disconnect($conn = null, $debug = null) {
00124                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00125                 global $_RESULT;
00126                 $_RESULT = array();
00127                 if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection', 1);
00128                 else {
00129                         if (!fwrite($conn, 'QUIT'.self::CRLF)) $_RESULT[437] = 'can not write';
00130                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[438] = $resp;
00131                         else $_RESULT[439] = $resp;
00132                         return @fclose($conn);
00133                 }
00134         }
00135 
00136         static public function pnoop($conn = null, $debug = null) {
00137                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00138                 global $_RESULT;
00139                 $_RESULT = array();
00140                 if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
00141                 else {
00142                         $ret = false;
00143                         if (!fwrite($conn, 'NOOP'.self::CRLF)) $_RESULT[408] = 'can not write';
00144                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[409] = $resp;
00145                         else {
00146                                 $_RESULT[410] = $resp;
00147                                 $ret = true;
00148                         }
00149                         return $ret;
00150                 }
00151         }
00152 
00153         static public function prset($conn = null, $debug = null) {
00154                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00155                 global $_RESULT;
00156                 $_RESULT = array();
00157                 if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
00158                 else {
00159                         $ret = false;
00160                         if (!fwrite($conn, 'RSET'.self::CRLF)) $_RESULT[411] = 'can not write';
00161                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[412] = $resp;
00162                         else {
00163                                 $_RESULT[413] = $resp;
00164                                 $ret = true;
00165                         }
00166                         return $ret;
00167                 }
00168         }
00169 
00170         static public function pquit($conn = null, $debug = null) {
00171                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00172                 global $_RESULT;
00173                 $_RESULT = array();
00174                 if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
00175                 else {
00176                         $ret = false;
00177                         if (!fwrite($conn, 'QUIT'.self::CRLF)) $_RESULT[414] = 'can not write';
00178                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[415] = $resp;
00179                         else {
00180                                 $_RESULT[416] = $resp;
00181                                 $ret = true;
00182                         }
00183                         return $ret;
00184                 }
00185         }
00186 
00187         static public function pstat($conn = null, $debug = null) {
00188                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00189                 global $_RESULT;
00190                 $_RESULT = array();
00191                 if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
00192                 else {
00193                         $ret = false;
00194                         if (!fwrite($conn, 'STAT'.self::CRLF)) $_RESULT[417] = 'can not write';
00195                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[418] = $resp;
00196                         else {
00197                                 if (count($exp = explode(' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
00198                                         $val1 = intval($exp[0]);
00199                                         $val2 = intval($exp[1]);
00200                                         if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
00201                                                 $ret = array($val1 => $val2);
00202                                                 $_RESULT[421] = $resp;
00203                                         } else $_RESULT[420] = $resp;
00204                                 } else $_RESULT[419] = $resp;
00205                         }
00206                         return $ret;
00207                 }
00208         }
00209 
00210         static public function pdele($conn = null, $msg = null, $debug = null) {
00211                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00212                 global $_RESULT;
00213                 $_RESULT = array();
00214                 $err = array();
00215                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00216                 if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
00217                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00218                 else {
00219                         $ret = false;
00220                         if (!fwrite($conn, 'DELE '.$msg.self::CRLF)) $_RESULT[422] = 'can not write';
00221                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[423] = $resp;
00222                         else {
00223                                 $_RESULT[424] = $resp;
00224                                 $ret = true;
00225                         }
00226                         return $ret;
00227                 }
00228         }
00229 
00230         static public function pretr($conn = null, $msg = null, $debug = null) {
00231                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00232                 global $_RESULT;
00233                 $_RESULT = array();
00234                 $err = array();
00235                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00236                 if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
00237                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00238                 else {
00239                         $ret = false;
00240                         if (!fwrite($conn, 'RETR '.$msg.self::CRLF)) $_RESULT[425] = 'can not write';
00241                         else if (!self::_ok($conn,  $resp, $debug)) $_RESULT[426] = $resp;
00242                         else {
00243                                 $ret = '';
00244                                 do {
00245                                         if ($res = fgets($conn, self::BLEN)) $ret .= $res;
00246                                         else {
00247                                                 $_RESULT[427] = 'can not read';
00248                                                 $ret = false;
00249                                                 break;
00250                                         }
00251                                 } while ($res != '.'.self::CRLF);
00252                                 if ($ret) {
00253                                         $ret = substr($ret, 0, -strlen(self::CRLF.'.'.self::CRLF));
00254                                         $_RESULT[428] = $resp;
00255                                 }
00256                         }
00257                         return $ret;
00258                 }
00259         }
00260 
00261         static public function plist($conn = null, $msg = null, $debug = null) {
00262                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00263                 global $_RESULT;
00264                 $_RESULT = array();
00265                 $err = array();
00266                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00267                 if ($msg == null) $msg = 0;
00268                 if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
00269                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00270                 else {
00271                         $ret = false;
00272                         $num = ($msg > 0) ? true : false;
00273                         if (!fwrite($conn, 'LIST'.($num ? ' '.$msg : '').self::CRLF)) $_RESULT[429] = 'can not write';
00274                         else if (!self::_ok($conn, $resp, $debug)) $_RESULT[430] = $resp;
00275                         else {
00276                                 if ($num) {
00277                                         if (count($exp = explode(' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
00278                                                 $val1 = intval($exp[0]);
00279                                                 $val2 = intval($exp[1]);
00280                                                 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
00281                                                         $ret = array($val1 => $val2);
00282                                                         $_RESULT[433] = $resp;
00283                                                 } else $_RESULT[432] = $resp;
00284                                         } else $_RESULT[431] = $resp;
00285                                 } else {
00286                                         do {
00287                                                 if ($res = fgets($conn, self::BLEN)) {
00288                                                         if (count($exp = explode(' ', substr($res, 0, -strlen(self::CRLF)))) == 2) {
00289                                                                 $val1 = intval($exp[0]);
00290                                                                 $val2 = intval($exp[1]);
00291                                                                 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
00292                                                                         $ret[$val1] = $val2;
00293                                                                         $_RESULT[436] = $resp;
00294                                                                 }
00295                                                         } else if ($res[0] != '.') {
00296                                                                 $_RESULT[435] = $res;
00297                                                                 $ret = false;
00298                                                                 break;
00299                                                         }
00300                                                 } else {
00301                                                         $_RESULT[434] = 'can not read';
00302                                                         $ret = false;
00303                                                         break;
00304                                                 }
00305                                         } while ($res[0] != '.');
00306                                 }
00307                         }
00308                         return $ret;
00309                 }
00310         }
00311 
00312         static public function puidl($conn = null, $msg = null, $debug = null) {
00313                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00314                 global $_RESULT;
00315                 $_RESULT = array();
00316                 $err = array();
00317                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00318                 if ($msg == null) $msg = 0;
00319                 if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
00320                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00321                 else {
00322                         $ret = false;
00323                         $num = ($msg > 0) ? true : false;
00324                         if (!fwrite($conn, 'UIDL'.($num ? ' '.$msg : '').self::CRLF)) $_RESULT[440] = 'can not write';
00325                         else if (!self::_ok($conn, $resp, $debug)) $_RESULT[441] = $resp;
00326                         else {
00327                                 if ($num) {
00328                                         if (count($exp = explode(' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
00329                                                 $val1 = intval($exp[0]);
00330                                                 $val2 = trim($exp[1]);
00331                                                 if (strval($val1) === $exp[0] && $val2 != '') {
00332                                                         $ret = array($val1 => $val2);
00333                                                         $_RESULT[444] = $resp;
00334                                                 } else $_RESULT[443] = $resp;
00335                                         } else $_RESULT[442] = $resp;
00336                                 } else {
00337                                         do {
00338                                                 if ($res = fgets($conn, self::BLEN)) {
00339                                                         if (count($exp = explode(' ', substr($res, 0, -strlen(self::CRLF)))) == 2) {
00340                                                                 $val1 = intval($exp[0]);
00341                                                                 $val2 = trim($exp[1]);
00342                                                                 if (strval($val1) === $exp[0] && $val2 != '') {
00343                                                                         $ret[$val1] = $val2;
00344                                                                         $_RESULT[446] = $resp;
00345                                                                 }
00346                                                         } else if ($res[0] != '.') {
00347                                                                 $_RESULT[445] = $res;
00348                                                                 $ret = false;
00349                                                                 break;
00350                                                         }
00351                                                 } else {
00352                                                         $_RESULT[434] = 'can not read';
00353                                                         $ret = false;
00354                                                         break;
00355                                                 }
00356                                         } while ($res[0] != '.');
00357                                 }
00358                         }
00359                         return $ret;
00360                 }
00361         }
00362 
00363 }
00364 
00365 ?>
 All Data Structures Files Functions Variables Enumerations