fetch.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. sqgetGlobalVar('username', $username, SQ_SESSION);
  22. sqgetGlobalVar('key', $key, SQ_COOKIE);
  23. sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
  24. sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
  25. /* end globals */
  26. function Mail_Fetch_Status($msg) {
  27. echo html_tag( 'table',
  28. html_tag( 'tr',
  29. html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
  30. ),
  31. '', '', 'width="90%"' );
  32. flush();
  33. }
  34. function Mail_Fetch_Servers() {
  35. global $data_dir, $username;
  36. $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
  37. if (!isset($mailfetch['server_number']) || ($mailfetch['server_number'] < 1)) {
  38. $mailfetch['server_number'] = 0;
  39. }
  40. $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
  41. for ($i = 0; $i < $mailfetch['server_number']; $i++) {
  42. $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
  43. $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i");
  44. $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i");
  45. $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i");
  46. $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i");
  47. if($mailfetch['cypher'] == 'on') {
  48. $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
  49. }
  50. if ($mailfetch[$i]['pass'] == '') {
  51. sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
  52. }
  53. $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i");
  54. $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i");
  55. $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i");
  56. $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
  57. if($mailfetch[$i]['alias'] == '') {
  58. $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
  59. }
  60. }
  61. return $mailfetch;
  62. }
  63. function Mail_Fetch_Select_Server($mailfetch) {
  64. global $PHP_SELF;
  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. htmlspecialchars($mailfetch[$i]['alias']) .
  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[$i]['pass'] == '') {
  84. echo html_tag( 'tr',
  85. html_tag( 'td', _("Password for") . ' <b>' .
  86. htmlspecialchars($mailfetch[$i]['alias']) .
  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. }
  99. $mailfetch = Mail_Fetch_Servers();
  100. displayPageHeader($color, 'None');
  101. echo '<br><center>';
  102. echo html_tag( 'table',
  103. html_tag( 'tr',
  104. html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
  105. ) ,
  106. 'center', '', 'width="95%" cols="1"' );
  107. /* there are no servers defined yet... */
  108. if($mailfetch['server_number'] == 0) {
  109. echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
  110. displayInternalLink('plugins/mail_fetch/options.php',
  111. _("Click here to go to the options page.") );
  112. echo '</body></html>';
  113. exit();
  114. }
  115. // get $server_to_fetch from globals, if not set display a choice to the user
  116. if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
  117. Mail_Fetch_Select_Server($mailfetch);
  118. exit();
  119. }
  120. if ( $server_to_fetch == 'all' ) {
  121. $i_start = 0;
  122. $i_stop = $mailfetch['server_number'];
  123. } else {
  124. $i_start = $server_to_fetch;
  125. $i_stop = $i_start+1;
  126. }
  127. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
  128. $mailfetch_server = $mailfetch[$i_loop]['server'];
  129. $mailfetch_port = $mailfetch[$i_loop]['port'];
  130. $mailfetch_user = $mailfetch[$i_loop]['user'];
  131. $mailfetch_pass = $mailfetch[$i_loop]['pass'];
  132. $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
  133. $mailfetch_login = $mailfetch[$i_loop]['login'];
  134. $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
  135. $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
  136. if($mailfetch_subfolder == '') {
  137. $mailfetch_subfolder == 'INBOX';
  138. }
  139. $pop3 = new POP3($mailfetch_server, 60);
  140. echo '<br>' .
  141. html_tag( 'table',
  142. html_tag( 'tr',
  143. html_tag( 'td', '<b>' . _("Fetching from ") .
  144. htmlspecialchars($mailfetch[$i_loop]['alias']) .
  145. '</b>',
  146. 'center' ) ,
  147. '', $color[9] ) ,
  148. '', '', 'width="90%"' );
  149. flush();
  150. if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
  151. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
  152. continue;
  153. }
  154. Mail_Fetch_Status(_("Opening IMAP server"));
  155. $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
  156. Mail_Fetch_Status(_("Opening POP server"));
  157. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  158. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  159. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  160. continue;
  161. }
  162. // register_shutdown_function($pop3->quit());
  163. $msglist = $pop3->uidl();
  164. $i = 1;
  165. for ($j = 1; $j < sizeof($msglist); $j++) {
  166. if ($msglist[$j] == $mailfetch_uidl) {
  167. $i = $j+1;
  168. break;
  169. }
  170. }
  171. if ($Count < $i) {
  172. Mail_Fetch_Status(_("Login OK: No new messages"));
  173. $pop3->quit();
  174. continue;
  175. }
  176. if ($Count == 0) {
  177. Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  178. $pop3->quit();
  179. continue;
  180. } else {
  181. $newmsgcount = $Count - $i + 1;
  182. Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
  183. }
  184. Mail_Fetch_Status(_("Fetching UIDL..."));
  185. // Faster to get them all at once
  186. $mailfetch_uidl = $pop3->uidl();
  187. if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
  188. Mail_Fetch_Status(_("Server does not support UIDL."));
  189. if ($mailfetch_lmos == 'on') {
  190. Mail_Fetch_Status(_("Leaving Mail on Server..."));
  191. } else {
  192. Mail_Fetch_Status(_("Deleting messages from server..."));
  193. }
  194. for (; $i <= $Count; $i++) {
  195. Mail_Fetch_Status(_("Fetching message ") . "$i" );
  196. set_time_limit(20); // 20 seconds per message max
  197. $Message = '';
  198. $MessArray = $pop3->get($i);
  199. while ( (!$MessArray) or (gettype($MessArray) != "array")) {
  200. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
  201. // re-connect pop3
  202. Mail_Fetch_Status(_("Server error...Disconnect"));
  203. $pop3->quit();
  204. Mail_Fetch_Status(_("Reconnect from dead connection"));
  205. if (!$pop3->connect($mailfetch_server)) {
  206. Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
  207. Mail_Fetch_Status(_("Saving UIDL"));
  208. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  209. continue;
  210. }
  211. $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
  212. if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
  213. Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
  214. Mail_Fetch_Status(_("Saving UIDL"));
  215. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  216. continue;
  217. }
  218. Mail_Fetch_Status(_("Refetching message ") . "$i" );
  219. $MessArray = $pop3->get($i);
  220. } // end while
  221. while (list($lineNum, $line) = each ($MessArray)) {
  222. $Message .= $line;
  223. }
  224. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
  225. $Line = fgets($imap_stream, 1024);
  226. if (substr($Line, 0, 1) == '+') {
  227. fputs($imap_stream, $Message);
  228. fputs($imap_stream, "\r\n");
  229. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  230. if ($response != 'OK') {
  231. Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
  232. Mail_Fetch_Status(_("Closing POP"));
  233. $pop3->quit();
  234. Mail_Fetch_Status(_("Logging out from IMAP"));
  235. sqimap_logout($imap_stream);
  236. Mail_Fetch_Status(_("Saving UIDL"));
  237. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  238. exit;
  239. } else {
  240. Mail_Fetch_Status(_("Message appended to mailbox"));
  241. }
  242. if ($mailfetch_lmos != 'on') {
  243. if( $pop3->delete($i) ) {
  244. Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
  245. } else {
  246. Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
  247. }
  248. }
  249. } else {
  250. echo $Line;
  251. Mail_Fetch_Status(_("Error Appending Message!"));
  252. Mail_Fetch_Status(_("Closing POP"));
  253. $pop3->quit();
  254. Mail_Fetch_Status(_("Logging out from IMAP"));
  255. sqimap_logout($imap_stream);
  256. // not gurantee corect!
  257. Mail_Fetch_Status(_("Saving UIDL"));
  258. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
  259. exit;
  260. }
  261. }
  262. Mail_Fetch_Status(_("Closing POP"));
  263. $pop3->quit();
  264. Mail_Fetch_Status(_("Logging out from IMAP"));
  265. sqimap_logout($imap_stream);
  266. if (is_array($mailfetch_uidl)) {
  267. Mail_Fetch_Status(_("Saving UIDL"));
  268. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
  269. }
  270. Mail_Fetch_Status(_("Done"));
  271. }
  272. ?>
  273. </center>
  274. </body>
  275. </html>