bdcfd71d4dc8789958c3e9f79d8c80585be4b82b
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-2006 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/class.POP3.php');
23 /** declare plugin globals */
24 global $mail_fetch_allow_unsubscribed;
27 * Controls use of unsubscribed folders in plugin
28 * @global boolean $mail_fetch_allow_unsubscribed
29 * @since 1.5.1 and 1.4.5
31 $mail_fetch_allow_unsubscribed = false;
33 /** load site config */
34 if (file_exists(SM_PATH
. 'config/mail_fetch_config.php')) {
35 include_once(SM_PATH
. 'config/mail_fetch_config.php');
36 } elseif (file_exists(SM_PATH
. 'plugins/mail_fetch/config.php')) {
37 include_once(SM_PATH
. 'plugins/mail_fetch/config.php');
43 * internal function used to load user's preferences
47 function mail_fetch_load_pref_function() {
49 global $mailfetch_server_number;
50 global $mailfetch_cypher, $mailfetch_port_;
51 global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
52 global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
55 sqgetGlobalVar('username', $username, SQ_SESSION
);
57 if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
58 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number', 0);
59 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher', 'on' );
60 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
61 for ($i=0;$i<$mailfetch_server_number;$i++
) {
62 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
63 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
64 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
65 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
66 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
67 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
68 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
69 $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
70 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
71 if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
77 * Internal function used to fetch pop3 mails on login
81 function mail_fetch_login_function() {
82 //include_once (SM_PATH . 'include/validate.php');
83 include_once (SM_PATH
. 'functions/imap_general.php');
85 global $data_dir, $imapServerAddress, $imapPort;
87 sqgetGlobalVar('username', $username, SQ_SESSION
);
88 sqgetGlobalVar('key', $key, SQ_COOKIE
);
90 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
94 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
95 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
96 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
97 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
99 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++
) {
101 $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
102 $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
103 $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
104 if( $mailfetch_cypher == 'on' )
105 $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
107 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
108 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) ||
$mailfetch_fref_[$i_loop] == 'on' ) ) {
110 $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
111 $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
112 $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
113 $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
114 $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
115 $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
116 $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
118 $mailfetch_server=$mailfetch_server_[$i_loop];
119 $mailfetch_port=$mailfetch_port_[$i_loop];
120 $mailfetch_user=$mailfetch_user_[$i_loop];
121 $mailfetch_alias=$mailfetch_alias_[$i_loop];
122 $mailfetch_pass=$mailfetch_pass_[$i_loop];
123 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
124 $mailfetch_login=$mailfetch_login_[$i_loop];
125 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
126 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
128 // $outMsg .= "$mailfetch_alias checked<br />";
130 // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
132 $pop3 = new POP3($mailfetch_server, 60);
134 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
135 $outMsg .= _("Warning:") . ' ' . $pop3->ERROR
;
139 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
141 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
142 if (($Count == false ||
$Count == -1) && $pop3->ERROR
!= '') {
143 $outMsg .= _("Login Failed:") . ' ' . $pop3->ERROR
;
147 // register_shutdown_function($pop3->quit());
149 $msglist = $pop3->uidl();
152 for ($j = 1; $j < sizeof($msglist); $j++
) {
153 if ($msglist["$j"] == $mailfetch_uidl) {
168 // Faster to get them all at once
169 $mailfetch_uidl = $pop3->uidl();
171 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
172 $outMsg .= _("Server does not support UIDL.");
174 for (; $i <= $Count; $i++
) {
175 if (!ini_get('safe_mode'))
176 set_time_limit(20); // 20 seconds per message max
178 $MessArray = $pop3->get($i);
180 if ( (!$MessArray) or (gettype($MessArray) != "array")) {
181 $outMsg .= _("Warning:") . ' ' . $pop3->ERROR
;
185 while (list($lineNum, $line) = each ($MessArray)) {
189 // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
190 if ($mailfetch_subfolder=='' ||
191 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
192 fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n");
194 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
196 $Line = fgets($imap_stream, 1024);
197 if (substr($Line, 0, 1) == '+') {
198 fputs($imap_stream, $Message);
199 fputs($imap_stream, "\r\n");
200 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
202 if ($mailfetch_lmos != 'on') {
207 $outMsg .= _("Error Appending Message!");
212 sqimap_logout($imap_stream);
213 if (is_array($mailfetch_uidl)) {
214 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
219 if( trim( $outMsg ) <> '' ) {
220 echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
222 if( $mailfetch_newlog == 'on' ) {
223 setPref($data_dir, $username, 'mailfetch_newlog', 'off');
228 * Internal function used to detect new logins
230 function mail_fetch_setnew_function() {
233 sqgetGlobalVar('username', $username, SQ_SESSION
);
234 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
238 * Internal function used to register option block
242 function mailfetch_optpage_register_block_function() {
243 global $optpage_blocks;
245 $optpage_blocks[] = array(
246 'name' => _("POP3 Fetch Mail"),
247 'url' => '../plugins/mail_fetch/options.php',
248 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
254 * Internal function used to update mail_fetch settings
255 * when folders are renamed or deleted.
259 function mail_fetch_folderact_function($args) {
260 global $username, $data_dir;
262 if (empty($args) ||
!is_array($args)) {
266 /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
267 if (count($args) != 3) {
271 list($old_folder, $action, $new_folder) = $args;
273 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
275 for ($i = 0; $i < $mailfetch_server_number; $i++
) {
276 $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
278 if ($mailfetch_subfolder != $old_folder) {
282 if ($action == 'delete') {
283 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX');
284 } elseif ($action == 'rename') {
285 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder);
289 // end of hooked functions
292 * hex2bin - document me
294 function hex2bin( $data ) {
296 /* Original code by josh@superfork.com */
298 $len = strlen($data);
300 for( $i=0; $i < $len; $i +
= 2 ) {
301 $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
306 function mf_keyED( $txt ) {
310 if( !isset( $MF_TIT ) ) {
311 $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
314 $encrypt_key = md5( $MF_TIT );
317 for( $i = 0; $i < strlen( $txt ); $i++
) {
318 if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
319 $tmp.= substr( $txt, $i, 1 ) ^
substr( $encrypt_key, $ctr, 1 );
325 function encrypt( $txt ) {
327 srand( (double) microtime() * 1000000 );
328 $encrypt_key = md5( rand( 0, 32000 ) );
331 for( $i = 0; $i < strlen( $txt ); $i++
) {
332 if ($ctr==strlen($encrypt_key)) $ctr=0;
333 $tmp.= substr($encrypt_key,$ctr,1) .
334 (substr($txt,$i,1) ^
substr($encrypt_key,$ctr,1));
337 return bin2hex( mf_keyED( $tmp ) );
341 function decrypt( $txt ) {
343 $txt = mf_keyED( hex2bin( $txt ) );
345 for ( $i=0; $i < strlen( $txt ); $i++
) {
346 $md5 = substr( $txt, $i, 1 );
348 $tmp.= ( substr( $txt, $i, 1 ) ^
$md5 );
355 * @param stream $imap_stream imap connection resource
356 * @param string $imap_folder imap folder name
357 * @return boolean true, when folder can be used to store messages.
358 * @since 1.5.1 and 1.4.5
360 function mail_fetch_check_folder($imap_stream,$imap_folder) {
361 global $mail_fetch_allow_unsubscribed;
363 // check if folder is subscribed or only exists.
364 if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
366 } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
372 // make sure that folder can store messages
373 if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
381 * Checks if folder is noselect (can't store messages)
383 * Function does not check if folder subscribed.
384 * @param stream $imap_stream imap connection resource
385 * @param string $imap_folder imap folder name
386 * @return boolean true, when folder has noselect flag. false in any other case.
387 * @since 1.5.1 and 1.4.5
389 function mail_fetch_check_noselect($imap_stream,$imap_folder) {
390 $boxes=sqimap_mailbox_list($imap_stream);
391 foreach($boxes as $box) {
392 if ($box['unformatted']==$imap_folder) {
393 return (bool) check_is_noselect($box['raw']);