+
+ /**
+ * Implementing SMTP STARTTLS (rfc2487) in php 5.1.0+
+ * http://www.php.net/stream-socket-enable-crypto
+ */
+ if ($use_smtp_tls === 2) {
+ if (function_exists('stream_socket_enable_crypto')) {
+ // don't try starting tls, when client thinks that it is already active
+ if ($this->tls_enabled) {
+ $this->dlv_msg = _("TLS session is already activated.");
+ return 0;
+ } elseif (!array_key_exists('STARTTLS',$this->ehlo)) {
+ // check for starttls in ehlo response
+ $this->dlv_msg = _("SMTP STARTTLS is enabled in SquirrelMail configuration, but used SMTP server does not support it");
+ return 0;
+ }
+
+ // issue starttls command
+ fputs($stream, "STARTTLS\r\n");
+ // get response
+ $tmp = fgets($stream,1024);
+ if ($this->errorCheck($tmp,$stream)) {
+ return 0;
+ }
+
+ // start crypto on connection. suppress function errors.
+ if (@stream_socket_enable_crypto($stream,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
+ // starttls was successful (rfc2487 5.2 Result of the STARTTLS Command)
+ // get new EHLO response
+ fputs($stream, "EHLO $helohost\r\n");
+ // Read ehlo response
+ $tmp = $this->parse_ehlo_response($stream);
+ if ($this->errorCheck($tmp,$stream)) {
+ // don't revert to helo here. server must support ESMTP
+ return 0;
+ }
+ // set information about started tls
+ $this->tls_enabled = true;
+ } else {
+ /**
+ * stream_socket_enable_crypto() call failed.
+ */
+ $this->dlv_msg = _("Unable to start TLS.");
+ return 0;
+ // Bug: can't get error message. See comments in sqimap_create_stream().
+ }
+ } else {
+ // php install does not support stream_socket_enable_crypto() function
+ $this->dlv_msg = _("SMTP STARTTLS is enabled in SquirrelMail configuration, but used PHP version does not support functions that allow to enable encryption on open socket.");
+ return 0;
+ }
+ }
+
+ // FIXME: check ehlo response before using authentication
+ if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
+ // Doing some form of non-plain auth
+ if ($smtp_auth_mech == 'cram-md5') {
+ fputs($stream, "AUTH CRAM-MD5\r\n");
+ } elseif ($smtp_auth_mech == 'digest-md5') {
+ fputs($stream, "AUTH DIGEST-MD5\r\n");
+ }
+
+ $tmp = fgets($stream,1024);
+
+ if ($this->errorCheck($tmp,$stream)) {
+ return(0);
+ }
+
+ // At this point, $tmp should hold "334 <challenge string>"
+ $chall = substr($tmp,4);
+ // Depending on mechanism, generate response string
+ if ($smtp_auth_mech == 'cram-md5') {
+ $response = cram_md5_response($user,$pass,$chall);
+ } elseif ($smtp_auth_mech == 'digest-md5') {
+ $response = digest_md5_response($user,$pass,$chall,'smtp',$host);
+ }
+ fputs($stream, $response);
+
+ // Let's see what the server had to say about that
+ $tmp = fgets($stream,1024);
+ if ($this->errorCheck($tmp,$stream)) {
+ return(0);
+ }
+
+ // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
+ if ($smtp_auth_mech == 'digest-md5') {
+ // $tmp contains rspauth, but I don't store that yet. (No need yet)
+ fputs($stream,"\r\n");
+ $tmp = fgets($stream,1024);
+
+ if ($this->errorCheck($tmp,$stream)) {
+ return(0);
+ }
+ }
+ // CRAM-MD5 and DIGEST-MD5 code ends here
+ } elseif ($smtp_auth_mech == 'none') {
+ // No auth at all, just send helo and then send the mail
+ // We already said hi earlier, nothing more is needed.
+ } elseif ($smtp_auth_mech == 'login') {
+ // The LOGIN method
+ fputs($stream, "AUTH LOGIN\r\n");
+ $tmp = fgets($stream, 1024);
+
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+ fputs($stream, base64_encode ($user) . "\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+
+ fputs($stream, base64_encode($pass) . "\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+ } elseif ($smtp_auth_mech == "plain") {
+ /* SASL Plain */
+ $auth = base64_encode("$user\0$user\0$pass");
+
+ $query = "AUTH PLAIN\r\n";
+ fputs($stream, $query);
+ $read=fgets($stream, 1024);
+
+ if (substr($read,0,3) == '334') { // OK so far..
+ fputs($stream, "$auth\r\n");
+ $read = fgets($stream, 1024);
+ }
+
+ $results=explode(" ",$read,3);
+ $response=$results[1];
+ $message=$results[2];
+ } else {
+ /* Right here, they've reached an unsupported auth mechanism.
+ This is the ugliest hack I've ever done, but it'll do till I can fix
+ things up better tomorrow. So tired... */
+ if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
+ return(0);
+ }
+ }
+
+ /* Ok, who is sending the message? */
+ $fromaddress = ($from->mailbox && $from->host) ?
+ $from->mailbox.'@'.$from->host : '';
+ fputs($stream, 'MAIL FROM:<'.$fromaddress.">\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+
+ /* send who the recipients are */
+ for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
+ if (!$to[$i]->host) $to[$i]->host = $domain;
+ if ($to[$i]->mailbox) {
+ fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+ }
+ }
+
+ for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {
+ if (!$cc[$i]->host) $cc[$i]->host = $domain;
+ if ($cc[$i]->mailbox) {
+ fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+ }
+ }
+
+ for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
+ if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
+ if ($bcc[$i]->mailbox) {
+ fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+ }
+ }
+ /* Lets start sending the actual message */
+ fputs($stream, "DATA\r\n");
+ $tmp = fgets($stream, 1024);
+ if ($this->errorCheck($tmp, $stream)) {
+ return(0);
+ }
+ return $stream;