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