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