4 * mail_fetch/functions.php
6 * Functions for the mail_fetch plugin.
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>
12 * @copyright 1999-2012 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16 * @subpackage mail_fetch
21 include_once (SM_PATH
. 'plugins/mail_fetch/constants.php');
22 include_once (SM_PATH
. 'plugins/mail_fetch/class.mail_fetch.php');
24 /** declare plugin globals */
25 global $mail_fetch_allow_unsubscribed, $mail_fetch_allowable_ports,
26 $mail_fetch_block_server_pattern;
29 * Add link to menu at top of content pane
34 function mail_fetch_link_do() {
36 global $oTemplate, $nbsp;
37 $output = makeInternalLink('plugins/mail_fetch/fetch.php', _("Fetch"), '')
39 return array('menuline' => $output);
44 * Controls use of unsubscribed folders in plugin
45 * @global boolean $mail_fetch_allow_unsubscribed
46 * @since 1.5.1 and 1.4.5
48 $mail_fetch_allow_unsubscribed = false;
50 /** load site config */
51 if (file_exists(SM_PATH
. 'config/mail_fetch_config.php')) {
52 include_once(SM_PATH
. 'config/mail_fetch_config.php');
53 } elseif (file_exists(SM_PATH
. 'plugins/mail_fetch/config.php')) {
54 include_once(SM_PATH
. 'plugins/mail_fetch/config.php');
60 * Internal function used to fetch pop3 mails on login
64 function mail_fetch_login_function() {
65 include_once (SM_PATH
. 'functions/imap_general.php');
67 global $username, $data_dir, $imapServerAddress, $imapPort;
69 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
73 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
74 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
75 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
76 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
78 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++
) {
80 $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
81 $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
82 $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
83 if( $mailfetch_cypher == 'on' ) {
84 $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
87 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
88 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) ||
$mailfetch_fref_[$i_loop] == 'on' ) ) {
91 $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
92 $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
93 $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
94 $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
95 $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
96 $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
97 $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
98 $mailfetch_auth_[$i_loop] = getPref($data_dir, $username, "mailfetch_auth_$i_loop",MAIL_FETCH_AUTH_USER
);
99 $mailfetch_type_[$i_loop] = getPref($data_dir, $username, "mailfetch_type_$i_loop",MAIL_FETCH_USE_PLAIN
);
101 $mailfetch_server=$mailfetch_server_[$i_loop];
102 $mailfetch_port=$mailfetch_port_[$i_loop];
103 $mailfetch_user=$mailfetch_user_[$i_loop];
104 $mailfetch_alias=$mailfetch_alias_[$i_loop];
105 $mailfetch_pass=$mailfetch_pass_[$i_loop];
106 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
107 $mailfetch_login=$mailfetch_login_[$i_loop];
108 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
109 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
110 $mailfetch_auth=$mailfetch_auth_[$i_loop];
111 $mailfetch_type=$mailfetch_type_[$i_loop];
112 // end of what the heck
115 // $outMsg .= "$mailfetch_alias checked<br />";
117 // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
119 // FIXME: duplicate code with different output destination.
121 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
122 'port' => $mailfetch_port,
123 'auth' => $mailfetch_auth,
124 'tls' => $mailfetch_type,
127 if (!empty($pop3->error
)) {
128 $outMsg .= _("Warning:") . ' ' . $pop3->error
;
132 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
134 /* log into pop server*/
135 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
136 $outMsg .= _("Login Failed:") . ' ' . $pop3->error
;
140 $aMsgStat = $pop3->command_stat();
141 if (is_bool($aMsgStat)) {
142 $outMsg .= _("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error
);
146 $Count = $aMsgStat['count'];
151 // If we leave messages on server, try using UIDL
152 if ($mailfetch_lmos == 'on') {
153 $msglist = $pop3->command_uidl();
154 if (is_bool($msglist)) {
155 $outMsg .= _("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error
);
156 // User asked to leave messages on server, but we can't do that.
157 $pop3->command_quit();
159 // $mailfetch_lmos = 'off';
161 // calculate number of new messages
162 for ($j = 1; $j <= sizeof($msglist); $j++
) {
163 // do strict comparison ('1111.10' should not be equal to '1111.100')
164 if ($msglist[$j] === $mailfetch_uidl) {
171 // fetch list of messages with LIST
172 // we can use else control, but we can also set $mailfetch_lmos
173 // to off if server does not support UIDL.
174 if ($mailfetch_lmos != 'on') {
175 $msglist = $pop3->command_list();
180 $pop3->command_quit();
184 $pop3->command_quit();
188 for (; $i <= $Count; $i++
) {
189 if (!ini_get('safe_mode'))
190 set_time_limit(20); // 20 seconds per message max
191 $Message = $pop3->command_retr($i);
193 if (is_bool($Message)) {
194 $outMsg .= _("Warning:") . ' ' . htmlspecialchars($pop3->error
);
198 // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
199 if ($mailfetch_subfolder=='' ||
200 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
201 fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n");
203 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
205 $Line = fgets($imap_stream, 1024);
206 if (substr($Line, 0, 1) == '+') {
207 fputs($imap_stream, $Message);
208 fputs($imap_stream, "\r\n");
209 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
211 // Check results of append command
212 $response=(implode('',$response));
213 $message=(implode('',$message));
214 if ($response != 'OK') {
215 $outMsg .= _("Error Appending Message!")." ".htmlspecialchars($message);
217 if ($mailfetch_lmos == 'on') {
218 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
220 // Destroy msg list in order to prevent UIDL update
222 // if append fails, don't download other messages
226 if ($mailfetch_lmos != 'on') {
227 $pop3->command_dele($i);
231 $outMsg .= _("Error Appending Message!");
235 $pop3->command_quit();
236 sqimap_logout($imap_stream);
237 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
238 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
243 if( trim( $outMsg ) <> '' ) {
244 echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
246 if( $mailfetch_newlog == 'on' ) {
247 setPref($data_dir, $username, 'mailfetch_newlog', 'off');
252 * Internal function used to detect new logins
254 function mail_fetch_setnew_function() {
255 global $data_dir, $username;
257 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
261 * Internal function used to register option block
265 function mailfetch_optpage_register_block_function() {
266 global $optpage_blocks;
268 $optpage_blocks[] = array(
269 'name' => _("POP3 Fetch Mail"),
270 'url' => '../plugins/mail_fetch/options.php',
271 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
277 * Internal function used to update mail_fetch settings
278 * when folders are renamed or deleted.
282 function mail_fetch_folderact_function($args) {
283 global $username, $data_dir;
285 if (empty($args) ||
!is_array($args)) {
289 /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
290 if (count($args) != 3) {
294 list($old_folder, $action, $new_folder) = $args;
296 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
298 for ($i = 0; $i < $mailfetch_server_number; $i++
) {
299 $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
301 if ($mailfetch_subfolder != $old_folder) {
305 if ($action == 'delete') {
306 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX');
307 } elseif ($action == 'rename') {
308 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder);
312 // end of hooked functions
315 * hex2bin - document me
317 function hex2bin( $data ) {
319 /* Original code by josh@superfork.com */
321 $len = strlen($data);
323 for( $i=0; $i < $len; $i +
= 2 ) {
324 $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
329 function mf_keyED( $txt ) {
333 if( !isset( $MF_TIT ) ) {
334 $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
337 $encrypt_key = md5( $MF_TIT );
340 for( $i = 0; $i < strlen( $txt ); $i++
) {
341 if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
342 $tmp.= substr( $txt, $i, 1 ) ^
substr( $encrypt_key, $ctr, 1 );
348 function encrypt( $txt ) {
350 srand( (double) microtime() * 1000000 );
351 $encrypt_key = md5( rand( 0, 32000 ) );
354 for( $i = 0; $i < strlen( $txt ); $i++
) {
355 if ($ctr==strlen($encrypt_key)) $ctr=0;
356 $tmp.= substr($encrypt_key,$ctr,1) .
357 (substr($txt,$i,1) ^
substr($encrypt_key,$ctr,1));
360 return bin2hex( mf_keyED( $tmp ) );
364 function decrypt( $txt ) {
366 $txt = mf_keyED( hex2bin( $txt ) );
368 for ( $i=0; $i < strlen( $txt ); $i++
) {
369 $md5 = substr( $txt, $i, 1 );
371 $tmp.= ( substr( $txt, $i, 1 ) ^
$md5 );
378 * @param stream $imap_stream imap connection resource
379 * @param string $imap_folder imap folder name
380 * @return boolean true, when folder can be used to store messages.
381 * @since 1.5.1 and 1.4.5
383 function mail_fetch_check_folder($imap_stream,$imap_folder) {
384 global $mail_fetch_allow_unsubscribed;
386 // check if folder is subscribed or only exists.
387 if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
389 } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
395 // make sure that folder can store messages
396 if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
404 * Checks if folder is noselect (can't store messages)
406 * Function does not check if folder subscribed.
407 * @param stream $imap_stream imap connection resource
408 * @param string $imap_folder imap folder name
409 * @return boolean true, when folder has noselect flag. false in any other case.
410 * @since 1.5.1 and 1.4.5
412 function mail_fetch_check_noselect($imap_stream,$imap_folder) {
413 $boxes=sqimap_mailbox_list($imap_stream);
414 foreach($boxes as $box) {
415 if ($box['unformatted']==$imap_folder) {
416 return (bool) check_is_noselect($box['raw']);
423 * Validate a requested POP3 port number
425 * Allowable port numbers are configured in config.php
426 * (see config_example.php for an example and more
427 * rules about how the list of allowable port numbers
430 * @param int $requested_port The port number given by the user
432 * @return string An error string is returned if the port
433 * number is not allowable, otherwise an
434 * empty string is returned.
437 function validate_mail_fetch_port_number($requested_port) {
438 global $mail_fetch_allowable_ports;
439 if (empty($mail_fetch_allowable_ports))
440 $mail_fetch_allowable_ports = array(110, 995);
442 if (in_array('ALL', $mail_fetch_allowable_ports))
445 if (!in_array($requested_port, $mail_fetch_allowable_ports)) {
446 sq_change_text_domain('mail_fetch');
447 $error = _("Sorry, that port number is not allowed");
448 sq_change_text_domain('squirrelmail');
456 * Validate a requested POP3 server address
458 * Blocked server addresses are configured in config.php
459 * (see config_example.php for more details)
461 * @param int $requested_address The server address given by the user
463 * @return string An error string is returned if the server
464 * address is not allowable, otherwise an
465 * empty string is returned.
468 function validate_mail_fetch_server_address($requested_address) {
469 global $mail_fetch_block_server_pattern;
470 if (empty($mail_fetch_block_server_pattern))
471 $mail_fetch_block_server_pattern = '/(^10\.)|(^192\.)|(^127\.)|(^localhost)/';
473 if ($mail_fetch_block_server_pattern == 'UNRESTRICTED')
476 if (preg_match($mail_fetch_block_server_pattern, $requested_address)) {
477 sq_change_text_domain('mail_fetch');
478 $error = _("Sorry, that server address is not allowed");
479 sq_change_text_domain('squirrelmail');