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