Make sure the error handler is setup, and that icons are available to the template.
[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 *
47ccfad4 8 * @copyright &copy; 1999-2006 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/class.POP3.php');
22include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
d3c89357 23
91e0dccc 24/* globals */
3c66c567 25sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
3c66c567 26/* end globals */
86bb8549 27
5c89bd63 28function Mail_Fetch_Status($msg) {
29 echo html_tag( 'table',
30 html_tag( 'tr',
31 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
32 ),
33 '', '', 'width="90%"' );
34 flush();
35}
36
37function Mail_Fetch_Servers() {
38 global $data_dir, $username;
39
40 $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
41 if (!isset($mailfetch['server_number']) || ($mailfetch['server_number'] < 1)) {
42 $mailfetch['server_number'] = 0;
d3c89357 43 }
5c89bd63 44 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
45 for ($i = 0; $i < $mailfetch['server_number']; $i++) {
46 $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
47 $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i");
48 $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i");
49 $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i");
50 $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i");
51 if($mailfetch['cypher'] == 'on') {
52 $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
d622d38a 53 }
5c89bd63 54 if ($mailfetch[$i]['pass'] == '') {
55 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
91e0dccc 56 }
5c89bd63 57 $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i");
58 $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i");
59 $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i");
60 $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
61 if($mailfetch[$i]['alias'] == '') {
62 $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
d622d38a 63 }
9e56a6ad 64 }
5c89bd63 65 return $mailfetch;
66}
67
68function Mail_Fetch_Select_Server($mailfetch) {
69 global $PHP_SELF;
70
71 echo '<font size="-5"><br /></font>' .
72 '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
73 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
74 html_tag( 'tr' ) .
75 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', 'right' ) .
76 html_tag( 'td', '', 'left' ) .
77 '<select name="server_to_fetch" size="1">' .
78 '<option value="all" selected="selected">..' . _("All") . "...\n";
79 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
80 echo "<option value=\"$i\">" .
81 htmlspecialchars($mailfetch[$i]['alias']) .
82 '</option>' . "\n";
d622d38a 83 }
5c89bd63 84 echo '</select>' .
85 '</td>' .
86 '</tr>';
87
88 //if password not set, ask for it
89 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
90 if ($mailfetch[$i]['pass'] == '') {
91 echo html_tag( 'tr',
92 html_tag( 'td', _("Password for") . ' <b>' .
93 htmlspecialchars($mailfetch[$i]['alias']) .
94 '</b>: &nbsp; &nbsp; ',
95 'right' ) .
96 html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
97 );
98 }
d622d38a 99 }
5c89bd63 100 echo html_tag( 'tr',
101 html_tag( 'td', '&nbsp;' ) .
102 html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
103 ) .
104 '</table></form>';
105}
106
107$mailfetch = Mail_Fetch_Servers();
108displayPageHeader($color, 'None');
109
f265009a 110echo '<br /><div style="text-align: center;">';
5c89bd63 111
112echo html_tag( 'table',
113 html_tag( 'tr',
114 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
115 ) ,
116 'center', '', 'width="95%" cols="1"' );
117
118
119/* there are no servers defined yet... */
120if($mailfetch['server_number'] == 0) {
121 echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
122 displayInternalLink('plugins/mail_fetch/options.php',
123 _("Click here to go to the options page.") );
124 echo '</body></html>';
125 exit();
126}
127
128// get $server_to_fetch from globals, if not set display a choice to the user
129if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
130 Mail_Fetch_Select_Server($mailfetch);
131 exit();
132}
133
134if ( $server_to_fetch == 'all' ) {
135 $i_start = 0;
136 $i_stop = $mailfetch['server_number'];
137} else {
138 $i_start = $server_to_fetch;
139 $i_stop = $i_start+1;
140}
141
142for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
143 $mailfetch_server = $mailfetch[$i_loop]['server'];
144 $mailfetch_port = $mailfetch[$i_loop]['port'];
145 $mailfetch_user = $mailfetch[$i_loop]['user'];
146 $mailfetch_pass = $mailfetch[$i_loop]['pass'];
147 $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
148 $mailfetch_login = $mailfetch[$i_loop]['login'];
149 $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
150 $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
151
152 $pop3 = new POP3($mailfetch_server, 60);
153
154 echo '<br />' .
4cf43843 155 html_tag( 'table',
156 html_tag( 'tr',
3c021d16 157 html_tag( 'td', '<b>' .
158 sprintf(_("Fetching from %s"),
159 htmlspecialchars($mailfetch[$i_loop]['alias'])) .
4cf43843 160 '</b>',
161 'center' ) ,
162 '', $color[9] ) ,
163 '', '', 'width="90%"' );
8b56b0d9 164
5c89bd63 165 flush();
8b56b0d9 166
5c89bd63 167 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
3c021d16 168 Mail_Fetch_Status($pop3->ERROR );
5c89bd63 169 continue;
170 }
8b56b0d9 171
5c89bd63 172 Mail_Fetch_Status(_("Opening IMAP server"));
2128bbc6 173 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
8b56b0d9 174
5c89bd63 175 // check if destination folder is not set, is not subscribed and is not \noselect folder
176 if($mailfetch_subfolder == '' ||
177 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
178 $mailfetch_subfolder = 'INBOX';
179 }
5f438206 180
5c89bd63 181 Mail_Fetch_Status(_("Opening POP server"));
182 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
183 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
184 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) );
185 continue;
186 }
8b56b0d9 187
5c89bd63 188 // register_shutdown_function($pop3->quit());
8b56b0d9 189
5c89bd63 190 $msglist = $pop3->uidl();
09e47788 191
5c89bd63 192 $i = 1;
193 for ($j = 1; $j < sizeof($msglist); $j++) {
194 if ($msglist[$j] == $mailfetch_uidl) {
195 $i = $j+1;
196 break;
d622d38a 197 }
5c89bd63 198 }
d622d38a 199
5c89bd63 200 if ($Count < $i) {
201 Mail_Fetch_Status(_("Login OK: No new messages"));
202 $pop3->quit();
203 continue;
204 }
205 if ($Count == 0) {
206 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
207 $pop3->quit();
208 continue;
209 } else {
210 $newmsgcount = $Count - $i + 1;
211 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
212 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
213 }
8b56b0d9 214
5c89bd63 215 Mail_Fetch_Status(_("Fetching UIDL..."));
216 // Faster to get them all at once
217 $mailfetch_uidl = $pop3->uidl();
8b56b0d9 218
5c89bd63 219 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
220 Mail_Fetch_Status(_("Server does not support UIDL."));
d622d38a 221
5c89bd63 222 if ($mailfetch_lmos == 'on') {
223 Mail_Fetch_Status(_("Leaving mail on server..."));
224 } else {
225 Mail_Fetch_Status(_("Deleting messages from server..."));
226 }
8b56b0d9 227
5c89bd63 228 for (; $i <= $Count; $i++) {
3c021d16 229 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
05b06d34 230
5c89bd63 231 if (!ini_get('safe_mode'))
232 set_time_limit(20); // 20 seconds per message max
233 $Message = '';
234 $MessArray = $pop3->get($i);
8b56b0d9 235
5c89bd63 236 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
3c021d16 237 Mail_Fetch_Status($pop3->ERROR);
5c89bd63 238 // re-connect pop3
239 Mail_Fetch_Status(_("Server error. Disconnect"));
240 $pop3->quit();
241 Mail_Fetch_Status(_("Reconnect from dead connection"));
242 if (!$pop3->connect($mailfetch_server)) {
3c021d16 243 Mail_Fetch_Status($pop3->ERROR );
5c89bd63 244 Mail_Fetch_Status(_("Saving UIDL"));
245 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
246
247 continue;
d622d38a 248 }
5c89bd63 249 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
250 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
251 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
252 Mail_Fetch_Status(_("Saving UIDL"));
253 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
8b56b0d9 254
5c89bd63 255 continue;
256 }
3c021d16 257 Mail_Fetch_Status(sprintf(_("Refetching message %s."), $i));
5c89bd63 258 $MessArray = $pop3->get($i);
8b56b0d9 259
5c89bd63 260 } // end while
261
262 while (list($lineNum, $line) = each ($MessArray)) {
263 $Message .= $line;
264 }
265
266 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
267 $Line = fgets($imap_stream, 1024);
268 if (substr($Line, 0, 1) == '+') {
269 fputs($imap_stream, $Message);
270 fputs($imap_stream, "\r\n");
271 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
272 $response=(implode('',$response));
273 $message=(implode('',$message));
274 if ($response != 'OK') {
275 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
09e47788 276 Mail_Fetch_Status(_("Closing POP"));
277 $pop3->quit();
278 Mail_Fetch_Status(_("Logging out from IMAP"));
279 sqimap_logout($imap_stream);
280
09e47788 281 Mail_Fetch_Status(_("Saving UIDL"));
282 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
283 exit;
5c89bd63 284 } else {
285 Mail_Fetch_Status(_("Message appended to mailbox"));
d622d38a 286 }
8b56b0d9 287
5c89bd63 288 if ($mailfetch_lmos != 'on') {
289 if( $pop3->delete($i) ) {
290 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
291 } else {
292 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) );
293 }
294 }
295 } else {
296 echo $Line;
297 Mail_Fetch_Status(_("Error Appending Message!"));
298 Mail_Fetch_Status(_("Closing POP"));
299 $pop3->quit();
300 Mail_Fetch_Status(_("Logging out from IMAP"));
301 sqimap_logout($imap_stream);
302
303 // not gurantee corect!
d622d38a 304 Mail_Fetch_Status(_("Saving UIDL"));
5c89bd63 305 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
306 exit;
d622d38a 307 }
5c89bd63 308 }
8b56b0d9 309
5c89bd63 310 Mail_Fetch_Status(_("Closing POP"));
311 $pop3->quit();
312 Mail_Fetch_Status(_("Logging out from IMAP"));
313 sqimap_logout($imap_stream);
314 if (is_array($mailfetch_uidl)) {
315 Mail_Fetch_Status(_("Saving UIDL"));
316 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
317 }
d622d38a 318
5c89bd63 319 Mail_Fetch_Status(_("Done"));
320}
d622d38a 321?>
f265009a 322</div>
dcc1cc82 323</body>
2128bbc6 324</html>