fetch.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. while ( (!$MessArray) or (gettype($MessArray) != "array")) {
  155. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
  156. // re-connect pop3
  157. Mail_Fetch_Status(_("Server error...Disconnect"));
  158. $pop3->quit();
  159. Mail_Fetch_Status(_("Re-connect from dead connectoin"));
  160. if (!$pop3->connect($mailfetch_server)) {
  161. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
  162. Mail_Fetch_Status(_("Saving UIDL"));
  163. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  164. continue;
  165. }
  166. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  167. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  168. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  169. Mail_Fetch_Status(_("Saving UIDL"));
  170. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  171. continue;
  172. }
  173. Mail_Fetch_Status(_("Re-fetching message ") . "$i" );
  174. $MessArray = $pop3->get($i);
  175. } // end while
  176. while (list($lineNum, $line) = each ($MessArray)) {
  177. $Message .= $line;
  178. }
  179. if ($mailfetch_subfolder=="") {
  180. fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
  181. } else {
  182. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
  183. }
  184. $Line = fgets($imap_stream, 1024);
  185. if (substr($Line, 0, 1) == '+') {
  186. fputs($imap_stream, $Message);
  187. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  188. if ( $response <> 'OK' ) {
  189. Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
  190. Mail_Fetch_Status(_("Closing POP"));
  191. $pop3->quit();
  192. Mail_Fetch_Status(_("Logging out from IMAP"));
  193. sqimap_logout($imap_stream);
  194. Mail_Fetch_Status(_("Saving UIDL"));
  195. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  196. exit;
  197. } else {
  198. Mail_Fetch_Status(_("Message appended to mailbox"));
  199. }
  200. if ($mailfetch_lmos != 'on') {
  201. if( $pop3->delete($i) ) {
  202. Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
  203. } else {
  204. Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
  205. }
  206. }
  207. } else {
  208. echo "$Line";
  209. Mail_Fetch_Status(_("Error Appending Message!"));
  210. Mail_Fetch_Status(_("Closing POP"));
  211. $pop3->quit();
  212. Mail_Fetch_Status(_("Logging out from IMAP"));
  213. sqimap_logout($imap_stream);
  214. // not gurantee corect!
  215. Mail_Fetch_Status(_("Saving UIDL"));
  216. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  217. exit;
  218. }
  219. }
  220. Mail_Fetch_Status(_("Closing POP"));
  221. $pop3->quit();
  222. Mail_Fetch_Status(_("Logging out from IMAP"));
  223. sqimap_logout($imap_stream);
  224. if (is_array($mailfetch_uidl)) {
  225. Mail_Fetch_Status(_("Saving UIDL"));
  226. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  227. }
  228. Mail_Fetch_Status(_("Done"));
  229. }
  230. ?>
  231. </center>
  232. </body>
  233. </html>