03c9211b52b7f9ea6333999c98ada8651301463a
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 function Mail_Fetch_Servers() {
41 global $data_dir, $username;
43 $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
44 if (!isset($mailfetch['server_number']) ||
($mailfetch['server_number'] < 1)) {
45 $mailfetch['server_number'] = 0;
47 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
48 for ($i = 0; $i < $mailfetch['server_number']; $i++
) {
49 $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
50 $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i");
51 $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i");
52 $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i");
53 $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i");
54 if($mailfetch['cypher'] == 'on') {
55 $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
57 if ($mailfetch[$i]['pass'] == '') {
58 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST
);
60 $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i");
61 $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i");
62 $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i");
63 $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
64 if($mailfetch[$i]['alias'] == '') {
65 $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
71 function Mail_Fetch_Select_Server($mailfetch) {
74 echo '<font size=-5><br></font>' .
75 "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">" .
76 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
78 html_tag( 'td', _("Select Server:") . ' ', 'right' ) .
79 html_tag( 'td', '', 'left' ) .
80 '<select name="server_to_fetch" size="1">' .
81 '<option value="all" selected>..' . _("All") . "...\n";
82 for ($i = 0;$i < $mailfetch['server_number'];$i++
) {
83 echo "<option value=\"$i\">" .
84 htmlspecialchars($mailfetch[$i]['alias']) .
91 //if password not set, ask for it
92 for ($i = 0;$i < $mailfetch['server_number'];$i++
) {
93 if ($mailfetch[$i]['pass'] == '') {
95 html_tag( 'td', _("Password for") . ' <b>' .
96 htmlspecialchars($mailfetch[$i]['alias']) .
97 '</b>: ',
99 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
104 html_tag( 'td', ' ' ) .
105 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
110 $mailfetch = Mail_Fetch_Servers();
111 displayPageHeader($color, 'None');
115 echo html_tag( 'table',
117 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
119 'center', '', 'width="95%" cols="1"' );
122 /* there are no servers defined yet... */
123 if($mailfetch['server_number'] == 0) {
124 echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
125 displayInternalLink('plugins/mail_fetch/options.php',
126 _("Click here to go to the options page.") );
127 echo '</body></html>';
131 // get $server_to_fetch from globals, if not set display a choice to the user
132 if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST
) ) {
133 Mail_Fetch_Select_Server($mailfetch);
137 if ( $server_to_fetch == 'all' ) {
139 $i_stop = $mailfetch['server_number'];
141 $i_start = $server_to_fetch;
142 $i_stop = $i_start+
1;
145 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++
) {
146 $mailfetch_server = $mailfetch[$i_loop]['server'];
147 $mailfetch_port = $mailfetch[$i_loop]['port'];
148 $mailfetch_user = $mailfetch[$i_loop]['user'];
149 $mailfetch_pass = $mailfetch[$i_loop]['pass'];
150 $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
151 $mailfetch_login = $mailfetch[$i_loop]['login'];
152 $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
153 $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
154 if($mailfetch_subfolder == '') {
155 $mailfetch_subfolder == 'INBOX';
158 $pop3 = new POP3($mailfetch_server, 60);
163 html_tag( 'td', '<b>' . _("Fetching from ") .
164 htmlspecialchars($mailfetch[$i_loop]['alias']) .
168 '', '', 'width="90%"' );
172 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
173 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
177 Mail_Fetch_Status(_("Opening IMAP server"));
178 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
180 Mail_Fetch_Status(_("Opening POP server"));
181 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
182 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
183 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
187 // register_shutdown_function($pop3->quit());
189 $msglist = $pop3->uidl();
192 for ($j = 1; $j < sizeof($msglist); $j++
) {
193 if ($msglist[$j] == $mailfetch_uidl) {
200 Mail_Fetch_Status(_("Login OK: No new messages"));
205 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
209 $newmsgcount = $Count - $i +
1;
210 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
213 Mail_Fetch_Status(_("Fetching UIDL..."));
214 // Faster to get them all at once
215 $mailfetch_uidl = $pop3->uidl();
217 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
218 Mail_Fetch_Status(_("Server does not support UIDL."));
220 if ($mailfetch_lmos == 'on') {
221 Mail_Fetch_Status(_("Leaving Mail on Server..."));
223 Mail_Fetch_Status(_("Deleting messages from server..."));
226 for (; $i <= $Count; $i++
) {
227 Mail_Fetch_Status(_("Fetching message ") . "$i" );
228 set_time_limit(20); // 20 seconds per message max
230 $MessArray = $pop3->get($i);
232 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
233 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
235 Mail_Fetch_Status(_("Server error...Disconnect"));
237 Mail_Fetch_Status(_("Reconnect from dead connection"));
238 if (!$pop3->connect($mailfetch_server)) {
239 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR
);
240 Mail_Fetch_Status(_("Saving UIDL"));
241 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
245 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
246 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
247 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR
);
248 Mail_Fetch_Status(_("Saving UIDL"));
249 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
253 Mail_Fetch_Status(_("Refetching message ") . "$i" );
254 $MessArray = $pop3->get($i);
258 while (list($lineNum, $line) = each ($MessArray)) {
262 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
263 $Line = fgets($imap_stream, 1024);
264 if (substr($Line, 0, 1) == '+') {
265 fputs($imap_stream, $Message);
266 fputs($imap_stream, "\r\n");
267 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
268 if ($response != 'OK') {
269 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
270 Mail_Fetch_Status(_("Closing POP"));
272 Mail_Fetch_Status(_("Logging out from IMAP"));
273 sqimap_logout($imap_stream);
275 Mail_Fetch_Status(_("Saving UIDL"));
276 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
279 Mail_Fetch_Status(_("Message appended to mailbox"));
282 if ($mailfetch_lmos != 'on') {
283 if( $pop3->delete($i) ) {
284 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
286 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR
);
291 Mail_Fetch_Status(_("Error Appending Message!"));
292 Mail_Fetch_Status(_("Closing POP"));
294 Mail_Fetch_Status(_("Logging out from IMAP"));
295 sqimap_logout($imap_stream);
297 // not gurantee corect!
298 Mail_Fetch_Status(_("Saving UIDL"));
299 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
304 Mail_Fetch_Status(_("Closing POP"));
306 Mail_Fetch_Status(_("Logging out from IMAP"));
307 sqimap_logout($imap_stream);
308 if (is_array($mailfetch_uidl)) {
309 Mail_Fetch_Status(_("Saving UIDL"));
310 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
313 Mail_Fetch_Status(_("Done"));