Index: trunk/core/kernel/utility/smtp_client.php =================================================================== diff -u -r1560 -r3540 --- trunk/core/kernel/utility/smtp_client.php (.../smtp_client.php) (revision 1560) +++ trunk/core/kernel/utility/smtp_client.php (.../smtp_client.php) (revision 3540) @@ -23,10 +23,10 @@ var $pass; var $debug; var $buffer; - + var $authmethod; var $CRLF = "\r\n"; - + var $debugtext=''; /** * List of supported authentication methods, in preferential order. @@ -40,7 +40,7 @@ * @access private */ var $_code = -1; - + /** * The most recent server response arguments. * @var array @@ -53,7 +53,7 @@ * @access private */ var $_esmtp = array(); - + /*************************************** ** Constructor function. Arguments: ** $params - An assoc array of parameters: @@ -71,7 +71,7 @@ ***************************************/ function kSmtpClient($params = array()){ - + $this->timeout = 5; $this->status = SMTP_STATUS_NOT_CONNECTED; $this->host = 'localhost'; @@ -90,7 +90,7 @@ /*************************************** ** Connect function. This will, when called - ** statically, create a new smtp object, + ** statically, create a new smtp object, ** call the connect function (ie this function) ** and return it. When not called statically, ** it will connect to the server and send @@ -153,7 +153,7 @@ ***************************************/ function send($params = array()){ - + foreach($params as $key => $value){ $this->set($key, $value); } @@ -173,12 +173,12 @@ if(!$this->data()) return FALSE; - + // Transparency $headers = str_replace($this->CRLF.'.', $this->CRLF.'..', trim(implode($this->CRLF, $this->headers))); $body = str_replace($this->CRLF.'.', $this->CRLF.'..', $this->body); $body = $body[0] == '.' ? '.'.$body : $body; - + $this->send_data($headers); $this->send_data(''); $this->send_data($body); @@ -190,7 +190,7 @@ return FALSE; } } - + /*************************************** ** Function to implement HELO cmd ***************************************/ @@ -207,7 +207,7 @@ return FALSE; } } - + /*************************************** ** Function to implement EHLO cmd ***************************************/ @@ -228,41 +228,41 @@ strlen($argument) - strlen($verb) - 1); $this->_esmtp[$verb] = $arguments; } - + return TRUE; - + } - + /*************************************** ** Function to implement AUTH cmd ***************************************/ - + function _getBestAuthMethod() { if ($this->authmethod) return $this->authmethod; - + if (!isset($this->_esmtp['AUTH'])) return 'NOAUTH'; - + $available_methods = explode(' ', $this->_esmtp['AUTH']); - - + + foreach ($this->auth_methods as $method) { if (in_array($method, $available_methods)) return $method; } return false; } - - + + function auth(){ if(is_resource($this->connection)) { $method=$this->_getBestAuthMethod(); - + if ($method == 'NOAUTH') return true; - + switch ($method) { case 'DIGEST-MD5': $result = $this->_authDigest_MD5($this->user, $this->pass); @@ -309,7 +309,7 @@ function _authDigest_MD5($uid, $pwd) { $this->send_data('AUTH DIGEST-MD5'); - + /* 334: Continue authentication request */ if(($error=$this->_parseResponse(334)) !== true) { @@ -324,28 +324,28 @@ $auth_str = base64_encode($this->get_digestMD5Auth($uid, $pwd, $challenge, $this->host, "smtp")); - + $this->send_data($auth_str); - + /* 334: Continue authentication request */ if(($error=$this->_parseResponse(334)) !== true) return $error; - + /* * We don't use the protocol's third step because SMTP doesn't allow * subsequent authentication, so we just silently ignore it. */ $this->send_data(' '); - + /* 235: Authentication successful */ if(($error=$this->_parseResponse(235)) !== true) return $error; } - - + + /** * Provides the (main) client response for DIGEST-MD5 * requires a few extra parameters than the other * mechanisms, which are unavoidable. - * + * * @param string $authcid Authentication id (username) * @param string $pass Password * @param string $challenge The digest challenge sent by the server @@ -360,7 +360,7 @@ $challenge = $this->_parseChallenge($challenge); $authzid_string = ''; if ($authzid != '') { - $authzid_string = ',authzid="' . $authzid . '"'; + $authzid_string = ',authzid="' . $authzid . '"'; } if (!empty($challenge)) { @@ -373,7 +373,7 @@ return PEAR::raiseError('Invalid digest challenge'); } } - + /** * Parses and verifies the digest challenge* * @@ -422,17 +422,17 @@ $uname = posix_uname(); $tokens['realm'] = $uname['nodename']; } - + // Maxbuf if (empty($tokens['maxbuf'])) { $tokens['maxbuf'] = 65536; } - + // Required: nonce, algorithm if (empty($tokens['nonce']) OR empty($tokens['algorithm'])) { return array(); } - + return $tokens; } @@ -448,7 +448,7 @@ * @param string $authzid Authorization id * @return string The response= part of the digest response * @access private - */ + */ function _getResponseValue($authcid, $pass, $realm, $nonce, $cnonce, $digest_uri, $authzid = '') { if ($authzid == '') { @@ -480,17 +480,17 @@ for ($i=0; $i<32; $i++) { $str .= chr(mt_rand(0, 255)); } - + return base64_encode($str); } } - - - - - - + + + + + + /** * Authenticates the user using the CRAM-MD5 method. * @@ -505,7 +505,7 @@ function _authCRAM_MD5($uid, $pwd) { $this->send_data('AUTH CRAM-MD5'); - + /* 334: Continue authentication request */ if(($error=$this->_parseResponse(334)) !== true) { @@ -518,7 +518,7 @@ $challenge = base64_decode($this->_arguments[0]); $auth_str = base64_encode($uid . ' ' . $this->_HMAC_MD5($pwd, $challenge)); - + $this->send_data($auth_str); /* 235: Authentication successful */ @@ -541,7 +541,7 @@ function _authLogin($uid, $pwd) { $this->send_data('AUTH LOGIN'); - + /* 334: Continue authentication request */ if(($error=$this->_parseResponse(334)) !== true) { @@ -551,12 +551,12 @@ } return $error; } - + $this->send_data( base64_encode($uid) ); - + /* 334: Continue authentication request */ if(($error=$this->_parseResponse(334)) !== true) return $error; - + $this->send_data( base64_encode($pwd) ); /* 235: Authentication successful */ @@ -579,7 +579,7 @@ function _authPlain($uid, $pwd) { $this->send_data('AUTH PLAIN'); - + /* 334: Continue authentication request */ if(($error=$this->_parseResponse(334)) !== true) { @@ -612,22 +612,22 @@ if (strlen($key) > 64) { $key = pack('H32', md5($key)); } - + if (strlen($key) < 64) { $key = str_pad($key, 64, chr(0)); } - + $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64); $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64); - + $inner = pack('H32', md5($k_ipad . $data)); $digest = md5($k_opad . $inner); - + return $digest; } - + /** * Read a reply from the SMTP server. The reply consists of a response * code and a response message. @@ -646,46 +646,46 @@ */ function _parseResponse($valid) { - + $this->_code = -1; $this->_arguments = array(); - + if(!is_resource($this->connection)) return false; - + while ($line = fgets($this->connection, 512)) { if ($this->debug) { $this->debugtext.= "DEBUG: Recv: $line\n"; } - + /* If we receive an empty line, the connection has been closed. */ if (empty($line)) { $this->disconnect(); return 'Connection was unexpectedly closed'; } - + /* Read the code and store the rest in the arguments array. */ $code = substr($line, 0, 3); $this->_arguments[] = trim(substr($line, 4)); - + /* Check the syntax of the response code. */ if (is_numeric($code)) { $this->_code = (int)$code; } else { $this->_code = -1; break; } - + /* If this is not a multiline response, we're done. */ if (substr($line, 3, 1) != '-') { break; } } - + /* Compare the server's response code with the valid code. */ if (is_int($valid) && ($this->_code === $valid)) { return true; } - + /* If we were given an array of valid response codes, check each one. */ if (is_array($valid)) { foreach ($valid as $valid_code) { @@ -694,15 +694,15 @@ } } } - + return 'Invalid response code received from server'; } - + /*************************************** ** Function that handles the MAIL FROM: cmd ***************************************/ - + function mail($from){ if($this->is_connected() @@ -718,7 +718,7 @@ /*************************************** ** Function that handles the RCPT TO: cmd ***************************************/ - + function rcpt($to){ if($this->is_connected() @@ -742,7 +742,7 @@ if($this->is_connected() AND $this->send_data('DATA') AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){ - + return TRUE; }else{ @@ -766,46 +766,46 @@ ***************************************/ function send_data($data){ - - if($this->debug) - { - $this->buffer[] = "SEND: $data\n"; - } - if ($this->debug) { - $this->debugtext.= "DEBUG: Send: $data\n"; - } + + if($this->debug) + { + $this->buffer[] = "SEND: $data\n"; + } + if ($this->debug) { + $this->debugtext.= "DEBUG: Send: $data\n"; + } if(is_resource($this->connection)){ return fwrite($this->connection, $data.$this->CRLF, strlen($data)+2); }else - return FALSE; + return FALSE; } - function bytes_left($fp) - { - $status = socket_get_status ($fp); - //print_r($status); - $bytes = $status["unread_bytes"]; - return $bytes; - } + function bytes_left($fp) + { + $status = socket_get_status ($fp); + //print_r($status); + $bytes = $status["unread_bytes"]; + return $bytes; + } /*************************************** ** Function to get data. ***************************************/ function &get_data(){ - + $return = ''; $line = ''; if(is_resource($this->connection)) { while(strpos($return, $this->CRLF) === FALSE OR substr($line,3,1) !== ' ') - { + { $line = fgets($this->connection, 512); - $return .= $line; + $return .= $line; } if($this->debug) { - $this->buffer[] = "GET: ".$return."\n"; + $this->buffer[] = "GET: ".$return."\n"; } return $return; @@ -816,14 +816,14 @@ /*************************************** ** Sets a variable ***************************************/ - + function set($var, $value){ $this->$var = $value; return TRUE; } - + } ?> \ No newline at end of file