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