d622d38a |
1 | <?php |
d622d38a |
2 | |
8d6a115b |
3 | /** |
4 | * mail_fetch/fetch.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
8d6a115b |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Fetch code. |
10 | * |
11 | * $Id$ |
12 | */ |
d3c89357 |
13 | |
8d6a115b |
14 | define('SM_PATH','../../'); |
d3c89357 |
15 | |
8d6a115b |
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'); |
8d6a115b |
21 | require_once(SM_PATH . 'plugins/mail_fetch/functions.php' ); |
22 | require_once(SM_PATH . 'functions/html.php' ); |
d3c89357 |
23 | |
3c66c567 |
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); |
3c66c567 |
29 | /* end globals */ |
86bb8549 |
30 | |
d3c89357 |
31 | function Mail_Fetch_Status($msg) { |
4cf43843 |
32 | echo html_tag( 'table', |
33 | html_tag( 'tr', |
34 | html_tag( 'td', htmlspecialchars( $msg ) , 'left' ) |
35 | ), |
36 | '', '', 'width="90%"' ); |
d3c89357 |
37 | flush(); |
38 | } |
d622d38a |
39 | |
9e56a6ad |
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; |
d622d38a |
44 | } |
9e56a6ad |
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 | } |
8b56b0d9 |
65 | } |
9e56a6ad |
66 | return $mailfetch; |
d622d38a |
67 | } |
8b56b0d9 |
68 | |
9e56a6ad |
69 | function Mail_Fetch_Select_Server($mailfetch) { |
4cf43843 |
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:") . ' ', 'right' ) . |
75 | html_tag( 'td', '', 'left' ) . |
76 | '<select name="server_to_fetch" size="1">' . |
77 | '<option value="all" selected>..' . _("All") . "...\n"; |
9e56a6ad |
78 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
4cf43843 |
79 | echo "<option value=\"$i\">" . |
9e56a6ad |
80 | htmlspecialchars($mailfetch[$i]['alias']) . |
4cf43843 |
81 | '</option>' . "\n"; |
9e56a6ad |
82 | } |
4cf43843 |
83 | echo '</select>' . |
84 | '</td>' . |
85 | '</tr>'; |
8b56b0d9 |
86 | |
d622d38a |
87 | //if password not set, ask for it |
9e56a6ad |
88 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
89 | if ($mailfetch[$i]['pass'] == '') { |
4cf43843 |
90 | echo html_tag( 'tr', |
91 | html_tag( 'td', _("Password for") . ' <b>' . |
9e56a6ad |
92 | htmlspecialchars($mailfetch[$i]['alias']) . |
4cf43843 |
93 | '</b>: ', |
94 | 'right' ) . |
18cdccda |
95 | html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' ) |
4cf43843 |
96 | ); |
d622d38a |
97 | } |
98 | } |
4cf43843 |
99 | echo html_tag( 'tr', |
100 | html_tag( 'td', ' ' ) . |
101 | html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' ) |
e0f638ae |
102 | ) . |
4cf43843 |
103 | '</table></form>'; |
9e56a6ad |
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 | |
25e2a90e |
117 | // get $server_to_fetch from globals, if not set display a choice to the user |
118 | if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) { |
9e56a6ad |
119 | Mail_Fetch_Select_Server($mailfetch); |
d622d38a |
120 | exit(); |
121 | } |
122 | |
123 | if ( $server_to_fetch == 'all' ) { |
124 | $i_start = 0; |
9e56a6ad |
125 | $i_stop = $mailfetch['server_number']; |
d622d38a |
126 | } else { |
127 | $i_start = $server_to_fetch; |
8b56b0d9 |
128 | $i_stop = $i_start+1; |
d622d38a |
129 | } |
8b56b0d9 |
130 | |
d622d38a |
131 | for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) { |
9e56a6ad |
132 | $mailfetch_server = $mailfetch[$i_loop]['server']; |
133 | $mailfetch_port = $mailfetch[$i_loop]['port']; |
134 | $mailfetch_user = $mailfetch[$i_loop]['user']; |
135 | $mailfetch_pass = $mailfetch[$i_loop]['pass']; |
136 | $mailfetch_lmos = $mailfetch[$i_loop]['lmos']; |
137 | $mailfetch_login = $mailfetch[$i_loop]['login']; |
138 | $mailfetch_uidl = $mailfetch[$i_loop]['uidl']; |
139 | $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder']; |
8b56b0d9 |
140 | if($mailfetch_subfolder == '') { |
141 | $mailfetch_subfolder == 'INBOX'; |
d622d38a |
142 | } |
8b56b0d9 |
143 | |
d622d38a |
144 | $pop3 = new POP3($mailfetch_server, 60); |
8b56b0d9 |
145 | |
4cf43843 |
146 | echo '<br>' . |
147 | html_tag( 'table', |
148 | html_tag( 'tr', |
149 | html_tag( 'td', '<b>' . _("Fetching from ") . |
9e56a6ad |
150 | htmlspecialchars($mailfetch[$i_loop]['alias']) . |
4cf43843 |
151 | '</b>', |
152 | 'center' ) , |
153 | '', $color[9] ) , |
154 | '', '', 'width="90%"' ); |
8b56b0d9 |
155 | |
d622d38a |
156 | flush(); |
8b56b0d9 |
157 | |
e42b0dd2 |
158 | if (!$pop3->connect($mailfetch_server,$mailfetch_port)) { |
d622d38a |
159 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR ); |
160 | continue; |
161 | } |
8b56b0d9 |
162 | |
d622d38a |
163 | Mail_Fetch_Status(_("Opening IMAP server")); |
164 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); |
8b56b0d9 |
165 | |
d622d38a |
166 | Mail_Fetch_Status(_("Opening POP server")); |
167 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
168 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
d3c89357 |
169 | Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR ); |
d622d38a |
170 | continue; |
171 | } |
8b56b0d9 |
172 | |
d622d38a |
173 | // register_shutdown_function($pop3->quit()); |
8b56b0d9 |
174 | |
d622d38a |
175 | $msglist = $pop3->uidl(); |
09e47788 |
176 | |
d622d38a |
177 | $i = 1; |
178 | for ($j = 1; $j < sizeof($msglist); $j++) { |
9e56a6ad |
179 | if ($msglist[$j] == $mailfetch_uidl) { |
d622d38a |
180 | $i = $j+1; |
181 | break; |
182 | } |
183 | } |
184 | |
185 | if ($Count < $i) { |
186 | Mail_Fetch_Status(_("Login OK: No new messages")); |
187 | $pop3->quit(); |
188 | continue; |
189 | } |
190 | if ($Count == 0) { |
191 | Mail_Fetch_Status(_("Login OK: Inbox EMPTY")); |
192 | $pop3->quit(); |
193 | continue; |
194 | } else { |
195 | $newmsgcount = $Count - $i + 1; |
196 | Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages")); |
197 | } |
8b56b0d9 |
198 | |
d622d38a |
199 | Mail_Fetch_Status(_("Fetching UIDL...")); |
200 | // Faster to get them all at once |
201 | $mailfetch_uidl = $pop3->uidl(); |
8b56b0d9 |
202 | |
d622d38a |
203 | if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on') |
204 | Mail_Fetch_Status(_("Server does not support UIDL.")); |
205 | |
206 | if ($mailfetch_lmos == 'on') { |
207 | Mail_Fetch_Status(_("Leaving Mail on Server...")); |
208 | } else { |
209 | Mail_Fetch_Status(_("Deleting messages from server...")); |
210 | } |
8b56b0d9 |
211 | |
d622d38a |
212 | for (; $i <= $Count; $i++) { |
213 | Mail_Fetch_Status(_("Fetching message ") . "$i" ); |
214 | set_time_limit(20); // 20 seconds per message max |
8b56b0d9 |
215 | $Message = ''; |
d622d38a |
216 | $MessArray = $pop3->get($i); |
8b56b0d9 |
217 | |
09e47788 |
218 | while ( (!$MessArray) or (gettype($MessArray) != "array")) { |
219 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR); |
220 | // re-connect pop3 |
221 | Mail_Fetch_Status(_("Server error...Disconnect")); |
8b56b0d9 |
222 | $pop3->quit(); |
a3e3ddc4 |
223 | Mail_Fetch_Status(_("Reconnect from dead connection")); |
09e47788 |
224 | if (!$pop3->connect($mailfetch_server)) { |
225 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR ); |
226 | Mail_Fetch_Status(_("Saving UIDL")); |
227 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
228 | |
229 | continue; |
230 | } |
231 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
232 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
233 | Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR ); |
234 | Mail_Fetch_Status(_("Saving UIDL")); |
235 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
236 | |
237 | continue; |
238 | } |
a3e3ddc4 |
239 | Mail_Fetch_Status(_("Refetching message ") . "$i" ); |
8b56b0d9 |
240 | $MessArray = $pop3->get($i); |
09e47788 |
241 | |
242 | } // end while |
8b56b0d9 |
243 | |
d622d38a |
244 | while (list($lineNum, $line) = each ($MessArray)) { |
245 | $Message .= $line; |
246 | } |
8b56b0d9 |
247 | |
248 | fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n"); |
d622d38a |
249 | $Line = fgets($imap_stream, 1024); |
250 | if (substr($Line, 0, 1) == '+') { |
251 | fputs($imap_stream, $Message); |
8b56b0d9 |
252 | fputs($imap_stream, "\r\n"); |
d622d38a |
253 | sqimap_read_data($imap_stream, "A3$i", false, $response, $message); |
8b56b0d9 |
254 | if ($response != 'OK') { |
09e47788 |
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]); |
8b56b0d9 |
263 | exit; |
09e47788 |
264 | } else { |
265 | Mail_Fetch_Status(_("Message appended to mailbox")); |
266 | } |
8b56b0d9 |
267 | |
d622d38a |
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 { |
8b56b0d9 |
276 | echo $Line; |
d622d38a |
277 | Mail_Fetch_Status(_("Error Appending Message!")); |
09e47788 |
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; |
d622d38a |
287 | } |
288 | } |
8b56b0d9 |
289 | |
d622d38a |
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 | } |
8b56b0d9 |
298 | |
d622d38a |
299 | Mail_Fetch_Status(_("Done")); |
300 | |
301 | } |
302 | |
303 | ?> |
304 | </center> |
305 | </body> |
15e6162e |
306 | </html> |