fetch.php 12 KB

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