removing includes that are loaded by validate.php.
[squirrelmail.git] / plugins / mail_fetch / fetch.php
CommitLineData
d622d38a 1<?php
d622d38a 2
8d6a115b 3/**
4 * mail_fetch/fetch.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
8d6a115b 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Fetch code.
10 *
4f51df66 11 * @version $Id$
ea5f4b8e 12 * @package plugins
13 * @subpackage mail_fetch
8d6a115b 14 */
d3c89357 15
ea5f4b8e 16/** @ignore */
8d6a115b 17define('SM_PATH','../../');
d3c89357 18
8d6a115b 19require_once(SM_PATH . 'include/validate.php');
5c89bd63 20include_once(SM_PATH . 'functions/imap.php');
21include_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php');
22include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
d3c89357 23
91e0dccc 24/* globals */
3c66c567 25sqgetGlobalVar('username', $username, SQ_SESSION);
26sqgetGlobalVar('key', $key, SQ_COOKIE);
27sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
28sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
3c66c567 29/* end globals */
86bb8549 30
5c89bd63 31function 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
40function 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;
d3c89357 46 }
5c89bd63 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']);
d622d38a 56 }
5c89bd63 57 if ($mailfetch[$i]['pass'] == '') {
58 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
91e0dccc 59 }
5c89bd63 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'];
d622d38a 66 }
9e56a6ad 67 }
5c89bd63 68 return $mailfetch;
69}
70
71function 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="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";
d622d38a 86 }
5c89bd63 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 }
d622d38a 102 }
5c89bd63 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();
111displayPageHeader($color, 'None');
112
113echo '<br /><center>';
114
115echo 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... */
123if($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
132if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
133 Mail_Fetch_Select_Server($mailfetch);
134 exit();
135}
136
137if ( $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
145for ($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
155 $pop3 = new POP3($mailfetch_server, 60);
156
157 echo '<br />' .
4cf43843 158 html_tag( 'table',
159 html_tag( 'tr',
91e0dccc 160 html_tag( 'td', '<b>' . _("Fetching from ") .
161 htmlspecialchars($mailfetch[$i_loop]['alias']) .
4cf43843 162 '</b>',
163 'center' ) ,
164 '', $color[9] ) ,
165 '', '', 'width="90%"' );
8b56b0d9 166
5c89bd63 167 flush();
8b56b0d9 168
5c89bd63 169 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
170 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
171 continue;
172 }
8b56b0d9 173
5c89bd63 174 Mail_Fetch_Status(_("Opening IMAP server"));
175 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
8b56b0d9 176
5c89bd63 177 // check if destination folder is not set, is not subscribed and is not \noselect folder
178 if($mailfetch_subfolder == '' ||
179 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
180 $mailfetch_subfolder = 'INBOX';
181 }
5f438206 182
5c89bd63 183 Mail_Fetch_Status(_("Opening POP server"));
184 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
185 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
186 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) );
187 continue;
188 }
8b56b0d9 189
5c89bd63 190 // register_shutdown_function($pop3->quit());
8b56b0d9 191
5c89bd63 192 $msglist = $pop3->uidl();
09e47788 193
5c89bd63 194 $i = 1;
195 for ($j = 1; $j < sizeof($msglist); $j++) {
196 if ($msglist[$j] == $mailfetch_uidl) {
197 $i = $j+1;
198 break;
d622d38a 199 }
5c89bd63 200 }
d622d38a 201
5c89bd63 202 if ($Count < $i) {
203 Mail_Fetch_Status(_("Login OK: No new messages"));
204 $pop3->quit();
205 continue;
206 }
207 if ($Count == 0) {
208 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
209 $pop3->quit();
210 continue;
211 } else {
212 $newmsgcount = $Count - $i + 1;
213 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
214 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
215 }
8b56b0d9 216
5c89bd63 217 Mail_Fetch_Status(_("Fetching UIDL..."));
218 // Faster to get them all at once
219 $mailfetch_uidl = $pop3->uidl();
8b56b0d9 220
5c89bd63 221 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
222 Mail_Fetch_Status(_("Server does not support UIDL."));
d622d38a 223
5c89bd63 224 if ($mailfetch_lmos == 'on') {
225 Mail_Fetch_Status(_("Leaving mail on server..."));
226 } else {
227 Mail_Fetch_Status(_("Deleting messages from server..."));
228 }
8b56b0d9 229
5c89bd63 230 for (; $i <= $Count; $i++) {
231 Mail_Fetch_Status(_("Fetching message ") . "$i" );
05b06d34 232
5c89bd63 233 if (!ini_get('safe_mode'))
234 set_time_limit(20); // 20 seconds per message max
235 $Message = '';
236 $MessArray = $pop3->get($i);
8b56b0d9 237
5c89bd63 238 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
239 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
240 // re-connect pop3
241 Mail_Fetch_Status(_("Server error. Disconnect"));
242 $pop3->quit();
243 Mail_Fetch_Status(_("Reconnect from dead connection"));
244 if (!$pop3->connect($mailfetch_server)) {
245 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
246 Mail_Fetch_Status(_("Saving UIDL"));
247 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
248
249 continue;
d622d38a 250 }
5c89bd63 251 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
252 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
253 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
254 Mail_Fetch_Status(_("Saving UIDL"));
255 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
8b56b0d9 256
5c89bd63 257 continue;
258 }
259 Mail_Fetch_Status(_("Refetching message ") . "$i" );
260 $MessArray = $pop3->get($i);
8b56b0d9 261
5c89bd63 262 } // end while
263
264 while (list($lineNum, $line) = each ($MessArray)) {
265 $Message .= $line;
266 }
267
268 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
269 $Line = fgets($imap_stream, 1024);
270 if (substr($Line, 0, 1) == '+') {
271 fputs($imap_stream, $Message);
272 fputs($imap_stream, "\r\n");
273 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
274 $response=(implode('',$response));
275 $message=(implode('',$message));
276 if ($response != 'OK') {
277 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
09e47788 278 Mail_Fetch_Status(_("Closing POP"));
279 $pop3->quit();
280 Mail_Fetch_Status(_("Logging out from IMAP"));
281 sqimap_logout($imap_stream);
282
09e47788 283 Mail_Fetch_Status(_("Saving UIDL"));
284 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
285 exit;
5c89bd63 286 } else {
287 Mail_Fetch_Status(_("Message appended to mailbox"));
d622d38a 288 }
8b56b0d9 289
5c89bd63 290 if ($mailfetch_lmos != 'on') {
291 if( $pop3->delete($i) ) {
292 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
293 } else {
294 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) );
295 }
296 }
297 } else {
298 echo $Line;
299 Mail_Fetch_Status(_("Error Appending Message!"));
300 Mail_Fetch_Status(_("Closing POP"));
301 $pop3->quit();
302 Mail_Fetch_Status(_("Logging out from IMAP"));
303 sqimap_logout($imap_stream);
304
305 // not gurantee corect!
d622d38a 306 Mail_Fetch_Status(_("Saving UIDL"));
5c89bd63 307 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
308 exit;
d622d38a 309 }
5c89bd63 310 }
8b56b0d9 311
5c89bd63 312 Mail_Fetch_Status(_("Closing POP"));
313 $pop3->quit();
314 Mail_Fetch_Status(_("Logging out from IMAP"));
315 sqimap_logout($imap_stream);
316 if (is_array($mailfetch_uidl)) {
317 Mail_Fetch_Status(_("Saving UIDL"));
318 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
319 }
d622d38a 320
5c89bd63 321 Mail_Fetch_Status(_("Done"));
322}
d622d38a 323?>
324</center>
dcc1cc82 325</body>
4f51df66 326</html>