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