Xataface Email Module  0.3.2
Email/Mailmerge Module for Xataface
 All Data Structures Files Functions Variables Pages
POP34.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('FUNC4')) require_once 'FUNC4.php';
24 
25 $_RESULT = array();
26 
27 class POP34 {
28 
29  var $CRLF = "\r\n";
30  var $PORT = 110;
31  var $TOUT = 30;
32  var $COUT = 5;
33  var $BLEN = 1024;
34 
35  function _ok($conn, &$resp, $debug) {
36  if (!is_resource($conn)) return FUNC4::trace($debug, 'invalid resource connection', 1);
37  else {
38  $_pop3 = new POP34;
39  $ret = true;
40  do {
41  if ($result = fgets($conn, $_pop3->BLEN)) {
42  $resp[] = $result;
43  if (substr($result, 0, 3) != '+OK') {
44  $ret = false;
45  break;
46  }
47  } else {
48  $resp[] = 'can not read';
49  $ret = false;
50  break;
51  }
52  } while ($result[3] == '-');
53  return $ret;
54  }
55  }
56 
57  function connect($host = null, $user = null, $pass = null, $port = null, $vssl = null, $tout = null, $context = null, $debug = null) {
58  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
59  global $_RESULT;
60  $_RESULT = array();
61  $_pop3 = new POP34;
62  if ($port == null) $port = $_pop3->PORT;
63  if ($tout == null) $tout = $_pop3->TOUT;
64  $err = array();
65  if (!is_string($host)) $err[] = 'invalid host type';
66  else {
67  if (!(trim($host) != '' && (FUNC4::is_ipv4($host) || FUNC4::is_hostname($host, true, $debug)))) $err[] = 'invalid host value';
68  }
69  if (!is_string($user)) $err[] = 'invalid username type';
70  else if (($user = FUNC4::str_clear($user)) == '') $err[] = 'invalid username value';
71  if (!is_string($pass)) $err[] = 'invalid password type';
72  else if (($pass = FUNC4::str_clear($pass)) == '') $err[] = 'invalid password value';
73  if (!(is_int($port) && $port > 0)) $err[] = 'invalid port value';
74  if ($vssl != null) {
75  if (!is_string($vssl)) $err[] = 'invalid ssl version type';
76  else {
77  $vssl = strtolower($vssl);
78  if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) $err[] = 'invalid ssl version value';
79  }
80  }
81  if (!(is_int($tout) && $tout > 0)) $err[] = 'invalid timeout value';
82  if ($context != null && !is_resource($context)) $err[] = 'invalid context type';
83  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
84  else {
85  $_pop3 = new POP34;
86  $ret = false;
87  $prt = ($vssl == null) ? 'tcp' : $vssl;
88  $conn = ($context == null) ? fsockopen($prt.'://'.$host, $port, $errno, $errstr, $tout) : fsockopen($prt.'://'.$host, $port, $errno, $errstr, $tout, $context);
89  if (!$conn) $_RESULT[401] = $errstr;
90  else if (!socket_set_timeout($conn, $_pop3->COUT)) $_RESULT[402] = 'could not set socket timeout';
91  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[403] = $resp;
92  else $ret = POP34::auth($conn, $user, $pass, $debug);
93  if (!$ret) {
94  if (is_resource($conn)) @fclose($conn);
95  $conn = false;
96  }
97  return $conn;
98  }
99  }
100 
101  function auth($conn = null, $user = null, $pass = null, $debug = null) {
102  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
103  global $_RESULT;
104  $_RESULT = array();
105  $err = array();
106  if (!is_resource($conn)) $err[] = 'invalid resource connection';
107  if (!is_string($user)) $err[] = 'invalid username type';
108  else if (($user = FUNC4::str_clear($user)) == '') $err[] = 'invalid username value';
109  if (!is_string($pass)) $err[] = 'invalid password type';
110  else if (($pass = FUNC4::str_clear($pass)) == '') $err[] = 'invalid password value';
111  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
112  else {
113  $_pop3 = new POP34;
114  $ret = false;
115  if (!fwrite($conn, 'USER '.$user.$_pop3->CRLF)) $_RESULT[404] = 'can not write';
116  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[405] = $resp;
117  else if (!fwrite($conn, 'PASS '.$pass.$_pop3->CRLF)) $_RESULT[405] = 'can not write';
118  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[406] = $resp;
119  else {
120  $_RESULT[407] = $resp;
121  $ret = true;
122  }
123  return $ret;
124  }
125  }
126 
127  function disconnect($conn = null, $debug = null) {
128  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
129  global $_RESULT;
130  $_RESULT = array();
131  if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection', 1);
132  else {
133  $_pop3 = new POP34;
134  if (!fwrite($conn, 'QUIT'.$_pop3->CRLF)) $_RESULT[437] = 'can not write';
135  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[438] = $resp;
136  else $_RESULT[439] = $resp;
137  return @fclose($conn);
138  }
139  }
140 
141  function pnoop($conn = null, $debug = null) {
142  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
143  global $_RESULT;
144  $_RESULT = array();
145  if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
146  else {
147  $_pop3 = new POP34;
148  $ret = false;
149  if (!fwrite($conn, 'NOOP'.$_pop3->CRLF)) $_RESULT[408] = 'can not write';
150  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[409] = $resp;
151  else {
152  $_RESULT[410] = $resp;
153  $ret = true;
154  }
155  return $ret;
156  }
157  }
158 
159  function prset($conn = null, $debug = null) {
160  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
161  global $_RESULT;
162  $_RESULT = array();
163  if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
164  else {
165  $_pop3 = new POP34;
166  $ret = false;
167  if (!fwrite($conn, 'RSET'.$_pop3->CRLF)) $_RESULT[411] = 'can not write';
168  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[412] = $resp;
169  else {
170  $_RESULT[413] = $resp;
171  $ret = true;
172  }
173  return $ret;
174  }
175  }
176 
177  function pquit($conn = null, $debug = null) {
178  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
179  global $_RESULT;
180  $_RESULT = array();
181  if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
182  else {
183  $_pop3 = new POP34;
184  $ret = false;
185  if (!fwrite($conn, 'QUIT'.$_pop3->CRLF)) $_RESULT[414] = 'can not write';
186  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[415] = $resp;
187  else {
188  $_RESULT[416] = $resp;
189  $ret = true;
190  }
191  return $ret;
192  }
193  }
194 
195  function pstat($conn = null, $debug = null) {
196  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
197  global $_RESULT;
198  $_RESULT = array();
199  if (!is_resource($conn)) FUNC4::trace($debug, 'invalid resource connection');
200  else {
201  $_pop3 = new POP34;
202  $ret = false;
203  if (!fwrite($conn, 'STAT'.$_pop3->CRLF)) $_RESULT[417] = 'can not write';
204  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[418] = $resp;
205  else {
206  if (count($exp = explode(' ', substr($resp[0], 4, -strlen($_pop3->CRLF)))) == 2) {
207  $val1 = intval($exp[0]);
208  $val2 = intval($exp[1]);
209  if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
210  $ret = array($val1 => $val2);
211  $_RESULT[421] = $resp;
212  } else $_RESULT[420] = $resp;
213  } else $_RESULT[419] = $resp;
214  }
215  return $ret;
216  }
217  }
218 
219  function pdele($conn = null, $msg = null, $debug = null) {
220  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
221  global $_RESULT;
222  $_RESULT = array();
223  $err = array();
224  if (!is_resource($conn)) $err[] = 'invalid resource connection';
225  if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
226  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
227  else {
228  $_pop3 = new POP34;
229  $ret = false;
230  if (!fwrite($conn, 'DELE '.$msg.$_pop3->CRLF)) $_RESULT[422] = 'can not write';
231  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[423] = $resp;
232  else {
233  $_RESULT[424] = $resp;
234  $ret = true;
235  }
236  return $ret;
237  }
238  }
239 
240  function pretr($conn = null, $msg = null, $debug = null) {
241  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
242  global $_RESULT;
243  $_RESULT = array();
244  $err = array();
245  if (!is_resource($conn)) $err[] = 'invalid resource connection';
246  if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
247  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
248  else {
249  $_pop3 = new POP34;
250  $ret = false;
251  if (!fwrite($conn, 'RETR '.$msg.$_pop3->CRLF)) $_RESULT[425] = 'can not write';
252  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[426] = $resp;
253  else {
254  $ret = '';
255  do {
256  if ($res = fgets($conn, $_pop3->BLEN)) $ret .= $res;
257  else {
258  $_RESULT[427] = 'can not read';
259  $ret = false;
260  break;
261  }
262  } while ($res != '.'.$_pop3->CRLF);
263  if ($ret) {
264  $ret = substr($ret, 0, -strlen($_pop3->CRLF.'.'.$_pop3->CRLF));
265  $_RESULT[428] = $resp;
266  }
267  }
268  return $ret;
269  }
270  }
271 
272  function plist($conn = null, $msg = null, $debug = null) {
273  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
274  global $_RESULT;
275  $_RESULT = array();
276  $err = array();
277  if (!is_resource($conn)) $err[] = 'invalid resource connection';
278  if ($msg == null) $msg = 0;
279  if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
280  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
281  else {
282  $_pop3 = new POP34;
283  $ret = false;
284  $num = ($msg > 0) ? true : false;
285  if (!fwrite($conn, 'LIST'.($num ? ' '.$msg : '').$_pop3->CRLF)) $_RESULT[429] = 'can not write';
286  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[430] = $resp;
287  else {
288  if ($num) {
289  if (count($exp = explode(' ', substr($resp[0], 4, -strlen($_pop3->CRLF)))) == 2) {
290  $val1 = intval($exp[0]);
291  $val2 = intval($exp[1]);
292  if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
293  $ret = array($val1 => $val2);
294  $_RESULT[433] = $resp;
295  } else $_RESULT[432] = $resp;
296  } else $_RESULT[431] = $resp;
297  } else {
298  do {
299  if ($res = fgets($conn, $_pop3->BLEN)) {
300  if (count($exp = explode(' ', substr($res, 0, -strlen($_pop3->CRLF)))) == 2) {
301  $val1 = intval($exp[0]);
302  $val2 = intval($exp[1]);
303  if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
304  $ret[$val1] = $val2;
305  $_RESULT[436] = $resp;
306  }
307  } else if ($res[0] != '.') {
308  $_RESULT[435] = $res;
309  $ret = false;
310  break;
311  }
312  } else {
313  $_RESULT[434] = 'can not read';
314  $ret = false;
315  break;
316  }
317  } while ($res[0] != '.');
318  }
319  }
320  return $ret;
321  }
322  }
323 
324  function puidl($conn = null, $msg = null, $debug = null) {
325  if (!FUNC4::is_debug($debug)) $debug = debug_backtrace();
326  global $_RESULT;
327  $_RESULT = array();
328  $err = array();
329  if (!is_resource($conn)) $err[] = 'invalid resource connection';
330  if ($msg == null) $msg = 0;
331  if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
332  if (count($err) > 0) FUNC4::trace($debug, implode(', ', $err));
333  else {
334  $_pop3 = new POP34;
335  $ret = false;
336  $num = ($msg > 0) ? true : false;
337  if (!fwrite($conn, 'UIDL'.($num ? ' '.$msg : '').$_pop3->CRLF)) $_RESULT[440] = 'can not write';
338  else if (!POP34::_ok($conn, $resp, $debug)) $_RESULT[441] = $resp;
339  else {
340  if ($num) {
341  if (count($exp = explode(' ', substr($resp[0], 4, -strlen($_pop3->CRLF)))) == 2) {
342  $val1 = intval($exp[0]);
343  $val2 = trim($exp[1]);
344  if (strval($val1) === $exp[0] && $val2 != '') {
345  $ret = array($val1 => $val2);
346  $_RESULT[444] = $resp;
347  } else $_RESULT[443] = $resp;
348  } else $_RESULT[442] = $resp;
349  } else {
350  do {
351  if ($res = fgets($conn, $_pop3->BLEN)) {
352  if (count($exp = explode(' ', substr($res, 0, -strlen($_pop3->CRLF)))) == 2) {
353  $val1 = intval($exp[0]);
354  $val2 = trim($exp[1]);
355  if (strval($val1) === $exp[0] && $val2 != '') {
356  $ret[$val1] = $val2;
357  $_RESULT[446] = $resp;
358  }
359  } else if ($res[0] != '.') {
360  $_RESULT[445] = $res;
361  $ret = false;
362  break;
363  }
364  } else {
365  $_RESULT[434] = 'can not read';
366  $ret = false;
367  break;
368  }
369  } while ($res[0] != '.');
370  }
371  }
372  return $ret;
373  }
374  }
375 
376 }
377 
378 ?>