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