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
);
31 function Mail_Fetch_Status($msg) {
32 echo html_tag( 'table',
34 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
36 '', '', 'width="90%"' );
40 displayPageHeader($color, 'None');
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;
46 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
47 for ($i = 0;$i < $mailfetch_server_number;$i++
) {
48 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
49 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
50 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
51 if($mailfetch_alias_[$i] == '') {
52 $mailfetch_alias_[$i] = $mailfetch_server_[$i];
54 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
55 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
56 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
57 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
58 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
59 $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
60 if($mailfetch_cypher == 'on') {
61 $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
63 if ($mailfetch_pass_[$i] == '') {
64 sqgetGlobalVar("pass_$i", $mailfetch_pass_[$i], SQ_POST
);
70 echo html_tag( 'table',
72 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
74 'center', '', 'width="95%" cols="1"' );
76 if (!isset( $server_to_fetch ) ) {
78 echo '<font size=-5><br></font>' .
79 "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">" .
80 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
82 html_tag( 'td', _("Select Server:") . ' ', 'right' ) .
83 html_tag( 'td', '', 'left' ) .
84 '<select name="server_to_fetch" size="1">' .
85 '<option value="all" selected>..' . _("All") . "...\n";
86 for ($i = 0;$i < $mailfetch_server_number;$i++
) {
87 echo "<option value=\"$i\">" .
88 htmlspecialchars($mailfetch_alias_[$i]) .
95 //if password not set, ask for it
96 for ($i = 0;$i < $mailfetch_server_number;$i++
) {
97 if ($mailfetch_pass_[$i] == '') {
99 html_tag( 'td', _("Password for") . ' <b>' .
100 htmlspecialchars($mailfetch_alias_[$i]) .
101 '</b>: ',
103 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
108 html_tag( 'td', ' ' ) .
109 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
115 if ( $server_to_fetch == 'all' ) {
117 $i_stop = $mailfetch_server_number;
119 $i_start = $server_to_fetch;
120 $i_stop = $i_start+
1;
123 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++
) {
124 $mailfetch_server = $mailfetch_server_[$i_loop];
125 $mailfetch_port = $mailfetch_port_[$i_loop];
126 $mailfetch_user = $mailfetch_user_[$i_loop];
127 $mailfetch_pass = $mailfetch_pass_[$i_loop];
128 $mailfetch_lmos = $mailfetch_lmos_[$i_loop];
129 $mailfetch_login = $mailfetch_login_[$i_loop];
130 $mailfetch_uidl = $mailfetch_uidl_[$i_loop];
131 $mailfetch_subfolder = $mailfetch_subfolder_[$i_loop];
132 if($mailfetch_subfolder == '') {
133 $mailfetch_subfolder == 'INBOX';
136 $pop3 = new POP3($mailfetch_server, 60);
141 html_tag( 'td', '<b>' . _("Fetching from ") .
142 htmlspecialchars($mailfetch_alias_[$i_loop]) .
146 '', '', 'width="90%"' );
150 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
151 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
155 Mail_Fetch_Status(_("Opening IMAP server"));
156 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
158 Mail_Fetch_Status(_("Opening POP server"));
159 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
160 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
161 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
165 // register_shutdown_function($pop3->quit());
167 $msglist = $pop3->uidl();
170 for ($j = 1; $j < sizeof($msglist); $j++
) {
171 if ($msglist["$j"] == $mailfetch_uidl) {
178 Mail_Fetch_Status(_("Login OK: No new messages"));
183 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
187 $newmsgcount = $Count - $i +
1;
188 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
191 Mail_Fetch_Status(_("Fetching UIDL..."));
192 // Faster to get them all at once
193 $mailfetch_uidl = $pop3->uidl();
195 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
196 Mail_Fetch_Status(_("Server does not support UIDL."));
198 if ($mailfetch_lmos == 'on') {
199 Mail_Fetch_Status(_("Leaving Mail on Server..."));
201 Mail_Fetch_Status(_("Deleting messages from server..."));
204 for (; $i <= $Count; $i++
) {
205 Mail_Fetch_Status(_("Fetching message ") . "$i" );
206 set_time_limit(20); // 20 seconds per message max
208 $MessArray = $pop3->get($i);
210 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
211 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
213 Mail_Fetch_Status(_("Server error...Disconnect"));
215 Mail_Fetch_Status(_("Reconnect from dead connection"));
216 if (!$pop3->connect($mailfetch_server)) {
217 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
218 Mail_Fetch_Status(_("Saving UIDL"));
219 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
223 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
224 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
225 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
226 Mail_Fetch_Status(_("Saving UIDL"));
227 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
231 Mail_Fetch_Status(_("Refetching message ") . "$i" );
232 $MessArray = $pop3->get($i);
236 while (list($lineNum, $line) = each ($MessArray)) {
240 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
241 $Line = fgets($imap_stream, 1024);
242 if (substr($Line, 0, 1) == '+') {
243 fputs($imap_stream, $Message);
244 fputs($imap_stream, "\r\n");
245 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
246 if ($response != 'OK') {
247 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
248 Mail_Fetch_Status(_("Closing POP"));
250 Mail_Fetch_Status(_("Logging out from IMAP"));
251 sqimap_logout($imap_stream);
253 Mail_Fetch_Status(_("Saving UIDL"));
254 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
257 Mail_Fetch_Status(_("Message appended to mailbox"));
260 if ($mailfetch_lmos != 'on') {
261 if( $pop3->delete($i) ) {
262 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
264 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR
);
269 Mail_Fetch_Status(_("Error Appending Message!"));
270 Mail_Fetch_Status(_("Closing POP"));
272 Mail_Fetch_Status(_("Logging out from IMAP"));
273 sqimap_logout($imap_stream);
275 // not gurantee corect!
276 Mail_Fetch_Status(_("Saving UIDL"));
277 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
282 Mail_Fetch_Status(_("Closing POP"));
284 Mail_Fetch_Status(_("Logging out from IMAP"));
285 sqimap_logout($imap_stream);
286 if (is_array($mailfetch_uidl)) {
287 Mail_Fetch_Status(_("Saving UIDL"));
288 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
291 Mail_Fetch_Status(_("Done"));