d622d38a |
1 | <?php |
4b4abf93 |
2 | |
4f51df66 |
3 | /** |
4 | * mail_fetch/functions.php |
5 | * |
60683821 |
6 | * Functions for the mail_fetch plugin. |
4f51df66 |
7 | * |
8 | * Original code from LexZEUS <lexzeus@mifinca.com> |
9 | * and josh@superfork.com (extracted from php manual) |
10 | * Adapted for MailFetch by Philippe Mingo <mingo@rotedic.com> |
11 | * |
d4e46166 |
12 | * @copyright © 1999-2009 The SquirrelMail Project Team |
4b4abf93 |
13 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
4f51df66 |
14 | * @version $Id$ |
15 | * @package plugins |
16 | * @subpackage mail_fetch |
17 | */ |
d622d38a |
18 | |
60683821 |
19 | |
20 | /** pop3 class */ |
929da10d |
21 | include_once (SM_PATH . 'plugins/mail_fetch/constants.php'); |
22 | include_once (SM_PATH . 'plugins/mail_fetch/class.mail_fetch.php'); |
60683821 |
23 | |
5f438206 |
24 | /** declare plugin globals */ |
25 | global $mail_fetch_allow_unsubscribed; |
26 | |
09b143cc |
27 | /** |
28 | * Add link to menu at top of content pane |
29 | * |
30 | * @return void |
31 | * |
32 | */ |
33 | function mail_fetch_link_do() { |
34 | |
551c7b53 |
35 | global $oTemplate, $nbsp; |
09b143cc |
36 | $output = makeInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '') |
37 | . $nbsp . $nbsp; |
38 | return array('menuline' => $output); |
39 | |
40 | } |
41 | |
5f438206 |
42 | /** |
43 | * Controls use of unsubscribed folders in plugin |
44 | * @global boolean $mail_fetch_allow_unsubscribed |
5c89bd63 |
45 | * @since 1.5.1 and 1.4.5 |
5f438206 |
46 | */ |
47 | $mail_fetch_allow_unsubscribed = false; |
48 | |
60683821 |
49 | /** load site config */ |
50 | if (file_exists(SM_PATH . 'config/mail_fetch_config.php')) { |
51 | include_once(SM_PATH . 'config/mail_fetch_config.php'); |
52 | } elseif (file_exists(SM_PATH . 'plugins/mail_fetch/config.php')) { |
53 | include_once(SM_PATH . 'plugins/mail_fetch/config.php'); |
54 | } |
55 | |
56 | // hooked functions |
57 | |
60683821 |
58 | /** |
59 | * Internal function used to fetch pop3 mails on login |
60 | * @since 1.5.1 |
61 | * @private |
62 | */ |
63 | function mail_fetch_login_function() { |
202bcbcc |
64 | include_once (SM_PATH . 'functions/imap_general.php'); |
65 | |
2128bbc6 |
66 | global $username, $data_dir, $imapServerAddress, $imapPort; |
60683821 |
67 | |
68 | $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog'); |
69 | |
70 | $outMsg = ''; |
71 | |
72 | $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number'); |
73 | if (!isset($mailfetch_server_number)) $mailfetch_server_number=0; |
74 | $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher'); |
75 | if ($mailfetch_server_number<1) $mailfetch_server_number=0; |
76 | |
77 | for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) { |
78 | |
79 | $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop"); |
80 | $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop"); |
81 | $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop"); |
f54ec17d |
82 | if( $mailfetch_cypher == 'on' ) { |
60683821 |
83 | $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] ); |
f54ec17d |
84 | } |
60683821 |
85 | |
86 | if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed |
87 | ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) { |
88 | |
929da10d |
89 | // What the heck |
60683821 |
90 | $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop"); |
91 | $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop"); |
92 | $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop"); |
93 | $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop"); |
94 | $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop"); |
95 | $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop"); |
96 | $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop"); |
929da10d |
97 | $mailfetch_auth_[$i_loop] = getPref($data_dir, $username, "mailfetch_auth_$i_loop",MAIL_FETCH_AUTH_USER); |
98 | $mailfetch_type_[$i_loop] = getPref($data_dir, $username, "mailfetch_type_$i_loop",MAIL_FETCH_USE_PLAIN); |
60683821 |
99 | |
100 | $mailfetch_server=$mailfetch_server_[$i_loop]; |
101 | $mailfetch_port=$mailfetch_port_[$i_loop]; |
102 | $mailfetch_user=$mailfetch_user_[$i_loop]; |
103 | $mailfetch_alias=$mailfetch_alias_[$i_loop]; |
104 | $mailfetch_pass=$mailfetch_pass_[$i_loop]; |
105 | $mailfetch_lmos=$mailfetch_lmos_[$i_loop]; |
106 | $mailfetch_login=$mailfetch_login_[$i_loop]; |
107 | $mailfetch_uidl=$mailfetch_uidl_[$i_loop]; |
108 | $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop]; |
929da10d |
109 | $mailfetch_auth=$mailfetch_auth_[$i_loop]; |
110 | $mailfetch_type=$mailfetch_type_[$i_loop]; |
111 | // end of what the heck |
112 | |
60683821 |
113 | |
114 | // $outMsg .= "$mailfetch_alias checked<br />"; |
115 | |
116 | // $outMsg .= "$mailfetch_alias_[$i_loop]<br />"; |
117 | |
929da10d |
118 | // FIXME: duplicate code with different output destination. |
119 | |
120 | $pop3 = new mail_fetch(array('host' => $mailfetch_server, |
121 | 'port' => $mailfetch_port, |
122 | 'auth' => $mailfetch_auth, |
123 | 'tls' => $mailfetch_type, |
124 | 'timeout' => 60)); |
60683821 |
125 | |
929da10d |
126 | if (!empty($pop3->error)) { |
127 | $outMsg .= _("Warning:") . ' ' . $pop3->error; |
60683821 |
128 | continue; |
129 | } |
130 | |
2128bbc6 |
131 | $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10); |
60683821 |
132 | |
929da10d |
133 | /* log into pop server*/ |
134 | if (! $pop3->login($mailfetch_user, $mailfetch_pass)) { |
135 | $outMsg .= _("Login Failed:") . ' ' . $pop3->error; |
60683821 |
136 | continue; |
137 | } |
138 | |
929da10d |
139 | $aMsgStat = $pop3->command_stat(); |
140 | if (is_bool($aMsgStat)) { |
141 | $outMsg .= _("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error); |
142 | continue; |
143 | } |
60683821 |
144 | |
929da10d |
145 | $Count = $aMsgStat['count']; |
60683821 |
146 | |
147 | $i = 1; |
929da10d |
148 | |
149 | if ($Count>0) { |
150 | // If we leave messages on server, try using UIDL |
151 | if ($mailfetch_lmos == 'on') { |
152 | $msglist = $pop3->command_uidl(); |
153 | if (is_bool($msglist)) { |
154 | $outMsg .= _("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error); |
155 | // User asked to leave messages on server, but we can't do that. |
156 | $pop3->command_quit(); |
157 | continue; |
158 | // $mailfetch_lmos = 'off'; |
159 | } else { |
160 | // calculate number of new messages |
161 | for ($j = 1; $j <= sizeof($msglist); $j++) { |
162 | // do strict comparison ('1111.10' should not be equal to '1111.100') |
163 | if ($msglist[$j] === $mailfetch_uidl) { |
164 | $i = $j+1; |
165 | break; |
166 | } |
167 | } |
168 | } |
169 | } |
170 | // fetch list of messages with LIST |
171 | // we can use else control, but we can also set $mailfetch_lmos |
172 | // to off if server does not support UIDL. |
173 | if ($mailfetch_lmos != 'on') { |
174 | $msglist = $pop3->command_list(); |
60683821 |
175 | } |
176 | } |
177 | |
178 | if ($Count < $i) { |
929da10d |
179 | $pop3->command_quit(); |
60683821 |
180 | continue; |
181 | } |
182 | if ($Count == 0) { |
929da10d |
183 | $pop3->command_quit(); |
60683821 |
184 | continue; |
185 | } |
186 | |
60683821 |
187 | for (; $i <= $Count; $i++) { |
188 | if (!ini_get('safe_mode')) |
189 | set_time_limit(20); // 20 seconds per message max |
929da10d |
190 | $Message = $pop3->command_retr($i); |
60683821 |
191 | |
929da10d |
192 | if (is_bool($Message)) { |
193 | $outMsg .= _("Warning:") . ' ' . htmlspecialchars($pop3->error); |
194 | continue; |
60683821 |
195 | } |
196 | |
197 | // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders) |
198 | if ($mailfetch_subfolder=='' || |
199 | ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) { |
200 | fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n"); |
201 | } else { |
202 | fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n"); |
203 | } |
204 | $Line = fgets($imap_stream, 1024); |
205 | if (substr($Line, 0, 1) == '+') { |
206 | fputs($imap_stream, $Message); |
207 | fputs($imap_stream, "\r\n"); |
208 | sqimap_read_data($imap_stream, "A3$i", false, $response, $message); |
209 | |
929da10d |
210 | // Check results of append command |
211 | $response=(implode('',$response)); |
212 | $message=(implode('',$message)); |
213 | if ($response != 'OK') { |
f7357fd2 |
214 | $outMsg .= _("Error Appending Mail!")." ".htmlspecialchars($message); |
929da10d |
215 | |
216 | if ($mailfetch_lmos == 'on') { |
217 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]); |
218 | } |
219 | // Destroy msg list in order to prevent UIDL update |
220 | $msglist = false; |
221 | // if append fails, don't download other messages |
222 | break; |
223 | } |
224 | |
60683821 |
225 | if ($mailfetch_lmos != 'on') { |
929da10d |
226 | $pop3->command_dele($i); |
60683821 |
227 | } |
228 | } else { |
229 | echo "$Line"; |
f7357fd2 |
230 | $outMsg .= _("Error Appending Mail!"); |
60683821 |
231 | } |
232 | } |
233 | |
929da10d |
234 | $pop3->command_quit(); |
60683821 |
235 | sqimap_logout($imap_stream); |
929da10d |
236 | if ($mailfetch_lmos == 'on' && is_array($msglist)) { |
237 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist)); |
60683821 |
238 | } |
239 | } |
240 | } |
241 | |
242 | if( trim( $outMsg ) <> '' ) { |
243 | echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>"; |
244 | } |
245 | if( $mailfetch_newlog == 'on' ) { |
246 | setPref($data_dir, $username, 'mailfetch_newlog', 'off'); |
247 | } |
248 | } |
249 | |
250 | /** |
251 | * Internal function used to detect new logins |
252 | */ |
253 | function mail_fetch_setnew_function() { |
2128bbc6 |
254 | global $data_dir, $username; |
60683821 |
255 | |
60683821 |
256 | setPref( $data_dir, $username, 'mailfetch_newlog', 'on' ); |
257 | } |
258 | |
259 | /** |
260 | * Internal function used to register option block |
261 | * @since 1.5.1 |
262 | * @private |
263 | */ |
264 | function mailfetch_optpage_register_block_function() { |
265 | global $optpage_blocks; |
266 | |
267 | $optpage_blocks[] = array( |
268 | 'name' => _("POP3 Fetch Mail"), |
269 | 'url' => '../plugins/mail_fetch/options.php', |
270 | 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."), |
271 | 'js' => false |
272 | ); |
273 | } |
274 | |
275 | /** |
202bcbcc |
276 | * Internal function used to update mail_fetch settings |
60683821 |
277 | * when folders are renamed or deleted. |
278 | * @since 1.5.1 |
279 | * @private |
280 | */ |
281 | function mail_fetch_folderact_function($args) { |
282 | global $username, $data_dir; |
283 | |
284 | if (empty($args) || !is_array($args)) { |
285 | return; |
286 | } |
287 | |
288 | /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */ |
289 | if (count($args) != 3) { |
290 | return; |
291 | } |
292 | |
293 | list($old_folder, $action, $new_folder) = $args; |
294 | |
295 | $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number'); |
296 | |
297 | for ($i = 0; $i < $mailfetch_server_number; $i++) { |
298 | $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i); |
299 | |
300 | if ($mailfetch_subfolder != $old_folder) { |
301 | continue; |
302 | } |
303 | |
304 | if ($action == 'delete') { |
305 | setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX'); |
306 | } elseif ($action == 'rename') { |
307 | setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder); |
308 | } |
309 | } |
310 | } |
311 | // end of hooked functions |
312 | |
4f51df66 |
313 | /** |
314 | * hex2bin - document me |
315 | */ |
316 | function hex2bin( $data ) { |
d622d38a |
317 | |
4f51df66 |
318 | /* Original code by josh@superfork.com */ |
d622d38a |
319 | |
4f51df66 |
320 | $len = strlen($data); |
321 | $newdata = ''; |
322 | for( $i=0; $i < $len; $i += 2 ) { |
323 | $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) ); |
d622d38a |
324 | } |
4f51df66 |
325 | return $newdata; |
326 | } |
d622d38a |
327 | |
4f51df66 |
328 | function mf_keyED( $txt ) { |
d622d38a |
329 | |
4f51df66 |
330 | global $MF_TIT; |
d622d38a |
331 | |
4f51df66 |
332 | if( !isset( $MF_TIT ) ) { |
333 | $MF_TIT = "MailFetch Secure for SquirrelMail 1.x"; |
334 | } |
d622d38a |
335 | |
4f51df66 |
336 | $encrypt_key = md5( $MF_TIT ); |
337 | $ctr = 0; |
338 | $tmp = ""; |
339 | for( $i = 0; $i < strlen( $txt ); $i++ ) { |
340 | if( $ctr == strlen( $encrypt_key ) ) $ctr=0; |
341 | $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 ); |
342 | $ctr++; |
d622d38a |
343 | } |
4f51df66 |
344 | return $tmp; |
345 | } |
d622d38a |
346 | |
4f51df66 |
347 | function encrypt( $txt ) { |
d622d38a |
348 | |
4f51df66 |
349 | srand( (double) microtime() * 1000000 ); |
350 | $encrypt_key = md5( rand( 0, 32000 ) ); |
351 | $ctr = 0; |
352 | $tmp = ""; |
353 | for( $i = 0; $i < strlen( $txt ); $i++ ) { |
d622d38a |
354 | if ($ctr==strlen($encrypt_key)) $ctr=0; |
4f51df66 |
355 | $tmp.= substr($encrypt_key,$ctr,1) . |
356 | (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); |
d622d38a |
357 | $ctr++; |
d622d38a |
358 | } |
4f51df66 |
359 | return bin2hex( mf_keyED( $tmp ) ); |
360 | |
361 | } |
d622d38a |
362 | |
4f51df66 |
363 | function decrypt( $txt ) { |
d622d38a |
364 | |
4f51df66 |
365 | $txt = mf_keyED( hex2bin( $txt ) ); |
366 | $tmp = ''; |
367 | for ( $i=0; $i < strlen( $txt ); $i++ ) { |
368 | $md5 = substr( $txt, $i, 1 ); |
369 | $i++; |
370 | $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 ); |
d622d38a |
371 | } |
4f51df66 |
372 | return $tmp; |
373 | } |
d622d38a |
374 | |
5f438206 |
375 | /** |
376 | * check mail folder |
377 | * @param stream $imap_stream imap connection resource |
378 | * @param string $imap_folder imap folder name |
379 | * @return boolean true, when folder can be used to store messages. |
5c89bd63 |
380 | * @since 1.5.1 and 1.4.5 |
5f438206 |
381 | */ |
382 | function mail_fetch_check_folder($imap_stream,$imap_folder) { |
383 | global $mail_fetch_allow_unsubscribed; |
384 | |
385 | // check if folder is subscribed or only exists. |
386 | if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) { |
387 | $ret = true; |
388 | } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) { |
389 | $ret = true; |
390 | } else { |
391 | $ret = false; |
392 | } |
393 | |
394 | // make sure that folder can store messages |
395 | if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) { |
396 | $ret = false; |
397 | } |
398 | |
399 | return $ret; |
400 | } |
401 | |
402 | /** |
403 | * Checks if folder is noselect (can't store messages) |
f8a1ed5a |
404 | * |
5f438206 |
405 | * Function does not check if folder subscribed. |
406 | * @param stream $imap_stream imap connection resource |
407 | * @param string $imap_folder imap folder name |
408 | * @return boolean true, when folder has noselect flag. false in any other case. |
5c89bd63 |
409 | * @since 1.5.1 and 1.4.5 |
5f438206 |
410 | */ |
411 | function mail_fetch_check_noselect($imap_stream,$imap_folder) { |
412 | $boxes=sqimap_mailbox_list($imap_stream); |
413 | foreach($boxes as $box) { |
414 | if ($box['unformatted']==$imap_folder) { |
415 | return (bool) check_is_noselect($box['raw']); |
416 | } |
417 | } |
418 | return false; |
419 | } |