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