d622d38a |
1 | <?php |
4b4abf93 |
2 | |
8d6a115b |
3 | /** |
4 | * mail_fetch/fetch.php |
5 | * |
8d6a115b |
6 | * Fetch code. |
7 | * |
47ccfad4 |
8 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
4f51df66 |
10 | * @version $Id$ |
ea5f4b8e |
11 | * @package plugins |
12 | * @subpackage mail_fetch |
8d6a115b |
13 | */ |
d3c89357 |
14 | |
202bcbcc |
15 | /** |
16 | * Include the SquirrelMail initialization file. |
17 | */ |
18 | require('../../include/init.php'); |
d3c89357 |
19 | |
202bcbcc |
20 | include_once(SM_PATH . 'functions/imap_general.php'); |
5c89bd63 |
21 | include_once(SM_PATH . 'plugins/mail_fetch/functions.php' ); |
d3c89357 |
22 | |
91e0dccc |
23 | /* globals */ |
3c66c567 |
24 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
3c66c567 |
25 | /* end globals */ |
86bb8549 |
26 | |
929da10d |
27 | /** |
28 | * @param string $msg message |
29 | */ |
5c89bd63 |
30 | function Mail_Fetch_Status($msg) { |
31 | echo html_tag( 'table', |
32 | html_tag( 'tr', |
33 | html_tag( 'td', htmlspecialchars( $msg ) , 'left' ) |
34 | ), |
35 | '', '', 'width="90%"' ); |
36 | flush(); |
37 | } |
38 | |
929da10d |
39 | /** |
40 | * @return array |
41 | */ |
5c89bd63 |
42 | function Mail_Fetch_Servers() { |
43 | global $data_dir, $username; |
44 | |
929da10d |
45 | $mailfetch = array(); |
5c89bd63 |
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; |
d3c89357 |
49 | } |
5c89bd63 |
50 | $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher"); |
929da10d |
51 | |
5c89bd63 |
52 | for ($i = 0; $i < $mailfetch['server_number']; $i++) { |
53 | $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i"); |
54 | $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i"); |
55 | $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i"); |
56 | $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i"); |
57 | $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i"); |
58 | if($mailfetch['cypher'] == 'on') { |
59 | $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']); |
d622d38a |
60 | } |
5c89bd63 |
61 | if ($mailfetch[$i]['pass'] == '') { |
62 | sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST); |
91e0dccc |
63 | } |
5c89bd63 |
64 | $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i"); |
65 | $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i"); |
66 | $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i"); |
67 | $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i"); |
68 | if($mailfetch[$i]['alias'] == '') { |
69 | $mailfetch[$i]['alias'] == $mailfetch[$i]['server']; |
d622d38a |
70 | } |
929da10d |
71 | // Authentication type (added in 1.5.2) |
72 | $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER); |
73 | // Connection type (added in 1.5.2) |
74 | $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN); |
9e56a6ad |
75 | } |
929da10d |
76 | return $mailfetch; |
5c89bd63 |
77 | } |
78 | |
929da10d |
79 | /** |
80 | * @param array $mailfetch |
81 | */ |
5c89bd63 |
82 | function Mail_Fetch_Select_Server($mailfetch) { |
83 | global $PHP_SELF; |
84 | |
85 | echo '<font size="-5"><br /></font>' . |
86 | '<form action="'.$PHP_SELF.'" method="post" target="_self">' . |
87 | html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) . |
88 | html_tag( 'tr' ) . |
89 | html_tag( 'td', _("Select Server:") . ' ', 'right' ) . |
90 | html_tag( 'td', '', 'left' ) . |
91 | '<select name="server_to_fetch" size="1">' . |
92 | '<option value="all" selected="selected">..' . _("All") . "...\n"; |
93 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
94 | echo "<option value=\"$i\">" . |
95 | htmlspecialchars($mailfetch[$i]['alias']) . |
96 | '</option>' . "\n"; |
d622d38a |
97 | } |
5c89bd63 |
98 | echo '</select>' . |
99 | '</td>' . |
100 | '</tr>'; |
101 | |
102 | //if password not set, ask for it |
103 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
104 | if ($mailfetch[$i]['pass'] == '') { |
105 | echo html_tag( 'tr', |
106 | html_tag( 'td', _("Password for") . ' <b>' . |
107 | htmlspecialchars($mailfetch[$i]['alias']) . |
108 | '</b>: ', |
109 | 'right' ) . |
110 | html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' ) |
111 | ); |
112 | } |
d622d38a |
113 | } |
5c89bd63 |
114 | echo html_tag( 'tr', |
115 | html_tag( 'td', ' ' ) . |
116 | html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' ) |
117 | ) . |
118 | '</table></form>'; |
119 | } |
120 | |
121 | $mailfetch = Mail_Fetch_Servers(); |
122 | displayPageHeader($color, 'None'); |
123 | |
929da10d |
124 | echo '<br />'; |
5c89bd63 |
125 | |
126 | echo html_tag( 'table', |
127 | html_tag( 'tr', |
128 | html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] ) |
129 | ) , |
130 | 'center', '', 'width="95%" cols="1"' ); |
131 | |
132 | |
133 | /* there are no servers defined yet... */ |
134 | if($mailfetch['server_number'] == 0) { |
135 | echo '<p>' . _("No POP3 servers configured yet.") . '</p>'; |
136 | displayInternalLink('plugins/mail_fetch/options.php', |
137 | _("Click here to go to the options page.") ); |
929da10d |
138 | $oTemplate->display('footer.tpl'); |
5c89bd63 |
139 | exit(); |
140 | } |
141 | |
142 | // get $server_to_fetch from globals, if not set display a choice to the user |
143 | if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) { |
144 | Mail_Fetch_Select_Server($mailfetch); |
929da10d |
145 | $oTemplate->display('footer.tpl'); |
5c89bd63 |
146 | exit(); |
147 | } |
148 | |
149 | if ( $server_to_fetch == 'all' ) { |
150 | $i_start = 0; |
151 | $i_stop = $mailfetch['server_number']; |
152 | } else { |
153 | $i_start = $server_to_fetch; |
154 | $i_stop = $i_start+1; |
155 | } |
156 | |
157 | for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) { |
158 | $mailfetch_server = $mailfetch[$i_loop]['server']; |
159 | $mailfetch_port = $mailfetch[$i_loop]['port']; |
160 | $mailfetch_user = $mailfetch[$i_loop]['user']; |
161 | $mailfetch_pass = $mailfetch[$i_loop]['pass']; |
162 | $mailfetch_lmos = $mailfetch[$i_loop]['lmos']; |
163 | $mailfetch_login = $mailfetch[$i_loop]['login']; |
164 | $mailfetch_uidl = $mailfetch[$i_loop]['uidl']; |
165 | $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder']; |
929da10d |
166 | $mailfetch_auth = $mailfetch[$i_loop]['auth']; |
167 | $mailfetch_type = $mailfetch[$i_loop]['type']; |
5c89bd63 |
168 | |
169 | echo '<br />' . |
4cf43843 |
170 | html_tag( 'table', |
171 | html_tag( 'tr', |
3c021d16 |
172 | html_tag( 'td', '<b>' . |
173 | sprintf(_("Fetching from %s"), |
174 | htmlspecialchars($mailfetch[$i_loop]['alias'])) . |
4cf43843 |
175 | '</b>', |
176 | 'center' ) , |
177 | '', $color[9] ) , |
178 | '', '', 'width="90%"' ); |
8b56b0d9 |
179 | |
5c89bd63 |
180 | flush(); |
8b56b0d9 |
181 | |
929da10d |
182 | $pop3 = new mail_fetch(array('host' => $mailfetch_server, |
183 | 'port' => $mailfetch_port, |
184 | 'auth' => $mailfetch_auth, |
185 | 'tls' => $mailfetch_type, |
186 | 'timeout' => 60)); |
187 | |
188 | if (!empty($pop3->error)) { |
189 | Mail_Fetch_Status($pop3->error); |
5c89bd63 |
190 | continue; |
191 | } |
8b56b0d9 |
192 | |
5c89bd63 |
193 | Mail_Fetch_Status(_("Opening IMAP server")); |
2128bbc6 |
194 | $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10); |
8b56b0d9 |
195 | |
5c89bd63 |
196 | // check if destination folder is not set, is not subscribed and is not \noselect folder |
197 | if($mailfetch_subfolder == '' || |
198 | ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) { |
199 | $mailfetch_subfolder = 'INBOX'; |
200 | } |
5f438206 |
201 | |
5c89bd63 |
202 | Mail_Fetch_Status(_("Opening POP server")); |
929da10d |
203 | |
204 | /* log into pop server*/ |
205 | if (! $pop3->login($mailfetch_user, $mailfetch_pass)) { |
206 | Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error)); |
5c89bd63 |
207 | continue; |
208 | } |
8b56b0d9 |
209 | |
929da10d |
210 | $aMsgStat = $pop3->command_stat(); |
211 | if (is_bool($aMsgStat)) { |
212 | Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) ); |
213 | continue; |
214 | } |
8b56b0d9 |
215 | |
929da10d |
216 | $Count = $aMsgStat['count']; |
09e47788 |
217 | |
5c89bd63 |
218 | $i = 1; |
929da10d |
219 | |
220 | if ($Count>0) { |
221 | // If we leave messages on server, try using UIDL |
222 | if ($mailfetch_lmos == 'on') { |
223 | Mail_Fetch_Status(_("Fetching UIDL...")); |
224 | $msglist = $pop3->command_uidl(); |
225 | if (is_bool($msglist)) { |
226 | Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error)); |
227 | // User asked to leave messages on server, but we can't do that. |
228 | $pop3->command_quit(); |
229 | continue; |
230 | // $mailfetch_lmos = 'off'; |
231 | } else { |
232 | // calculate number of new messages |
233 | for ($j = 1; $j <= sizeof($msglist); $j++) { |
234 | // do strict comparison ('1111.10' should not be equal to '1111.100') |
235 | if ($msglist[$j] === $mailfetch_uidl) { |
236 | $i = $j+1; |
237 | break; |
238 | } |
239 | } |
240 | } |
241 | } |
242 | // fetch list of messages with LIST |
243 | // we can use else control, but we can also set $mailfetch_lmos |
244 | // to off if server does not support UIDL. |
245 | if ($mailfetch_lmos != 'on') { |
246 | Mail_Fetch_Status(_("Fetching list of messages...")); |
247 | $msglist = $pop3->command_list(); |
d622d38a |
248 | } |
5c89bd63 |
249 | } |
d622d38a |
250 | |
5c89bd63 |
251 | if ($Count < $i) { |
252 | Mail_Fetch_Status(_("Login OK: No new messages")); |
929da10d |
253 | $pop3->command_quit(); |
5c89bd63 |
254 | continue; |
255 | } |
256 | if ($Count == 0) { |
257 | Mail_Fetch_Status(_("Login OK: Inbox EMPTY")); |
929da10d |
258 | $pop3->command_quit(); |
5c89bd63 |
259 | continue; |
260 | } else { |
261 | $newmsgcount = $Count - $i + 1; |
262 | Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message", |
263 | "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount)); |
264 | } |
8b56b0d9 |
265 | |
5c89bd63 |
266 | if ($mailfetch_lmos == 'on') { |
267 | Mail_Fetch_Status(_("Leaving mail on server...")); |
268 | } else { |
269 | Mail_Fetch_Status(_("Deleting messages from server...")); |
270 | } |
8b56b0d9 |
271 | |
5c89bd63 |
272 | for (; $i <= $Count; $i++) { |
3c021d16 |
273 | Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i)); |
05b06d34 |
274 | |
5c89bd63 |
275 | if (!ini_get('safe_mode')) |
276 | set_time_limit(20); // 20 seconds per message max |
5c89bd63 |
277 | |
929da10d |
278 | $Message = $pop3->command_retr($i); |
8b56b0d9 |
279 | |
929da10d |
280 | if (is_bool($Message)) { |
281 | Mail_Fetch_Status(htmlspecialchars($pop3->error)); |
282 | continue; |
5c89bd63 |
283 | } |
284 | |
285 | fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n"); |
286 | $Line = fgets($imap_stream, 1024); |
287 | if (substr($Line, 0, 1) == '+') { |
288 | fputs($imap_stream, $Message); |
289 | fputs($imap_stream, "\r\n"); |
290 | sqimap_read_data($imap_stream, "A3$i", false, $response, $message); |
291 | $response=(implode('',$response)); |
292 | $message=(implode('',$message)); |
293 | if ($response != 'OK') { |
294 | Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) ); |
09e47788 |
295 | Mail_Fetch_Status(_("Closing POP")); |
929da10d |
296 | $pop3->command_quit(); |
09e47788 |
297 | Mail_Fetch_Status(_("Logging out from IMAP")); |
298 | sqimap_logout($imap_stream); |
299 | |
929da10d |
300 | if ($mailfetch_lmos == 'on') { |
301 | Mail_Fetch_Status(_("Saving UIDL")); |
302 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]); |
303 | } |
09e47788 |
304 | exit; |
5c89bd63 |
305 | } else { |
306 | Mail_Fetch_Status(_("Message appended to mailbox")); |
d622d38a |
307 | } |
8b56b0d9 |
308 | |
5c89bd63 |
309 | if ($mailfetch_lmos != 'on') { |
929da10d |
310 | if( $pop3->command_dele($i) ) { |
5c89bd63 |
311 | Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i)); |
312 | } else { |
929da10d |
313 | Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) ); |
5c89bd63 |
314 | } |
315 | } |
316 | } else { |
317 | echo $Line; |
318 | Mail_Fetch_Status(_("Error Appending Message!")); |
319 | Mail_Fetch_Status(_("Closing POP")); |
929da10d |
320 | $pop3->command_quit(); |
5c89bd63 |
321 | Mail_Fetch_Status(_("Logging out from IMAP")); |
322 | sqimap_logout($imap_stream); |
323 | |
324 | // not gurantee corect! |
929da10d |
325 | if ($mailfetch_lmos == 'on') { |
326 | Mail_Fetch_Status(_("Saving UIDL")); |
327 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]); |
328 | } |
5c89bd63 |
329 | exit; |
d622d38a |
330 | } |
5c89bd63 |
331 | } |
8b56b0d9 |
332 | |
5c89bd63 |
333 | Mail_Fetch_Status(_("Closing POP")); |
929da10d |
334 | $pop3->command_quit(); |
5c89bd63 |
335 | Mail_Fetch_Status(_("Logging out from IMAP")); |
336 | sqimap_logout($imap_stream); |
929da10d |
337 | if ($mailfetch_lmos == 'on' && is_array($msglist)) { |
5c89bd63 |
338 | Mail_Fetch_Status(_("Saving UIDL")); |
929da10d |
339 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist)); |
5c89bd63 |
340 | } |
d622d38a |
341 | |
5c89bd63 |
342 | Mail_Fetch_Status(_("Done")); |
343 | } |
929da10d |
344 | |
345 | $oTemplate->display('footer.tpl'); |
346 | |