2510b6cb36a5d3516430e8b7e8913e5831e10133
[squirrelmail.git] / plugins / mail_fetch / fetch.php
1 <?php
2
3 /**
4 ** mail_fetch/fetch.php
5 **
6 ** Copyright (c) 1999-2002 The SquirrelMail Project Team
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 require_once( '../functions/html.php' );
23
24
25 function Mail_Fetch_Status($msg) {
26 echo html_tag( 'table',
27 html_tag( 'tr',
28 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
29 ),
30 '', '', 'width="90%"' );
31 flush();
32 }
33
34 displayPageHeader($color, 'None');
35
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] );
51 }
52 }
53
54 echo '<br><center>';
55
56 echo html_tag( 'table',
57 html_tag( 'tr',
58 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
59 ) ,
60 'center', '', 'width="95%" cols="1"' );
61
62 if (!isset( $server_to_fetch ) ) {
63
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"' ) .
67 html_tag( 'tr' ) .
68 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', '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]) .
75 '</option>' . "\n";
76 }
77 echo '</select>' .
78 '</td>' .
79 '</tr>';
80
81 //if password not set, ask for it
82 for ($i=0;$i<$mailfetch_server_number;$i++) {
83 if ($mailfetch_pass_[$i]=='') {
84 echo html_tag( 'tr',
85 html_tag( 'td', _("Password for") . ' <b>' .
86 (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) .
87 '</b>: &nbsp; &nbsp; ',
88 'right' ) .
89 html_tag( 'td', '<input type="password" name="pass_' . $i , '">', 'left' )
90 );
91 }
92 }
93 echo html_tag( 'tr',
94 html_tag( 'td', '&nbsp;' ) .
95 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
96 );
97
98 '</table></form>';
99 exit();
100 }
101
102 if ( $server_to_fetch == 'all' ) {
103 $i_start = 0;
104 $i_stop = $mailfetch_server_number;
105 } else {
106 $i_start = $server_to_fetch;
107 $i_stop = $i_start+1;
108 }
109
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]=="") {
114 $tmp="pass_$i_loop";
115 $mailfetch_pass=$$tmp;
116 } else {
117 $mailfetch_pass=$mailfetch_pass_[$i_loop];
118 }
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];
123
124
125 $pop3 = new POP3($mailfetch_server, 60);
126
127 echo '<br>' .
128 html_tag( 'table',
129 html_tag( 'tr',
130 html_tag( 'td', '<b>' . _("Fetching from ") .
131 (($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop]) .
132 '</b>',
133 'center' ) ,
134 '', $color[9] ) ,
135 '', '', 'width="90%"' );
136
137 flush();
138
139 if (!$pop3->connect($mailfetch_server)) {
140 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
141 continue;
142 }
143
144 Mail_Fetch_Status(_("Opening IMAP server"));
145 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
146
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 );
151 continue;
152 }
153
154 // register_shutdown_function($pop3->quit());
155
156 $msglist = $pop3->uidl();
157
158 $i = 1;
159 for ($j = 1; $j < sizeof($msglist); $j++) {
160 if ($msglist["$j"] == $mailfetch_uidl) {
161 $i = $j+1;
162 break;
163 }
164 }
165
166 if ($Count < $i) {
167 Mail_Fetch_Status(_("Login OK: No new messages"));
168 $pop3->quit();
169 continue;
170 }
171 if ($Count == 0) {
172 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
173 $pop3->quit();
174 continue;
175 } else {
176 $newmsgcount = $Count - $i + 1;
177 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
178 }
179
180 Mail_Fetch_Status(_("Fetching UIDL..."));
181 // Faster to get them all at once
182 $mailfetch_uidl = $pop3->uidl();
183
184 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
185 Mail_Fetch_Status(_("Server does not support UIDL."));
186
187 if ($mailfetch_lmos == 'on') {
188 Mail_Fetch_Status(_("Leaving Mail on Server..."));
189 } else {
190 Mail_Fetch_Status(_("Deleting messages from server..."));
191 }
192
193 for (; $i <= $Count; $i++) {
194 Mail_Fetch_Status(_("Fetching message ") . "$i" );
195 set_time_limit(20); // 20 seconds per message max
196 $Message = "";
197 $MessArray = $pop3->get($i);
198
199 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
200 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
201 // re-connect pop3
202 Mail_Fetch_Status(_("Server error...Disconnect"));
203 $pop3->quit();
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]);
209
210 continue;
211 }
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]);
217
218 continue;
219 }
220 Mail_Fetch_Status(_("Re-fetching message ") . "$i" );
221 $MessArray = $pop3->get($i);
222
223 } // end while
224
225 while (list($lineNum, $line) = each ($MessArray)) {
226 $Message .= $line;
227 }
228
229 if ($mailfetch_subfolder=="") {
230 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
231 } else {
232 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
233 }
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"));
241 $pop3->quit();
242 Mail_Fetch_Status(_("Logging out from IMAP"));
243 sqimap_logout($imap_stream);
244
245 Mail_Fetch_Status(_("Saving UIDL"));
246 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
247 exit;
248 } else {
249 Mail_Fetch_Status(_("Message appended to mailbox"));
250 }
251
252 if ($mailfetch_lmos != 'on') {
253 if( $pop3->delete($i) ) {
254 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
255 } else {
256 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
257 }
258 }
259 } else {
260 echo "$Line";
261 Mail_Fetch_Status(_("Error Appending Message!"));
262 Mail_Fetch_Status(_("Closing POP"));
263 $pop3->quit();
264 Mail_Fetch_Status(_("Logging out from IMAP"));
265 sqimap_logout($imap_stream);
266
267 // not gurantee corect!
268 Mail_Fetch_Status(_("Saving UIDL"));
269 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
270 exit;
271 }
272 }
273
274 Mail_Fetch_Status(_("Closing POP"));
275 $pop3->quit();
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));
281 }
282
283 Mail_Fetch_Status(_("Done"));
284
285 }
286
287 ?>
288 </center>
289 </body>
290 </html>