fetch.php 13 KB

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