fetch.php 11 KB

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