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