Cleaned up code. Added "\r\n" after we submit a message so the imap server
[squirrelmail.git] / plugins / mail_fetch / fetch.php
CommitLineData
d622d38a 1<?php
d622d38a 2
8d6a115b 3/**
4 * mail_fetch/fetch.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
8d6a115b 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Fetch code.
10 *
11 * $Id$
12 */
d3c89357 13
8d6a115b 14define('SM_PATH','../../');
d3c89357 15
8d6a115b 16require_once(SM_PATH . 'include/validate.php');
17require_once(SM_PATH . 'functions/page_header.php');
18require_once(SM_PATH . 'functions/imap.php');
19require_once(SM_PATH . 'include/load_prefs.php');
20require_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php');
8d6a115b 21require_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
22require_once(SM_PATH . 'functions/html.php' );
d3c89357 23
3c66c567 24/* globals */
25sqgetGlobalVar('username', $username, SQ_SESSION);
26sqgetGlobalVar('key', $key, SQ_COOKIE);
27sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
28sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
3c66c567 29/* end globals */
86bb8549 30
d3c89357 31 function Mail_Fetch_Status($msg) {
4cf43843 32 echo html_tag( 'table',
33 html_tag( 'tr',
34 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
35 ),
36 '', '', 'width="90%"' );
d3c89357 37 flush();
38 }
d622d38a 39
d622d38a 40 displayPageHeader($color, 'None');
41
8b56b0d9 42 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
43 if (!isset($mailfetch_server_number) || ($mailfetch_server_number < 1)) {
44 $mailfetch_server_number = 0;
45 }
46 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
47 for ($i = 0;$i < $mailfetch_server_number;$i++) {
d622d38a 48 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
8b56b0d9 49 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
50 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
51 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
52 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
53 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
54 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
55 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
d622d38a 56 $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
8b56b0d9 57 if($mailfetch_cypher == 'on') {
d622d38a 58 $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
59 }
8b56b0d9 60 if ($mailfetch_pass_[$i] == '') {
61 sqgetGlobalVar("pass_$i", $mailfetch_pass_[$i], SQ_POST);
62 }
d622d38a 63 }
8b56b0d9 64
d622d38a 65 echo '<br><center>';
8b56b0d9 66
4cf43843 67 echo html_tag( 'table',
68 html_tag( 'tr',
69 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
70 ) ,
71 'center', '', 'width="95%" cols="1"' );
8b56b0d9 72
d622d38a 73 if (!isset( $server_to_fetch ) ) {
8b56b0d9 74
4cf43843 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>..' . _("All") . "...\n";
8b56b0d9 83 for ($i = 0;$i < $mailfetch_server_number;$i++) {
4cf43843 84 echo "<option value=\"$i\">" .
8b56b0d9 85 htmlspecialchars($mailfetch_alias_[$i]) .
4cf43843 86 '</option>' . "\n";
8b56b0d9 87 }
4cf43843 88 echo '</select>' .
89 '</td>' .
90 '</tr>';
8b56b0d9 91
d622d38a 92 //if password not set, ask for it
8b56b0d9 93 for ($i = 0;$i < $mailfetch_server_number;$i++) {
94 if ($mailfetch_pass_[$i] == '') {
4cf43843 95 echo html_tag( 'tr',
96 html_tag( 'td', _("Password for") . ' <b>' .
8b56b0d9 97 htmlspecialchars($mailfetch_alias_[$i]) .
4cf43843 98 '</b>: &nbsp; &nbsp; ',
99 'right' ) .
18cdccda 100 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
4cf43843 101 );
d622d38a 102 }
103 }
4cf43843 104 echo html_tag( 'tr',
105 html_tag( 'td', '&nbsp;' ) .
106 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
e0f638ae 107 ) .
4cf43843 108 '</table></form>';
d622d38a 109 exit();
110 }
111
112 if ( $server_to_fetch == 'all' ) {
113 $i_start = 0;
8b56b0d9 114 $i_stop = $mailfetch_server_number;
d622d38a 115 } else {
116 $i_start = $server_to_fetch;
8b56b0d9 117 $i_stop = $i_start+1;
d622d38a 118 }
8b56b0d9 119
d622d38a 120 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
8b56b0d9 121 $mailfetch_server = $mailfetch_server_[$i_loop];
122 $mailfetch_port = $mailfetch_port_[$i_loop];
123 $mailfetch_user = $mailfetch_user_[$i_loop];
124 $mailfetch_pass = $mailfetch_pass_[$i_loop];
125 $mailfetch_lmos = $mailfetch_lmos_[$i_loop];
126 $mailfetch_login = $mailfetch_login_[$i_loop];
127 $mailfetch_uidl = $mailfetch_uidl_[$i_loop];
128 $mailfetch_subfolder = $mailfetch_subfolder_[$i_loop];
129 if($mailfetch_subfolder == '') {
130 $mailfetch_subfolder == 'INBOX';
d622d38a 131 }
8b56b0d9 132
d622d38a 133 $pop3 = new POP3($mailfetch_server, 60);
8b56b0d9 134
4cf43843 135 echo '<br>' .
136 html_tag( 'table',
137 html_tag( 'tr',
138 html_tag( 'td', '<b>' . _("Fetching from ") .
8b56b0d9 139 htmlspecialchars($mailfetch_alias_[$i_loop]) .
4cf43843 140 '</b>',
141 'center' ) ,
142 '', $color[9] ) ,
143 '', '', 'width="90%"' );
8b56b0d9 144
d622d38a 145 flush();
8b56b0d9 146
e42b0dd2 147 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
d622d38a 148 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
149 continue;
150 }
8b56b0d9 151
d622d38a 152 Mail_Fetch_Status(_("Opening IMAP server"));
153 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
8b56b0d9 154
d622d38a 155 Mail_Fetch_Status(_("Opening POP server"));
156 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
157 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
d3c89357 158 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
d622d38a 159 continue;
160 }
8b56b0d9 161
d622d38a 162 // register_shutdown_function($pop3->quit());
8b56b0d9 163
d622d38a 164 $msglist = $pop3->uidl();
09e47788 165
d622d38a 166 $i = 1;
167 for ($j = 1; $j < sizeof($msglist); $j++) {
168 if ($msglist["$j"] == $mailfetch_uidl) {
169 $i = $j+1;
170 break;
171 }
172 }
173
174 if ($Count < $i) {
175 Mail_Fetch_Status(_("Login OK: No new messages"));
176 $pop3->quit();
177 continue;
178 }
179 if ($Count == 0) {
180 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
181 $pop3->quit();
182 continue;
183 } else {
184 $newmsgcount = $Count - $i + 1;
185 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
186 }
8b56b0d9 187
d622d38a 188 Mail_Fetch_Status(_("Fetching UIDL..."));
189 // Faster to get them all at once
190 $mailfetch_uidl = $pop3->uidl();
8b56b0d9 191
d622d38a 192 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
193 Mail_Fetch_Status(_("Server does not support UIDL."));
194
195 if ($mailfetch_lmos == 'on') {
196 Mail_Fetch_Status(_("Leaving Mail on Server..."));
197 } else {
198 Mail_Fetch_Status(_("Deleting messages from server..."));
199 }
8b56b0d9 200
d622d38a 201 for (; $i <= $Count; $i++) {
202 Mail_Fetch_Status(_("Fetching message ") . "$i" );
203 set_time_limit(20); // 20 seconds per message max
8b56b0d9 204 $Message = '';
d622d38a 205 $MessArray = $pop3->get($i);
8b56b0d9 206
09e47788 207 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
208 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
209 // re-connect pop3
210 Mail_Fetch_Status(_("Server error...Disconnect"));
8b56b0d9 211 $pop3->quit();
a3e3ddc4 212 Mail_Fetch_Status(_("Reconnect from dead connection"));
09e47788 213 if (!$pop3->connect($mailfetch_server)) {
214 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
215 Mail_Fetch_Status(_("Saving UIDL"));
216 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
217
218 continue;
219 }
220 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
221 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
222 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
223 Mail_Fetch_Status(_("Saving UIDL"));
224 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
225
226 continue;
227 }
a3e3ddc4 228 Mail_Fetch_Status(_("Refetching message ") . "$i" );
8b56b0d9 229 $MessArray = $pop3->get($i);
09e47788 230
231 } // end while
8b56b0d9 232
d622d38a 233 while (list($lineNum, $line) = each ($MessArray)) {
234 $Message .= $line;
235 }
8b56b0d9 236
237 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
d622d38a 238 $Line = fgets($imap_stream, 1024);
239 if (substr($Line, 0, 1) == '+') {
240 fputs($imap_stream, $Message);
8b56b0d9 241 fputs($imap_stream, "\r\n");
d622d38a 242 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
8b56b0d9 243 if ($response != 'OK') {
09e47788 244 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
245 Mail_Fetch_Status(_("Closing POP"));
246 $pop3->quit();
247 Mail_Fetch_Status(_("Logging out from IMAP"));
248 sqimap_logout($imap_stream);
249
250 Mail_Fetch_Status(_("Saving UIDL"));
251 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
8b56b0d9 252 exit;
09e47788 253 } else {
254 Mail_Fetch_Status(_("Message appended to mailbox"));
255 }
8b56b0d9 256
d622d38a 257 if ($mailfetch_lmos != 'on') {
258 if( $pop3->delete($i) ) {
259 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
260 } else {
261 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
262 }
263 }
264 } else {
8b56b0d9 265 echo $Line;
d622d38a 266 Mail_Fetch_Status(_("Error Appending Message!"));
09e47788 267 Mail_Fetch_Status(_("Closing POP"));
268 $pop3->quit();
269 Mail_Fetch_Status(_("Logging out from IMAP"));
270 sqimap_logout($imap_stream);
271
272 // not gurantee corect!
273 Mail_Fetch_Status(_("Saving UIDL"));
274 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
275 exit;
d622d38a 276 }
277 }
8b56b0d9 278
d622d38a 279 Mail_Fetch_Status(_("Closing POP"));
280 $pop3->quit();
281 Mail_Fetch_Status(_("Logging out from IMAP"));
282 sqimap_logout($imap_stream);
283 if (is_array($mailfetch_uidl)) {
284 Mail_Fetch_Status(_("Saving UIDL"));
285 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
286 }
8b56b0d9 287
d622d38a 288 Mail_Fetch_Status(_("Done"));
289
290 }
291
292?>
293</center>
294</body>
15e6162e 295</html>