1ac19ee54ddc899ee7f70bf1228b1f10eed25907
[squirrelmail.git] / plugins / mail_fetch / fetch.php
1 <?php
2
3 /**
4 * mail_fetch/fetch.php
5 *
6 * Copyright (c) 1999-2003 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 define('SM_PATH','../../');
15
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' );
23
24 /* globals */
25 sqgetGlobalVar('username', $username, SQ_SESSION);
26 sqgetGlobalVar('key', $key, SQ_COOKIE);
27 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
28 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
29 /* end globals */
30
31 function Mail_Fetch_Status($msg) {
32 echo html_tag( 'table',
33 html_tag( 'tr',
34 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
35 ),
36 '', '', 'width="90%"' );
37 flush();
38 }
39
40 displayPageHeader($color, 'None');
41
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;
45 }
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];
53 }
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] );
62 }
63 if ($mailfetch_pass_[$i] == '') {
64 sqgetGlobalVar("pass_$i", $mailfetch_pass_[$i], SQ_POST);
65 }
66 }
67
68 echo '<br><center>';
69
70 echo html_tag( 'table',
71 html_tag( 'tr',
72 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
73 ) ,
74 'center', '', 'width="95%" cols="1"' );
75
76 if (!isset( $server_to_fetch ) ) {
77
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"' ) .
81 html_tag( 'tr' ) .
82 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', '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]) .
89 '</option>' . "\n";
90 }
91 echo '</select>' .
92 '</td>' .
93 '</tr>';
94
95 //if password not set, ask for it
96 for ($i = 0;$i < $mailfetch_server_number;$i++) {
97 if ($mailfetch_pass_[$i] == '') {
98 echo html_tag( 'tr',
99 html_tag( 'td', _("Password for") . ' <b>' .
100 htmlspecialchars($mailfetch_alias_[$i]) .
101 '</b>: &nbsp; &nbsp; ',
102 'right' ) .
103 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
104 );
105 }
106 }
107 echo html_tag( 'tr',
108 html_tag( 'td', '&nbsp;' ) .
109 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
110 ) .
111 '</table></form>';
112 exit();
113 }
114
115 if ( $server_to_fetch == 'all' ) {
116 $i_start = 0;
117 $i_stop = $mailfetch_server_number;
118 } else {
119 $i_start = $server_to_fetch;
120 $i_stop = $i_start+1;
121 }
122
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';
134 }
135
136 $pop3 = new POP3($mailfetch_server, 60);
137
138 echo '<br>' .
139 html_tag( 'table',
140 html_tag( 'tr',
141 html_tag( 'td', '<b>' . _("Fetching from ") .
142 htmlspecialchars($mailfetch_alias_[$i_loop]) .
143 '</b>',
144 'center' ) ,
145 '', $color[9] ) ,
146 '', '', 'width="90%"' );
147
148 flush();
149
150 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
151 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
152 continue;
153 }
154
155 Mail_Fetch_Status(_("Opening IMAP server"));
156 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
157
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 );
162 continue;
163 }
164
165 // register_shutdown_function($pop3->quit());
166
167 $msglist = $pop3->uidl();
168
169 $i = 1;
170 for ($j = 1; $j < sizeof($msglist); $j++) {
171 if ($msglist["$j"] == $mailfetch_uidl) {
172 $i = $j+1;
173 break;
174 }
175 }
176
177 if ($Count < $i) {
178 Mail_Fetch_Status(_("Login OK: No new messages"));
179 $pop3->quit();
180 continue;
181 }
182 if ($Count == 0) {
183 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
184 $pop3->quit();
185 continue;
186 } else {
187 $newmsgcount = $Count - $i + 1;
188 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
189 }
190
191 Mail_Fetch_Status(_("Fetching UIDL..."));
192 // Faster to get them all at once
193 $mailfetch_uidl = $pop3->uidl();
194
195 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
196 Mail_Fetch_Status(_("Server does not support UIDL."));
197
198 if ($mailfetch_lmos == 'on') {
199 Mail_Fetch_Status(_("Leaving Mail on Server..."));
200 } else {
201 Mail_Fetch_Status(_("Deleting messages from server..."));
202 }
203
204 for (; $i <= $Count; $i++) {
205 Mail_Fetch_Status(_("Fetching message ") . "$i" );
206 set_time_limit(20); // 20 seconds per message max
207 $Message = '';
208 $MessArray = $pop3->get($i);
209
210 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
211 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
212 // re-connect pop3
213 Mail_Fetch_Status(_("Server error...Disconnect"));
214 $pop3->quit();
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]);
220
221 continue;
222 }
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]);
228
229 continue;
230 }
231 Mail_Fetch_Status(_("Refetching message ") . "$i" );
232 $MessArray = $pop3->get($i);
233
234 } // end while
235
236 while (list($lineNum, $line) = each ($MessArray)) {
237 $Message .= $line;
238 }
239
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"));
249 $pop3->quit();
250 Mail_Fetch_Status(_("Logging out from IMAP"));
251 sqimap_logout($imap_stream);
252
253 Mail_Fetch_Status(_("Saving UIDL"));
254 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
255 exit;
256 } else {
257 Mail_Fetch_Status(_("Message appended to mailbox"));
258 }
259
260 if ($mailfetch_lmos != 'on') {
261 if( $pop3->delete($i) ) {
262 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
263 } else {
264 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
265 }
266 }
267 } else {
268 echo $Line;
269 Mail_Fetch_Status(_("Error Appending Message!"));
270 Mail_Fetch_Status(_("Closing POP"));
271 $pop3->quit();
272 Mail_Fetch_Status(_("Logging out from IMAP"));
273 sqimap_logout($imap_stream);
274
275 // not gurantee corect!
276 Mail_Fetch_Status(_("Saving UIDL"));
277 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
278 exit;
279 }
280 }
281
282 Mail_Fetch_Status(_("Closing POP"));
283 $pop3->quit();
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));
289 }
290
291 Mail_Fetch_Status(_("Done"));
292
293 }
294
295 ?>
296 </center>
297 </body>
298 </html>