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-2007 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;
28 * Controls use of unsubscribed folders in plugin
29 * @global boolean $mail_fetch_allow_unsubscribed
30 * @since 1.5.1 and 1.4.5
32 $mail_fetch_allow_unsubscribed = false;
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');
44 * Internal function used to fetch pop3 mails on login
48 function mail_fetch_login_function() {
49 include_once (SM_PATH
. 'functions/imap_general.php');
51 global $username, $data_dir, $imapServerAddress, $imapPort;
53 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
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;
62 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++
) {
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] );
70 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
71 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) ||
$mailfetch_fref_[$i_loop] == 'on' ) ) {
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");
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
);
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];
93 $mailfetch_auth=$mailfetch_auth_[$i_loop];
94 $mailfetch_type=$mailfetch_type_[$i_loop];
95 // end of what the heck
98 // $outMsg .= "$mailfetch_alias checked<br />";
100 // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
102 // FIXME: duplicate code with different output destination.
104 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
105 'port' => $mailfetch_port,
106 'auth' => $mailfetch_auth,
107 'tls' => $mailfetch_type,
110 if (!empty($pop3->error
)) {
111 $outMsg .= _("Warning:") . ' ' . $pop3->error
;
115 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
117 /* log into pop server*/
118 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
119 $outMsg .= _("Login Failed:") . ' ' . $pop3->error
;
123 $aMsgStat = $pop3->command_stat();
124 if (is_bool($aMsgStat)) {
125 $outMsg .= _("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error
);
129 $Count = $aMsgStat['count'];
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();
142 // $mailfetch_lmos = 'off';
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) {
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();
163 $pop3->command_quit();
167 $pop3->command_quit();
171 for (; $i <= $Count; $i++
) {
172 if (!ini_get('safe_mode'))
173 set_time_limit(20); // 20 seconds per message max
174 $Message = $pop3->command_retr($i);
176 if (is_bool($Message)) {
177 $outMsg .= _("Warning:") . ' ' . htmlspecialchars($pop3->error
);
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");
186 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
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);
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);
200 if ($mailfetch_lmos == 'on') {
201 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
203 // Destroy msg list in order to prevent UIDL update
205 // if append fails, don't download other messages
209 if ($mailfetch_lmos != 'on') {
210 $pop3->command_dele($i);
214 $outMsg .= _("Error Appending Message!");
218 $pop3->command_quit();
219 sqimap_logout($imap_stream);
220 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
221 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
226 if( trim( $outMsg ) <> '' ) {
227 echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
229 if( $mailfetch_newlog == 'on' ) {
230 setPref($data_dir, $username, 'mailfetch_newlog', 'off');
235 * Internal function used to detect new logins
237 function mail_fetch_setnew_function() {
238 global $data_dir, $username;
240 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
244 * Internal function used to register option block
248 function mailfetch_optpage_register_block_function() {
249 global $optpage_blocks;
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."),
260 * Internal function used to update mail_fetch settings
261 * when folders are renamed or deleted.
265 function mail_fetch_folderact_function($args) {
266 global $username, $data_dir;
268 if (empty($args) ||
!is_array($args)) {
272 /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
273 if (count($args) != 3) {
277 list($old_folder, $action, $new_folder) = $args;
279 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
281 for ($i = 0; $i < $mailfetch_server_number; $i++
) {
282 $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
284 if ($mailfetch_subfolder != $old_folder) {
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);
295 // end of hooked functions
298 * hex2bin - document me
300 function hex2bin( $data ) {
302 /* Original code by josh@superfork.com */
304 $len = strlen($data);
306 for( $i=0; $i < $len; $i +
= 2 ) {
307 $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
312 function mf_keyED( $txt ) {
316 if( !isset( $MF_TIT ) ) {
317 $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
320 $encrypt_key = md5( $MF_TIT );
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 );
331 function encrypt( $txt ) {
333 srand( (double) microtime() * 1000000 );
334 $encrypt_key = md5( rand( 0, 32000 ) );
337 for( $i = 0; $i < strlen( $txt ); $i++
) {
338 if ($ctr==strlen($encrypt_key)) $ctr=0;
339 $tmp.= substr($encrypt_key,$ctr,1) .
340 (substr($txt,$i,1) ^
substr($encrypt_key,$ctr,1));
343 return bin2hex( mf_keyED( $tmp ) );
347 function decrypt( $txt ) {
349 $txt = mf_keyED( hex2bin( $txt ) );
351 for ( $i=0; $i < strlen( $txt ); $i++
) {
352 $md5 = substr( $txt, $i, 1 );
354 $tmp.= ( substr( $txt, $i, 1 ) ^
$md5 );
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.
364 * @since 1.5.1 and 1.4.5
366 function mail_fetch_check_folder($imap_stream,$imap_folder) {
367 global $mail_fetch_allow_unsubscribed;
369 // check if folder is subscribed or only exists.
370 if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
372 } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
378 // make sure that folder can store messages
379 if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
387 * Checks if folder is noselect (can't store messages)
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.
393 * @since 1.5.1 and 1.4.5
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']);