PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/bk/app/webroot/smtp/pop3.php

https://gitlab.com/digaotinfo/agendaLegislativa
PHP | 272 lines | 239 code | 13 blank | 20 comment | 81 complexity | d461cb1e91c183e17a6afad625089289 MD5 | raw file
  1. <?php
  2. /***************************************************************************************
  3. * *
  4. * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
  5. * *
  6. * XPertMailer is free software; you can redistribute it and/or modify it under the *
  7. * terms of the GNU General Public License as published by the Free Software *
  8. * Foundation; either version 2 of the License, or (at your option) any later version. *
  9. * *
  10. * XPertMailer is distributed in the hope that it will be useful, but WITHOUT ANY *
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
  12. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License along with *
  15. * XPertMailer; if not, write to the Free Software Foundation, Inc., 51 Franklin St, *
  16. * Fifth Floor, Boston, MA 02110-1301 USA *
  17. * *
  18. * XPertMailer SMTP & POP3 PHP Mail Client. Can send and read messages in MIME Format. *
  19. * Copyright (C) 2006 Tanase Laurentiu Iulian *
  20. * *
  21. ***************************************************************************************/
  22. require_once 'func.php';
  23. class POP3 {
  24. function connect($host, $user, $pass, $port = 110, $ssl = false, $timeout = 30){
  25. $setver = true;
  26. if(is_string($host)){
  27. $host = FUNC::str_clear($host);
  28. $host = trim($host);
  29. if($host != ""){
  30. if(FUNC::is_ipv4($host)) $iphost = $host;
  31. else{
  32. $iphost = gethostbyname($host);
  33. if($iphost == $host){
  34. $setver = false;
  35. trigger_error('Invalid 3 hostname value "'.$host.'" (doesn\'t have an IPv4 address), on class POP3::connect()', 512);
  36. }
  37. }
  38. }else{
  39. $setver = false;
  40. trigger_error('Invalid 2 hostname/ip value, on class POP3::connect()', 512);
  41. }
  42. }else{
  43. $setver = false;
  44. trigger_error('Invalid 1 hostname/ip type value, on class POP3::connect()', 512);
  45. }
  46. if(is_string($user)){
  47. $user = FUNC::str_clear($user);
  48. $user = trim($user);
  49. if($user == ""){
  50. $setver = false;
  51. trigger_error('Invalid 2 username value, on class POP3::connect()', 512);
  52. }
  53. }else{
  54. $setver = false;
  55. trigger_error('Invalid 1 username type value, on class POP3::connect()', 512);
  56. }
  57. if(is_string($pass)){
  58. $pass = FUNC::str_clear($pass);
  59. $pass = trim($pass);
  60. if($pass == ""){
  61. $setver = false;
  62. trigger_error('Invalid 2 password value, on class POP3::connect()', 512);
  63. }
  64. }else{
  65. $setver = false;
  66. trigger_error('Invalid 1 password type value, on class POP3::connect()', 512);
  67. }
  68. if(!is_int($port)){
  69. $port = 110;
  70. trigger_error('Invalid port type value, on class POP3::connect()', 512);
  71. }
  72. if(is_string($ssl)){
  73. $ssl = FUNC::str_clear($ssl);
  74. $ssl = trim(strtolower($ssl));
  75. if(!($ssl == "tls" || $ssl == "ssl")){
  76. $ssl = false;
  77. trigger_error('Invalid TLS/SSL value, on class POP3::connect()', 512);
  78. }
  79. }else{
  80. if(is_bool($ssl)){
  81. $ssl = $ssl ? 'ssl' : false;
  82. }else{
  83. $ssl = false;
  84. trigger_error('Invalid TLS/SSL type value, on class POP3::connect()', 512);
  85. }
  86. }
  87. if(!is_int($timeout)){
  88. $timeout = 30;
  89. trigger_error('Invalid timeout type value, on class POP3::connect()', 512);
  90. }
  91. if($setver){
  92. $proto = $ssl ? $ssl.'://' : '';
  93. if(!$fp = fsockopen($proto.$iphost, $port, $err_num, $err_msg, $timeout)){
  94. $setver = false;
  95. trigger_error('Response 1 error "'.$err_msg.'", on class POP3::connect()', 512);
  96. }else{
  97. stream_set_timeout($fp, $timeout);
  98. $rcv = fgets($fp, 1024);
  99. if(substr($rcv, 0, 3) != '+OK'){
  100. fclose($fp);
  101. $setver = false;
  102. trigger_error('Response 2 error "'.$rcv.'", on class POP3::connect()', 512);
  103. }
  104. if($setver){
  105. fputs($fp, "USER ".$user."\r\n");
  106. $rcv = fgets($fp, 1024);
  107. if(substr($rcv, 0, 3) != '+OK'){
  108. fclose($fp);
  109. $setver = false;
  110. trigger_error('Response 3 error "'.$rcv.'", on class POP3::connect()', 512);
  111. }
  112. }
  113. if($setver){
  114. fputs($fp, "PASS ".$pass."\r\n");
  115. $rcv = fgets($fp, 1024);
  116. if(substr($rcv, 0, 3) != '+OK'){
  117. fclose($fp);
  118. $setver = false;
  119. trigger_error('Response 4 error "'.$rcv.'", on class POP3::connect()', 512);
  120. }
  121. }
  122. if($setver) $setver = $fp;
  123. }
  124. }
  125. return $setver;
  126. }
  127. function pstat($connection){
  128. $ret = false;
  129. if(FUNC::is_connection($connection)){
  130. fputs($connection, "STAT\r\n");
  131. $rcv = fgets($connection, 1024);
  132. if(substr($rcv, 0, 3) == '+OK'){
  133. $get = substr($rcv, 4, -1*strlen("\r\n"));
  134. $exp = explode(' ', $get);
  135. if(count($exp) == 2){
  136. $val1 = intval($exp[0]);
  137. $val2 = intval($exp[1]);
  138. if(strval($val1) === $exp[0] && strval($val2) === $exp[1]) $ret = array($val1, $val2);
  139. }else trigger_error('Response 2 error "'.$rcv.'", on class POP3::pstat()', 512);
  140. }else trigger_error('Response 1 error "'.$rcv.'", on class POP3::pstat()', 512);
  141. }else trigger_error('Invalid resource connection, on class POP3::pstat()', 512);
  142. return $ret;
  143. }
  144. function plist($connection, $msg = 0){
  145. $ret = $num = false;
  146. if($msg){
  147. if(is_int($msg)) $num = true;
  148. else{
  149. trigger_error('Invalid message number, on class POP3::plist()', 512);
  150. return false;
  151. }
  152. }
  153. if(FUNC::is_connection($connection)){
  154. fputs($connection, "LIST".($num ? " ".$msg : "")."\r\n");
  155. $rcv = fgets($connection, 1024);
  156. if(substr($rcv, 0, 3) != '+OK') trigger_error('Response error "'.$rcv.'", on class POP3::plist()', 512);
  157. else{
  158. $arr = array();
  159. if($num){
  160. $get = substr($rcv, 4, -1*strlen("\r\n"));
  161. $exp = explode(' ', $get);
  162. if(count($exp) == 2){
  163. $val1 = intval($exp[0]);
  164. $val2 = intval($exp[1]);
  165. if(strval($val1) === $exp[0] && strval($val2) === $exp[1]) $arr[$val1] = $val2;
  166. }
  167. }else{
  168. $list = "";
  169. while(!feof($connection)){
  170. $rcv = fgets($connection, 1024);
  171. $list .= $rcv;
  172. if(substr($rcv, 0, 1) == ".") break;
  173. }
  174. $data = substr($list, 0, -1*strlen("\r\n.\r\n"));
  175. if(!empty($data)){
  176. $exp1 = explode("\r\n", $data);
  177. foreach($exp1 as $line){
  178. $exp2 = explode(' ', $line);
  179. if(count($exp2) == 2){
  180. $val1 = intval($exp2[0]);
  181. $val2 = intval($exp2[1]);
  182. if(strval($val1) === $exp2[0] && strval($val2) === $exp2[1]) $arr[$val1] = $val2;
  183. }
  184. }
  185. }
  186. }
  187. if(count($arr) > 0) $ret = $arr;
  188. }
  189. }else trigger_error('Invalid resource connection, on class POP3::plist()', 512);
  190. return $ret;
  191. }
  192. function pretr($connection, $msg){
  193. $ret = false;
  194. if(!(is_int($msg) && $msg > 0)){
  195. trigger_error('Invalid message number, on class POP3::pretr()', 512);
  196. return false;
  197. }
  198. if(FUNC::is_connection($connection)){
  199. fputs($connection, "RETR ".$msg."\r\n");
  200. $rcv = fgets($connection, 1024);
  201. if(substr($rcv, 0, 3) != '+OK') trigger_error('Response error "'.$rcv.'", on class POP3::pretr()', 512);
  202. else{
  203. $ret = "";
  204. while(!feof($connection)){
  205. $line = fgets($connection, 1024);
  206. if($line == ".\r\n") break;
  207. $ret .= $line;
  208. }
  209. }
  210. }else trigger_error('Invalid resource connection, on class POP3::pretr()', 512);
  211. return $ret;
  212. }
  213. function pdele($connection, $msg){
  214. $ret = false;
  215. if(!(is_int($msg) && $msg > 0)){
  216. trigger_error('Invalid message number, on class POP3::pdele()', 512);
  217. return false;
  218. }
  219. if(FUNC::is_connection($connection)){
  220. fputs($connection, "DELE ".$msg."\r\n");
  221. $rcv = fgets($connection, 1024);
  222. if(substr($rcv, 0, 3) == '+OK') $ret = true;
  223. else trigger_error('Response error "'.$rcv.'", on class POP3::pdele()', 512);
  224. }else trigger_error('Invalid resource connection, on class POP3::pdele()', 512);
  225. return $ret;
  226. }
  227. function pnoop($connection){
  228. $ret = false;
  229. if(FUNC::is_connection($connection)){
  230. fputs($connection, "NOOP\r\n");
  231. $rcv = fgets($connection, 1024);
  232. if(substr($rcv, 0, 3) == '+OK') $ret = true;
  233. else trigger_error('Response error "'.$rcv.'", on class POP3::pnoop()', 512);
  234. }else trigger_error('Invalid resource connection, on class POP3::pnoop()', 512);
  235. return $ret;
  236. }
  237. function prset($connection){
  238. $ret = false;
  239. if(FUNC::is_connection($connection)){
  240. fputs($connection, "RSET\r\n");
  241. $rcv = fgets($connection, 1024);
  242. if(substr($rcv, 0, 3) == '+OK') $ret = true;
  243. else trigger_error('Response error "'.$rcv.'", on class POP3::prset()', 512);
  244. }else trigger_error('Invalid resource connection, on class POP3::prset()', 512);
  245. return $ret;
  246. }
  247. function pquit($connection){
  248. $ret = false;
  249. if(FUNC::is_connection($connection)){
  250. fputs($connection, "QUIT\r\n");
  251. $rcv = fgets($connection, 1024);
  252. if(substr($rcv, 0, 3) == '+OK') $ret = true;
  253. else trigger_error('Response error "'.$rcv.'", on class POP3::pquit()', 512);
  254. FUNC::close($connection);
  255. }else trigger_error('Invalid resource connection, on class POP3::pquit()', 512);
  256. return $ret;
  257. }
  258. }
  259. ?>