utf-8 is already converted by ja_JP extra functions.
[squirrelmail.git] / plugins / mail_fetch / fetch.php
CommitLineData
d622d38a 1<?php
d622d38a 2
8d6a115b 3/**
4 * mail_fetch/fetch.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
8d6a115b 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Fetch code.
10 *
11 * $Id$
12 */
d3c89357 13
8d6a115b 14define('SM_PATH','../../');
d3c89357 15
8d6a115b 16require_once(SM_PATH . 'include/validate.php');
17require_once(SM_PATH . 'functions/page_header.php');
18require_once(SM_PATH . 'functions/imap.php');
19require_once(SM_PATH . 'include/load_prefs.php');
20require_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php');
8d6a115b 21require_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
22require_once(SM_PATH . 'functions/html.php' );
d3c89357 23
3c66c567 24/* globals */
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
d3c89357 31 function Mail_Fetch_Status($msg) {
4cf43843 32 echo html_tag( 'table',
33 html_tag( 'tr',
34 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
35 ),
36 '', '', 'width="90%"' );
d3c89357 37 flush();
38 }
d622d38a 39
9e56a6ad 40 function Mail_Fetch_Servers() {
760f848c 41 global $data_dir, $username;
42
9e56a6ad 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;
d622d38a 46 }
9e56a6ad 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 }
8b56b0d9 67 }
9e56a6ad 68 return $mailfetch;
d622d38a 69 }
8b56b0d9 70
9e56a6ad 71 function Mail_Fetch_Select_Server($mailfetch) {
760f848c 72 global $PHP_SELF;
73
4cf43843 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";
9e56a6ad 82 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
4cf43843 83 echo "<option value=\"$i\">" .
9e56a6ad 84 htmlspecialchars($mailfetch[$i]['alias']) .
4cf43843 85 '</option>' . "\n";
9e56a6ad 86 }
4cf43843 87 echo '</select>' .
88 '</td>' .
89 '</tr>';
8b56b0d9 90
d622d38a 91 //if password not set, ask for it
9e56a6ad 92 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
93 if ($mailfetch[$i]['pass'] == '') {
4cf43843 94 echo html_tag( 'tr',
95 html_tag( 'td', _("Password for") . ' <b>' .
9e56a6ad 96 htmlspecialchars($mailfetch[$i]['alias']) .
4cf43843 97 '</b>: &nbsp; &nbsp; ',
98 'right' ) .
18cdccda 99 html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
4cf43843 100 );
d622d38a 101 }
102 }
4cf43843 103 echo html_tag( 'tr',
104 html_tag( 'td', '&nbsp;' ) .
105 html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
e0f638ae 106 ) .
4cf43843 107 '</table></form>';
9e56a6ad 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
760f848c 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
25e2a90e 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) ) {
9e56a6ad 133 Mail_Fetch_Select_Server($mailfetch);
d622d38a 134 exit();
135 }
136
137 if ( $server_to_fetch == 'all' ) {
138 $i_start = 0;
9e56a6ad 139 $i_stop = $mailfetch['server_number'];
d622d38a 140 } else {
141 $i_start = $server_to_fetch;
8b56b0d9 142 $i_stop = $i_start+1;
d622d38a 143 }
8b56b0d9 144
d622d38a 145 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
9e56a6ad 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'];
8b56b0d9 154 if($mailfetch_subfolder == '') {
155 $mailfetch_subfolder == 'INBOX';
d622d38a 156 }
8b56b0d9 157
d622d38a 158 $pop3 = new POP3($mailfetch_server, 60);
8b56b0d9 159
4cf43843 160 echo '<br>' .
161 html_tag( 'table',
162 html_tag( 'tr',
163 html_tag( 'td', '<b>' . _("Fetching from ") .
9e56a6ad 164 htmlspecialchars($mailfetch[$i_loop]['alias']) .
4cf43843 165 '</b>',
166 'center' ) ,
167 '', $color[9] ) ,
168 '', '', 'width="90%"' );
8b56b0d9 169
d622d38a 170 flush();
8b56b0d9 171
e42b0dd2 172 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
d622d38a 173 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
174 continue;
175 }
8b56b0d9 176
d622d38a 177 Mail_Fetch_Status(_("Opening IMAP server"));
178 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
8b56b0d9 179
d622d38a 180 Mail_Fetch_Status(_("Opening POP server"));
181 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
182 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
d3c89357 183 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
d622d38a 184 continue;
185 }
8b56b0d9 186
d622d38a 187 // register_shutdown_function($pop3->quit());
8b56b0d9 188
d622d38a 189 $msglist = $pop3->uidl();
09e47788 190
d622d38a 191 $i = 1;
192 for ($j = 1; $j < sizeof($msglist); $j++) {
9e56a6ad 193 if ($msglist[$j] == $mailfetch_uidl) {
d622d38a 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 }
8b56b0d9 212
d622d38a 213 Mail_Fetch_Status(_("Fetching UIDL..."));
214 // Faster to get them all at once
215 $mailfetch_uidl = $pop3->uidl();
8b56b0d9 216
d622d38a 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 }
8b56b0d9 225
d622d38a 226 for (; $i <= $Count; $i++) {
227 Mail_Fetch_Status(_("Fetching message ") . "$i" );
228 set_time_limit(20); // 20 seconds per message max
8b56b0d9 229 $Message = '';
d622d38a 230 $MessArray = $pop3->get($i);
8b56b0d9 231
09e47788 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"));
8b56b0d9 236 $pop3->quit();
a3e3ddc4 237 Mail_Fetch_Status(_("Reconnect from dead connection"));
09e47788 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 }
a3e3ddc4 253 Mail_Fetch_Status(_("Refetching message ") . "$i" );
8b56b0d9 254 $MessArray = $pop3->get($i);
09e47788 255
256 } // end while
8b56b0d9 257
d622d38a 258 while (list($lineNum, $line) = each ($MessArray)) {
259 $Message .= $line;
260 }
8b56b0d9 261
262 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
d622d38a 263 $Line = fgets($imap_stream, 1024);
264 if (substr($Line, 0, 1) == '+') {
265 fputs($imap_stream, $Message);
8b56b0d9 266 fputs($imap_stream, "\r\n");
d622d38a 267 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
8b56b0d9 268 if ($response != 'OK') {
09e47788 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]);
8b56b0d9 277 exit;
09e47788 278 } else {
279 Mail_Fetch_Status(_("Message appended to mailbox"));
280 }
8b56b0d9 281
d622d38a 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 {
8b56b0d9 290 echo $Line;
d622d38a 291 Mail_Fetch_Status(_("Error Appending Message!"));
09e47788 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;
d622d38a 301 }
302 }
8b56b0d9 303
d622d38a 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 }
8b56b0d9 312
d622d38a 313 Mail_Fetch_Status(_("Done"));
314
315 }
316
317?>
318</center>
319</body>
15e6162e 320</html>