included imap_parse.php
[squirrelmail.git] / plugins / mail_fetch / fetch.php
CommitLineData
d622d38a 1<?php
d622d38a 2
d3c89357 3 /**
4 ** mail_fetch/fetch.php
5 **
15e6162e 6 ** Copyright (c) 1999-2002 The SquirrelMail Project Team
d3c89357 7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 **
9 ** Fetch code.
10 **
11 ** $Id$
12 **/
13
14 chdir('..');
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
23
24 function Mail_Fetch_Status($msg) {
6dc0e464 25 echo '<table width="90%"><tr><td>' .
d3c89357 26 htmlspecialchars( $msg ) .
27 '</td></tr></table>';
28 flush();
29 }
d622d38a 30
d622d38a 31 displayPageHeader($color, 'None');
32
33 $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
34 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
35 $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
36 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
37 for ($i=0;$i<$mailfetch_server_number;$i++) {
38 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
39 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
40 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
41 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
42 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
43 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
44 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
45 $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
46 if( $mailfetch_cypher == 'on' ) {
47 $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
48 }
49 }
50
51 echo '<br><center>';
52
53 echo '<TABLE WIDTH=95% COLS=1 ALIGN=CENTER>' .
54 "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER><b>" . _("Remote POP server Fetching Mail") . '</b></TD></TR>' .
55 '</TABLE>';
56
57 if (!isset( $server_to_fetch ) ) {
58
59 echo '<font size=-5><BR></font>' .
60 "<form action=\"$PHP_SELF\" METHOD=POST TARGET=_self>" .
61 '<TABLE WIDTH=70% COLS=2 ALIGN=CENTER>' .
62 '<TR>' .
63 '<TD ALIGN=RIGHT>' . _("Select Server:") . ' &nbsp; &nbsp; </TD>' .
64 '<TD><SELECT NAME=server_to_fetch SIZE=1>' .
65 '<OPTION VALUE="all" SELECTED>..' . _("All") . "...\n";
66 for ($i=0;$i<$mailfetch_server_number;$i++) {
67 echo "<OPTION VALUE=\"$i\">" .
68 (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]);
69 }
70 echo '</SELECT>' .
71 '</TD>' .
72 '</TR>';
73
74 //if password not set, ask for it
75 for ($i=0;$i<$mailfetch_server_number;$i++) {
4965e012 76 if ($mailfetch_pass_[$i]=='') {
77 echo '<tr>' .
d622d38a 78 '<TD ALIGN=RIGHT>' . _("Password for") . ' <B>' . (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) . '</B>: &nbsp; &nbsp; </TD>' .
79 "<TD><INPUT TYPE=PASSWORD NAME=pass_$i></TD>" .
4965e012 80 '</TR>';
d622d38a 81 }
82 }
83 echo '<TR>' .
84 '<TD>&nbsp;</TD>' .
85 '<TD><input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '"></TD>'.
86 '</TR>' .
87 '</TABLE></form>';
88 exit();
89 }
90
91 if ( $server_to_fetch == 'all' ) {
92 $i_start = 0;
93 $i_stop = $mailfetch_server_number;
94 } else {
95 $i_start = $server_to_fetch;
96 $i_stop = $i_start+1;
97 }
98
99 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
100 $mailfetch_server=$mailfetch_server_[$i_loop];
101 $mailfetch_user=$mailfetch_user_[$i_loop];
102 if ($mailfetch_pass_[$i_loop]=="") {
103 $tmp="pass_$i_loop";
104 $mailfetch_pass=$$tmp;
105 } else {
106 $mailfetch_pass=$mailfetch_pass_[$i_loop];
107 }
108 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
109 $mailfetch_login=$mailfetch_login_[$i_loop];
110 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
111 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
112
113
114 $pop3 = new POP3($mailfetch_server, 60);
115
6dc0e464 116 echo "<br><table width=\"90%\"><tr bgcolor=\"$color[9]\"><td><b>" .
d622d38a 117 _("Fetching from ") .
118 (($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop]) .
119 "</b></td></tr></table>";
120
121 flush();
122
123 if (!$pop3->connect($mailfetch_server)) {
124 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
125 continue;
126 }
127
128 Mail_Fetch_Status(_("Opening IMAP server"));
129 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
f1d5238e 130
d622d38a 131 Mail_Fetch_Status(_("Opening POP server"));
132 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
133 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
d3c89357 134 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
d622d38a 135 continue;
136 }
137
138 // register_shutdown_function($pop3->quit());
139
140 $msglist = $pop3->uidl();
09e47788 141
d622d38a 142 $i = 1;
143 for ($j = 1; $j < sizeof($msglist); $j++) {
144 if ($msglist["$j"] == $mailfetch_uidl) {
145 $i = $j+1;
146 break;
147 }
148 }
149
150 if ($Count < $i) {
151 Mail_Fetch_Status(_("Login OK: No new messages"));
152 $pop3->quit();
153 continue;
154 }
155 if ($Count == 0) {
156 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
157 $pop3->quit();
158 continue;
159 } else {
160 $newmsgcount = $Count - $i + 1;
161 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
162 }
163
164 Mail_Fetch_Status(_("Fetching UIDL..."));
165 // Faster to get them all at once
166 $mailfetch_uidl = $pop3->uidl();
167
168 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
169 Mail_Fetch_Status(_("Server does not support UIDL."));
170
171 if ($mailfetch_lmos == 'on') {
172 Mail_Fetch_Status(_("Leaving Mail on Server..."));
173 } else {
174 Mail_Fetch_Status(_("Deleting messages from server..."));
175 }
176
177 for (; $i <= $Count; $i++) {
178 Mail_Fetch_Status(_("Fetching message ") . "$i" );
179 set_time_limit(20); // 20 seconds per message max
180 $Message = "";
181 $MessArray = $pop3->get($i);
182
09e47788 183 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
184 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
185 // re-connect pop3
186 Mail_Fetch_Status(_("Server error...Disconnect"));
187 $pop3->quit();
188 Mail_Fetch_Status(_("Re-connect from dead connectoin"));
189 if (!$pop3->connect($mailfetch_server)) {
190 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
191 Mail_Fetch_Status(_("Saving UIDL"));
192 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
193
194 continue;
195 }
196 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
197 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
198 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
199 Mail_Fetch_Status(_("Saving UIDL"));
200 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
201
202 continue;
203 }
204 Mail_Fetch_Status(_("Re-fetching message ") . "$i" );
205 $MessArray = $pop3->get($i);
206
207 } // end while
d622d38a 208
209 while (list($lineNum, $line) = each ($MessArray)) {
210 $Message .= $line;
211 }
212
213 if ($mailfetch_subfolder=="") {
214 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
215 } else {
216 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
217 }
218 $Line = fgets($imap_stream, 1024);
219 if (substr($Line, 0, 1) == '+') {
220 fputs($imap_stream, $Message);
221 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
09e47788 222 if ( $response <> 'OK' ) {
223 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
224 Mail_Fetch_Status(_("Closing POP"));
225 $pop3->quit();
226 Mail_Fetch_Status(_("Logging out from IMAP"));
227 sqimap_logout($imap_stream);
228
229 Mail_Fetch_Status(_("Saving UIDL"));
230 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
231 exit;
232 } else {
233 Mail_Fetch_Status(_("Message appended to mailbox"));
234 }
d622d38a 235
236 if ($mailfetch_lmos != 'on') {
237 if( $pop3->delete($i) ) {
238 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
239 } else {
240 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
241 }
242 }
243 } else {
244 echo "$Line";
245 Mail_Fetch_Status(_("Error Appending Message!"));
09e47788 246 Mail_Fetch_Status(_("Closing POP"));
247 $pop3->quit();
248 Mail_Fetch_Status(_("Logging out from IMAP"));
249 sqimap_logout($imap_stream);
250
251 // not gurantee corect!
252 Mail_Fetch_Status(_("Saving UIDL"));
253 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
254 exit;
d622d38a 255 }
256 }
257
258 Mail_Fetch_Status(_("Closing POP"));
259 $pop3->quit();
260 Mail_Fetch_Status(_("Logging out from IMAP"));
261 sqimap_logout($imap_stream);
262 if (is_array($mailfetch_uidl)) {
263 Mail_Fetch_Status(_("Saving UIDL"));
264 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
265 }
266
267 Mail_Fetch_Status(_("Done"));
268
269 }
270
271?>
272</center>
273</body>
15e6162e 274</html>