03c9211b52b7f9ea6333999c98ada8651301463a
[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 function Mail_Fetch_Servers() {
41 global $data_dir, $username;
42
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;
46 }
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']);
56 }
57 if ($mailfetch[$i]['pass'] == '') {
58 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
59 }
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'];
66 }
67 }
68 return $mailfetch;
69 }
70
71 function Mail_Fetch_Select_Server($mailfetch) {
72 global $PHP_SELF;
73
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"' ) .
77 html_tag( 'tr' ) .
78 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', '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']) .
85 '</option>' . "\n";
86 }
87 echo '</select>' .
88 '</td>' .
89 '</tr>';
90
91 //if password not set, ask for it
92 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
93 if ($mailfetch[$i]['pass'] == '') {
94 echo html_tag( 'tr',
95 html_tag( 'td', _("Password for") . ' <b>' .
96 htmlspecialchars($mailfetch[$i]['alias']) .
97 '</b>: &nbsp; &nbsp; ',
98 'right' ) .
99 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
100 );
101 }
102 }
103 echo html_tag( 'tr',
104 html_tag( 'td', '&nbsp;' ) .
105 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
106 ) .
107 '</table></form>';
108 }
109
110 $mailfetch = Mail_Fetch_Servers();
111 displayPageHeader($color, 'None');
112
113 echo '<br><center>';
114
115 echo html_tag( 'table',
116 html_tag( 'tr',
117 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
118 ) ,
119 'center', '', 'width="95%" cols="1"' );
120
121
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>';
128 exit();
129 }
130
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);
134 exit();
135 }
136
137 if ( $server_to_fetch == 'all' ) {
138 $i_start = 0;
139 $i_stop = $mailfetch['server_number'];
140 } else {
141 $i_start = $server_to_fetch;
142 $i_stop = $i_start+1;
143 }
144
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';
156 }
157
158 $pop3 = new POP3($mailfetch_server, 60);
159
160 echo '<br>' .
161 html_tag( 'table',
162 html_tag( 'tr',
163 html_tag( 'td', '<b>' . _("Fetching from ") .
164 htmlspecialchars($mailfetch[$i_loop]['alias']) .
165 '</b>',
166 'center' ) ,
167 '', $color[9] ) ,
168 '', '', 'width="90%"' );
169
170 flush();
171
172 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
173 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
174 continue;
175 }
176
177 Mail_Fetch_Status(_("Opening IMAP server"));
178 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
179
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 );
184 continue;
185 }
186
187 // register_shutdown_function($pop3->quit());
188
189 $msglist = $pop3->uidl();
190
191 $i = 1;
192 for ($j = 1; $j < sizeof($msglist); $j++) {
193 if ($msglist[$j] == $mailfetch_uidl) {
194 $i = $j+1;
195 break;
196 }
197 }
198
199 if ($Count < $i) {
200 Mail_Fetch_Status(_("Login OK: No new messages"));
201 $pop3->quit();
202 continue;
203 }
204 if ($Count == 0) {
205 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
206 $pop3->quit();
207 continue;
208 } else {
209 $newmsgcount = $Count - $i + 1;
210 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
211 }
212
213 Mail_Fetch_Status(_("Fetching UIDL..."));
214 // Faster to get them all at once
215 $mailfetch_uidl = $pop3->uidl();
216
217 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
218 Mail_Fetch_Status(_("Server does not support UIDL."));
219
220 if ($mailfetch_lmos == 'on') {
221 Mail_Fetch_Status(_("Leaving Mail on Server..."));
222 } else {
223 Mail_Fetch_Status(_("Deleting messages from server..."));
224 }
225
226 for (; $i <= $Count; $i++) {
227 Mail_Fetch_Status(_("Fetching message ") . "$i" );
228 set_time_limit(20); // 20 seconds per message max
229 $Message = '';
230 $MessArray = $pop3->get($i);
231
232 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
233 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
234 // re-connect pop3
235 Mail_Fetch_Status(_("Server error...Disconnect"));
236 $pop3->quit();
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]);
242
243 continue;
244 }
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]);
250
251 continue;
252 }
253 Mail_Fetch_Status(_("Refetching message ") . "$i" );
254 $MessArray = $pop3->get($i);
255
256 } // end while
257
258 while (list($lineNum, $line) = each ($MessArray)) {
259 $Message .= $line;
260 }
261
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"));
271 $pop3->quit();
272 Mail_Fetch_Status(_("Logging out from IMAP"));
273 sqimap_logout($imap_stream);
274
275 Mail_Fetch_Status(_("Saving UIDL"));
276 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
277 exit;
278 } else {
279 Mail_Fetch_Status(_("Message appended to mailbox"));
280 }
281
282 if ($mailfetch_lmos != 'on') {
283 if( $pop3->delete($i) ) {
284 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
285 } else {
286 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
287 }
288 }
289 } else {
290 echo $Line;
291 Mail_Fetch_Status(_("Error Appending Message!"));
292 Mail_Fetch_Status(_("Closing POP"));
293 $pop3->quit();
294 Mail_Fetch_Status(_("Logging out from IMAP"));
295 sqimap_logout($imap_stream);
296
297 // not gurantee corect!
298 Mail_Fetch_Status(_("Saving UIDL"));
299 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
300 exit;
301 }
302 }
303
304 Mail_Fetch_Status(_("Closing POP"));
305 $pop3->quit();
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));
311 }
312
313 Mail_Fetch_Status(_("Done"));
314
315 }
316
317 ?>
318 </center>
319 </body>
320 </html>