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