PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/xmail/PHP5/POP35.php

https://github.com/ECP-Black/ECP
PHP | 365 lines | 327 code | 19 blank | 19 comment | 209 complexity | 48a63408fff3d792f1f6cd8d3c0f66d5 MD5 | raw file
  1. <?php
  2. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  3. * *
  4. * XPertMailer is a PHP Mail Class that can send and read messages in MIME format. *
  5. * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
  6. * Copyright (C) 2007 Tanase Laurentiu Iulian *
  7. * *
  8. * This library is free software; you can redistribute it and/or modify it under the *
  9. * terms of the GNU Lesser General Public License as published by the Free Software *
  10. * Foundation; either version 2.1 of the License, or (at your option) any later version. *
  11. * *
  12. * This library is distributed in the hope that it will be useful, but WITHOUT ANY *
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
  14. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU Lesser General Public License along with *
  17. * this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
  18. * Fifth Floor, Boston, MA 02110-1301, USA *
  19. * *
  20. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. if (!class_exists('FUNC5')) require_once 'FUNC5.php';
  22. $_RESULT = array();
  23. class POP35 {
  24. const CRLF = "\r\n";
  25. const PORT = 110;
  26. const TOUT = 30;
  27. const COUT = 5;
  28. const BLEN = 1024;
  29. static private function _ok($conn, &$resp, $debug = null) {
  30. if (!is_resource($conn)) return FUNC5::trace($debug, 'invalid resource connection', 1);
  31. else {
  32. $ret = true;
  33. do {
  34. if ($result = fgets($conn, self::BLEN)) {
  35. $resp[] = $result;
  36. if (substr($result, 0, 3) != '+OK') {
  37. $ret = false;
  38. break;
  39. }
  40. } else {
  41. $resp[] = 'can not read';
  42. $ret = false;
  43. break;
  44. }
  45. } while ($result[3] == '-');
  46. return $ret;
  47. }
  48. }
  49. static public function connect($host = null, $user = null, $pass = null, $port = null, $vssl = null, $tout = null, $context = null, $debug = null) {
  50. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  51. global $_RESULT;
  52. $_RESULT = array();
  53. if ($port == null) $port = self::PORT;
  54. if ($tout == null) $tout = self::TOUT;
  55. $err = array();
  56. if (!is_string($host)) $err[] = 'invalid host type';
  57. else {
  58. if (!(trim($host) != '' && (FUNC5::is_ipv4($host) || FUNC5::is_hostname($host, true, $debug)))) $err[] = 'invalid host value';
  59. }
  60. if (!is_string($user)) $err[] = 'invalid username type';
  61. else if (($user = FUNC5::str_clear($user)) == '') $err[] = 'invalid username value';
  62. if (!is_string($pass)) $err[] = 'invalid password type';
  63. else if (($pass = FUNC5::str_clear($pass)) == '') $err[] = 'invalid password value';
  64. if (!(is_int($port) && $port > 0)) $err[] = 'invalid port value';
  65. if ($vssl != null) {
  66. if (!is_string($vssl)) $err[] = 'invalid ssl version type';
  67. else {
  68. $vssl = strtolower($vssl);
  69. if (!($vssl == 'tls' || $vssl == 'ssl' || $vssl == 'sslv2' || $vssl == 'sslv3')) $err[] = 'invalid ssl version value';
  70. }
  71. }
  72. if (!(is_int($tout) && $tout > 0)) $err[] = 'invalid timeout value';
  73. if ($context != null && !is_resource($context)) $err[] = 'invalid context type';
  74. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  75. else {
  76. $ret = false;
  77. $prt = ($vssl == null) ? 'tcp' : $vssl;
  78. $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);
  79. if (!$conn) $_RESULT[401] = $errstr;
  80. else if (!stream_set_timeout($conn, self::COUT)) $_RESULT[402] = 'could not set stream timeout';
  81. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[403] = $resp;
  82. else $ret = self::auth($conn, $user, $pass, $debug);
  83. if (!$ret) {
  84. if (is_resource($conn)) @fclose($conn);
  85. $conn = false;
  86. }
  87. return $conn;
  88. }
  89. }
  90. static public function auth($conn = null, $user = null, $pass = null, $debug = null) {
  91. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  92. global $_RESULT;
  93. $_RESULT = array();
  94. $err = array();
  95. if (!is_resource($conn)) $err[] = 'invalid resource connection';
  96. if (!is_string($user)) $err[] = 'invalid username type';
  97. else if (($user = FUNC5::str_clear($user)) == '') $err[] = 'invalid username value';
  98. if (!is_string($pass)) $err[] = 'invalid password type';
  99. else if (($pass = FUNC5::str_clear($pass)) == '') $err[] = 'invalid password value';
  100. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  101. else {
  102. $ret = false;
  103. if (!fwrite($conn, 'USER '.$user.self::CRLF)) $_RESULT[404] = 'can not write';
  104. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[405] = $resp;
  105. else if (!fwrite($conn, 'PASS '.$pass.self::CRLF)) $_RESULT[405] = 'can not write';
  106. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[406] = $resp;
  107. else {
  108. $_RESULT[407] = $resp;
  109. $ret = true;
  110. }
  111. return $ret;
  112. }
  113. }
  114. static public function disconnect($conn = null, $debug = null) {
  115. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  116. global $_RESULT;
  117. $_RESULT = array();
  118. if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection', 1);
  119. else {
  120. if (!fwrite($conn, 'QUIT'.self::CRLF)) $_RESULT[437] = 'can not write';
  121. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[438] = $resp;
  122. else $_RESULT[439] = $resp;
  123. return @fclose($conn);
  124. }
  125. }
  126. static public function pnoop($conn = null, $debug = null) {
  127. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  128. global $_RESULT;
  129. $_RESULT = array();
  130. if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
  131. else {
  132. $ret = false;
  133. if (!fwrite($conn, 'NOOP'.self::CRLF)) $_RESULT[408] = 'can not write';
  134. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[409] = $resp;
  135. else {
  136. $_RESULT[410] = $resp;
  137. $ret = true;
  138. }
  139. return $ret;
  140. }
  141. }
  142. static public function prset($conn = null, $debug = null) {
  143. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  144. global $_RESULT;
  145. $_RESULT = array();
  146. if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
  147. else {
  148. $ret = false;
  149. if (!fwrite($conn, 'RSET'.self::CRLF)) $_RESULT[411] = 'can not write';
  150. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[412] = $resp;
  151. else {
  152. $_RESULT[413] = $resp;
  153. $ret = true;
  154. }
  155. return $ret;
  156. }
  157. }
  158. static public function pquit($conn = null, $debug = null) {
  159. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  160. global $_RESULT;
  161. $_RESULT = array();
  162. if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
  163. else {
  164. $ret = false;
  165. if (!fwrite($conn, 'QUIT'.self::CRLF)) $_RESULT[414] = 'can not write';
  166. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[415] = $resp;
  167. else {
  168. $_RESULT[416] = $resp;
  169. $ret = true;
  170. }
  171. return $ret;
  172. }
  173. }
  174. static public function pstat($conn = null, $debug = null) {
  175. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  176. global $_RESULT;
  177. $_RESULT = array();
  178. if (!is_resource($conn)) FUNC5::trace($debug, 'invalid resource connection');
  179. else {
  180. $ret = false;
  181. if (!fwrite($conn, 'STAT'.self::CRLF)) $_RESULT[417] = 'can not write';
  182. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[418] = $resp;
  183. else {
  184. if (count($exp = explode(' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
  185. $val1 = intval($exp[0]);
  186. $val2 = intval($exp[1]);
  187. if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
  188. $ret = array($val1 => $val2);
  189. $_RESULT[421] = $resp;
  190. } else $_RESULT[420] = $resp;
  191. } else $_RESULT[419] = $resp;
  192. }
  193. return $ret;
  194. }
  195. }
  196. static public function pdele($conn = null, $msg = null, $debug = null) {
  197. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  198. global $_RESULT;
  199. $_RESULT = array();
  200. $err = array();
  201. if (!is_resource($conn)) $err[] = 'invalid resource connection';
  202. if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
  203. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  204. else {
  205. $ret = false;
  206. if (!fwrite($conn, 'DELE '.$msg.self::CRLF)) $_RESULT[422] = 'can not write';
  207. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[423] = $resp;
  208. else {
  209. $_RESULT[424] = $resp;
  210. $ret = true;
  211. }
  212. return $ret;
  213. }
  214. }
  215. static public function pretr($conn = null, $msg = null, $debug = null) {
  216. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  217. global $_RESULT;
  218. $_RESULT = array();
  219. $err = array();
  220. if (!is_resource($conn)) $err[] = 'invalid resource connection';
  221. if (!(is_int($msg) && $msg > 0)) $err[] = 'invalid message number';
  222. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  223. else {
  224. $ret = false;
  225. if (!fwrite($conn, 'RETR '.$msg.self::CRLF)) $_RESULT[425] = 'can not write';
  226. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[426] = $resp;
  227. else {
  228. $ret = '';
  229. do {
  230. if ($res = fgets($conn, self::BLEN)) $ret .= $res;
  231. else {
  232. $_RESULT[427] = 'can not read';
  233. $ret = false;
  234. break;
  235. }
  236. } while ($res != '.'.self::CRLF);
  237. if ($ret) {
  238. $ret = substr($ret, 0, -strlen(self::CRLF.'.'.self::CRLF));
  239. $_RESULT[428] = $resp;
  240. }
  241. }
  242. return $ret;
  243. }
  244. }
  245. static public function plist($conn = null, $msg = null, $debug = null) {
  246. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  247. global $_RESULT;
  248. $_RESULT = array();
  249. $err = array();
  250. if (!is_resource($conn)) $err[] = 'invalid resource connection';
  251. if ($msg == null) $msg = 0;
  252. if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
  253. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  254. else {
  255. $ret = false;
  256. $num = ($msg > 0) ? true : false;
  257. if (!fwrite($conn, 'LIST'.($num ? ' '.$msg : '').self::CRLF)) $_RESULT[429] = 'can not write';
  258. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[430] = $resp;
  259. else {
  260. if ($num) {
  261. if (count($exp = explode(' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
  262. $val1 = intval($exp[0]);
  263. $val2 = intval($exp[1]);
  264. if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
  265. $ret = array($val1 => $val2);
  266. $_RESULT[433] = $resp;
  267. } else $_RESULT[432] = $resp;
  268. } else $_RESULT[431] = $resp;
  269. } else {
  270. do {
  271. if ($res = fgets($conn, self::BLEN)) {
  272. if (count($exp = explode(' ', substr($res, 0, -strlen(self::CRLF)))) == 2) {
  273. $val1 = intval($exp[0]);
  274. $val2 = intval($exp[1]);
  275. if (strval($val1) === $exp[0] && strval($val2) === $exp[1]) {
  276. $ret[$val1] = $val2;
  277. $_RESULT[436] = $resp;
  278. }
  279. } else if ($res[0] != '.') {
  280. $_RESULT[435] = $res;
  281. $ret = false;
  282. break;
  283. }
  284. } else {
  285. $_RESULT[434] = 'can not read';
  286. $ret = false;
  287. break;
  288. }
  289. } while ($res[0] != '.');
  290. }
  291. }
  292. return $ret;
  293. }
  294. }
  295. static public function puidl($conn = null, $msg = null, $debug = null) {
  296. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  297. global $_RESULT;
  298. $_RESULT = array();
  299. $err = array();
  300. if (!is_resource($conn)) $err[] = 'invalid resource connection';
  301. if ($msg == null) $msg = 0;
  302. if (!(is_int($msg) && $msg >= 0)) $err[] = 'invalid message number';
  303. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  304. else {
  305. $ret = false;
  306. $num = ($msg > 0) ? true : false;
  307. if (!fwrite($conn, 'UIDL'.($num ? ' '.$msg : '').self::CRLF)) $_RESULT[440] = 'can not write';
  308. else if (!self::_ok($conn, $resp, $debug)) $_RESULT[441] = $resp;
  309. else {
  310. if ($num) {
  311. if (count($exp = explode(' ', substr($resp[0], 4, -strlen(self::CRLF)))) == 2) {
  312. $val1 = intval($exp[0]);
  313. $val2 = trim($exp[1]);
  314. if (strval($val1) === $exp[0] && $val2 != '') {
  315. $ret = array($val1 => $val2);
  316. $_RESULT[444] = $resp;
  317. } else $_RESULT[443] = $resp;
  318. } else $_RESULT[442] = $resp;
  319. } else {
  320. do {
  321. if ($res = fgets($conn, self::BLEN)) {
  322. if (count($exp = explode(' ', substr($res, 0, -strlen(self::CRLF)))) == 2) {
  323. $val1 = intval($exp[0]);
  324. $val2 = trim($exp[1]);
  325. if (strval($val1) === $exp[0] && $val2 != '') {
  326. $ret[$val1] = $val2;
  327. $_RESULT[446] = $resp;
  328. }
  329. } else if ($res[0] != '.') {
  330. $_RESULT[445] = $res;
  331. $ret = false;
  332. break;
  333. }
  334. } else {
  335. $_RESULT[434] = 'can not read';
  336. $ret = false;
  337. break;
  338. }
  339. } while ($res[0] != '.');
  340. }
  341. }
  342. return $ret;
  343. }
  344. }
  345. }
  346. ?>