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