fetch.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /**
  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, 'None');
  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. echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
  120. displayInternalLink('plugins/mail_fetch/options.php',
  121. _("Click here to go to the options page.") );
  122. $oTemplate->display('footer.tpl');
  123. exit();
  124. }
  125. // get $server_to_fetch from globals, if not set display a choice to the user
  126. if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
  127. Mail_Fetch_Select_Server($mailfetch);
  128. $oTemplate->display('footer.tpl');
  129. exit();
  130. }
  131. if ( $server_to_fetch == 'all' ) {
  132. $i_start = 0;
  133. $i_stop = $mailfetch['server_number'];
  134. } else {
  135. $i_start = $server_to_fetch;
  136. $i_stop = $i_start+1;
  137. }
  138. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
  139. $mailfetch_server = $mailfetch[$i_loop]['server'];
  140. $mailfetch_port = $mailfetch[$i_loop]['port'];
  141. $mailfetch_user = $mailfetch[$i_loop]['user'];
  142. $mailfetch_pass = $mailfetch[$i_loop]['pass'];
  143. $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
  144. $mailfetch_login = $mailfetch[$i_loop]['login'];
  145. $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
  146. $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
  147. $mailfetch_auth = $mailfetch[$i_loop]['auth'];
  148. $mailfetch_type = $mailfetch[$i_loop]['type'];
  149. echo '<br />' .
  150. html_tag( 'table',
  151. html_tag( 'tr',
  152. html_tag( 'td', '<b>' .
  153. sprintf(_("Fetching from %s"),
  154. htmlspecialchars($mailfetch[$i_loop]['alias'])) .
  155. '</b>',
  156. 'center' ) ,
  157. '', $color[9] ) ,
  158. '', '', 'width="90%"' );
  159. flush();
  160. $pop3 = new mail_fetch(array('host' => $mailfetch_server,
  161. 'port' => $mailfetch_port,
  162. 'auth' => $mailfetch_auth,
  163. 'tls' => $mailfetch_type,
  164. 'timeout' => 60));
  165. if (!empty($pop3->error)) {
  166. Mail_Fetch_Status($pop3->error);
  167. continue;
  168. }
  169. Mail_Fetch_Status(_("Opening IMAP server"));
  170. $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
  171. // check if destination folder is not set, is not subscribed and is not \noselect folder
  172. if($mailfetch_subfolder == '' ||
  173. ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  174. $mailfetch_subfolder = 'INBOX';
  175. }
  176. Mail_Fetch_Status(_("Opening POP server"));
  177. /* log into pop server*/
  178. if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
  179. Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
  180. continue;
  181. }
  182. $aMsgStat = $pop3->command_stat();
  183. if (is_bool($aMsgStat)) {
  184. Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
  185. continue;
  186. }
  187. $Count = $aMsgStat['count'];
  188. $i = 1;
  189. if ($Count>0) {
  190. // If we leave messages on server, try using UIDL
  191. if ($mailfetch_lmos == 'on') {
  192. Mail_Fetch_Status(_("Fetching UIDL..."));
  193. $msglist = $pop3->command_uidl();
  194. if (is_bool($msglist)) {
  195. Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error));
  196. // User asked to leave messages on server, but we can't do that.
  197. $pop3->command_quit();
  198. continue;
  199. // $mailfetch_lmos = 'off';
  200. } else {
  201. // calculate number of new messages
  202. for ($j = 1; $j <= sizeof($msglist); $j++) {
  203. // do strict comparison ('1111.10' should not be equal to '1111.100')
  204. if ($msglist[$j] === $mailfetch_uidl) {
  205. $i = $j+1;
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. // fetch list of messages with LIST
  212. // we can use else control, but we can also set $mailfetch_lmos
  213. // to off if server does not support UIDL.
  214. if ($mailfetch_lmos != 'on') {
  215. Mail_Fetch_Status(_("Fetching list of messages..."));
  216. $msglist = $pop3->command_list();
  217. }
  218. }
  219. if ($Count < $i) {
  220. Mail_Fetch_Status(_("Login OK: No new messages"));
  221. $pop3->command_quit();
  222. continue;
  223. }
  224. if ($Count == 0) {
  225. Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  226. $pop3->command_quit();
  227. continue;
  228. } else {
  229. $newmsgcount = $Count - $i + 1;
  230. Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
  231. "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
  232. }
  233. if ($mailfetch_lmos == 'on') {
  234. Mail_Fetch_Status(_("Leaving mail on server..."));
  235. } else {
  236. Mail_Fetch_Status(_("Deleting messages from server..."));
  237. }
  238. for (; $i <= $Count; $i++) {
  239. Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
  240. if (!ini_get('safe_mode'))
  241. set_time_limit(20); // 20 seconds per message max
  242. $Message = $pop3->command_retr($i);
  243. if (is_bool($Message)) {
  244. Mail_Fetch_Status(htmlspecialchars($pop3->error));
  245. continue;
  246. }
  247. fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
  248. $Line = fgets($imap_stream, 1024);
  249. if (substr($Line, 0, 1) == '+') {
  250. fputs($imap_stream, $Message);
  251. fputs($imap_stream, "\r\n");
  252. sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
  253. $response=(implode('',$response));
  254. $message=(implode('',$message));
  255. if ($response != 'OK') {
  256. Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
  257. Mail_Fetch_Status(_("Closing POP"));
  258. $pop3->command_quit();
  259. Mail_Fetch_Status(_("Logging out from IMAP"));
  260. sqimap_logout($imap_stream);
  261. if ($mailfetch_lmos == 'on') {
  262. Mail_Fetch_Status(_("Saving UIDL"));
  263. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
  264. }
  265. exit;
  266. } else {
  267. Mail_Fetch_Status(_("Message appended to mailbox"));
  268. }
  269. if ($mailfetch_lmos != 'on') {
  270. if( $pop3->command_dele($i) ) {
  271. Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
  272. } else {
  273. Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
  274. }
  275. }
  276. } else {
  277. echo $Line;
  278. Mail_Fetch_Status(_("Error Appending Message!"));
  279. Mail_Fetch_Status(_("Closing POP"));
  280. $pop3->command_quit();
  281. Mail_Fetch_Status(_("Logging out from IMAP"));
  282. sqimap_logout($imap_stream);
  283. // not gurantee corect!
  284. if ($mailfetch_lmos == 'on') {
  285. Mail_Fetch_Status(_("Saving UIDL"));
  286. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
  287. }
  288. exit;
  289. }
  290. }
  291. Mail_Fetch_Status(_("Closing POP"));
  292. $pop3->command_quit();
  293. Mail_Fetch_Status(_("Logging out from IMAP"));
  294. sqimap_logout($imap_stream);
  295. if ($mailfetch_lmos == 'on' && is_array($msglist)) {
  296. Mail_Fetch_Status(_("Saving UIDL"));
  297. setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
  298. }
  299. Mail_Fetch_Status(_("Done"));
  300. }
  301. $oTemplate->display('footer.tpl');