fetch.php 12 KB

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