Fix for security token missing in newmail plugin (#2919418).
[squirrelmail.git] / plugins / mail_fetch / fetch.php
... / ...
CommitLineData
1<?php
2
3/**
4 * mail_fetch/fetch.php
5 *
6 * Fetch code.
7 *
8 * @copyright 1999-2009 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package plugins
12 * @subpackage mail_fetch
13 */
14
15/**
16 * Include the SquirrelMail initialization file.
17 */
18require('../../include/init.php');
19
20include_once(SM_PATH . 'functions/imap_general.php');
21include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
22
23// don't load this page if this plugin is not enabled
24//
25global $plugins;
26if (!in_array('mail_fetch', $plugins)) exit;
27
28/* globals */
29sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
30/* end globals */
31
32/**
33 * @param string $msg message
34 */
35function Mail_Fetch_Status($msg) {
36 echo html_tag( 'table',
37 html_tag( 'tr',
38 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
39 ),
40 '', '', 'width="90%"' );
41 flush();
42}
43
44/**
45 * @return array
46 */
47function Mail_Fetch_Servers() {
48 global $data_dir, $username;
49
50 $mailfetch = array();
51 $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
52 if (!isset($mailfetch['server_number']) || ($mailfetch['server_number'] < 1)) {
53 $mailfetch['server_number'] = 0;
54 }
55 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
56
57 for ($i = 0; $i < $mailfetch['server_number']; $i++) {
58 $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
59 $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i");
60 $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i");
61 $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i");
62 $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i");
63 if($mailfetch['cypher'] == 'on') {
64 $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
65 }
66 if ($mailfetch[$i]['pass'] == '') {
67 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
68 }
69 $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i");
70 $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i");
71 $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i");
72 $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
73 if($mailfetch[$i]['alias'] == '') {
74 $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
75 }
76 // Authentication type (added in 1.5.2)
77 $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER);
78 // Connection type (added in 1.5.2)
79 $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN);
80 }
81 return $mailfetch;
82}
83
84/**
85 * @param array $mailfetch
86 */
87function Mail_Fetch_Select_Server($mailfetch) {
88 global $PHP_SELF;
89
90 echo '<font size="-5"><br /></font>' .
91 '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
92 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
93 html_tag( 'tr' ) .
94 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', 'right' ) .
95 html_tag( 'td', '', 'left' ) .
96 '<select name="server_to_fetch" size="1">' .
97 '<option value="all" selected="selected">..' . _("All") . "...\n";
98 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
99 echo "<option value=\"$i\">" .
100 htmlspecialchars($mailfetch[$i]['alias']) .
101 '</option>' . "\n";
102 }
103 echo '</select>' .
104 '</td>' .
105 '</tr>';
106
107 //if password not set, ask for it
108 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
109 if ($mailfetch[$i]['pass'] == '') {
110 echo html_tag( 'tr',
111 html_tag( 'td', _("Password for") . ' <b>' .
112 htmlspecialchars($mailfetch[$i]['alias']) .
113 '</b>: &nbsp; &nbsp; ',
114 'right' ) .
115 html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
116 );
117 }
118 }
119 echo html_tag( 'tr',
120 html_tag( 'td', '&nbsp;' ) .
121 html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
122 ) .
123 '</table></form>';
124}
125
126$mailfetch = Mail_Fetch_Servers();
127displayPageHeader($color);
128
129echo '<br />';
130
131echo html_tag( 'table',
132 html_tag( 'tr',
133 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
134 ) ,
135 'center', '', 'width="95%" cols="1"' );
136
137
138/* there are no servers defined yet... */
139if($mailfetch['server_number'] == 0) {
140//FIXME: do not echo directly to browser -- use templates only
141 echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
142 echo makeInternalLink('plugins/mail_fetch/options.php',
143 _("Click here to go to the options page.") );
144 $oTemplate->display('footer.tpl');
145 exit();
146}
147
148// get $server_to_fetch from globals, if not set display a choice to the user
149if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
150 Mail_Fetch_Select_Server($mailfetch);
151 $oTemplate->display('footer.tpl');
152 exit();
153}
154
155if ( $server_to_fetch == 'all' ) {
156 $i_start = 0;
157 $i_stop = $mailfetch['server_number'];
158} else {
159 $i_start = $server_to_fetch;
160 $i_stop = $i_start+1;
161}
162
163for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
164 $mailfetch_server = $mailfetch[$i_loop]['server'];
165 $mailfetch_port = $mailfetch[$i_loop]['port'];
166 $mailfetch_user = $mailfetch[$i_loop]['user'];
167 $mailfetch_pass = $mailfetch[$i_loop]['pass'];
168 $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
169 $mailfetch_login = $mailfetch[$i_loop]['login'];
170 $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
171 $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
172 $mailfetch_auth = $mailfetch[$i_loop]['auth'];
173 $mailfetch_type = $mailfetch[$i_loop]['type'];
174
175 echo '<br />' .
176 html_tag( 'table',
177 html_tag( 'tr',
178 html_tag( 'td', '<b>' .
179 sprintf(_("Fetching from %s"),
180 htmlspecialchars($mailfetch[$i_loop]['alias'])) .
181 '</b>',
182 'center' ) ,
183 '', $color[9] ) ,
184 '', '', 'width="90%"' );
185
186 flush();
187
188 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
189 'port' => $mailfetch_port,
190 'auth' => $mailfetch_auth,
191 'tls' => $mailfetch_type,
192 'timeout' => 60));
193
194 if (!empty($pop3->error)) {
195 Mail_Fetch_Status($pop3->error);
196 continue;
197 }
198
199 Mail_Fetch_Status(_("Opening IMAP server"));
200 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
201
202 // check if destination folder is not set, is not subscribed and is not \noselect folder
203 if($mailfetch_subfolder == '' ||
204 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
205 $mailfetch_subfolder = 'INBOX';
206 }
207
208 Mail_Fetch_Status(_("Opening POP server"));
209
210 /* log into pop server*/
211 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
212 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
213 continue;
214 }
215
216 $aMsgStat = $pop3->command_stat();
217 if (is_bool($aMsgStat)) {
218 Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
219 continue;
220 }
221
222 $Count = $aMsgStat['count'];
223
224 $i = 1;
225
226 if ($Count>0) {
227 // If we leave messages on server, try using UIDL
228 if ($mailfetch_lmos == 'on') {
229 Mail_Fetch_Status(_("Fetching UIDL..."));
230 $msglist = $pop3->command_uidl();
231 if (is_bool($msglist)) {
232 Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error));
233 // User asked to leave messages on server, but we can't do that.
234 $pop3->command_quit();
235 continue;
236 // $mailfetch_lmos = 'off';
237 } else {
238 // calculate number of new messages
239 for ($j = 1; $j <= sizeof($msglist); $j++) {
240 // do strict comparison ('1111.10' should not be equal to '1111.100')
241 if ($msglist[$j] === $mailfetch_uidl) {
242 $i = $j+1;
243 break;
244 }
245 }
246 }
247 }
248 // fetch list of messages with LIST
249 // we can use else control, but we can also set $mailfetch_lmos
250 // to off if server does not support UIDL.
251 if ($mailfetch_lmos != 'on') {
252 Mail_Fetch_Status(_("Fetching list of messages..."));
253 $msglist = $pop3->command_list();
254 }
255 }
256
257 if ($Count < $i) {
258 Mail_Fetch_Status(_("Login OK: No new messages"));
259 $pop3->command_quit();
260 continue;
261 }
262 if ($Count == 0) {
263 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
264 $pop3->command_quit();
265 continue;
266 } else {
267 $newmsgcount = $Count - $i + 1;
268 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
269 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
270 }
271
272 if ($mailfetch_lmos == 'on') {
273 Mail_Fetch_Status(_("Leaving messages on server..."));
274 } else {
275 Mail_Fetch_Status(_("Deleting messages from server..."));
276 }
277
278 for (; $i <= $Count; $i++) {
279 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
280
281 if (!ini_get('safe_mode'))
282 set_time_limit(20); // 20 seconds per message max
283
284 $Message = $pop3->command_retr($i);
285
286 if (is_bool($Message)) {
287 Mail_Fetch_Status(htmlspecialchars($pop3->error));
288 continue;
289 }
290
291 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
292 $Line = fgets($imap_stream, 1024);
293 if (substr($Line, 0, 1) == '+') {
294 fputs($imap_stream, $Message);
295 fputs($imap_stream, "\r\n");
296 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
297 $response=(implode('',$response));
298 $message=(implode('',$message));
299 if ($response != 'OK') {
300 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
301 Mail_Fetch_Status(_("Closing POP"));
302 $pop3->command_quit();
303 Mail_Fetch_Status(_("Logging out from IMAP"));
304 sqimap_logout($imap_stream);
305
306 if ($mailfetch_lmos == 'on') {
307 Mail_Fetch_Status(_("Saving UIDL"));
308 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
309 }
310 exit;
311 } else {
312 Mail_Fetch_Status(_("Message appended to mailbox"));
313 }
314
315 if ($mailfetch_lmos != 'on') {
316 if( $pop3->command_dele($i) ) {
317 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
318 } else {
319 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
320 }
321 }
322 } else {
323 echo $Line;
324 Mail_Fetch_Status(_("Error Appending Message!"));
325 Mail_Fetch_Status(_("Closing POP"));
326 $pop3->command_quit();
327 Mail_Fetch_Status(_("Logging out from IMAP"));
328 sqimap_logout($imap_stream);
329
330 // not gurantee corect!
331 if ($mailfetch_lmos == 'on') {
332 Mail_Fetch_Status(_("Saving UIDL"));
333 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
334 }
335 exit;
336 }
337 }
338
339 Mail_Fetch_Status(_("Closing POP"));
340 $pop3->command_quit();
341 Mail_Fetch_Status(_("Logging out from IMAP"));
342 sqimap_logout($imap_stream);
343 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
344 Mail_Fetch_Status(_("Saving UIDL"));
345 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
346 }
347
348 Mail_Fetch_Status(_("Done"));
349}
350
351$oTemplate->display('footer.tpl');