fetch.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. require_once( '../functions/html.php' );
  21. function Mail_Fetch_Status($msg) {
  22. echo html_tag( 'table',
  23. html_tag( 'tr',
  24. html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
  25. ),
  26. '', '', 'width="90%"' );
  27. flush();
  28. }
  29. displayPageHeader($color, 'None');
  30. $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
  31. if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  32. $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
  33. if ($mailfetch_server_number<1) $mailfetch_server_number=0;
  34. for ($i=0;$i<$mailfetch_server_number;$i++) {
  35. $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
  36. $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
  37. $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
  38. $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
  39. $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
  40. $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
  41. $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
  42. $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
  43. if( $mailfetch_cypher == 'on' ) {
  44. $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
  45. }
  46. }
  47. echo '<br><center>';
  48. echo html_tag( 'table',
  49. html_tag( 'tr',
  50. html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
  51. ) ,
  52. 'center', '', 'width="95%" cols="1"' );
  53. if (!isset( $server_to_fetch ) ) {
  54. echo '<font size=-5><br></font>' .
  55. "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">" .
  56. html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
  57. html_tag( 'tr' ) .
  58. html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', 'right' ) .
  59. html_tag( 'td', '', 'left' ) .
  60. '<select name="server_to_fetch" size="1">' .
  61. '<option value="all" selected>..' . _("All") . "...\n";
  62. for ($i=0;$i<$mailfetch_server_number;$i++) {
  63. echo "<option value=\"$i\">" .
  64. (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) .
  65. '</option>' . "\n";
  66. }
  67. echo '</select>' .
  68. '</td>' .
  69. '</tr>';
  70. //if password not set, ask for it
  71. for ($i=0;$i<$mailfetch_server_number;$i++) {
  72. if ($mailfetch_pass_[$i]=='') {
  73. echo html_tag( 'tr',
  74. html_tag( 'td', _("Password for") . ' <b>' .
  75. (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) .
  76. '</b>: &nbsp; &nbsp; ',
  77. 'right' ) .
  78. html_tag( 'td', '<input type="password" name="pass_' . $i , '">', 'left' )
  79. );
  80. }
  81. }
  82. echo html_tag( 'tr',
  83. html_tag( 'td', '&nbsp;' ) .
  84. html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
  85. );
  86. '</table></form>';
  87. exit();
  88. }
  89. if ( $server_to_fetch == 'all' ) {
  90. $i_start = 0;
  91. $i_stop = $mailfetch_server_number;
  92. } else {
  93. $i_start = $server_to_fetch;
  94. $i_stop = $i_start+1;
  95. }
  96. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
  97. $mailfetch_server=$mailfetch_server_[$i_loop];
  98. $mailfetch_user=$mailfetch_user_[$i_loop];
  99. if ($mailfetch_pass_[$i_loop]=="") {
  100. $tmp="pass_$i_loop";
  101. $mailfetch_pass=$$tmp;
  102. } else {
  103. $mailfetch_pass=$mailfetch_pass_[$i_loop];
  104. }
  105. $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  106. $mailfetch_login=$mailfetch_login_[$i_loop];
  107. $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  108. $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  109. $pop3 = new POP3($mailfetch_server, 60);
  110. echo '<br>' .
  111. html_tag( 'table',
  112. html_tag( 'tr',
  113. html_tag( 'td', '<b>' . _("Fetching from ") .
  114. (($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop]) .
  115. '</b>',
  116. 'center' ) ,
  117. '', $color[9] ) ,
  118. '', '', 'width="90%"' );
  119. flush();
  120. if (!$pop3->connect($mailfetch_server)) {
  121. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
  122. continue;
  123. }
  124. Mail_Fetch_Status(_("Opening IMAP server"));
  125. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
  126. Mail_Fetch_Status(_("Opening POP server"));
  127. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  128. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  129. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  130. continue;
  131. }
  132. // register_shutdown_function($pop3->quit());
  133. $msglist = $pop3->uidl();
  134. $i = 1;
  135. for ($j = 1; $j < sizeof($msglist); $j++) {
  136. if ($msglist["$j"] == $mailfetch_uidl) {
  137. $i = $j+1;
  138. break;
  139. }
  140. }
  141. if ($Count < $i) {
  142. Mail_Fetch_Status(_("Login OK: No new messages"));
  143. $pop3->quit();
  144. continue;
  145. }
  146. if ($Count == 0) {
  147. Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  148. $pop3->quit();
  149. continue;
  150. } else {
  151. $newmsgcount = $Count - $i + 1;
  152. Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
  153. }
  154. Mail_Fetch_Status(_("Fetching UIDL..."));
  155. // Faster to get them all at once
  156. $mailfetch_uidl = $pop3->uidl();
  157. if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
  158. Mail_Fetch_Status(_("Server does not support UIDL."));
  159. if ($mailfetch_lmos == 'on') {
  160. Mail_Fetch_Status(_("Leaving Mail on Server..."));
  161. } else {
  162. Mail_Fetch_Status(_("Deleting messages from server..."));
  163. }
  164. for (; $i <= $Count; $i++) {
  165. Mail_Fetch_Status(_("Fetching message ") . "$i" );
  166. set_time_limit(20); // 20 seconds per message max
  167. $Message = "";
  168. $MessArray = $pop3->get($i);
  169. while ( (!$MessArray) or (gettype($MessArray) != "array")) {
  170. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
  171. // re-connect pop3
  172. Mail_Fetch_Status(_("Server error...Disconnect"));
  173. $pop3->quit();
  174. Mail_Fetch_Status(_("Re-connect from dead connectoin"));
  175. if (!$pop3->connect($mailfetch_server)) {
  176. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
  177. Mail_Fetch_Status(_("Saving UIDL"));
  178. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  179. continue;
  180. }
  181. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  182. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  183. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  184. Mail_Fetch_Status(_("Saving UIDL"));
  185. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  186. continue;
  187. }
  188. Mail_Fetch_Status(_("Re-fetching message ") . "$i" );
  189. $MessArray = $pop3->get($i);
  190. } // end while
  191. while (list($lineNum, $line) = each ($MessArray)) {
  192. $Message .= $line;
  193. }
  194. if ($mailfetch_subfolder=="") {
  195. fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
  196. } else {
  197. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
  198. }
  199. $Line = fgets($imap_stream, 1024);
  200. if (substr($Line, 0, 1) == '+') {
  201. fputs($imap_stream, $Message);
  202. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  203. if ( $response <> 'OK' ) {
  204. Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
  205. Mail_Fetch_Status(_("Closing POP"));
  206. $pop3->quit();
  207. Mail_Fetch_Status(_("Logging out from IMAP"));
  208. sqimap_logout($imap_stream);
  209. Mail_Fetch_Status(_("Saving UIDL"));
  210. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  211. exit;
  212. } else {
  213. Mail_Fetch_Status(_("Message appended to mailbox"));
  214. }
  215. if ($mailfetch_lmos != 'on') {
  216. if( $pop3->delete($i) ) {
  217. Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
  218. } else {
  219. Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
  220. }
  221. }
  222. } else {
  223. echo "$Line";
  224. Mail_Fetch_Status(_("Error Appending Message!"));
  225. Mail_Fetch_Status(_("Closing POP"));
  226. $pop3->quit();
  227. Mail_Fetch_Status(_("Logging out from IMAP"));
  228. sqimap_logout($imap_stream);
  229. // not gurantee corect!
  230. Mail_Fetch_Status(_("Saving UIDL"));
  231. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  232. exit;
  233. }
  234. }
  235. Mail_Fetch_Status(_("Closing POP"));
  236. $pop3->quit();
  237. Mail_Fetch_Status(_("Logging out from IMAP"));
  238. sqimap_logout($imap_stream);
  239. if (is_array($mailfetch_uidl)) {
  240. Mail_Fetch_Status(_("Saving UIDL"));
  241. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  242. }
  243. Mail_Fetch_Status(_("Done"));
  244. }
  245. ?>
  246. </center>
  247. </body>
  248. </html>