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