fetch.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. ** mail_fetch/fetch.php
  4. **
  5. ** Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. ** Licensed under the GNU GPL. For full terms see the file COPYING.
  7. **
  8. ** Fetch code.
  9. **
  10. ** $Id$
  11. **/
  12. chdir('..');
  13. require_once('../src/validate.php');
  14. require_once('../functions/page_header.php');
  15. require_once('../functions/imap.php');
  16. require_once('../src/load_prefs.php');
  17. require_once('../plugins/mail_fetch/class.POP3.php');
  18. require_once('../functions/i18n.php');
  19. require_once( '../plugins/mail_fetch/functions.php' );
  20. function Mail_Fetch_Status($msg) {
  21. echo '<table width="90%"><tr><td>' .
  22. htmlspecialchars( $msg ) .
  23. '</td></tr></table>';
  24. flush();
  25. }
  26. displayPageHeader($color, 'None');
  27. $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
  28. if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  29. $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
  30. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  31. for ($i=0;$i<$mailfetch_server_number;$i++) {
  32. $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
  33. $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
  34. $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
  35. $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
  36. $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
  37. $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
  38. $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
  39. $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
  40. if( $mailfetch_cypher == 'on' ) {
  41. $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
  42. }
  43. }
  44. echo '<br><center>';
  45. echo '<TABLE WIDTH=95% COLS=1 ALIGN=CENTER>' .
  46. "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER><b>" . _("Remote POP server Fetching Mail") . '</b></TD></TR>' .
  47. '</TABLE>';
  48. if (!isset( $server_to_fetch ) ) {
  49. echo '<font size=-5><BR></font>' .
  50. "<form action=\"$PHP_SELF\" METHOD=POST TARGET=_self>" .
  51. '<TABLE WIDTH=70% COLS=2 ALIGN=CENTER>' .
  52. '<TR>' .
  53. '<TD ALIGN=RIGHT>' . _("Select Server:") . ' &nbsp; &nbsp; </TD>' .
  54. '<TD><SELECT NAME=server_to_fetch SIZE=1>' .
  55. '<OPTION VALUE="all" SELECTED>..' . _("All") . "...\n";
  56. for ($i=0;$i<$mailfetch_server_number;$i++) {
  57. echo "<OPTION VALUE=\"$i\">" .
  58. (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]);
  59. }
  60. echo '</SELECT>' .
  61. '</TD>' .
  62. '</TR>';
  63. //if password not set, ask for it
  64. for ($i=0;$i<$mailfetch_server_number;$i++) {
  65. if ($mailfetch_pass_[$i]=="") {
  66. echo "<tr>" .
  67. '<TD ALIGN=RIGHT>' . _("Password for") . ' <B>' . (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) . '</B>: &nbsp; &nbsp; </TD>' .
  68. "<TD><INPUT TYPE=PASSWORD NAME=pass_$i></TD>" .
  69. "</TR>";
  70. }
  71. }
  72. echo '<TR>' .
  73. '<TD>&nbsp;</TD>' .
  74. '<TD><input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '"></TD>'.
  75. '</TR>' .
  76. '</TABLE></form>';
  77. exit();
  78. }
  79. if ( $server_to_fetch == 'all' ) {
  80. $i_start = 0;
  81. $i_stop = $mailfetch_server_number;
  82. } else {
  83. $i_start = $server_to_fetch;
  84. $i_stop = $i_start+1;
  85. }
  86. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
  87. $mailfetch_server=$mailfetch_server_[$i_loop];
  88. $mailfetch_user=$mailfetch_user_[$i_loop];
  89. if ($mailfetch_pass_[$i_loop]=="") {
  90. $tmp="pass_$i_loop";
  91. $mailfetch_pass=$$tmp;
  92. } else {
  93. $mailfetch_pass=$mailfetch_pass_[$i_loop];
  94. }
  95. $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  96. $mailfetch_login=$mailfetch_login_[$i_loop];
  97. $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  98. $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  99. $pop3 = new POP3($mailfetch_server, 60);
  100. echo "<br><table width=\"90%\"><tr bgcolor=\"$color[9]\"><td><b>" .
  101. _("Fetching from ") .
  102. (($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop]) .
  103. "</b></td></tr></table>";
  104. flush();
  105. if (!$pop3->connect($mailfetch_server)) {
  106. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
  107. continue;
  108. }
  109. Mail_Fetch_Status(_("Opening IMAP server"));
  110. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
  111. Mail_Fetch_Status(_("Opening POP server"));
  112. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  113. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  114. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  115. continue;
  116. }
  117. // register_shutdown_function($pop3->quit());
  118. $msglist = $pop3->uidl();
  119. $i = 1;
  120. for ($j = 1; $j < sizeof($msglist); $j++) {
  121. if ($msglist["$j"] == $mailfetch_uidl) {
  122. $i = $j+1;
  123. break;
  124. }
  125. }
  126. if ($Count < $i) {
  127. Mail_Fetch_Status(_("Login OK: No new messages"));
  128. $pop3->quit();
  129. continue;
  130. }
  131. if ($Count == 0) {
  132. Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  133. $pop3->quit();
  134. continue;
  135. } else {
  136. $newmsgcount = $Count - $i + 1;
  137. Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
  138. }
  139. Mail_Fetch_Status(_("Fetching UIDL..."));
  140. // Faster to get them all at once
  141. $mailfetch_uidl = $pop3->uidl();
  142. if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
  143. Mail_Fetch_Status(_("Server does not support UIDL."));
  144. if ($mailfetch_lmos == 'on') {
  145. Mail_Fetch_Status(_("Leaving Mail on Server..."));
  146. } else {
  147. Mail_Fetch_Status(_("Deleting messages from server..."));
  148. }
  149. for (; $i <= $Count; $i++) {
  150. Mail_Fetch_Status(_("Fetching message ") . "$i" );
  151. set_time_limit(20); // 20 seconds per message max
  152. $Message = "";
  153. $MessArray = $pop3->get($i);
  154. if ( (!$MessArray) or (gettype($MessArray) != "array")) {
  155. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
  156. continue 2;
  157. }
  158. while (list($lineNum, $line) = each ($MessArray)) {
  159. $Message .= $line;
  160. }
  161. if ($mailfetch_subfolder=="") {
  162. fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
  163. } else {
  164. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
  165. }
  166. $Line = fgets($imap_stream, 1024);
  167. if (substr($Line, 0, 1) == '+') {
  168. fputs($imap_stream, $Message);
  169. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  170. Mail_Fetch_Status(_("Message appended to mailbox"));
  171. if ($mailfetch_lmos != 'on') {
  172. if( $pop3->delete($i) ) {
  173. Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
  174. } else {
  175. Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
  176. }
  177. }
  178. } else {
  179. echo "$Line";
  180. Mail_Fetch_Status(_("Error Appending Message!"));
  181. }
  182. }
  183. Mail_Fetch_Status(_("Closing POP"));
  184. $pop3->quit();
  185. Mail_Fetch_Status(_("Logging out from IMAP"));
  186. sqimap_logout($imap_stream);
  187. if (is_array($mailfetch_uidl)) {
  188. Mail_Fetch_Status(_("Saving UIDL"));
  189. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  190. }
  191. Mail_Fetch_Status(_("Done"));
  192. }
  193. ?>
  194. </center>
  195. </body>
  196. </html>