d622d38a |
1 | <?php |
d622d38a |
2 | |
8d6a115b |
3 | /** |
4 | * mail_fetch/fetch.php |
5 | * |
6c84ba1e |
6 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
8d6a115b |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Fetch code. |
10 | * |
4f51df66 |
11 | * @version $Id$ |
ea5f4b8e |
12 | * @package plugins |
13 | * @subpackage mail_fetch |
8d6a115b |
14 | */ |
d3c89357 |
15 | |
ea5f4b8e |
16 | /** @ignore */ |
8d6a115b |
17 | define('SM_PATH','../../'); |
d3c89357 |
18 | |
8d6a115b |
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'); |
8d6a115b |
24 | require_once(SM_PATH . 'plugins/mail_fetch/functions.php' ); |
25 | require_once(SM_PATH . 'functions/html.php' ); |
d3c89357 |
26 | |
91e0dccc |
27 | /* globals */ |
3c66c567 |
28 | sqgetGlobalVar('username', $username, SQ_SESSION); |
29 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
30 | sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); |
31 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
3c66c567 |
32 | /* end globals */ |
86bb8549 |
33 | |
d3c89357 |
34 | function Mail_Fetch_Status($msg) { |
4cf43843 |
35 | echo html_tag( 'table', |
36 | html_tag( 'tr', |
37 | html_tag( 'td', htmlspecialchars( $msg ) , 'left' ) |
38 | ), |
39 | '', '', 'width="90%"' ); |
d3c89357 |
40 | flush(); |
41 | } |
d622d38a |
42 | |
9e56a6ad |
43 | function Mail_Fetch_Servers() { |
760f848c |
44 | global $data_dir, $username; |
45 | |
9e56a6ad |
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; |
d622d38a |
49 | } |
9e56a6ad |
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 | } |
8b56b0d9 |
70 | } |
9e56a6ad |
71 | return $mailfetch; |
d622d38a |
72 | } |
8b56b0d9 |
73 | |
9e56a6ad |
74 | function Mail_Fetch_Select_Server($mailfetch) { |
760f848c |
75 | global $PHP_SELF; |
76 | |
6fd95361 |
77 | echo '<font size="-5"><br /></font>' . |
78 | '<form action="'.$PHP_SELF.'" method="post" target="_self">' . |
4cf43843 |
79 | html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) . |
80 | html_tag( 'tr' ) . |
81 | html_tag( 'td', _("Select Server:") . ' ', 'right' ) . |
82 | html_tag( 'td', '', 'left' ) . |
83 | '<select name="server_to_fetch" size="1">' . |
6fd95361 |
84 | '<option value="all" selected="selected">..' . _("All") . "...\n"; |
9e56a6ad |
85 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
4cf43843 |
86 | echo "<option value=\"$i\">" . |
9e56a6ad |
87 | htmlspecialchars($mailfetch[$i]['alias']) . |
4cf43843 |
88 | '</option>' . "\n"; |
91e0dccc |
89 | } |
4cf43843 |
90 | echo '</select>' . |
91 | '</td>' . |
92 | '</tr>'; |
8b56b0d9 |
93 | |
d622d38a |
94 | //if password not set, ask for it |
9e56a6ad |
95 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
96 | if ($mailfetch[$i]['pass'] == '') { |
4cf43843 |
97 | echo html_tag( 'tr', |
98 | html_tag( 'td', _("Password for") . ' <b>' . |
9e56a6ad |
99 | htmlspecialchars($mailfetch[$i]['alias']) . |
4cf43843 |
100 | '</b>: ', |
101 | 'right' ) . |
6fd95361 |
102 | html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' ) |
4cf43843 |
103 | ); |
d622d38a |
104 | } |
105 | } |
4cf43843 |
106 | echo html_tag( 'tr', |
107 | html_tag( 'td', ' ' ) . |
6fd95361 |
108 | html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' ) |
e0f638ae |
109 | ) . |
4cf43843 |
110 | '</table></form>'; |
9e56a6ad |
111 | } |
112 | |
113 | $mailfetch = Mail_Fetch_Servers(); |
114 | displayPageHeader($color, 'None'); |
115 | |
6fd95361 |
116 | echo '<br /><center>'; |
9e56a6ad |
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 | |
760f848c |
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.") ); |
dcc1cc82 |
130 | echo '</body></html>'; |
760f848c |
131 | exit(); |
132 | } |
133 | |
25e2a90e |
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) ) { |
9e56a6ad |
136 | Mail_Fetch_Select_Server($mailfetch); |
d622d38a |
137 | exit(); |
138 | } |
139 | |
140 | if ( $server_to_fetch == 'all' ) { |
141 | $i_start = 0; |
9e56a6ad |
142 | $i_stop = $mailfetch['server_number']; |
d622d38a |
143 | } else { |
144 | $i_start = $server_to_fetch; |
8b56b0d9 |
145 | $i_stop = $i_start+1; |
d622d38a |
146 | } |
8b56b0d9 |
147 | |
d622d38a |
148 | for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) { |
9e56a6ad |
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']; |
8b56b0d9 |
157 | |
d622d38a |
158 | $pop3 = new POP3($mailfetch_server, 60); |
8b56b0d9 |
159 | |
6fd95361 |
160 | echo '<br />' . |
4cf43843 |
161 | html_tag( 'table', |
162 | html_tag( 'tr', |
91e0dccc |
163 | html_tag( 'td', '<b>' . _("Fetching from ") . |
164 | htmlspecialchars($mailfetch[$i_loop]['alias']) . |
4cf43843 |
165 | '</b>', |
166 | 'center' ) , |
167 | '', $color[9] ) , |
168 | '', '', 'width="90%"' ); |
8b56b0d9 |
169 | |
d622d38a |
170 | flush(); |
8b56b0d9 |
171 | |
e42b0dd2 |
172 | if (!$pop3->connect($mailfetch_server,$mailfetch_port)) { |
d622d38a |
173 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR ); |
174 | continue; |
175 | } |
8b56b0d9 |
176 | |
d622d38a |
177 | Mail_Fetch_Status(_("Opening IMAP server")); |
178 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); |
8b56b0d9 |
179 | |
5f438206 |
180 | // check if destination folder is not set, is not subscribed and is not \noselect folder |
181 | if($mailfetch_subfolder == '' || |
182 | ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) { |
183 | $mailfetch_subfolder = 'INBOX'; |
184 | } |
185 | |
d622d38a |
186 | Mail_Fetch_Status(_("Opening POP server")); |
187 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
188 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
a9e1e670 |
189 | Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) ); |
d622d38a |
190 | continue; |
191 | } |
8b56b0d9 |
192 | |
d622d38a |
193 | // register_shutdown_function($pop3->quit()); |
8b56b0d9 |
194 | |
d622d38a |
195 | $msglist = $pop3->uidl(); |
09e47788 |
196 | |
d622d38a |
197 | $i = 1; |
198 | for ($j = 1; $j < sizeof($msglist); $j++) { |
9e56a6ad |
199 | if ($msglist[$j] == $mailfetch_uidl) { |
d622d38a |
200 | $i = $j+1; |
201 | break; |
202 | } |
203 | } |
204 | |
205 | if ($Count < $i) { |
206 | Mail_Fetch_Status(_("Login OK: No new messages")); |
207 | $pop3->quit(); |
208 | continue; |
209 | } |
210 | if ($Count == 0) { |
211 | Mail_Fetch_Status(_("Login OK: Inbox EMPTY")); |
212 | $pop3->quit(); |
213 | continue; |
214 | } else { |
215 | $newmsgcount = $Count - $i + 1; |
1a4cfc0a |
216 | Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message", |
91e0dccc |
217 | "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount)); |
d622d38a |
218 | } |
8b56b0d9 |
219 | |
d622d38a |
220 | Mail_Fetch_Status(_("Fetching UIDL...")); |
221 | // Faster to get them all at once |
222 | $mailfetch_uidl = $pop3->uidl(); |
8b56b0d9 |
223 | |
d622d38a |
224 | if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on') |
225 | Mail_Fetch_Status(_("Server does not support UIDL.")); |
226 | |
227 | if ($mailfetch_lmos == 'on') { |
333daac7 |
228 | Mail_Fetch_Status(_("Leaving mail on server...")); |
d622d38a |
229 | } else { |
230 | Mail_Fetch_Status(_("Deleting messages from server...")); |
231 | } |
8b56b0d9 |
232 | |
d622d38a |
233 | for (; $i <= $Count; $i++) { |
234 | Mail_Fetch_Status(_("Fetching message ") . "$i" ); |
05b06d34 |
235 | |
236 | if (!ini_get('safe_mode')) |
237 | set_time_limit(20); // 20 seconds per message max |
8b56b0d9 |
238 | $Message = ''; |
d622d38a |
239 | $MessArray = $pop3->get($i); |
8b56b0d9 |
240 | |
09e47788 |
241 | while ( (!$MessArray) or (gettype($MessArray) != "array")) { |
242 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR); |
243 | // re-connect pop3 |
333daac7 |
244 | Mail_Fetch_Status(_("Server error. Disconnect")); |
8b56b0d9 |
245 | $pop3->quit(); |
a3e3ddc4 |
246 | Mail_Fetch_Status(_("Reconnect from dead connection")); |
09e47788 |
247 | if (!$pop3->connect($mailfetch_server)) { |
248 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR ); |
249 | Mail_Fetch_Status(_("Saving UIDL")); |
250 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
251 | |
252 | continue; |
253 | } |
254 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
255 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
256 | Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR ); |
257 | Mail_Fetch_Status(_("Saving UIDL")); |
258 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
259 | |
260 | continue; |
261 | } |
a3e3ddc4 |
262 | Mail_Fetch_Status(_("Refetching message ") . "$i" ); |
8b56b0d9 |
263 | $MessArray = $pop3->get($i); |
09e47788 |
264 | |
265 | } // end while |
8b56b0d9 |
266 | |
d622d38a |
267 | while (list($lineNum, $line) = each ($MessArray)) { |
268 | $Message .= $line; |
269 | } |
8b56b0d9 |
270 | |
271 | fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n"); |
d622d38a |
272 | $Line = fgets($imap_stream, 1024); |
273 | if (substr($Line, 0, 1) == '+') { |
274 | fputs($imap_stream, $Message); |
8b56b0d9 |
275 | fputs($imap_stream, "\r\n"); |
d622d38a |
276 | sqimap_read_data($imap_stream, "A3$i", false, $response, $message); |
91e0dccc |
277 | $response=(implode('',$response)); |
278 | $message=(implode('',$message)); |
8b56b0d9 |
279 | if ($response != 'OK') { |
2b0a4162 |
280 | Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) ); |
09e47788 |
281 | Mail_Fetch_Status(_("Closing POP")); |
282 | $pop3->quit(); |
283 | Mail_Fetch_Status(_("Logging out from IMAP")); |
284 | sqimap_logout($imap_stream); |
285 | |
286 | Mail_Fetch_Status(_("Saving UIDL")); |
287 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
8b56b0d9 |
288 | exit; |
09e47788 |
289 | } else { |
290 | Mail_Fetch_Status(_("Message appended to mailbox")); |
291 | } |
8b56b0d9 |
292 | |
d622d38a |
293 | if ($mailfetch_lmos != 'on') { |
294 | if( $pop3->delete($i) ) { |
8e00482f |
295 | Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i)); |
d622d38a |
296 | } else { |
a9e1e670 |
297 | Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) ); |
d622d38a |
298 | } |
299 | } |
300 | } else { |
8b56b0d9 |
301 | echo $Line; |
d622d38a |
302 | Mail_Fetch_Status(_("Error Appending Message!")); |
09e47788 |
303 | Mail_Fetch_Status(_("Closing POP")); |
304 | $pop3->quit(); |
305 | Mail_Fetch_Status(_("Logging out from IMAP")); |
306 | sqimap_logout($imap_stream); |
307 | |
308 | // not gurantee corect! |
309 | Mail_Fetch_Status(_("Saving UIDL")); |
310 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
311 | exit; |
d622d38a |
312 | } |
313 | } |
8b56b0d9 |
314 | |
d622d38a |
315 | Mail_Fetch_Status(_("Closing POP")); |
316 | $pop3->quit(); |
317 | Mail_Fetch_Status(_("Logging out from IMAP")); |
318 | sqimap_logout($imap_stream); |
319 | if (is_array($mailfetch_uidl)) { |
320 | Mail_Fetch_Status(_("Saving UIDL")); |
321 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl)); |
322 | } |
8b56b0d9 |
323 | |
d622d38a |
324 | Mail_Fetch_Status(_("Done")); |
325 | |
326 | } |
327 | |
328 | ?> |
329 | </center> |
dcc1cc82 |
330 | </body> |
4f51df66 |
331 | </html> |