fetch.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. ** mail_fetch/fetch.php
  4. **
  5. ** Copyright (c) 1999-2001 The Squirrelmail Development 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. echo $mailfetch_user . ' ' . $mailfetch_pass . ' ';
  112. Mail_Fetch_Status(_("Opening POP server"));
  113. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  114. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  115. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  116. continue;
  117. }
  118. // register_shutdown_function($pop3->quit());
  119. $msglist = $pop3->uidl();
  120. $i = 1;
  121. for ($j = 1; $j < sizeof($msglist); $j++) {
  122. if ($msglist["$j"] == $mailfetch_uidl) {
  123. $i = $j+1;
  124. break;
  125. }
  126. }
  127. if ($Count < $i) {
  128. Mail_Fetch_Status(_("Login OK: No new messages"));
  129. $pop3->quit();
  130. continue;
  131. }
  132. if ($Count == 0) {
  133. Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  134. $pop3->quit();
  135. continue;
  136. } else {
  137. $newmsgcount = $Count - $i + 1;
  138. Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
  139. }
  140. Mail_Fetch_Status(_("Fetching UIDL..."));
  141. // Faster to get them all at once
  142. $mailfetch_uidl = $pop3->uidl();
  143. if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
  144. Mail_Fetch_Status(_("Server does not support UIDL."));
  145. if ($mailfetch_lmos == 'on') {
  146. Mail_Fetch_Status(_("Leaving Mail on Server..."));
  147. } else {
  148. Mail_Fetch_Status(_("Deleting messages from server..."));
  149. }
  150. for (; $i <= $Count; $i++) {
  151. Mail_Fetch_Status(_("Fetching message ") . "$i" );
  152. set_time_limit(20); // 20 seconds per message max
  153. $Message = "";
  154. $MessArray = $pop3->get($i);
  155. if ( (!$MessArray) or (gettype($MessArray) != "array")) {
  156. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
  157. continue 2;
  158. }
  159. while (list($lineNum, $line) = each ($MessArray)) {
  160. $Message .= $line;
  161. }
  162. if ($mailfetch_subfolder=="") {
  163. fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
  164. } else {
  165. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
  166. }
  167. $Line = fgets($imap_stream, 1024);
  168. if (substr($Line, 0, 1) == '+') {
  169. fputs($imap_stream, $Message);
  170. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  171. Mail_Fetch_Status(_("Message appended to mailbox"));
  172. if ($mailfetch_lmos != 'on') {
  173. if( $pop3->delete($i) ) {
  174. Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
  175. } else {
  176. Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
  177. }
  178. }
  179. } else {
  180. echo "$Line";
  181. Mail_Fetch_Status(_("Error Appending Message!"));
  182. }
  183. }
  184. Mail_Fetch_Status(_("Closing POP"));
  185. $pop3->quit();
  186. Mail_Fetch_Status(_("Logging out from IMAP"));
  187. sqimap_logout($imap_stream);
  188. if (is_array($mailfetch_uidl)) {
  189. Mail_Fetch_Status(_("Saving UIDL"));
  190. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  191. }
  192. Mail_Fetch_Status(_("Done"));
  193. }
  194. ?>
  195. </center>
  196. </body>
  197. </html>