sieve.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,165 行 · 第 1/3 页
PHP
1,165 行
$this->_state = NET_SIEVE_STATE_TRANSACTION;
return true;
}
/* Handles the authentication using any known method
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
* @param string The method to use ( if $usermethod == '' then the class chooses the best method (the stronger is the best ) )
* @param string The effective uid to authenticate as.
*
* @return mixed string or PEAR_Error
*
* @access private
* @since 1.0
*/
function _cmdAuthenticate($uid , $pwd , $userMethod = null , $euser = '' )
{
if ( PEAR::isError( $method = $this->_getBestAuthMethod($userMethod) ) ) {
return $method;
}
switch ($method) {
case 'DIGEST-MD5':
$result = $this->_authDigest_MD5( $uid , $pwd , $euser );
return $result;
break;
case 'CRAM-MD5':
$result = $this->_authCRAM_MD5( $uid , $pwd, $euser);
break;
case 'LOGIN':
$result = $this->_authLOGIN( $uid , $pwd , $euser );
break;
case 'PLAIN':
$result = $this->_authPLAIN( $uid , $pwd , $euser );
break;
default :
$result = new PEAR_Error( "$method is not a supported authentication method" );
break;
}
if (PEAR::isError($res = $this->_doCmd() )) {
return $res;
}
return $result;
}
/* Authenticates the user using the PLAIN method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
* @param string The effective uid to authenticate as.
*
* @return array Returns an array containing the response
*
* @access private
* @since 1.0
*/
function _authPLAIN($user, $pass , $euser )
{
if ($euser != '') {
$cmd=sprintf('AUTHENTICATE "PLAIN" "%s"', base64_encode($euser . chr(0) . $user . chr(0) . $pass ) ) ;
} else {
$cmd=sprintf('AUTHENTICATE "PLAIN" "%s"', base64_encode( chr(0) . $user . chr(0) . $pass ) );
}
return $this->_sendCmd( $cmd ) ;
}
/* Authenticates the user using the PLAIN method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
* @param string The effective uid to authenticate as.
*
* @return array Returns an array containing the response
*
* @access private
* @since 1.0
*/
function _authLOGIN($user, $pass , $euser )
{
$this->_sendCmd('AUTHENTICATE "LOGIN"');
$this->_doCmd(sprintf('"%s"', base64_encode($user)));
$this->_doCmd(sprintf('"%s"', base64_encode($pass)));
}
/* Authenticates the user using the CRAM-MD5 method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
* @param string The cmdID.
*
* @return array Returns an array containing the response
*
* @access private
* @since 1.0
*/
function _authCRAM_MD5($uid, $pwd, $euser)
{
if ( PEAR::isError( $challenge = $this->_doCmd( 'AUTHENTICATE "CRAM-MD5"' ) ) ) {
$this->_error=challenge ;
return challenge ;
}
$challenge=trim($challenge);
$challenge = base64_decode( trim($challenge) );
$cram = &Auth_SASL::factory('crammd5');
if ( PEAR::isError($resp=$cram->getResponse( $uid , $pwd , $challenge ) ) ) {
$this->_error=$resp;
return $resp;
}
$auth_str = base64_encode( $resp );
if ( PEAR::isError($error = $this->_sendStringResponse( $auth_str ) ) ) {
$this->_error=$error;
return $error;
}
}
/* Authenticates the user using the DIGEST-MD5 method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
* @param string The efective user
*
* @return array Returns an array containing the response
*
* @access private
* @since 1.0
*/
function _authDigest_MD5($uid, $pwd, $euser)
{
if ( PEAR::isError( $challenge = $this->_doCmd('AUTHENTICATE "DIGEST-MD5"') ) ) {
$this->_error=challenge ;
return challenge ;
}
$challenge = base64_decode( $challenge );
$digest = &Auth_SASL::factory('digestmd5');
if(PEAR::isError($param=$digest->getResponse($uid, $pwd, $challenge, "localhost", "sieve" , $euser) )) {
return $param;
}
$auth_str = base64_encode($param);
if ( PEAR::isError($error = $this->_sendStringResponse( $auth_str ) ) ) {
$this->_error=$error;
return $error;
}
if ( PEAR::isError( $challenge = $this->_doCmd() ) ) {
$this->_error=$challenge ;
return $challenge ;
}
if( strtoupper(substr($challenge,0,2))== 'OK' ){
return true;
}
/*
* We don't use the protocol's third step because SIEVE doesn't allow
* subsequent authentication, so we just silently ignore it.
*/
if ( PEAR::isError($error = $this->_sendStringResponse( '' ) ) ) {
$this->_error=$error;
return $error;
}
if (PEAR::isError($res = $this->_doCmd() )) {
return $res;
}
}
/**
* Removes a script from the server
*
* @access private
* @param string $scriptname Name of the script to delete
* @return mixed True on success, PEAR_Error otherwise
*/
function _cmdDeleteScript($scriptname)
{
if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
$msg='Not currently in AUTHORISATION state';
$code=1;
return $this->_raiseError($msg,$code);
}
if (PEAR::isError($res = $this->_doCmd(sprintf('DELETESCRIPT "%s"', $scriptname) ) )) {
return $res;
}
return true;
}
/**
* Retrieves the contents of the named script
*
* @access private
* @param string $scriptname Name of the script to retrieve
* @return mixed The script if successful, PEAR_Error otherwise
*/
function _cmdGetScript($scriptname)
{
if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
$msg='Not currently in AUTHORISATION state';
$code=1;
return $this->_raiseError($msg,$code);
}
if (PEAR::isError($res = $this->_doCmd(sprintf('GETSCRIPT "%s"', $scriptname) ) ) ) {
return $res;
}
return preg_replace('/{[0-9]+}\r\n/', '', $res);
}
/**
* Sets the ACTIVE script, ie the one that gets run on new mail
* by the server
*
* @access private
* @param string $scriptname The name of the script to mark as active
* @return mixed True on success, PEAR_Error otherwise
*/
function _cmdSetActive($scriptname)
{
if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
$msg='Not currently in AUTHORISATION state';
$code=1;
return $this->_raiseError($msg,$code);
}
if (PEAR::isError($res = $this->_doCmd(sprintf('SETACTIVE "%s"', $scriptname) ) ) ) {
return $res;
}
$this->_activeScript = $scriptname;
return true;
}
/**
* Sends the LISTSCRIPTS command
*
* @access private
* @return mixed Two item array of scripts, and active script on success,
* PEAR_Error otherwise.
*/
function _cmdListScripts()
{
if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
$msg='Not currently in AUTHORISATION state';
$code=1;
return $this->_raiseError($msg,$code);
}
$scripts = array();
$activescript = null;
if (PEAR::isError($res = $this->_doCmd('LISTSCRIPTS'))) {
return $res;
}
$res = explode("\r\n", $res);
foreach ($res as $value) {
if (preg_match('/^"(.*)"( ACTIVE)?$/i', $value, $matches)) {
$scripts[] = $matches[1];
if (!empty($matches[2])) {
$activescript = $matches[1];
}
}
}
return array($scripts, $activescript);
}
/**
* Sends the PUTSCRIPT command to add a script to
* the server.
*
* @access private
* @param string $scriptname Name of the new script
* @param string $scriptdata The new script
* @return mixed True on success, PEAR_Error otherwise
*/
function _cmdPutScript($scriptname, $scriptdata)
{
if (NET_SIEVE_STATE_TRANSACTION != $this->_state) {
$msg='Not currently in TRANSACTION state';
$code=1;
return $this->_raiseError($msg,$code);
}
if (PEAR::isError($res = $this->_doCmd(sprintf("PUTSCRIPT \"%s\" {%d+}\r\n%s", $scriptname, strlen($scriptdata),$scriptdata ) ))) {
return $res;
}
return true;
}
/**
* Sends the LOGOUT command and terminates the connection
*
* @access private
* @return mixed True on success, PEAR_Error otherwise
*/
function _cmdLogout($sendLogoutCMD=true)
{
if (NET_SIEVE_STATE_DISCONNECTED === $this->_state) {
$msg='Not currently connected';
$code=1;
return $this->_raiseError($msg,$code);
//return PEAR::raiseError('Not currently connected');
}
if($sendLogoutCMD){
if (PEAR::isError($res = $this->_doCmd('LOGOUT'))) {
return $res;
}
}
$this->_sock->disconnect();
$this->_state = NET_SIEVE_STATE_DISCONNECTED;
return true;
}
/**
* Sends the CAPABILITY command
*
* @access private
* @return mixed True on success, PEAR_Error otherwise
*/
function _cmdCapability()
{
if (NET_SIEVE_STATE_DISCONNECTED === $this->_state) {
$msg='Not currently connected';
$code=1;
return $this->_raiseError($msg,$code);
}
if (PEAR::isError($res = $this->_doCmd('CAPABILITY'))) {
return $res;
}
$this->_parseCapability($res);
return true;
}
/**
* Checks if the server has space to store the script
* by the server
*
* @access public
* @param string $scriptname The name of the script to mark as active
* @return mixed True on success, PEAR_Error otherwise
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?