<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Smtp_trace
// Moregroupware.org

$myEnv["module"] = "webmail2";
$myEnv["stdsmenu"] = false;
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");
require_once(INCLUDEPATH . "crypt.inc.php");
include(WM2_INC_DIR.'smtp_trace_inc.php');

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("account");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}
$MGW = $_SESSION["MGW"];
$user_id = $MGW->userid;
$username = $MGW->username;

$myC = new Crypto();

if ($username == "admin") {
  $sql = "select * from mgw_webmail2_accounts where id=".(int)$account;
} else {
  $sql = "select * from mgw_webmail2_accounts where id=".(int)$account." and user_id=".(int)$user_id;
}
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
$row = $res->FetchRow();

$poppassword = stripslashes($myC->decrypt($username, $row["password"]));
$password = stripslashes($myC->decrypt($username, $row["smtppassword"]));

if ($row["smtpauth"] == "checked") { $auth = "true"; } else { $auth = "false";}

$mailboxname = stripslashes(myquote($row["mailboxname"]));

$pophostname = stripslashes($row["pophost"]);
$popusername = stripslashes($row["username"]);

$mail_from = stripslashes($row["replyto"]);
$hostname = stripslashes($row["smtphost"]);
$username = stripslashes($row["smtpusername"]);
	  
if ($row["smtpusepopuser"] == "checked") {
  $username = $popusername;
  $password = $poppassword;
}

$port = "25";
if ($row["enable_ssl_smtp"]=="checked") {
  $port = "465";
  $hostname = "ssl://".$hostname;
}

$mail_to = "moregroupware-user@lists.sourceforge.net";
$clrf = "\r\n";

$message = "";
$result2 = "true";

if ($row["popbeforesmtp"] == "checked" and $row["servertype"]=="wm2_servertype_pop3") {
    if ($row["enable_ssl"]=="checked") {
      $default_port = "995";
    } else {
      $default_port = "110";
    }
    require_once(WM2_INC_DIR.'class.pop3.php');
    $email = new pop3();
	
 	$message .= '<font color="#4E4E4E"><b>'.$hashmap["webmail2_settings_smtptrace_popbefore_smtp"].' </b></font>'; 
    if (!$email->connect($pophostname,$default_port)) {
	  $message .= '<font color="#FF0000"><b>'.$hashmap["sendmail_popbeforesmtpfailed_host"].'</b></font>'; 
	} elseif (!$email->user($popusername) or $email->pass($poppassword)<0){ $message .= '<font color="#FF0000"><b>'.$hashmap["sendmail_popbeforesmtpfailed_userpwd"].'</b></font>'; }
	  else { $message .= '<font color="#008000"><b>'.$hashmap["webmail2_settings_smtptrace_ok"].'</b></font>'; }
	$message .= "<br><br>";
}

error_reporting  (E_ERROR | E_PARSE | E_NOTICE);
$connection = fsockopen($hostname, $port, $errno, $errstr, 5);
error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

if(function_exists('socket_set_timeout')){
  @socket_set_timeout($connection, 5, 0);
}

if (is_resource($connection)) {

  execute_command("","220");
  if ($auth == "true") {
    execute_command("EHLO ".$_SERVER["HTTP_HOST"],"250");

	$result = execute_command("AUTH CRAM-MD5","334",true);
	if (substr(trim($result), 0, 3)=="334") {
	  execute_command(cram_md5_response($username,$password,substr($result,4)),"235",false,$hashmap["webmail2_settings_smtptrace_username"]." ".$username." ".$hashmap["webmail2_settings_smtptrace_password"]);
	} else {
//	  $result = execute_command("AUTH DIGEST-MD5",334,true);
//  	  if (substr(trim($result), 0, 3)=="334") {
//		echo execute_command(digest_md5_response($username,$password,substr($result,4),$_SERVER["HTTP_HOST"]),"235",true,$hashmap["webmail2_settings_smtptrace_username"]." ".$username." ".$hashmap["webmail2_settings_smtptrace_password"]);
//		die;
//  	    $message .= '<font color="#4E4E4E"><b>'.htmlspecialchars("AUTH CRAM-MD5 failed. Using AUTH DIGEST-MD5.")."</b></font><br>";
//	  } else {
  	    // $result = execute_command("AUTH NTLM",334,true);
	    // if (substr(trim($result), 0, 3)=="334") {
		// } else {
   	      $message .= '<font color="#4E4E4E"><b>'.htmlspecialchars("AUTH CRAM-MD5 failed. Using plain.")."</b></font><br>";

          execute_command("AUTH LOGIN","334",false);
          execute_command(base64_encode($username),"334",false,$hashmap["webmail2_settings_smtptrace_username"]." ".$username);
          execute_command(base64_encode($password),"235",false,$hashmap["webmail2_settings_smtptrace_password"]);
		// }
//	  }
	}

  } else {
    execute_command("HELO ".$_SERVER["SERVER_NAME"],"250");
  }
  execute_command("MAIL FROM: <".$mail_from.">",array("250","502"));
  execute_command("RCPT TO: <".$mail_to.">","250");

/*
  execute_command("RCPT TO: <".$mail_from.">","250");
  execute_command("DATA",array("354","250"));
  execute_command("Subject: test".$clrf.$clrf."Hello".$clrf.".",array("250","354"));
*/
  execute_command("QUIT",array("221","250"));

  fclose($connection);
} else {
  $message .= $hashmap["webmail2_settings_smtptrace_failed_conntect"].'<br>'.$errstr;
  $result2 = false;  
}

  if ($result2 == "true") {
    $result2 = '<font color="#008000"><b>'.$hashmap["webmail2_settings_smtptrace_settings_ok"].'</b></font>';
  } else {
    $result2 = '<font color="#FF0000"><b>'.$hashmap["webmail2_settings_smtptrace_settings_not_ok"].'</b></font>';
  }

  $smarty->assign("mytarget","_top");
  $smarty->assign("mysendmail","index.php?rightframe=sendmail.php?test&");
  $smarty->assign("myfetchmail","index.php?rightframe=fetchmail.php");

  $smarty->assign("mailboxname",$mailboxname);
  $smarty->assign("result",$result2);
  $smarty->assign("myimgpathmaster", "templates/default/media");
  $smarty->assign("message",$message);
  $smarty->assign("hostname",$hostname);
  $smarty->display('smtp_trace.tpl');
?>