73755e9cb6fa6f60b8cd2da2c06181bd00496b9e
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
14 define('SM_PATH','../../');
16 require_once(SM_PATH
. 'include/validate.php');
17 require_once(SM_PATH
. 'functions/page_header.php');
18 require_once(SM_PATH
. 'functions/imap.php');
19 require_once(SM_PATH
. 'include/load_prefs.php');
20 require_once(SM_PATH
. 'plugins/mail_fetch/class.POP3.php');
21 require_once(SM_PATH
. 'plugins/mail_fetch/functions.php' );
22 require_once(SM_PATH
. 'functions/html.php' );
25 sqgetGlobalVar('username', $username, SQ_SESSION
);
26 sqgetGlobalVar('key', $key, SQ_COOKIE
);
27 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
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
39 function Mail_Fetch_Status($msg) {
40 echo html_tag( 'table',
42 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
44 '', '', 'width="90%"' );
48 displayPageHeader($color, 'None');
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");
56 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
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] );
71 echo html_tag( 'table',
73 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
75 'center', '', 'width="95%" cols="1"' );
77 if (!isset( $server_to_fetch ) ) {
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"' ) .
83 html_tag( 'td', _("Select Server:") . ' ', 'right' ) .
84 html_tag( 'td', '', 'left' ) .
85 '<select name="server_to_fetch" size="1">' .
86 '<option value="all" selected>..' . _("All") . "...\n";
87 for ($i=0;$i<$mailfetch_server_number;$i++
) {
88 echo "<option value=\"$i\">" .
89 htmlspecialchars((($mailfetch_alias_[$i]=='')?
$mailfetch_server_[$i]:$mailfetch_alias_[$i])) .
96 //if password not set, ask for it
97 for ($i=0;$i<$mailfetch_server_number;$i++
) {
98 if ($mailfetch_pass_[$i]=='') {
100 html_tag( 'td', _("Password for") . ' <b>' .
101 htmlspecialchars((($mailfetch_alias_[$i]=='')?
$mailfetch_server_[$i]:$mailfetch_alias_[$i])) .
102 '</b>: ',
104 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
109 html_tag( 'td', ' ' ) .
110 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
116 if ( $server_to_fetch == 'all' ) {
118 $i_stop = $mailfetch_server_number;
120 $i_start = $server_to_fetch;
121 $i_stop = $i_start+
1;
124 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++
) {
125 $mailfetch_server=$mailfetch_server_[$i_loop];
126 $mailfetch_port=$mailfetch_port_[$i_loop];
127 $mailfetch_user=$mailfetch_user_[$i_loop];
128 if ($mailfetch_pass_[$i_loop]=="") {
130 $mailfetch_pass=$
$tmp;
132 $mailfetch_pass=$mailfetch_pass_[$i_loop];
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];
140 $pop3 = new POP3($mailfetch_server, 60);
145 html_tag( 'td', '<b>' . _("Fetching from ") .
146 htmlspecialchars((($mailfetch_alias_[$i_loop] == '')?
$mailfetch_server:$mailfetch_alias_[$i_loop])) .
150 '', '', 'width="90%"' );
154 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
155 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
159 Mail_Fetch_Status(_("Opening IMAP server"));
160 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
162 Mail_Fetch_Status(_("Opening POP server"));
163 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
164 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
165 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
169 // register_shutdown_function($pop3->quit());
171 $msglist = $pop3->uidl();
174 for ($j = 1; $j < sizeof($msglist); $j++
) {
175 if ($msglist["$j"] == $mailfetch_uidl) {
182 Mail_Fetch_Status(_("Login OK: No new messages"));
187 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
191 $newmsgcount = $Count - $i +
1;
192 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
195 Mail_Fetch_Status(_("Fetching UIDL..."));
196 // Faster to get them all at once
197 $mailfetch_uidl = $pop3->uidl();
199 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
200 Mail_Fetch_Status(_("Server does not support UIDL."));
202 if ($mailfetch_lmos == 'on') {
203 Mail_Fetch_Status(_("Leaving Mail on Server..."));
205 Mail_Fetch_Status(_("Deleting messages from server..."));
208 for (; $i <= $Count; $i++
) {
209 Mail_Fetch_Status(_("Fetching message ") . "$i" );
210 set_time_limit(20); // 20 seconds per message max
212 $MessArray = $pop3->get($i);
214 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
215 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
217 Mail_Fetch_Status(_("Server error...Disconnect"));
219 Mail_Fetch_Status(_("Reconnect from dead connection"));
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]);
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]);
235 Mail_Fetch_Status(_("Refetching message ") . "$i" );
236 $MessArray = $pop3->get($i);
240 while (list($lineNum, $line) = each ($MessArray)) {
244 if ($mailfetch_subfolder=="") {
245 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
247 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
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);
253 if ( $response <> 'OK' ) {
254 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
255 Mail_Fetch_Status(_("Closing POP"));
257 Mail_Fetch_Status(_("Logging out from IMAP"));
258 sqimap_logout($imap_stream);
260 Mail_Fetch_Status(_("Saving UIDL"));
261 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
264 Mail_Fetch_Status(_("Message appended to mailbox"));
267 if ($mailfetch_lmos != 'on') {
268 if( $pop3->delete($i) ) {
269 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
271 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR
);
276 Mail_Fetch_Status(_("Error Appending Message!"));
277 Mail_Fetch_Status(_("Closing POP"));
279 Mail_Fetch_Status(_("Logging out from IMAP"));
280 sqimap_logout($imap_stream);
282 // not gurantee corect!
283 Mail_Fetch_Status(_("Saving UIDL"));
284 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
289 Mail_Fetch_Status(_("Closing POP"));
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));
298 Mail_Fetch_Status(_("Done"));