一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

使用PHP Socket寫(xiě)的POP3類(lèi)

查看 POP3/SMTP 協(xié)議的時(shí)候想嘗試一下自己寫(xiě)一個(gè)操作類(lèi),核心沒(méi)啥,就是使用 fsockopen ,然后寫(xiě)入/接收數(shù)據(jù),只實(shí)現(xiàn)了最核心的部分功能,當(dāng)作是學(xué)習(xí) Socket 操作的練手。其中參考了 RFC 2449和一個(gè)國(guó)外的簡(jiǎn)單Web郵件系統(tǒng) Uebimiau 的部分代碼,不過(guò)絕對(duì)沒(méi)有抄他滴,HOHO,絕對(duì)原創(chuàng)。復(fù)制代碼 代碼如下:
<?php
class SocketPOPClient
{
    var $strMessage        = '';
    var $intErrorNum    = 0;
    var $bolDebug        = false;

    var $strEmail        = '';
    var $strPasswd        = '';
    var $strHost        = '';
    var $intPort        = 110;
    var $intConnSecond    = 30;
    var $intBuffSize    = 8192;

    var $resHandler        = NULL;
    var $bolIsLogin        = false;
    var $strRequest        = '';
    var $strResponse    = '';
    var $arrRequest        = array();
    var $arrResponse    = array();


    //---------------
    // 基礎(chǔ)操作
    //---------------

    //構(gòu)造函數(shù)
    function SocketPOP3Client($strLoginEmail, $strLoginPasswd, $strPopHost='', $intPort='')
    {
        $this->strEmail        = trim(strtolower($strLoginEmail));
        $this->strPasswd    = trim($strLoginPasswd);
        $this->strHost        = trim(strtolower($strPopHost));

        if ($this->strEmail=='' || $this->strPasswd=='')
        {
            $this->setMessage('Email address or Passwd is empty', 1001);
            return false;
        }
        if (!PReg_match("/^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$/i", $this->strEmail))
        {
            $this->setMessage('Email address invalid', 1002);
            return false;
        }
        if ($this->strHost=='')
        {
            $this->strHost = substr(strrchr($this->strEmail, "@"), 1);
        }
        if ($intPort!='')
        {
            $this->intPort = $intPort;
        }
        $this->connectHost();
    }

    //連接服務(wù)器
    function connectHost()
    {
        if ($this->bolDebug)
        {
            echo "Connection ".$this->strHost." .../r/n";
        }
        if (!$this->getIsConnect())
        {
            if ($this->strHost=='' || $this->intPort=='')
            {
                $this->setMessage('POP3 host or Port is empty', 1003);
                return false;            
            }
            $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond);
            if (!$this->resHandler)
            {
                $strErrMsg = 'Connection POP3 host: '.$this->strHost.' failed';
                $intErrNum = 2001;
                $this->setMessage($strErrMsg, $intErrNum);
                return false;
            }
            $this->getLineResponse();
            if (!$this->getRestIsSucceed())
            {
                return false;
            }
        }
        return true;
    }

    //關(guān)閉連接
    function closeHost()
    {
        if ($this->resHandler)
        {
            fclose($this->resHandler);
        }
        return true;
    }

    //發(fā)送指令
    function sendCommand($strCommand)
    {
        if ($this->bolDebug)
        {
            if (!preg_match("/PASS/", $strCommand))
            {
                echo "Send Command: ".$strCommand."/r/n";
            }
            else
            {
                echo "Send Command: PASS ******/r/n";
            }

        }
        if (!$this->getIsConnect())
        {
            return false;
        }
        if (trim($strCommand)=='')
        {
            $this->setMessage('Request command is empty', 1004);
            return false;
        }
        $this->strRequest = $strCommand."/r/n";
        $this->arrRequest[] = $strCommand;
        fputs($this->resHandler, $this->strRequest);
        return true;
    }

    //提取響應(yīng)信息第一行
    function getLineResponse()
    {
        if (!$this->getIsConnect())
        {
            return false;
        }
        $this->strResponse = fgets($this->resHandler, $this->intBuffSize);
        $this->arrResponse[] = $this->strResponse;

        return $this->strResponse;        
    }

    //提取若干響應(yīng)信息,$intReturnType是返回值類(lèi)型, 1為字符串, 2為數(shù)組
    function getRespMessage($intReturnType)
    {
        if (!$this->getIsConnect())
        {
            return false;
        }
        if ($intReturnType == 1)
        {
            $strAllResponse = '';
            while(!feof($this->resHandler))
            {
                $strLineResponse = $this->getLineResponse();
                if (preg_match("/^/+OK/", $strLineResponse))
                {
                    continue;
                }
                if (trim($strLineResponse)=='.')
                {
                    break;
                }
                $strAllResponse .= $strLineResponse;
            }
            return $strAllResponse;
        }
        else
        {
            $arrAllResponse = array();
            while(!feof($this->resHandler))
            {
                $strLineResponse = $this->getLineResponse();
                if (preg_match("/^/+OK/", $strLineResponse))
                {
                    continue;
                }
                if (trim($strLineResponse)=='.')
                {
                    break;
                }
                $arrAllResponse[] = $strLineResponse;
            }
            return $arrAllResponse;            
        }
    }

    //提取請(qǐng)求是否成功
    function getRestIsSucceed($strRespMessage='')
    {
        if (trim($responseMessage)=='')
        {
            if ($this->strResponse=='')
            {
                $this->getLineResponse();
            }
            $strRespMessage = $this->strResponse;
        }
        if (trim($strRespMessage)=='')
        {
            $this->setMessage('Response message is empty', 2003);
            return false;
        }
        if (!preg_match("/^/+OK/", $strRespMessage))
        {
            $this->setMessage($strRespMessage, 2000);
            return false;
        }
        return true;
    }

    //獲取是否已連接
    function getIsConnect()
    {
        if (!$this->resHandler)
        {
            $this->setMessage("Nonexistent availability connection handler", 2002);
            return false;
        }
        return true;
    }


    //設(shè)置消息
    function setMessage($strMessage, $intErrorNum)
    {
        if (trim($strMessage)=='' || $intErrorNum=='')
        {
            return false;
        }
        $this->strMessage    = $strMessage;
        $this->intErrorNum    = $intErrorNum;
        return true;
    }

    //獲取消息
    function getMessage()
    {
        return $this->strMessage;
    }

    //獲取錯(cuò)誤號(hào)
    function getErrorNum()
    {
        return $this->intErrorNum;
    }

    //獲取請(qǐng)求信息
    function getRequest()
    {
        return $this->strRequest;        
    }

    //獲取響應(yīng)信息
    function getResponse()
    {
        return $this->strResponse;
    }


    //---------------
    // 郵件原子操作
    //---------------

    //登錄郵箱
    function popLogin()
    {
        if (!$this->getIsConnect())
        {
            return false;
        }
        $this->sendCommand("USER ".$this->strEmail);
        $this->getLineResponse();
        $bolUserRight = $this->getRestIsSucceed();

        $this->sendCommand("PASS ".$this->strPasswd);
        $this->getLineResponse();
        $bolPassRight = $this->getRestIsSucceed();

        if (!$bolUserRight || !$bolPassRight)
        {
            $this->setMessage($this->strResponse, 2004);
            return false;
        }        
        $this->bolIsLogin = true;
        return true;
    }

    //退出登錄
    function popLogout()
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("QUIT");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;
    }

    //獲取是否在線
    function getIsOnline()
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("NOOP");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;        
    }

    //獲取郵件數(shù)量和字節(jié)數(shù)(返回?cái)?shù)組)
    function getMailSum($intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("STAT");
        $strLineResponse = $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        if ($intReturnType==1)
        {
            return     $this->strResponse;
        }
        else
        {
            $arrResponse = explode(" ", $this->strResponse);
            if (!is_array($arrResponse) || count($arrResponse)<=0)
            {
                $this->setMessage('STAT command response message is error', 2006);
                return false;
            }
            return array($arrResponse[1], $arrResponse[2]);
        }
    }

    //獲取指定郵件得session Id
    function getMailSessId($intMailId, $intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId = intval($intMailId))
        {
            $this->setMessage('Mail message id invalid', 1005);
            return false;
        }
        $this->sendCommand("UIDL ". $intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        if ($intReturnType == 1)
        {
            return     $this->strResponse;
        }
        else
        {
            $arrResponse = explode(" ", $this->strResponse);
            if (!is_array($arrResponse) || count($arrResponse)<=0)
            {
                $this->setMessage('UIDL command response message is error', 2006);
                return false;
            }
            return array($arrResponse[1], $arrResponse[2]);
        }
    }

    //取得某個(gè)郵件的大小
    function getMailSize($intMailId, $intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("LIST ".$intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        if ($intReturnType == 1)
        {
            return $this->strResponse;
        }
        else
        {
            $arrMessage = explode(' ', $this->strResponse);
            return array($arrMessage[1], $arrMessage[2]);
        }
    }

    //獲取郵件基本列表數(shù)組
    function getMailBaseList($intReturnType=2)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("LIST");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return $this->getRespMessage($intReturnType);
    }

    //獲取指定郵件所有信息,intReturnType是返回值類(lèi)型,1是字符串,2是數(shù)組
    function getMailMessage($intMailId, $intReturnType=1)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId = intval($intMailId))
        {
            $this->setMessage('Mail message id invalid', 1005);
            return false;
        }
        $this->sendCommand("RETR ". $intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return $this->getRespMessage($intReturnType);
    }

    //獲取某郵件前指定行, $intReturnType 返回值類(lèi)型,1是字符串,2是數(shù)組
    function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId=intval($intMailId) || !$intTopLines=int($intTopLines))
        {
            $this->setMessage('Mail message id or Top lines number invalid', 1005);
            return false;
        }
        $this->sendCommand("TOP ". $intMailId ." ". $intTopLines);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return $this->getRespMessage($intReturnType);
    }

    //刪除郵件
    function delMail($intMailId)
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        if (!$intMailId=intval($intMailId))
        {
            $this->setMessage('Mail message id invalid', 1005);
            return false;
        }
        $this->sendCommand("DELE ".$intMailId);
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;
    }

    //重置被刪除得郵件標(biāo)記為未刪除
    function resetDeleMail()
    {
        if (!$this->getIsConnect() && $this->bolIsLogin)
        {
            return false;
        }
        $this->sendCommand("RSET");
        $this->getLineResponse();
        if (!$this->getRestIsSucceed())
        {
            return false;
        }
        return true;        
    }

    //---------------
    // 調(diào)試操作
    //---------------

    //輸出對(duì)象信息
    function printObject()
    {
        print_r($this);
        exit;
    }

    //輸出錯(cuò)誤信息
    function printError()
    {
        echo "[Error Msg] : $strMessage     <br>/n";
        echo "[Error Num] : $intErrorNum <br>/n";
        exit;
    }

    //輸出主機(jī)信息
    function printHost()
    {
        echo "[Host]  : $this->strHost <br>/n";
        echo "[Port]  : $this->intPort <br>/n";
        echo "[Email] : $this->strEmail <br>/n";
        echo "[Passwd] : ******** <br>/n";
        exit;
    }

    //輸出連接信息
    function printConnect()
    {
        echo "[Connect] : $this->resHandler <br>/n";
        echo "[Request] : $this->strRequest <br>/n";
        echo "[Response] : $this->strResponse <br>/n";
        exit;
    }
}

?>
<?
//測(cè)試代碼
//例如:$o = SocketPOP3Client('郵箱地址', '密碼', 'POP3服務(wù)器', 'POP3端口')

/*
set_time_limit(0);
$o = new SocketPOPClient('abc@126.com', '123456', 'pop.126.com', '110');
$o->popLogin();
print_r($o->getMailBaseList());
print_r($o->getMailSum(1));
print_r($o->getMailTopMessage(2, 2, 2));
$o->popLogout();
$o->closeHost();
$o->printObject();
*/
?>

php技術(shù)使用PHP Socket寫(xiě)的POP3類(lèi),轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 中文字幕一区婷婷久久 | 欧美高清另类videosbestsex | 丁香婷婷在线 | 精品一区二区三区三区 | 九九99九九精彩 | 国产一级一级一级成人毛片 | 一区二区三区舞蹈区 | 激情图片激情小说激情视频 | 欧美中文小说在线观看 | 五月天六月婷婷开心激情 | 久久伊人中文字幕有码 | 精品四虎免费观看国产高清 | 国产亚洲综合精品一区二区三区 | 在线观看国产区 | 亚洲一区二区三区精品视频 | 中文字幕亚洲一区婷婷 | 久草久| 91短视频在线观看免费最新91 | 91久久青青青国产免费 | 欧美日韩a | 亚洲国产人成在线观看 | 久在草影院 | 久久九九综合 | 国产在线综合一区二区三区 | 91亚洲欧美 | 色天网站| 国产免费午夜 | 成人在线视频国产 | 美女被男人桶免费网站 | 麻豆国内精品久久久久久 | 法国性xxxxx极品 | 色干综合 | www.四虎在线观看 | 怡红院美国十次成人影院 | 青春草久久 | 欧美综合区 | 欧美激情视频在线观看一区二区三区 | 六月久久 | 中文字幕久久综合伊人 | www激情| 日本视频www色 |