Xataface Email Module 0.3
Email/Mailmerge Module for Xataface
lib/XPM/PHP4/POP34.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('FUNC4')) require_once 'FUNC4.php';
00024 
00025 $_RESULT = array();
00026 
00027 class POP34 {
00028 
00029         var $CRLF = "\r\n";
00030         var $PORT = 110;
00031         var $TOUT = 30;
00032         var $COUT = 5;
00033         var $BLEN = 1024;
00034 
00035         function _ok($conn, &$resp, $debug) {
00036                 if (!is_resource($conn)) return FUNC4::trace($debug, 'invalid resource connection', 1);
00037                 else {
00038                         $_pop3 = new POP34;
00039                         $ret = true;
00040                         do {
00041                                 if ($result = fgets($conn, $_pop3->BLEN)) {
00042                                         $resp[] = $result;
00043                                         if (substr($result, 0, 3) != '+OK') {
00044                                                 $ret = false;
00045                                                 break;
00046                                         }
00047                                 } else {
00048                                         $resp[] = 'can not read';
00049                                         $ret = false;
00050                                         break;
00051                                 }
00052                         } while ($result[3] == '-');
00053                         return $ret;
00054                 }
00055         }
00056 
00057         function connect($host = null, $user = null, $pass = null, $port = null, $vssl = null, $tout = null, $context = null, $debug = null) {
00058                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00059                 global $_RESULT;
00060                 $_RESULT = array();
00061                 $_pop3 = new POP34;
00062                 if ($port == null) $port = $_pop3->PORT;
00063                 if ($tout == null) $tout = $_pop3->TOUT;
00064                 $err = array();
00065                 if (!is_string($host)) $err[] = 'invalid host type';
00066                 else {
00067                         if (!(trim($host) != '' && (FUNC4::is_ipv4($host) || FUNC4::is_hostname($host, true, $debug)))) $err[] = 'invalid host value';
00068                 }
00069                 if (!is_string($user)) $err[] = 'invalid username type';
00070                 else if (($user = FUNC4::str_clear($user)) == '') $err[] = 'invalid username value';
00071                 if (!is_string($pass)) $err[] = 'invalid password type';
00072                 else if (($pass = FUNC4::str_clear($pass)) == '') $err[] = 'invalid password value';
00073                 if (!(is_int($port) && $port > 0)) $err[] = 'invalid port value';
00074                 if ($vssl != null) {
00075                         if (!is_string($vssl)) $err[] = 'invalid ssl version type';
00076                         else {
00077                                 $vssl = strtolower($vssl);
00078                                 if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) $err[] = 'invalid ssl version value';
00079                         }
00080                 }
00081                 if (!(is_int($tout) && $tout > 0)) $err[] = 'invalid timeout value';
00082                 if ($context != null && !is_resource($context)) $err[] = 'invalid context type';
00083                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00084                 else {
00085                         $_pop3 = new POP34;
00086                         $ret = false;
00087                         $prt = ($vssl == null) ? 'tcp' : $vssl;
00088                         $conn = ($context == null) ? fsockopen($prt.'://'.$host, $port, $errno, $errstr, $tout) : fsockopen($prt.'://'.$host, $port, $errno, $errstr, $tout, $context);
00089                         if (!$conn) $_RESULT[401] = $errstr;
00090                         else if (!socket_set_timeout($conn, $_pop3->COUT)) $_RESULT[402] = 'could not set socket timeout';
00091                         else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[403] = $resp;
00092                         else $ret = POP34::auth($conn, $user, $pass, $debug);
00093                         if (!$ret) {
00094                                 if (is_resource($conn)) @fclose($conn);
00095                                 $conn = false;
00096                         }
00097                         return $conn;
00098                 }
00099         }
00100 
00101         function auth($conn = null, $user = null, $pass = null, $debug = null) {
00102                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00103                 global $_RESULT;
00104                 $_RESULT = array();
00105                 $err = array();
00106                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00107                 if (!is_string($user)) $err[] = 'invalid username type';
00108                 else if (($user = FUNC4::str_clear($user)) == '') $err[] = 'invalid username value';
00109                 if (!is_string($pass)) $err[] = 'invalid password type';
00110                 else if (($pass = FUNC4::str_clear($pass)) == '') $err[] = 'invalid password value';
00111                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00112                 else {
00113                         $_pop3 = new POP34;
00114                         $ret = false;
00115                         if (!fwrite($conn, 'USER '.$user.$_pop3->CRLF)) $_RESULT[404] = 'can not write';
00116                         else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[405] = $resp;
00117                         else if (!fwrite($conn, 'PASS '.$pass.$_pop3->CRLF)) $_RESULT[405] = 'can not write';
00118                         else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[406] = $resp;
00119                         else {
00120                                 $_RESULT[407] = $resp;
00121                                 $ret = true;
00122                         }
00123                         return $ret;
00124                 }
00125         }
00126 
00127         function disconnect($conn = null, $debug = null) {
00128                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00129                 global $_RESULT;
00130                 $_RESULT = array();
00131                 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection', 1);
00132                 else {
00133                         $_pop3 = new POP34;
00134                         if (!fwrite($conn, 'QUIT'.$_pop3->CRLF)) $_RESULT[437] = 'can not write';
00135                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[438] = $resp;
00136                         else $_RESULT[439] = $resp;
00137                         return @fclose($conn);
00138                 }
00139         }
00140 
00141         function pnoop($conn = null, $debug = null) {
00142                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00143                 global $_RESULT;
00144                 $_RESULT = array();
00145                 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
00146                 else {
00147                         $_pop3 = new POP34;
00148                         $ret = false;
00149                         if (!fwrite($conn, 'NOOP'.$_pop3->CRLF)) $_RESULT[408] = 'can not write';
00150                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[409] = $resp;
00151                         else {
00152                                 $_RESULT[410] = $resp;
00153                                 $ret = true;
00154                         }
00155                         return $ret;
00156                 }
00157         }
00158 
00159         function prset($conn = null, $debug = null) {
00160                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00161                 global $_RESULT;
00162                 $_RESULT = array();
00163                 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
00164                 else {
00165                         $_pop3 = new POP34;
00166                         $ret = false;
00167                         if (!fwrite($conn, 'RSET'.$_pop3->CRLF)) $_RESULT[411] = 'can not write';
00168                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[412] = $resp;
00169                         else {
00170                                 $_RESULT[413] = $resp;
00171                                 $ret = true;
00172                         }
00173                         return $ret;
00174                 }
00175         }
00176 
00177         function pquit($conn = null, $debug = null) {
00178                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00179                 global $_RESULT;
00180                 $_RESULT = array();
00181                 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
00182                 else {
00183                         $_pop3 = new POP34;
00184                         $ret = false;
00185                         if (!fwrite($conn, 'QUIT'.$_pop3->CRLF)) $_RESULT[414] = 'can not write';
00186                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[415] = $resp;
00187                         else {
00188                                 $_RESULT[416] = $resp;
00189                                 $ret = true;
00190                         }
00191                         return $ret;
00192                 }
00193         }
00194 
00195         function pstat($conn = null, $debug = null) {
00196                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00197                 global $_RESULT;
00198                 $_RESULT = array();
00199                 if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
00200                 else {
00201                         $_pop3 = new POP34;
00202                         $ret = false;
00203                         if (!fwrite($conn, 'STAT'.$_pop3->CRLF)) $_RESULT[417] = 'can not write';
00204                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[418] = $resp;
00205                         else {
00206                                 if (count($exp = explode(' ', substr($resp[0], 4, -strlen($_pop3->CRLF)))) == 2) {
00207                                         $val1 = intval($exp[0]);
00208                                         $val2 = intval($exp[1]);
00209                                         if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
00210                                                 $ret = array($val1 => $val2);
00211                                                 $_RESULT[421] = $resp;
00212                                         } else $_RESULT[420] = $resp;
00213                                 } else $_RESULT[419] = $resp;
00214                         }
00215                         return $ret;
00216                 }
00217         }
00218 
00219         function pdele($conn = null, $msg = null, $debug = null) {
00220                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00221                 global $_RESULT;
00222                 $_RESULT = array();
00223                 $err = array();
00224                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00225                 if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
00226                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00227                 else {
00228                         $_pop3 = new POP34;
00229                         $ret = false;
00230                         if (!fwrite($conn, 'DELE '.$msg.$_pop3->CRLF)) $_RESULT[422] = 'can not write';
00231                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[423] = $resp;
00232                         else {
00233                                 $_RESULT[424] = $resp;
00234                                 $ret = true;
00235                         }
00236                         return $ret;
00237                 }
00238         }
00239 
00240         function pretr($conn = null, $msg = null, $debug = null) {
00241                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00242                 global $_RESULT;
00243                 $_RESULT = array();
00244                 $err = array();
00245                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00246                 if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
00247                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00248                 else {
00249                         $_pop3 = new POP34;
00250                         $ret = false;
00251                         if (!fwrite($conn, 'RETR '.$msg.$_pop3->CRLF)) $_RESULT[425] = 'can not write';
00252                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[426] = $resp;
00253                         else {
00254                                 $ret = '';
00255                                 do {
00256                                         if ($res = fgets($conn, $_pop3->BLEN)) $ret .= $res;
00257                                         else {
00258                                                 $_RESULT[427] = 'can not read';
00259                                                 $ret = false;
00260                                                 break;
00261                                         }
00262                                 } while ($res != '.'.$_pop3->CRLF);
00263                                 if ($ret) {
00264                                         $ret = substr($ret, 0, -strlen($_pop3->CRLF.'.'.$_pop3->CRLF));
00265                                         $_RESULT[428] = $resp;
00266                                 }
00267                         }
00268                         return $ret;
00269                 }
00270         }
00271 
00272         function plist($conn = null, $msg = null, $debug = null) {
00273                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00274                 global $_RESULT;
00275                 $_RESULT = array();
00276                 $err = array();
00277                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00278                 if ($msg == null) $msg = 0;
00279                 if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
00280                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00281                 else {
00282                         $_pop3 = new POP34;
00283                         $ret = false;
00284                         $num = ($msg > 0) ? true : false;
00285                         if (!fwrite($conn, 'LIST'.($num ? ' '.$msg : '').$_pop3->CRLF)) $_RESULT[429] = 'can not write';
00286                         else if (!POP34::_ok($conn,  $resp, $debug)) $_RESULT[430] = $resp;
00287                         else {
00288                                 if ($num) {
00289                                         if (count($exp = explode(' ', substr($resp[0], 4, -strlen($_pop3->CRLF)))) == 2) {
00290                                                 $val1 = intval($exp[0]);
00291                                                 $val2 = intval($exp[1]);
00292                                                 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
00293                                                         $ret = array($val1 => $val2);
00294                                                         $_RESULT[433] = $resp;
00295                                                 } else $_RESULT[432] = $resp;
00296                                         } else $_RESULT[431] = $resp;
00297                                 } else {
00298                                         do {
00299                                                 if ($res = fgets($conn, $_pop3->BLEN)) {
00300                                                         if (count($exp = explode(' ', substr($res, 0, -strlen($_pop3->CRLF)))) == 2) {
00301                                                                 $val1 = intval($exp[0]);
00302                                                                 $val2 = intval($exp[1]);
00303                                                                 if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
00304                                                                         $ret[$val1] = $val2;
00305                                                                         $_RESULT[436] = $resp;
00306                                                                 }
00307                                                         } else if ($res[0] != '.') {
00308                                                                 $_RESULT[435] = $res;
00309                                                                 $ret = false;
00310                                                                 break;
00311                                                         }
00312                                                 } else {
00313                                                         $_RESULT[434] = 'can not read';
00314                                                         $ret = false;
00315                                                         break;
00316                                                 }
00317                                         } while ($res[0] != '.');
00318                                 }
00319                         }
00320                         return $ret;
00321                 }
00322         }
00323 
00324         function puidl($conn = null, $msg = null, $debug = null) {
00325                 if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
00326                 global $_RESULT;
00327                 $_RESULT = array();
00328                 $err = array();
00329                 if (!is_resource($conn)) $err[] = 'invalid resource connection';
00330                 if ($msg == null) $msg = 0;
00331                 if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
00332                 if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
00333                 else {
00334                         $_pop3 = new POP34;
00335                         $ret = false;
00336                         $num = ($msg > 0) ? true : false;
00337                         if (!fwrite($conn, 'UIDL'.($num ? ' '.$msg : '').$_pop3->CRLF)) $_RESULT[440] = 'can not write';
00338                         else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[441] = $resp;
00339                         else {
00340                                 if ($num) {
00341                                         if (count($exp = explode(' ', substr($resp[0], 4, -strlen($_pop3->CRLF)))) == 2) {
00342                                                 $val1 = intval($exp[0]);
00343                                                 $val2 = trim($exp[1]);
00344                                                 if (strval($val1) === $exp[0] && $val2 != '') {
00345                                                         $ret = array($val1 => $val2);
00346                                                         $_RESULT[444] = $resp;
00347                                                 } else $_RESULT[443] = $resp;
00348                                         } else $_RESULT[442] = $resp;
00349                                 } else {
00350                                         do {
00351                                                 if ($res = fgets($conn, $_pop3->BLEN)) {
00352                                                         if (count($exp = explode(' ', substr($res, 0, -strlen($_pop3->CRLF)))) == 2) {
00353                                                                 $val1 = intval($exp[0]);
00354                                                                 $val2 = trim($exp[1]);
00355                                                                 if (strval($val1) === $exp[0] && $val2 != '') {
00356                                                                         $ret[$val1] = $val2;
00357                                                                         $_RESULT[446] = $resp;
00358                                                                 }
00359                                                         } else if ($res[0] != '.') {
00360                                                                 $_RESULT[445] = $res;
00361                                                                 $ret = false;
00362                                                                 break;
00363                                                         }
00364                                                 } else {
00365                                                         $_RESULT[434] = 'can not read';
00366                                                         $ret = false;
00367                                                         break;
00368                                                 }
00369                                         } while ($res[0] != '.');
00370                                 }
00371                         }
00372                         return $ret;
00373                 }
00374         }
00375 
00376 }
00377 
00378 ?>
 All Data Structures Files Functions Variables Enumerations