Location: PHPKode > scripts > PHP-Lib-Xtreme > php-lib-xtreme/pop3.php
<?php
/** @defgroup pop3group POP3-Mail-Class
    PHP-Class for mail receiving with POP3 (Post Office Protocol 3).
@{ */
/*!***********************************************************************
 *************************************************************************
 * \file            pop3.php
 *
 * \author          copyright 1999-2005 <br>
 *                  Kai Klenovsek <br>
 *
 * \date            First Step: 2004-12-27 <br>
 *
 * \note <br>
 * \b LICENSE: <br>
 *      This library is free software; you can redistribute it and/or <br>
 *      modify it under the terms of the GNU Lesser General Public <br>
 *      License as published by the Free Software Foundation; either <br>
 *      version 2.1 of the License, or (at your option) any later version. <br>
 *
 *      This library is distributed in the hope that it will be useful, <br>
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of <br>
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU <br>
 *      Lesser General Public License for more details. <br>
 *
 *      You should have received a copy of the GNU Lesser General Public <br>
 *      License along with this library; if not, write to the Free Software <br>
 *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA <br>
 * 
 * \b NOTES: 
 *        
 * \todo  
 *               
 * \bug 
 *           
 * \b CHANGE \b HISTORY: <br>
 *
 *      <b>- Kai K. / 2005-07-13</b>   
 *          - Error log to error logfile implemented.
 *          - Bugfix return value problem pop3_get_errorlist().
 *
 *      <b>- Kai K. / 2005-04-13</b>   
 *              - Error message handler implemented.
 *
 *      <b>- Kai K. / 2005-02-18</b>   
 *              - Some code optimization.
 *              - Many new functions.
 *
 *      <b>- Kai K. / 2005-02-07</b>   
 *              - More error messages implemented.
 *              - More stream data implemented.
 *
***************************************************************************
***************************************************************************/

if ( !defined("POP3") )
{
define("POP3", "pop3");



/********************************************************************
*
* DEFINES
*
********************************************************************/ 

define("POP3MODULNAME", "[MODUL->POP3]:");



class pop3
{
    /********************************************************************
    *
    * PRIVATE VARS
    *
    ********************************************************************/    
    
    var $pop3_error_hdl     = 0;                     /*!< Error message handler */
    
    var $pop3_host;                                  /*!< Host name or IP */
    
    var $pop3_password;                              /*!< Connection password */

    var $pop3_user_id;                               /*!< Connection user name */
    
    var $pop3_socket_hdl    = 0;                     /*!< Socket handle */
    
    var $pop3_socket_stream = array();               /*!< Socket stream data array */
    
    var $pop3_timeout;                               /*!< Connection timeout */    
    
    
    
    /********************************************************************
    *
    * PUBLIC FUNCTIONS
    *
    ********************************************************************/   
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3()
     *  \brief Class Constructor */
    
    function pop3()
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_error_hdl = new msghandler();
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function _pop3()
     *  \brief Class Destructor */
    
    function _pop3()
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
     
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_close()
     *  \brief Close connection to pop3 server
     *  \return State = true or false */
    
    function pop3_close()
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        if ( $this->pop3_socket_hdl )
        {
            $this->pop3_send_quit();
      
            if ( $this->pop3_socket_hdl->fsocket_close() )
            {
                // Rescue socket stream before the fsocket class will be destroyed.
                $this->pop3_socket_stream = $this->pop3_socket_hdl->fsocket_get_stream();
                
                // Reset handle
                $this->pop3_socket_hdl = 0;
                return ( true );    
            }
        }
        
        $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Invalid socket handle used. Cant close connection to ".$this->pop3_host." in pop3->close().";
        $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );
                
        return ( false );
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_connect( $host, $user_id, $pwd, $timeout=20 )
     *  \brief Connect to pop3 server
     *  \param $host    = host adress or IP
     *  \param $user_id = Login user name
     *  \param $pwd     = Login password
     *  \param $timeout = Connection timeout
     *  \return State   = true or false */
    
    function pop3_connect( $host, $user_id="", $pwd="", $timeout=20 )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        if ( !$this->pop3_socket_hdl )
        {
            // Load member vars
            $this->pop3_host     = $host;
            $this->pop3_user_id  = $user_id;
            $this->pop3_password = $pwd;
            $this->pop3_timeout  = $timeout;
        
            // Create a new socket handle
            $this->pop3_socket_hdl = new fsocket();
        
            // Connect to POP3 server on port 110
            if ( $this->pop3_socket_hdl->fsocket_connect( $this->pop3_host, 110, $this->pop3_timeout ) )
            {
               // Get welcome message from server and verify it.
               $data = $this->pop3_get_data();
               if ( !$this->pop3_verify( $data ) )
                    return ( false );
        
               if ( !$this->pop3_send_user() )
                    return ( false );
                    
               if ( !$this->pop3_send_pass() )
                    return ( false );               
               
               return ( true ); 
            }
        }
        
        $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Socket connection to ".$this->pop3_host." already established in pop3->connect().";
        $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );
                
        return ( false );
    }
  
  
  
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_delete_mail( $id ) 
     *  \brief Sends delete command to server
     *  \param $id = Mail ID
     *  \return State = true or false */
    
    function pop3_delete_mail( $id )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("DELE ".$id);
        $data = $this->pop3_get_data();
               
        if ( $this->pop3_verify( $data ) )
            return ( true );
            
        return ( false );       
    }
    


    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_flush_errors()
     *  \brief Will flush the complete pop3 error buffer
     *  \return State = true or false */
    
    function pop3_flush_errors()
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        return ( $this->pop3_error_hdl->msg_flushlist() );   
    }
    
        
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_flush_stream()
     *  \brief Will flush the complete pop3 stream
     *  \return State = true or false */
    
    function pop3_flush_stream()
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        return ($this->pop3_socket_hdl->fsocket_flush_stream());   
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_errorlist( ) 
     *  \brief Returns error string array. After the call the function delete the error list.
     *  \return Error string array */
    
    function pop3_get_errorlist( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {   
        return( $this->pop3_error_hdl->msg_getlist() );       
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mail( $id ) 
     *  \brief Return mail string
     *  \param $id = Mail ID
     *  \return Mail string */
    
    function pop3_get_mail( $id )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("RETR ".$id);
        $data = $this->pop3_get_data();
        
        // Get status message
        if ( $this->pop3_verify( $data ) )
        {
            // Now we can load the socket data until the end string is reached.
            $data = $this->pop3_get_data("\.\r\n");
            
            // Format data into an string
            for ( $loop=0; $loop<=sizeof($data)-2; $loop++ )
                $string .= $data[$loop];
                
            return ( $string );
        }

        return ( false );         
    }



    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mail_key( $id ) 
     *  \brief Return mail unique key
     *  \param $id = Mail ID
     *  \return Mail unique key string */
    
    function pop3_get_mail_key( $id )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("UIDL ".$id);
        $data = $this->pop3_get_data();
        
        if ( $this->pop3_verify( $data ) )
        {
            // Filter Unique key
            $remove = "+OK ".$id;
            
            return str_replace( $remove, "", $data );
        }
        
        return ( false );            
    }
    
        
        
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mail_keys( ) 
     *  \brief Returns array with unique keys and mail ID`s.
     *  \return Mail unique key array */
    
    function pop3_get_mail_keys( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("UIDL");
        $data = $this->pop3_get_data();
        
        // Get status message
        if ( $this->pop3_verify( $data ) )
        {
            // Now we can load the socket data until end string is reached
            $data = $this->pop3_get_data("\.\r\n");
            
            // Filter unique infos 
            for ( $loop=0; $loop<=sizeof($data)-2; $loop++ )
                $info[] = $data[$loop];
         
            return ( $info );
        }
                    
        return ( false );           
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mail_info( $id ) 
     *  \brief Return mail info string with mail ID and mail size
     *  \param $id = Mail ID
     *  \return Mail info string */
    
    function pop3_get_mail_info( $id )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("LIST ".$id);
        $data = $this->pop3_get_data();
        
        if ( $this->pop3_verify( $data ) )
        {
            $remove = "+OK ";
            
            return str_replace( $remove, "", $data );    
        }
                        
        return ( false );        
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mailbox_info( ) 
     *  \brief Returns mailbox info array with mail ID`s and mail size.
     *  \return Mailbox info array */
    
    function pop3_get_mailbox_info( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("LIST");
        $data = $this->pop3_get_data();
        
        // Get status message
        if ( $this->pop3_verify( $data ) )
        {
            // Now we can load the socket data
            $data = $this->pop3_get_data("\.\r\n");
            
            // Filter mail infos 
            for ( $loop=0; $loop<=sizeof($data)-2; $loop++ )
                $info[] = $data[$loop];
         
            return ( $info );
        }
                    
        return ( false );   
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mail_size( $id ) 
     *  \brief Return size of an single mail
     *  \param $id = Mail ID
     *  \return Mail size */
    
    function pop3_get_mail_size( $id )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("LIST ".$id);
        $data = $this->pop3_get_data();
        
        if ( $this->pop3_verify( $data ) )
        {
            $remove = "+OK ".$id;
            
            return str_replace( $remove, "", $data );    
        }
                        
        return ( false );        
    }
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_mail_stat( ) 
     *  \brief Returns mailbox statistik. It includes number of mails in 
               and size of all mails in the mailbox.
     *  \return Mailstat info string */
    
    function pop3_get_mail_stat( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("STAT");
        $data = $this->pop3_get_data();
        
        if ( $this->pop3_verify( $data ) )
        {
            $remove = "+OK ";
            
            return str_replace( $remove, "", $data );    
        }
                        
        return ( false );        
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_stream( ) 
     *  \brief Return pop3 stream string array
     *  \return Socket stream string array */
    
    function pop3_get_stream( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        // Get the stream if we are connected. Else the data were saved by pop3_close();
        if ( $this->pop3_socket_hdl )
            $this->pop3_socket_stream = $this->pop3_socket_hdl->fsocket_get_stream();
       
       return $this->pop3_socket_stream;             
    }

    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_keepalive( ) 
     *  \brief Keeps the connection open
     *  \return State = true or false */
    
    function pop3_keepalive( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {  
        $this->pop3_send_cmd("NOOP");
        $data = $this->pop3_get_data();
        if ( $this->pop3_verify( $data ) )
            return ( true );
            
        return ( false ); 
    }
    
    
    
    /********************************************************************
    *
    * PRIVATE FUNCTIONS
    *
    ********************************************************************/
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_get_data( $endsign="LINE" ) 
     *  \brief Return raw data from server and check if it is an error or not
     *  \param $endsign = Look into fsocket_get_socketdata() function doc.
     *  \return Raw data from server */
    
    function pop3_get_data( $endsign="LINE" )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        if ( $this->pop3_socket_hdl )
        {
            if ( !$data = $this->pop3_socket_hdl->fsocket_get_socketdata($endsign) )
            { 
                $error = $this->pop3_socket_hdl->fsocket_get_errorlist();
                for ( $loop=0; $loop <= sizeof($error); $loop++ ) 
                    $this->pop3_error_hdl->msg_addfull( $error[$loop], PHPLIBEX_ERROR_LOG );
              
                return ( false );
            }
            else
                return $data;
        }
            
        $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Invalid socket handle used in pop3->get_data().";
        $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );
                
        return ( false );             
    }
        
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_send_cmd( $cmd ) 
     *  \brief Send command to server
     *  \param $cmd = Command to send
     *  \return State = true or false */
    
    function pop3_send_cmd( $cmd )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        if ( $this->pop3_socket_hdl )
        {
            if ( $this->pop3_socket_hdl->fsocket_send_socketdata($cmd."\r\n") )
                return ( true );
                    
            $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Sending POP3 command \"".$cmd."\" failed";
            $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );           
            
            return ( false );
        }
            
        $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Invalid socket handle used in pop3->send_cmd(".$cmd.").";
        $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );
             
        return ( false );             
    }
 


    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_send_pass( ) 
     *  \brief Sends password to server
     *  \return State = true or false */
    
    function pop3_send_pass( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
       $this->pop3_send_cmd("PASS ".$this->pop3_password);
       $data = $this->pop3_get_data();
                
       if ( $this->pop3_verify( $data ) )
            return ( true );
            
       return ( false );            
    }
    
    
     
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_send_quit() 
     *  \brief Sends quit command to server
     *  \return State = true or false */
    
    function pop3_send_quit( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        $this->pop3_send_cmd("QUIT");
        $data = $this->pop3_get_data();
               
        if ( $this->pop3_verify( $data ) )
            return ( true );
            
        return ( false );       
    }
    
    
       
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_send_user( ) 
     *  \brief Sends user to server
     *  \return State = true or false */
    
    function pop3_send_user( )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {
        // Verify user data with server
        $this->pop3_send_cmd("USER ".$this->pop3_user_id);
        $data = $this->pop3_get_data();
               
        if ( $this->pop3_verify( $data ) )
            return ( true );
            
        return ( false );            
    }
    
    
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    /*! \fn function pop3_verify( $data ) 
     *  \brief Answer string check. The function search for "+OK" or "-ERR".
     *  \param $data  = Data for checking
     *   return State = true or false */
    
    function pop3_verify( $data )
    
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    {  
       if ( $data )
       {
            // Find "+OK" for validating
            if ( ereg("\+OK", $data, $regs) )
                return ( true );
            else
            {
                // Find "-ERR" for an error
                if ( ereg("\-ERR", $data, $regs) )
                {
                    $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Error Msg from POP3 Server in pop3->verify()".$this->pop3_host.": \"".$data."\"";
                    $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );
                }
                else
                    $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Cant recognized command \"".$data."\" in pop3->verify() from ".$this->pop3_host.".";
                    $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );      
            }
       
            $this->pop3_close();
            
            return ( false );
       }
       
       $error_msg = PHPLIBEX_PREFIX.POP3MODULNAME." Nothing to verify in pop3->verify().";
       $this->pop3_error_hdl->msg_addfull( $error_msg, PHPLIBEX_ERROR_LOG );
              
       return ( false );           
    }
  
} // END CLASS POP3
} // END IF POP3
/** @} */
?>
Return current item: PHP-Lib-Xtreme