2510b6cb36a5d3516430e8b7e8913e5831e10133
4 ** mail_fetch/fetch.php
6 ** Copyright (c) 1999-2002 The SquirrelMail Project Team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
15 require_once('../src/validate.php');
16 require_once('../functions/page_header.php');
17 require_once('../functions/imap.php');
18 require_once('../src/load_prefs.php');
19 require_once('../plugins/mail_fetch/class.POP3.php');
20 require_once('../functions/i18n.php');
21 require_once( '../plugins/mail_fetch/functions.php' );
22 require_once( '../functions/html.php' );
25 function Mail_Fetch_Status($msg) {
26 echo html_tag( 'table',
28 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
30 '', '', 'width="90%"' );
34 displayPageHeader($color, 'None');
36 $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
37 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
38 $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
39 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
40 for ($i=0;$i<$mailfetch_server_number;$i++
) {
41 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
42 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
43 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
44 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
45 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
46 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
47 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
48 $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
49 if( $mailfetch_cypher == 'on' ) {
50 $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
56 echo html_tag( 'table',
58 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
60 'center', '', 'width="95%" cols="1"' );
62 if (!isset( $server_to_fetch ) ) {
64 echo '<font size=-5><br></font>' .
65 "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">" .
66 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
68 html_tag( 'td', _("Select Server:") . ' ', 'right' ) .
69 html_tag( 'td', '', 'left' ) .
70 '<select name="server_to_fetch" size="1">' .
71 '<option value="all" selected>..' . _("All") . "...\n";
72 for ($i=0;$i<$mailfetch_server_number;$i++
) {
73 echo "<option value=\"$i\">" .
74 (($mailfetch_alias_[$i]=='')?
$mailfetch_server_[$i]:$mailfetch_alias_[$i]) .
81 //if password not set, ask for it
82 for ($i=0;$i<$mailfetch_server_number;$i++
) {
83 if ($mailfetch_pass_[$i]=='') {
85 html_tag( 'td', _("Password for") . ' <b>' .
86 (($mailfetch_alias_[$i]=='')?
$mailfetch_server_[$i]:$mailfetch_alias_[$i]) .
87 '</b>: ',
89 html_tag( 'td', '<input type="password" name="pass_' . $i , '">', 'left' )
94 html_tag( 'td', ' ' ) .
95 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
102 if ( $server_to_fetch == 'all' ) {
104 $i_stop = $mailfetch_server_number;
106 $i_start = $server_to_fetch;
107 $i_stop = $i_start+
1;
110 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++
) {
111 $mailfetch_server=$mailfetch_server_[$i_loop];
112 $mailfetch_user=$mailfetch_user_[$i_loop];
113 if ($mailfetch_pass_[$i_loop]=="") {
115 $mailfetch_pass=$
$tmp;
117 $mailfetch_pass=$mailfetch_pass_[$i_loop];
119 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
120 $mailfetch_login=$mailfetch_login_[$i_loop];
121 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
122 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
125 $pop3 = new POP3($mailfetch_server, 60);
130 html_tag( 'td', '<b>' . _("Fetching from ") .
131 (($mailfetch_alias_[$i_loop] == '')?
$mailfetch_server:$mailfetch_alias_[$i_loop]) .
135 '', '', 'width="90%"' );
139 if (!$pop3->connect($mailfetch_server)) {
140 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
144 Mail_Fetch_Status(_("Opening IMAP server"));
145 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
147 Mail_Fetch_Status(_("Opening POP server"));
148 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
149 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
150 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
154 // register_shutdown_function($pop3->quit());
156 $msglist = $pop3->uidl();
159 for ($j = 1; $j < sizeof($msglist); $j++
) {
160 if ($msglist["$j"] == $mailfetch_uidl) {
167 Mail_Fetch_Status(_("Login OK: No new messages"));
172 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
176 $newmsgcount = $Count - $i +
1;
177 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
180 Mail_Fetch_Status(_("Fetching UIDL..."));
181 // Faster to get them all at once
182 $mailfetch_uidl = $pop3->uidl();
184 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
185 Mail_Fetch_Status(_("Server does not support UIDL."));
187 if ($mailfetch_lmos == 'on') {
188 Mail_Fetch_Status(_("Leaving Mail on Server..."));
190 Mail_Fetch_Status(_("Deleting messages from server..."));
193 for (; $i <= $Count; $i++
) {
194 Mail_Fetch_Status(_("Fetching message ") . "$i" );
195 set_time_limit(20); // 20 seconds per message max
197 $MessArray = $pop3->get($i);
199 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
200 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
202 Mail_Fetch_Status(_("Server error...Disconnect"));
204 Mail_Fetch_Status(_("Re-connect from dead connectoin"));
205 if (!$pop3->connect($mailfetch_server)) {
206 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
207 Mail_Fetch_Status(_("Saving UIDL"));
208 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
212 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
213 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
214 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
215 Mail_Fetch_Status(_("Saving UIDL"));
216 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
220 Mail_Fetch_Status(_("Re-fetching message ") . "$i" );
221 $MessArray = $pop3->get($i);
225 while (list($lineNum, $line) = each ($MessArray)) {
229 if ($mailfetch_subfolder=="") {
230 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
232 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
234 $Line = fgets($imap_stream, 1024);
235 if (substr($Line, 0, 1) == '+') {
236 fputs($imap_stream, $Message);
237 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
238 if ( $response <> 'OK' ) {
239 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
240 Mail_Fetch_Status(_("Closing POP"));
242 Mail_Fetch_Status(_("Logging out from IMAP"));
243 sqimap_logout($imap_stream);
245 Mail_Fetch_Status(_("Saving UIDL"));
246 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
249 Mail_Fetch_Status(_("Message appended to mailbox"));
252 if ($mailfetch_lmos != 'on') {
253 if( $pop3->delete($i) ) {
254 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
256 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR
);
261 Mail_Fetch_Status(_("Error Appending Message!"));
262 Mail_Fetch_Status(_("Closing POP"));
264 Mail_Fetch_Status(_("Logging out from IMAP"));
265 sqimap_logout($imap_stream);
267 // not gurantee corect!
268 Mail_Fetch_Status(_("Saving UIDL"));
269 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
274 Mail_Fetch_Status(_("Closing POP"));
276 Mail_Fetch_Status(_("Logging out from IMAP"));
277 sqimap_logout($imap_stream);
278 if (is_array($mailfetch_uidl)) {
279 Mail_Fetch_Status(_("Saving UIDL"));
280 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
283 Mail_Fetch_Status(_("Done"));