Xataface Email Module  0.3.2
Email/Mailmerge Module for Xataface
 All Data Structures Files Functions Variables Pages
POP35.php
Go to the documentation of this file.
1 <?php
2 
3 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * *
5  * XPertMailer is a PHP Mail Class that can send and read messages in MIME format. *
6  * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
7  * Copyright (C) 2007 Tanase Laurentiu Iulian *
8  * *
9  * This library is free software; you can redistribute it and/or modify it under the *
10  * terms of the GNU Lesser General Public License as published by the Free Software *
11  * Foundation; either version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY *
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
15  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public License along with *
18  * this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
19  * Fifth Floor, Boston, MA 02110-1301, USA *
20  * *
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
23 if (!class_exists('FUNC5')) require_once 'FUNC5.php';
24 
25 $_RESULT = array();
26 
27 class POP35 {
28 
29  const CRLF = "\r\n";
30  const PORT = 110;
31  const TOUT = 30;
32  const COUT = 5;
33  const BLEN = 1024;
34 
35  static private function _ok($conn, &$resp, $debug = null) {
36  if (!is_resource($conn)) return FUNC5::trace($debug, 'invalid resource connection', 1);
37  else {
38  $ret = true;
39  do {
40  if ($result = fgets($conn, self::BLEN)) {
41  $resp[] = $result;
42  if (substr($result, 0, 3) != '+OK') {
43  $ret = false;
44  break;
45  }
46  } else {
47  $resp[] = 'can not read';
48  $ret = false;
49  break;
50  }
51  } while ($result[3] == '-');
52  return $ret;
53  }
54  }
55 
56  static public function connect($host = null, $user = null, $pass = null, $port = null, $vssl = null, $tout = null, $context = null, $debug = null) {
57  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
58  global $_RESULT;
59  $_RESULT = array();
60  if ($port == null) $port = self::PORT;
61  if ($tout == null) $tout = self::TOUT;
62  $err = array();
63  if (!is_string($host)) $err[] = 'invalid host type';
64  else {
65  if (!(trim($host) != '' && (FUNC5::is_ipv4($host) || FUNC5::is_hostname($host, true, $debug)))) $err[] = 'invalid host value';
66  }
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';
72  if ($vssl != null) {
73  if (!is_string($vssl)) $err[] = 'invalid ssl version type';
74  else {
75  $vssl = strtolower($vssl);
76  if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) $err[] = 'invalid ssl version value';
77  }
78  }
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));
82  else {
83  $ret = false;
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;
89  else $ret = self::auth($conn, $user, $pass, $debug);
90  if (!$ret) {
91  if (is_resource($conn)) @fclose($conn);
92  $conn = false;
93  }
94  return $conn;
95  }
96  }
97 
98  static public function auth($conn = null, $user = null, $pass = null, $debug = null) {
99  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
100  global $_RESULT;
101  $_RESULT = array();
102  $err = array();
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));
109  else {
110  $ret = false;
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;
115  else {
116  $_RESULT[407] = $resp;
117  $ret = true;
118  }
119  return $ret;
120  }
121  }
122 
123  static public function disconnect($conn = null, $debug = null) {
124  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
125  global $_RESULT;
126  $_RESULT = array();
127  if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection', 1);
128  else {
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);
133  }
134  }
135 
136  static public function pnoop($conn = null, $debug = null) {
137  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
138  global $_RESULT;
139  $_RESULT = array();
140  if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
141  else {
142  $ret = false;
143  if (!fwrite($conn, 'NOOP'.self::CRLF)) $_RESULT[408] = 'can not write';
144  else if (!self::_ok($conn, $resp, $debug)) $_RESULT[409] = $resp;
145  else {
146  $_RESULT[410] = $resp;
147  $ret = true;
148  }
149  return $ret;
150  }
151  }
152 
153  static public function prset($conn = null, $debug = null) {
154  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
155  global $_RESULT;
156  $_RESULT = array();
157  if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
158  else {
159  $ret = false;
160  if (!fwrite($conn, 'RSET'.self::CRLF)) $_RESULT[411] = 'can not write';
161  else if (!self::_ok($conn, $resp, $debug)) $_RESULT[412] = $resp;
162  else {
163  $_RESULT[413] = $resp;
164  $ret = true;
165  }
166  return $ret;
167  }
168  }
169 
170  static public function pquit($conn = null, $debug = null) {
171  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
172  global $_RESULT;
173  $_RESULT = array();
174  if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
175  else {
176  $ret = false;
177  if (!fwrite($conn, 'QUIT'.self::CRLF)) $_RESULT[414] = 'can not write';
178  else if (!self::_ok($conn, $resp, $debug)) $_RESULT[415] = $resp;
179  else {
180  $_RESULT[416] = $resp;
181  $ret = true;
182  }
183  return $ret;
184  }
185  }
186 
187  static public function pstat($conn = null, $debug = null) {
188  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
189  global $_RESULT;
190  $_RESULT = array();
191  if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
192  else {
193  $ret = false;
194  if (!fwrite($conn, 'STAT'.self::CRLF)) $_RESULT[417] = 'can not write';
195  else if (!self::_ok($conn, $resp, $debug)) $_RESULT[418] = $resp;
196  else {
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;
205  }
206  return $ret;
207  }
208  }
209 
210  static public function pdele($conn = null, $msg = null, $debug = null) {
211  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
212  global $_RESULT;
213  $_RESULT = array();
214  $err = array();
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));
218  else {
219  $ret = false;
220  if (!fwrite($conn, 'DELE '.$msg.self::CRLF)) $_RESULT[422] = 'can not write';
221  else if (!self::_ok($conn, $resp, $debug)) $_RESULT[423] = $resp;
222  else {
223  $_RESULT[424] = $resp;
224  $ret = true;
225  }
226  return $ret;
227  }
228  }
229 
230  static public function pretr($conn = null, $msg = null, $debug = null) {
231  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
232  global $_RESULT;
233  $_RESULT = array();
234  $err = array();
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));
238  else {
239  $ret = false;
240  if (!fwrite($conn, 'RETR '.$msg.self::CRLF)) $_RESULT[425] = 'can not write';
241  else if (!self::_ok($conn, $resp, $debug)) $_RESULT[426] = $resp;
242  else {
243  $ret = '';
244  do {
245  if ($res = fgets($conn, self::BLEN)) $ret .= $res;
246  else {
247  $_RESULT[427] = 'can not read';
248  $ret = false;
249  break;
250  }
251  } while ($res != '.'.self::CRLF);
252  if ($ret) {
253  $ret = substr($ret, 0, -strlen(self::CRLF.'.'.self::CRLF));
254  $_RESULT[428] = $resp;
255  }
256  }
257  return $ret;
258  }
259  }
260 
261  static public function plist($conn = null, $msg = null, $debug = null) {
262  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
263  global $_RESULT;
264  $_RESULT = array();
265  $err = array();
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));
270  else {
271  $ret = false;
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;
275  else {
276  if ($num) {
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;
285  } else {
286  do {
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]) {
292  $ret[$val1] = $val2;
293  $_RESULT[436] = $resp;
294  }
295  } else if ($res[0] != '.') {
296  $_RESULT[435] = $res;
297  $ret = false;
298  break;
299  }
300  } else {
301  $_RESULT[434] = 'can not read';
302  $ret = false;
303  break;
304  }
305  } while ($res[0] != '.');
306  }
307  }
308  return $ret;
309  }
310  }
311 
312  static public function puidl($conn = null, $msg = null, $debug = null) {
313  if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
314  global $_RESULT;
315  $_RESULT = array();
316  $err = array();
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));
321  else {
322  $ret = false;
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;
326  else {
327  if ($num) {
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;
336  } else {
337  do {
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 != '') {
343  $ret[$val1] = $val2;
344  $_RESULT[446] = $resp;
345  }
346  } else if ($res[0] != '.') {
347  $_RESULT[445] = $res;
348  $ret = false;
349  break;
350  }
351  } else {
352  $_RESULT[434] = 'can not read';
353  $ret = false;
354  break;
355  }
356  } while ($res[0] != '.');
357  }
358  }
359  return $ret;
360  }
361  }
362 
363 }
364 
365 ?>