POP3 class code replaced with mail_fetch class code.
authortokul <tokul@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Sat, 26 Aug 2006 17:33:58 +0000 (17:33 +0000)
committertokul <tokul@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Sat, 26 Aug 2006 17:33:58 +0000 (17:33 +0000)
Added APOP and TLS and STLS support.
If port is set to 0, system uses default ports
Removed loading_prefs hook. It is not needed. Plugin always gets prefs
before fetching emails.
Fixed IMAP error handling in left_main. Older code ignored APPEND errors.

git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@11646 7612ce4b-ef26-0410-bec9-ea0150e637f0

plugins/mail_fetch/class.mail_fetch.php
plugins/mail_fetch/fetch.php
plugins/mail_fetch/functions.php
plugins/mail_fetch/options.php
plugins/mail_fetch/setup.php

index 0d1867520c7a2f90f7499cd0ddc3df5be2f3448b..6e24339427e956b9c3a31f24f573a88b76a4409e 100644 (file)
@@ -31,7 +31,8 @@
  * 3. login($username,$password) - true = login successful, false = login error.
  * 4. command_stat() - get number of messages
  * 5. command_list() - get message ids, use command_uidl(), if you implement 
- * 'keep mess on server' functions.
+ * 'keep mess on server' functions. Make sure that you handle possible UIDL 
+ * command errors.
  * 6. command_retr($some_message_id) - get message contents
  * 7. command_dele($some_message_id) - mark message for deletion
  * 8. command_quit() - close connection. You must close connection in order 
@@ -58,7 +59,7 @@ class mail_fetch {
      * 0 - plain text (default)
      * 1 - tls (php 4.3 and openssl extension requirement)
      * 2 - stls (stream_socket_enable_crypto() requirement. PHP 5.1.0, POP3
-     *     server with POP3EXT and STLS support) (untested)
+     *     server with POP3EXT and STLS support)
      * @var integer
      */
     var $tls = 0;
@@ -112,6 +113,8 @@ class mail_fetch {
     var $error = '';
 
     /**
+     * Response buffer
+     *
      * Variable is used to store last positive POP server response 
      * checked in check_response() method. Used internally to handle
      * mixed single and multiline command responses.
@@ -325,7 +328,10 @@ class mail_fetch {
      * @return mixed array with message ids (keys) and sizes (values) or boolean false
      */
     function command_list($msg='') {
-        fwrite($this->conn,"LIST $msg\r\n");
+        // add space between command and msg_id
+        if(!empty($msg)) $msg = ' ' . $msg;
+
+        fwrite($this->conn,"LIST$msg\r\n");
         
         if($this->check_response()) {
             $ret = array();
@@ -443,19 +449,23 @@ class mail_fetch {
      * or boolean false
      */
     function command_uidl($msg='') {
-        fwrite($this->conn,"UIDL $msg\r\n");
+        //return $this->set_error('Unsupported command.');
+        // add space between command and msg_id
+        if(!empty($msg)) $msg = ' ' . $msg;
+        fwrite($this->conn,"UIDL$msg\r\n");
         if($this->check_response()) {
             $ids = array();
             if (!empty($msg)) {
                 list($ok,$msg_id,$unique_id) = explode(' ',trim($this->response));
-                $ids[$msg_id] = $unique_id;
+                $ids[$msg_id] = "$unique_id";
             } else {
                 while($line = fgets($this->conn)) {
                     if (trim($line)=='.') {
                         break;
                     } else {
                         list($msg_id,$unique_id) = explode(' ',trim($line));
-                        $ids[$msg_id] = $unique_id;
+                        // make sure that unique_id is a string.
+                        $ids[$msg_id] = "$unique_id";
                     }
                 }
             }
@@ -549,12 +559,13 @@ class mail_fetch {
     function command_stls() {
         if (! function_exists('stream_socket_enable_crypto')) {
             return $this->set_error('Used PHP version does not support functions required for POP STLS.',true);
-        } elseif (in_array('STLS',$this->capabilities)) {
+        } elseif (in_array('STLS',$this->capabilities)) {
             return $this->set_error('Selected POP3 server does not support STLS.',true);
         }
         fwrite($this->conn,"STLS\r\n");
         if (! $this->check_response()) {
-            return false;
+           $this->command_quit();
+           return false;
         }
 
         if (@stream_socket_enable_crypto($this->conn,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
index c22928fca7bd7c2e694e7d2344494e206ca671f8..b3b75491e74453587ffb1fd51aeaa659a1374b96 100644 (file)
 require('../../include/init.php');
 
 include_once(SM_PATH . 'functions/imap_general.php');
-include_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php');
 include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
 
 /* globals */
 sqgetGlobalVar('delimiter',  $delimiter,  SQ_SESSION);
 /* end globals */
 
+/**
+ * @param string $msg message
+ */
 function Mail_Fetch_Status($msg) {
     echo html_tag( 'table',
              html_tag( 'tr',
@@ -34,14 +36,19 @@ function Mail_Fetch_Status($msg) {
     flush();
 }
 
+/**
+ * @return array
+ */
 function Mail_Fetch_Servers() {
     global $data_dir, $username;
 
+    $mailfetch = array();
     $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
     if (!isset($mailfetch['server_number']) || ($mailfetch['server_number'] < 1)) {
         $mailfetch['server_number'] = 0;
     }
     $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
+
     for ($i = 0; $i < $mailfetch['server_number']; $i++) {
         $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
         $mailfetch[$i]['port']   = getPref($data_dir, $username, "mailfetch_port_$i");
@@ -61,10 +68,17 @@ function Mail_Fetch_Servers() {
         if($mailfetch[$i]['alias'] == '') {
             $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
         }
+        // Authentication type (added in 1.5.2)
+        $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER);
+        // Connection type (added in 1.5.2)
+        $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN);
     }
-        return $mailfetch;
+    return $mailfetch;
 }
 
+/**
+ * @param array $mailfetch
+ */
 function Mail_Fetch_Select_Server($mailfetch) {
     global $PHP_SELF;
 
@@ -107,7 +121,7 @@ function Mail_Fetch_Select_Server($mailfetch) {
 $mailfetch = Mail_Fetch_Servers();
 displayPageHeader($color, 'None');
 
-echo '<br /><div style="text-align: center;">';
+echo '<br />';
 
 echo html_tag( 'table',
          html_tag( 'tr',
@@ -121,13 +135,14 @@ if($mailfetch['server_number'] == 0) {
     echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
     displayInternalLink('plugins/mail_fetch/options.php',
                         _("Click here to go to the options page.") );
-    echo '</body></html>';
+    $oTemplate->display('footer.tpl');
     exit();
 }
 
 // get $server_to_fetch from globals, if not set display a choice to the user
 if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
     Mail_Fetch_Select_Server($mailfetch);
+    $oTemplate->display('footer.tpl');
     exit();
 }
 
@@ -148,8 +163,8 @@ for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
     $mailfetch_login  = $mailfetch[$i_loop]['login'];
     $mailfetch_uidl   = $mailfetch[$i_loop]['uidl'];
     $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
-
-    $pop3 = new POP3($mailfetch_server, 60);
+    $mailfetch_auth = $mailfetch[$i_loop]['auth'];
+    $mailfetch_type = $mailfetch[$i_loop]['type'];
 
     echo '<br />' .
         html_tag( 'table',
@@ -164,8 +179,14 @@ for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
 
     flush();
 
-    if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
-        Mail_Fetch_Status($pop3->ERROR );
+    $pop3 = new mail_fetch(array('host'    => $mailfetch_server,
+                                 'port'    => $mailfetch_port,
+                                 'auth'    => $mailfetch_auth,
+                                 'tls'     => $mailfetch_type,
+                                 'timeout' => 60));
+
+    if (!empty($pop3->error)) {
+        Mail_Fetch_Status($pop3->error);
         continue;
     }
 
@@ -179,32 +200,62 @@ for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
     }
 
     Mail_Fetch_Status(_("Opening POP server"));
-    $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
-    if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
-        Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) );
+
+    /* log into pop server*/
+    if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
+        Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
         continue;
     }
 
-    //   register_shutdown_function($pop3->quit());
+    $aMsgStat = $pop3->command_stat();
+    if (is_bool($aMsgStat)) {
+        Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
+        continue;
+    }
 
-    $msglist = $pop3->uidl();
+    $Count = $aMsgStat['count'];
 
     $i = 1;
-    for ($j = 1; $j < sizeof($msglist); $j++) {
-        if ($msglist[$j] == $mailfetch_uidl) {
-            $i = $j+1;
-            break;
+
+    if ($Count>0) {
+        // If we leave messages on server, try using UIDL
+        if ($mailfetch_lmos == 'on') {
+            Mail_Fetch_Status(_("Fetching UIDL..."));
+            $msglist = $pop3->command_uidl();
+            if (is_bool($msglist)) {
+                Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error));
+                // User asked to leave messages on server, but we can't do that.
+                $pop3->command_quit();
+                continue;
+                // $mailfetch_lmos = 'off';
+            } else {
+                // calculate number of new messages
+                for ($j = 1; $j <= sizeof($msglist); $j++) {
+                    // do strict comparison ('1111.10' should not be equal to '1111.100')
+                    if ($msglist[$j] === $mailfetch_uidl) {
+                        $i = $j+1;
+                        break;
+                    }
+                }
+            }
+        }
+        // fetch list of messages with LIST
+        // we can use else control, but we can also set $mailfetch_lmos 
+        // to off if server does not support UIDL.
+        if ($mailfetch_lmos != 'on') {
+            Mail_Fetch_Status(_("Fetching list of messages..."));
+            $msglist = $pop3->command_list();
         }
     }
 
     if ($Count < $i) {
         Mail_Fetch_Status(_("Login OK: No new messages"));
-        $pop3->quit();
+        $pop3->command_quit();
         continue;
     }
     if ($Count == 0) {
         Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
-        $pop3->quit();
+        $pop3->command_quit();
         continue;
     } else {
         $newmsgcount = $Count - $i + 1;
@@ -212,13 +263,6 @@ for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
                                            "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
     }
 
-    Mail_Fetch_Status(_("Fetching UIDL..."));
-    // Faster to get them all at once
-    $mailfetch_uidl = $pop3->uidl();
-
-    if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
-        Mail_Fetch_Status(_("Server does not support UIDL."));
-
     if ($mailfetch_lmos == 'on') {
         Mail_Fetch_Status(_("Leaving mail on server..."));
     } else {
@@ -230,37 +274,12 @@ for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
 
         if (!ini_get('safe_mode'))
             set_time_limit(20); // 20 seconds per message max
-        $Message = '';
-        $MessArray = $pop3->get($i);
-
-        while ( (!$MessArray) or (gettype($MessArray) != "array")) {
-            Mail_Fetch_Status($pop3->ERROR);
-            // re-connect pop3
-            Mail_Fetch_Status(_("Server error. Disconnect"));
-            $pop3->quit();
-            Mail_Fetch_Status(_("Reconnect from dead connection"));
-            if (!$pop3->connect($mailfetch_server)) {
-                Mail_Fetch_Status($pop3->ERROR );
-                Mail_Fetch_Status(_("Saving UIDL"));
-                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
 
-                continue;
-            }
-            $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
-            if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
-                Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
-                Mail_Fetch_Status(_("Saving UIDL"));
-                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
+        $Message = $pop3->command_retr($i);
 
-                continue;
-            }
-            Mail_Fetch_Status(sprintf(_("Refetching message %s."), $i));
-            $MessArray = $pop3->get($i);
-
-        } // end while
-
-        while (list($lineNum, $line) = each ($MessArray)) {
-            $Message .= $line;
+        if (is_bool($Message)) {
+            Mail_Fetch_Status(htmlspecialchars($pop3->error));
+            continue;
         }
 
         fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
@@ -274,51 +293,54 @@ for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
             if ($response != 'OK') {
                 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
                 Mail_Fetch_Status(_("Closing POP"));
-                $pop3->quit();
+                $pop3->command_quit();
                 Mail_Fetch_Status(_("Logging out from IMAP"));
                 sqimap_logout($imap_stream);
 
-                Mail_Fetch_Status(_("Saving UIDL"));
-                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
+                if ($mailfetch_lmos == 'on') {
+                    Mail_Fetch_Status(_("Saving UIDL"));
+                    setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
+                }
                 exit;
             } else {
                 Mail_Fetch_Status(_("Message appended to mailbox"));
             }
 
             if ($mailfetch_lmos != 'on') {
-                if( $pop3->delete($i) ) {
+                if( $pop3->command_dele($i) ) {
                     Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
                 } else {
-                    Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) );
+                    Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
                 }
             }
         } else {
             echo $Line;
             Mail_Fetch_Status(_("Error Appending Message!"));
             Mail_Fetch_Status(_("Closing POP"));
-            $pop3->quit();
+            $pop3->command_quit();
             Mail_Fetch_Status(_("Logging out from IMAP"));
             sqimap_logout($imap_stream);
 
             // not gurantee corect!
-            Mail_Fetch_Status(_("Saving UIDL"));
-            setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
+            if ($mailfetch_lmos == 'on') {
+                Mail_Fetch_Status(_("Saving UIDL"));
+                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
+            }
             exit;
         }
     }
 
     Mail_Fetch_Status(_("Closing POP"));
-    $pop3->quit();
+    $pop3->command_quit();
     Mail_Fetch_Status(_("Logging out from IMAP"));
     sqimap_logout($imap_stream);
-    if (is_array($mailfetch_uidl)) {
+    if ($mailfetch_lmos == 'on' && is_array($msglist)) {
         Mail_Fetch_Status(_("Saving UIDL"));
-        setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
+        setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
     }
 
     Mail_Fetch_Status(_("Done"));
 }
-?>
-</div>
-</body>
-</html>
+
+$oTemplate->display('footer.tpl');
+
index bedab89fdaafc7eb01fa35afc7820180160e2632..ca9b0630da68d84d466aaad3d6cff631edd25772 100644 (file)
@@ -18,7 +18,8 @@
 
 
 /** pop3 class */
-include_once (SM_PATH . 'plugins/mail_fetch/class.POP3.php');
+include_once (SM_PATH . 'plugins/mail_fetch/constants.php');
+include_once (SM_PATH . 'plugins/mail_fetch/class.mail_fetch.php');
 
 /** declare plugin globals */
 global $mail_fetch_allow_unsubscribed;
@@ -39,45 +40,12 @@ if (file_exists(SM_PATH . 'config/mail_fetch_config.php')) {
 
 // hooked functions
 
-/**
- * internal function used to load user's preferences
- * @since 1.5.1
- * @private
- */
-function  mail_fetch_load_pref_function() {
-    global $data_dir, $username;
-    global $mailfetch_server_number;
-    global $mailfetch_cypher, $mailfetch_port_;
-    global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
-    global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
-    global $PHP_SELF;
-
-    if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
-        $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number', 0);
-        $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher', 'on' );
-        if ($mailfetch_server_number<1) $mailfetch_server_number=0;
-        for ($i=0;$i<$mailfetch_server_number;$i++) {
-            $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
-            $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
-            $mailfetch_alias_[$i]  = getPref($data_dir, $username, "mailfetch_alias_$i");
-            $mailfetch_user_[$i]   = getPref($data_dir, $username, "mailfetch_user_$i");
-            $mailfetch_pass_[$i]   = getPref($data_dir, $username, "mailfetch_pass_$i");
-            $mailfetch_lmos_[$i]   = getPref($data_dir, $username, "mailfetch_lmos_$i");
-            $mailfetch_login_[$i]  = getPref($data_dir, $username, "mailfetch_login_$i");
-            $mailfetch_fref_[$i]   = getPref($data_dir, $username, "mailfetch_fref_$i");
-            $mailfetch_uidl_[$i]   = getPref($data_dir, $username, "mailfetch_uidl_$i");
-            if( $mailfetch_cypher   == 'on' ) $mailfetch_pass_[$i] =    decrypt( $mailfetch_pass_[$i] );
-        }
-    }
-}
-
 /**
  * Internal function used to fetch pop3 mails on login
  * @since 1.5.1
  * @private
  */
 function mail_fetch_login_function() {
-    //include_once (SM_PATH . 'include/validate.php');
     include_once (SM_PATH . 'functions/imap_general.php');
 
     global $username, $data_dir, $imapServerAddress, $imapPort;
@@ -102,6 +70,7 @@ function mail_fetch_login_function() {
         if( $mailfetch_pass_[$i_loop] <> '' &&          // Empty passwords no allowed
                 ( ( $mailfetch_login_[$i_loop] == 'on' &&  $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
 
+            // What the heck
             $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
             $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
             $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
@@ -109,6 +78,8 @@ function mail_fetch_login_function() {
             $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
             $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
             $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
+            $mailfetch_auth_[$i_loop] = getPref($data_dir, $username, "mailfetch_auth_$i_loop",MAIL_FETCH_AUTH_USER);
+            $mailfetch_type_[$i_loop] = getPref($data_dir, $username, "mailfetch_type_$i_loop",MAIL_FETCH_USE_PLAIN);
 
             $mailfetch_server=$mailfetch_server_[$i_loop];
             $mailfetch_port=$mailfetch_port_[$i_loop];
@@ -119,66 +90,92 @@ function mail_fetch_login_function() {
             $mailfetch_login=$mailfetch_login_[$i_loop];
             $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
             $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
+            $mailfetch_auth=$mailfetch_auth_[$i_loop];
+            $mailfetch_type=$mailfetch_type_[$i_loop];
+            // end of what the heck
+
 
             // $outMsg .= "$mailfetch_alias checked<br />";
 
             // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
 
-            $pop3 = new POP3($mailfetch_server, 60);
+            // FIXME: duplicate code with different output destination.
+
+            $pop3 = new mail_fetch(array('host'    => $mailfetch_server,
+                                         'port'    => $mailfetch_port,
+                                         'auth'    => $mailfetch_auth,
+                                         'tls'     => $mailfetch_type,
+                                         'timeout' => 60));
 
-            if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
-                $outMsg .= _("Warning:") . ' ' . $pop3->ERROR;
+            if (!empty($pop3->error)) {
+                $outMsg .= _("Warning:") . ' ' . $pop3->error;
                 continue;
             }
 
             $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
 
-            $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
-            if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
-                $outMsg .= _("Login Failed:") . ' ' . $pop3->ERROR;
+            /* log into pop server*/
+            if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
+                $outMsg .= _("Login Failed:") . ' ' . $pop3->error;
                 continue;
             }
 
-            //   register_shutdown_function($pop3->quit());
+            $aMsgStat = $pop3->command_stat();
+            if (is_bool($aMsgStat)) {
+                $outMsg .= _("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error);
+                continue;
+            }
 
-            $msglist = $pop3->uidl();
+            $Count = $aMsgStat['count'];
 
             $i = 1;
-            for ($j = 1; $j < sizeof($msglist); $j++) {
-                if ($msglist["$j"] == $mailfetch_uidl) {
-                    $i = $j+1;
-                    break;
+
+            if ($Count>0) {
+                // If we leave messages on server, try using UIDL
+                if ($mailfetch_lmos == 'on') {
+                    $msglist = $pop3->command_uidl();
+                    if (is_bool($msglist)) {
+                        $outMsg .= _("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error);
+                        // User asked to leave messages on server, but we can't do that.
+                        $pop3->command_quit();
+                        continue;
+                        // $mailfetch_lmos = 'off';
+                    } else {
+                        // calculate number of new messages
+                        for ($j = 1; $j <= sizeof($msglist); $j++) {
+                            // do strict comparison ('1111.10' should not be equal to '1111.100')
+                            if ($msglist[$j] === $mailfetch_uidl) {
+                                $i = $j+1;
+                                break;
+                            }
+                        }
+                    }
+                }
+                // fetch list of messages with LIST
+                // we can use else control, but we can also set $mailfetch_lmos 
+                // to off if server does not support UIDL.
+                if ($mailfetch_lmos != 'on') {
+                    $msglist = $pop3->command_list();
                 }
             }
 
             if ($Count < $i) {
-                $pop3->quit();
+                $pop3->command_quit();
                 continue;
             }
             if ($Count == 0) {
-                $pop3->quit();
+                $pop3->command_quit();
                 continue;
             }
 
-            // Faster to get them all at once
-            $mailfetch_uidl = $pop3->uidl();
-
-            if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
-                $outMsg .= _("Server does not support UIDL.");
-
             for (; $i <= $Count; $i++) {
                 if (!ini_get('safe_mode'))
                     set_time_limit(20); // 20 seconds per message max
-                $Message = "";
-                $MessArray = $pop3->get($i);
-
-                if ( (!$MessArray) or (gettype($MessArray) != "array")) {
-                    $outMsg .= _("Warning:") . ' ' . $pop3->ERROR;
-                    continue 2;
-                }
+                $Message = $pop3->command_retr($i);
 
-                while (list($lineNum, $line) = each ($MessArray)) {
-                    $Message .= $line;
+                if (is_bool($Message)) {
+                    $outMsg .= _("Warning:") . ' ' . htmlspecialchars($pop3->error);
+                    continue;
                 }
 
                 // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
@@ -194,8 +191,23 @@ function mail_fetch_login_function() {
                     fputs($imap_stream, "\r\n");
                     sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
 
+                    // Check results of append command
+                    $response=(implode('',$response));
+                    $message=(implode('',$message));
+                    if ($response != 'OK') {
+                        $outMsg .= _("Error Appending Message!")." ".htmlspecialchars($message);
+
+                        if ($mailfetch_lmos == 'on') {
+                            setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
+                        }
+                        // Destroy msg list in order to prevent UIDL update
+                        $msglist = false;
+                        // if append fails, don't download other messages
+                        break;
+                    }
+
                     if ($mailfetch_lmos != 'on') {
-                        $pop3->delete($i);
+                        $pop3->command_dele($i);
                     }
                 } else {
                     echo "$Line";
@@ -203,10 +215,10 @@ function mail_fetch_login_function() {
                 }
             }
 
-            $pop3->quit();
+            $pop3->command_quit();
             sqimap_logout($imap_stream);
-            if (is_array($mailfetch_uidl)) {
-                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
+            if ($mailfetch_lmos == 'on' && is_array($msglist)) {
+                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
             }
         }
     }
index caf1f7fff632fc8b5b895048de50791e11d302c5..4f9abd17e8db460b7b5ad3fa641176d6bbbe366e 100644 (file)
@@ -16,8 +16,9 @@
  * Include the SquirrelMail initialization file.
  */
 require('../../include/init.php');
-
+include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
 include_once(SM_PATH . 'functions/imap_general.php');
+include_once(SM_PATH . 'functions/forms.php');
 
 /* globals */
 sqgetGlobalVar('delimiter',  $delimiter,  SQ_SESSION);
@@ -47,6 +48,8 @@ sqgetGlobalVar('mf_subfolder',     $mf_subfolder,     SQ_POST);
 sqgetGlobalVar('mf_login',         $mf_login,         SQ_POST);
 sqgetGlobalVar('mf_fref',          $mf_fref,          SQ_POST);
 sqgetGlobalVar('mf_lmos',          $mf_lmos,          SQ_POST);
+sqgetGlobalVar('mf_auth',          $mf_auth,          SQ_POST);
+sqgetGlobalVar('mf_type',          $mf_type,          SQ_POST);
 sqgetGlobalVar('submit_mailfetch', $submit_mailfetch, SQ_POST);
 
 
@@ -68,6 +71,8 @@ switch( $mf_action ) {
      setPref($data_dir,$username,"mailfetch_login_$mf_sn",(isset($mf_login)?$mf_login:""));
      setPref($data_dir,$username,"mailfetch_fref_$mf_sn",(isset($mf_fref)?$mf_fref:""));
      setPref($data_dir,$username,"mailfetch_subfolder_$mf_sn",(isset($mf_subfolder)?$mf_subfolder:""));
+     setPref($data_dir,$username,"mailfetch_auth_$mf_sn",(isset($mf_auth)?$mf_auth:MAIL_FETCH_AUTH_USER));
+     setPref($data_dir,$username,"mailfetch_type_$mf_sn",(isset($mf_type)?$mf_type:MAIL_FETCH_USE_PLAIN));
      $mf_sn++;
      setPref($data_dir,$username,'mailfetch_server_number', $mf_sn);
      $mf_action = 'config';
@@ -85,6 +90,8 @@ switch( $mf_action ) {
      setPref($data_dir,$username,"mailfetch_login_$mf_sn",(isset($mf_login)?$mf_login:""));
      setPref($data_dir,$username,"mailfetch_fref_$mf_sn",(isset($mf_fref)?$mf_fref:""));
      setPref($data_dir,$username,"mailfetch_subfolder_$mf_sn",(isset($mf_subfolder)?$mf_subfolder:""));
+     setPref($data_dir,$username,"mailfetch_auth_$mf_sn",(isset($mf_auth)?$mf_auth:MAIL_FETCH_AUTH_USER));
+     setPref($data_dir,$username,"mailfetch_type_$mf_sn",(isset($mf_type)?$mf_type:MAIL_FETCH_USE_PLAIN));
      $mf_action = 'config';
      break;
  case 'confirm_delete':
@@ -118,6 +125,10 @@ switch( $mf_action ) {
                      getPref($data_dir,$username, 'mailfetch_fref_'.$tmp));
              setPref($data_dir,$username,'mailfetch_subfolder_'.$i,
                      getPref($data_dir,$username, 'mailfetch_subfolder_'.$tmp));
+             setPref($data_dir,$username,'mailfetch_auth_'.$i,
+                     getPref($data_dir,$username, 'mailfetch_auth_'.$tmp,MAIL_FETCH_AUTH_USER));
+             setPref($data_dir,$username,'mailfetch_type_'.$i,
+                     getPref($data_dir,$username, 'mailfetch_type_'.$tmp,MAIL_FETCH_USE_PLAIN));
              setPref($data_dir,$username,'mailfetch_uidl_'.$i,
                      getPref($data_dir,$username, 'mailfetch_uidl_'.$tmp));
          }
@@ -143,6 +154,8 @@ for ($i=0;$i<$mailfetch_server_number;$i++) {
     $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
     $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
     $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
+    $mailfetch_auth_[$i] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER);
+    $mailfetch_type_[$i] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN);
     if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
 }
 
@@ -206,6 +219,22 @@ switch( $mf_action ) {
              html_tag( 'th', _("Password:"), 'right' ) .
              html_tag( 'td', '<input type="password" name="mf_pass" value="" size="20" />', 'left' )
                    ) .
+         html_tag( 'tr',
+             html_tag( 'th', _("Authentication type:"), 'right' ) .
+             html_tag( 'td', addSelect('mf_auth',
+                                       array(MAIL_FETCH_AUTH_USER     => _("USER"),
+                                             MAIL_FETCH_AUTH_APOP     => _("APOP"),
+                                             MAIL_FETCH_AUTH_RFC1939  => _("APOP or USER")),
+                                       MAIL_FETCH_AUTH_USER,true), 'left' )
+                   ) .
+         html_tag( 'tr',
+             html_tag( 'th', _("Connection type:"), 'right' ) .
+             html_tag( 'td', addSelect('mf_type',
+                                       array(MAIL_FETCH_USE_PLAIN => _("Plain text"),
+                                             MAIL_FETCH_USE_TLS   => _("Use TLS"),
+                                             MAIL_FETCH_USE_STLS  => _("Use StartTLS")),
+                                       MAIL_FETCH_USE_PLAIN,true), 'left' )
+                   ) .
          html_tag( 'tr' ) .
              html_tag( 'th', _("Store in Folder:"), 'right' ) .
              html_tag( 'td', '', 'left' );
@@ -326,6 +355,20 @@ switch( $mf_action ) {
                  html_tag( 'td', '<input type="password" name="mf_pass" value="' .
                            htmlspecialchars($mailfetch_pass_[$mf_sn]) . '" size="20" />', 'left' )
                        ) .
+             html_tag( 'tr',
+                 html_tag( 'th', _("Authentication type:"), 'right' ) .
+                 html_tag( 'td', addSelect('mf_auth',array(MAIL_FETCH_AUTH_USER     => _("USER"),
+                                                           MAIL_FETCH_AUTH_APOP     => _("APOP"),
+                                                           MAIL_FETCH_AUTH_RFC1939  => _("APOP or USER")),
+                                                           $mailfetch_auth_[$mf_sn],true), 'left' )
+                       ) .
+             html_tag( 'tr',
+                 html_tag( 'th', _("Connection type:"), 'right' ) .
+                 html_tag( 'td', addSelect('mf_type',array(MAIL_FETCH_USE_PLAIN => _("Plain text"),
+                                                           MAIL_FETCH_USE_TLS   => _("Use TLS"),
+                                                           MAIL_FETCH_USE_STLS  => _("Use StartTLS")),
+                                                           $mailfetch_type_[$mf_sn],true), 'left' )
+                       ) .
              html_tag( 'tr' ) .
                  html_tag( 'th', _("Store in Folder:"), 'right' ) .
                  html_tag( 'td', '', 'left' );
@@ -382,5 +425,5 @@ switch( $mf_action ) {
                        ) ,
                    'center', '', 'width="70%"' );
 }
-?>
-</body></html>
+
+$oTemplate->display('footer.tpl');
\ No newline at end of file
index 89ad6119ce6edbaf35bc1af7976c74b4c7494bd9..293e6319471d2c0c31edb901b551f721a29beff6 100644 (file)
@@ -19,7 +19,6 @@ function squirrelmail_plugin_init_mail_fetch() {
     global $squirrelmail_plugin_hooks;
 
     $squirrelmail_plugin_hooks['menuline']['mail_fetch'] = 'mail_fetch_link';
-    $squirrelmail_plugin_hooks['loading_prefs']['mail_fetch'] = 'mail_fetch_load_pref';
     $squirrelmail_plugin_hooks['login_verified']['mail_fetch'] = 'mail_fetch_setnew';
     $squirrelmail_plugin_hooks['left_main_before']['mail_fetch'] = 'mail_fetch_login';
     $squirrelmail_plugin_hooks['optpage_register_block']['mail_fetch'] = 'mailfetch_optpage_register_block';
@@ -35,15 +34,6 @@ function mail_fetch_link() {
     echo '&nbsp;&nbsp;';
 }
 
-/**
- * load preferences
- * @private
- */
-function mail_fetch_load_pref() {
-    include_once(SM_PATH . 'plugins/mail_fetch/functions.php');
-    mail_fetch_load_pref_function();
-}
-
 /**
  * Fetch pop3 mails on login.
  * @private