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