PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/lib/db/read/pop3mail.class.php

https://github.com/usagi-project/mynets1
PHP | 155 lines | 103 code | 21 blank | 31 comment | 21 complexity | 14552cc9a59cc3271dd789b80772acab MD5 | raw file
  1. <?php
  2. /* ========================================================================
  3. *
  4. * @license This source file is subject to version 3.01 of the PHP license,
  5. * that is available at http://www.php.net/license/3_01.txt
  6. * If you did not receive a copy of the PHP license and are unable
  7. * to obtain it through the world-wide-web, please send a note to
  8. * license@php.net so we can mail you a copy immediately.
  9. *
  10. * @project OpenPNE UsagiProject 2006-2007
  11. * @author Kazuki <info@usagi-project.org>
  12. * @author KUNIHARU Tsujioka <kunitsuji@gmail.com>
  13. * @author Fesly Project <http://sourceforge.jp/projects/fesly>
  14. * @author Usagi Project <info@usagi-project.org>
  15. * @copyright 2006-2007 UsagiProject <author member ad http://usagi-project.org/member.html>
  16. * @chengelog [2008/08/02] Ver1.2.0Nighty package
  17. * ========================================================================
  18. */
  19. /**
  20. * キャッチオールで受信したメールを
  21. * POPで受信し、スクリプトへ渡すクラス
  22. *
  23. */
  24. require_once OPENPNE_WEBAPP_DIR . '/lib/OpenPNE/KtaiMail.php';
  25. require_once OPENPNE_WEBAPP_DIR . '/lib/mail/sns.php';
  26. define('POP_SERVER_PORT', 110);
  27. class pop3mail
  28. {
  29. var $sock = NULL;
  30. function getPopMail()
  31. {
  32. if (! POP_SERVER_DOMAIN || ! POP_SERVER_USER || ! POP_SERVER_PASS)
  33. {
  34. return FALSE;
  35. }
  36. $this->sock = fsockopen(POP_SERVER_DOMAIN, POP_SERVER_PORT, $err, $errno, 10);
  37. if (! $this->sock) {
  38. return FALSE;
  39. }
  40. $buf = fgets($this->sock, 512);
  41. if(substr($buf, 0, 3) != '+OK') {
  42. return FALSE;
  43. }
  44. $buf = $this->sendCommand("USER " . POP_SERVER_USER);
  45. $buf = $this->sendCommand("PASS " . POP_SERVER_PASS);
  46. $data = $this->sendCommand("STAT"); //件数とサイズ取得
  47. sscanf($data, '+OK %d %d', $num, $size);
  48. if ($num == "0") {
  49. //新着無し
  50. $buf = $this->sendCommand("QUIT");
  51. fclose($this->sock);
  52. return TRUE;
  53. }
  54. for($i=1; $i<=$num; $i++) {
  55. $line = $this->sendCommand("RETR $i"); //メッセージ取得
  56. $raw_mail = '';
  57. while ( ! preg_match("/^\.\r\n/", $line))
  58. {
  59. $line = fgets($this->sock, 512);
  60. $raw_mail .= $line;
  61. }
  62. $data = $this->sendCommand("DELE $i"); //メッセージ削除
  63. $this->m_process_mail($raw_mail);
  64. }
  65. $buf = $this->sendCommand("QUIT"); //受信完了
  66. fclose($this->sock);
  67. //var_dump($buf);
  68. return TRUE;
  69. }
  70. function sendCommand($cmd) {
  71. fputs($this->sock, $cmd . "\r\n");
  72. $buf = fgets($this->sock, 512);
  73. if(substr($buf, 0, 3) == '+OK')
  74. {
  75. return $buf;
  76. }
  77. return FALSE;
  78. }
  79. /**
  80. * メール処理
  81. */
  82. function m_process_mail($raw_mail)
  83. {
  84. $options['from_encoding'] = MAIL_FROM_ENCODING;
  85. $options['to_encoding'] = 'UTF-8';
  86. $options['img_tmp_dir'] = OPENPNE_VAR_DIR . '/tmp';
  87. $options['img_max_filesize'] = IMAGE_MAX_FILESIZE * 1024;
  88. $options['trim_doublebyte_space'] = OPENPNE_TRIM_DOUBLEBYTE_SPACE;
  89. $decoder =& new OpenPNE_KtaiMail($options);
  90. $decoder->decode($raw_mail);
  91. $from = $decoder->get_from();
  92. $to = $decoder->get_to();
  93. if (! db_common_is_mailaddress($from) || ! db_common_is_mailaddress($to))
  94. {
  95. m_debug_log('mail.php::m_process_mail() ERROR missin from_address to_address');
  96. return FALSE;
  97. }
  98. list($to_user, $to_host) = explode('@', $to, 2);
  99. // check prefix
  100. if (MAIL_ADDRESS_PREFIX) {
  101. if (strpos($to_user, MAIL_ADDRESS_PREFIX) !== 0) {
  102. m_debug_log('mail.php::m_process_mail() missing prefix '.$to_user);
  103. return FALSE;
  104. }
  105. $to_user = substr($to_user, strlen(MAIL_ADDRESS_PREFIX));
  106. }
  107. if ($to_host === MAIL_SERVER_DOMAIN) {
  108. $mail_sns =& new mail_sns($decoder);
  109. if (! $mail_sns->main()) {
  110. m_debug_log('mail.php::m_process_mail() ERROR code 1');
  111. return FALSE;
  112. }
  113. } else {
  114. m_debug_log('mail.php::m_process_mail() ERROR '.$to_host.' missing MAIL_SEVER_DOMAIN');
  115. return FALSE;
  116. }
  117. return TRUE;
  118. }
  119. }
  120. /**
  121. * デバッグ用ログ保存
  122. */
  123. if (!function_exists('m_debug_log'))
  124. {
  125. function m_debug_log($msg, $priority = PEAR_LOG_WARNING) {
  126. if (!MAIL_DEBUG_LOG) return;
  127. $log_path = OPENPNE_VAR_DIR . '/log/mail.log';
  128. require_once OPENPNE_LIB_DIR . '/include/Log.php';
  129. $file =& Log::singleton('file', $log_path, 'MAIL');
  130. mb_convert_encoding($msg, 'JIS', 'auto');
  131. $file->log($msg, $priority);
  132. }
  133. }
  134. ?>