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