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