fetch.php 13 KB

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