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] );
71 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
72 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) ||
$mailfetch_fref_[$i_loop] == 'on' ) ) {
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");
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
);
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];
94 $mailfetch_auth=$mailfetch_auth_[$i_loop];
95 $mailfetch_type=$mailfetch_type_[$i_loop];
96 // end of what the heck
99 // $outMsg .= "$mailfetch_alias checked<br />";
101 // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
103 // FIXME: duplicate code with different output destination.
105 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
106 'port' => $mailfetch_port,
107 'auth' => $mailfetch_auth,
108 'tls' => $mailfetch_type,
111 if (!empty($pop3->error
)) {
112 $outMsg .= _("Warning:") . ' ' . $pop3->error
;
116 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
118 /* log into pop server*/
119 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
120 $outMsg .= _("Login Failed:") . ' ' . $pop3->error
;
124 $aMsgStat = $pop3->command_stat();
125 if (is_bool($aMsgStat)) {
126 $outMsg .= _("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error
);
130 $Count = $aMsgStat['count'];
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();
143 // $mailfetch_lmos = 'off';
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) {
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();
164 $pop3->command_quit();
168 $pop3->command_quit();
172 for (; $i <= $Count; $i++
) {
173 if (!ini_get('safe_mode'))
174 set_time_limit(20); // 20 seconds per message max
175 $Message = $pop3->command_retr($i);
177 if (is_bool($Message)) {
178 $outMsg .= _("Warning:") . ' ' . htmlspecialchars($pop3->error
);
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");
187 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
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);
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);
201 if ($mailfetch_lmos == 'on') {
202 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
204 // Destroy msg list in order to prevent UIDL update
206 // if append fails, don't download other messages
210 if ($mailfetch_lmos != 'on') {
211 $pop3->command_dele($i);
215 $outMsg .= _("Error Appending Message!");
219 $pop3->command_quit();
220 sqimap_logout($imap_stream);
221 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
222 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
227 if( trim( $outMsg ) <> '' ) {
228 echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
230 if( $mailfetch_newlog == 'on' ) {
231 setPref($data_dir, $username, 'mailfetch_newlog', 'off');
236 * Internal function used to detect new logins
238 function mail_fetch_setnew_function() {
239 global $data_dir, $username;
241 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
245 * Internal function used to register option block
249 function mailfetch_optpage_register_block_function() {
250 global $optpage_blocks;
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."),
261 * Internal function used to update mail_fetch settings
262 * when folders are renamed or deleted.
266 function mail_fetch_folderact_function($args) {
267 global $username, $data_dir;
269 if (empty($args) ||
!is_array($args)) {
273 /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
274 if (count($args) != 3) {
278 list($old_folder, $action, $new_folder) = $args;
280 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
282 for ($i = 0; $i < $mailfetch_server_number; $i++
) {
283 $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
285 if ($mailfetch_subfolder != $old_folder) {
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);
296 // end of hooked functions
299 * hex2bin - document me
301 function hex2bin( $data ) {
303 /* Original code by josh@superfork.com */
305 $len = strlen($data);
307 for( $i=0; $i < $len; $i +
= 2 ) {
308 $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
313 function mf_keyED( $txt ) {
317 if( !isset( $MF_TIT ) ) {
318 $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
321 $encrypt_key = md5( $MF_TIT );
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 );
332 function encrypt( $txt ) {
334 srand( (double) microtime() * 1000000 );
335 $encrypt_key = md5( rand( 0, 32000 ) );
338 for( $i = 0; $i < strlen( $txt ); $i++
) {
339 if ($ctr==strlen($encrypt_key)) $ctr=0;
340 $tmp.= substr($encrypt_key,$ctr,1) .
341 (substr($txt,$i,1) ^
substr($encrypt_key,$ctr,1));
344 return bin2hex( mf_keyED( $tmp ) );
348 function decrypt( $txt ) {
350 $txt = mf_keyED( hex2bin( $txt ) );
352 for ( $i=0; $i < strlen( $txt ); $i++
) {
353 $md5 = substr( $txt, $i, 1 );
355 $tmp.= ( substr( $txt, $i, 1 ) ^
$md5 );
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.
365 * @since 1.5.1 and 1.4.5
367 function mail_fetch_check_folder($imap_stream,$imap_folder) {
368 global $mail_fetch_allow_unsubscribed;
370 // check if folder is subscribed or only exists.
371 if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
373 } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
379 // make sure that folder can store messages
380 if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
388 * Checks if folder is noselect (can't store messages)
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.
394 * @since 1.5.1 and 1.4.5
396 function 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']);