fetch.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * mail_fetch/fetch.php
  4. *
  5. * Fetch code.
  6. *
  7. * @copyright &copy; 1999-2007 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. /**
  14. * Include the SquirrelMail initialization file.
  15. */
  16. require('../../include/init.php');
  17. include_once(SM_PATH . 'functions/imap_general.php');
  18. include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
  19. /* globals */
  20. sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
  21. /* end globals */
  22. /**
  23. * @param string $msg message
  24. */
  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. /**
  34. * @return array
  35. */
  36. function Mail_Fetch_Servers() {
  37. global $data_dir, $username;
  38. $mailfetch = array();
  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. // Authentication type (added in 1.5.2)
  64. $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER);
  65. // Connection type (added in 1.5.2)
  66. $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN);
  67. }
  68. return $mailfetch;
  69. }
  70. /**
  71. * @param array $mailfetch
  72. */
  73. function Mail_Fetch_Select_Server($mailfetch) {
  74. global $PHP_SELF;
  75. echo '<font size="-5"><br /></font>' .
  76. '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
  77. html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
  78. html_tag( 'tr' ) .
  79. html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', 'right' ) .
  80. html_tag( 'td', '', 'left' ) .
  81. '<select name="server_to_fetch" size="1">' .
  82. '<option value="all" selected="selected">..' . _("All") . "...\n";
  83. for ($i = 0;$i < $mailfetch['server_number'];$i++) {
  84. echo "<option value=\"$i\">" .
  85. htmlspecialchars($mailfetch[$i]['alias']) .
  86. '</option>' . "\n";
  87. }
  88. echo '</select>' .
  89. '</td>' .
  90. '</tr>';
  91. //if password not set, ask for it
  92. for ($i = 0;$i < $mailfetch['server_number'];$i++) {
  93. if ($mailfetch[$i]['pass'] == '') {
  94. echo html_tag( 'tr',
  95. html_tag( 'td', _("Password for") . ' <b>' .
  96. htmlspecialchars($mailfetch[$i]['alias']) .
  97. '</b>: &nbsp; &nbsp; ',
  98. 'right' ) .
  99. html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
  100. );
  101. }
  102. }
  103. echo html_tag( 'tr',
  104. html_tag( 'td', '&nbsp;' ) .
  105. html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
  106. ) .
  107. '</table></form>';
  108. }
  109. $mailfetch = Mail_Fetch_Servers();
  110. displayPageHeader($color);
  111. echo '<br />';
  112. echo html_tag( 'table',
  113. html_tag( 'tr',
  114. html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
  115. ) ,
  116. 'center', '', 'width="95%" cols="1"' );
  117. /* there are no servers defined yet... */
  118. if($mailfetch['server_number'] == 0) {
  119. //FIXME: do not echo directly to browser -- use templates only
  120. echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
  121. echo makeInternalLink('plugins/mail_fetch/options.php',
  122. _("Click here to go to the options page.") );
  123. $oTemplate->display('footer.tpl');
  124. exit();
  125. }
  126. // get $server_to_fetch from globals, if not set display a choice to the user
  127. if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
  128. Mail_Fetch_Select_Server($mailfetch);
  129. $oTemplate->display('footer.tpl');
  130. exit();
  131. }
  132. if ( $server_to_fetch == 'all' ) {
  133. $i_start = 0;
  134. $i_stop = $mailfetch['server_number'];
  135. } else {
  136. $i_start = $server_to_fetch;
  137. $i_stop = $i_start+1;
  138. }
  139. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
  140. $mailfetch_server = $mailfetch[$i_loop]['server'];
  141. $mailfetch_port = $mailfetch[$i_loop]['port'];
  142. $mailfetch_user = $mailfetch[$i_loop]['user'];
  143. $mailfetch_pass = $mailfetch[$i_loop]['pass'];
  144. $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
  145. $mailfetch_login = $mailfetch[$i_loop]['login'];
  146. $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
  147. $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
  148. $mailfetch_auth = $mailfetch[$i_loop]['auth'];
  149. $mailfetch_type = $mailfetch[$i_loop]['type'];
  150. echo '<br />' .
  151. html_tag( 'table',
  152. html_tag( 'tr',
  153. html_tag( 'td', '<b>' .
  154. sprintf(_("Fetching from %s"),
  155. htmlspecialchars($mailfetch[$i_loop]['alias'])) .
  156. '</b>',
  157. 'center' ) ,
  158. '', $color[9] ) ,
  159. '', '', 'width="90%"' );
  160. flush();
  161. $pop3 = new mail_fetch(array('host' => $mailfetch_server,
  162. 'port' => $mailfetch_port,
  163. 'auth' => $mailfetch_auth,
  164. 'tls' => $mailfetch_type,
  165. 'timeout' => 60));
  166. if (!empty($pop3->error)) {
  167. Mail_Fetch_Status($pop3->error);
  168. continue;
  169. }
  170. Mail_Fetch_Status(_("Opening IMAP server"));
  171. $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
  172. // check if destination folder is not set, is not subscribed and is not \noselect folder
  173. if($mailfetch_subfolder == '' ||
  174. ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  175. $mailfetch_subfolder = 'INBOX';
  176. }
  177. Mail_Fetch_Status(_("Opening POP server"));
  178. /* log into pop server*/
  179. if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
  180. Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
  181. continue;
  182. }
  183. $aMsgStat = $pop3->command_stat();
  184. if (is_bool($aMsgStat)) {
  185. Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
  186. continue;
  187. }
  188. $Count = $aMsgStat['count'];
  189. $i = 1;
  190. if ($Count>0) {
  191. // If we leave messages on server, try using UIDL
  192. if ($mailfetch_lmos == 'on') {
  193. Mail_Fetch_Status(_("Fetching UIDL..."));
  194. $msglist = $pop3->command_uidl();
  195. if (is_bool($msglist)) {
  196. Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error));
  197. // User asked to leave messages on server, but we can't do that.
  198. $pop3->command_quit();
  199. continue;
  200. // $mailfetch_lmos = 'off';
  201. } else {
  202. // calculate number of new messages
  203. for ($j = 1; $j <= sizeof($msglist); $j++) {
  204. // do strict comparison ('1111.10' should not be equal to '1111.100')
  205. if ($msglist[$j] === $mailfetch_uidl) {
  206. $i = $j+1;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. // fetch list of messages with LIST
  213. // we can use else control, but we can also set $mailfetch_lmos
  214. // to off if server does not support UIDL.
  215. if ($mailfetch_lmos != 'on') {
  216. Mail_Fetch_Status(_("Fetching list of messages..."));
  217. $msglist = $pop3->command_list();
  218. }
  219. }
  220. if ($Count < $i) {
  221. Mail_Fetch_Status(_("Login OK: No new messages"));
  222. $pop3->command_quit();
  223. continue;
  224. }
  225. if ($Count == 0) {
  226. Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  227. $pop3->command_quit();
  228. continue;
  229. } else {
  230. $newmsgcount = $Count - $i + 1;
  231. Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
  232. "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
  233. }
  234. if ($mailfetch_lmos == 'on') {
  235. Mail_Fetch_Status(_("Leaving mail on server..."));
  236. } else {
  237. Mail_Fetch_Status(_("Deleting messages from server..."));
  238. }
  239. for (; $i <= $Count; $i++) {
  240. Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
  241. if (!ini_get('safe_mode'))
  242. set_time_limit(20); // 20 seconds per message max
  243. $Message = $pop3->command_retr($i);
  244. if (is_bool($Message)) {
  245. Mail_Fetch_Status(htmlspecialchars($pop3->error));
  246. continue;
  247. }
  248. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
  249. $Line = fgets($imap_stream, 1024);
  250. if (substr($Line, 0, 1) == '+') {
  251. fputs($imap_stream, $Message);
  252. fputs($imap_stream, "\r\n");
  253. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  254. $response=(implode('',$response));
  255. $message=(implode('',$message));
  256. if ($response != 'OK') {
  257. Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
  258. Mail_Fetch_Status(_("Closing POP"));
  259. $pop3->command_quit();
  260. Mail_Fetch_Status(_("Logging out from IMAP"));
  261. sqimap_logout($imap_stream);
  262. if ($mailfetch_lmos == 'on') {
  263. Mail_Fetch_Status(_("Saving UIDL"));
  264. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
  265. }
  266. exit;
  267. } else {
  268. Mail_Fetch_Status(_("Message appended to mailbox"));
  269. }
  270. if ($mailfetch_lmos != 'on') {
  271. if( $pop3->command_dele($i) ) {
  272. Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
  273. } else {
  274. Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
  275. }
  276. }
  277. } else {
  278. echo $Line;
  279. Mail_Fetch_Status(_("Error Appending Message!"));
  280. Mail_Fetch_Status(_("Closing POP"));
  281. $pop3->command_quit();
  282. Mail_Fetch_Status(_("Logging out from IMAP"));
  283. sqimap_logout($imap_stream);
  284. // not gurantee corect!
  285. if ($mailfetch_lmos == 'on') {
  286. Mail_Fetch_Status(_("Saving UIDL"));
  287. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
  288. }
  289. exit;
  290. }
  291. }
  292. Mail_Fetch_Status(_("Closing POP"));
  293. $pop3->command_quit();
  294. Mail_Fetch_Status(_("Logging out from IMAP"));
  295. sqimap_logout($imap_stream);
  296. if ($mailfetch_lmos == 'on' && is_array($msglist)) {
  297. Mail_Fetch_Status(_("Saving UIDL"));
  298. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
  299. }
  300. Mail_Fetch_Status(_("Done"));
  301. }
  302. $oTemplate->display('footer.tpl');