<?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-inc
// Moregroupware.org


function get_my_data($connection){
  global $connection, $clrf;
  $return = '';
  $line   = '';
  $loops  = 0;
  
  if(is_resource($connection)){
	while((strpos($return, $clrf) === FALSE OR substr($line,3,1) !== ' ') AND $loops < 100){
	  $line    = fgets($connection, 512);
	  $return .= $line;
	  $loops++;
	}
	return $return;
  } else {
	return FALSE;
  }
}

function validate($result,$nr) {
  global $message,$result2,$hashmap;
  if ($result == "") {
    $message .= '<font color="#FF0000"><b>'.$hashmap["webmail2_settings_smtptrace_error"].'</b></font><br>';
  } elseif (in_array(substr(trim($result), 0, 3),$nr)) {
    $message .= '<font color="#000080">'.$result.'</font> <font color="#4E4E4E">>></font> <font color="#008000"><b>'.$hashmap["webmail2_settings_smtptrace_ok"].'</b></font><br>';
  } else {
	$result2 = "false";
    $message .= '<font color="#000080"><b>'.$result.'</b></font> <font color="#4E4E4E">>></font> <font color="#FF0000"><b>'.$hashmap["webmail2_settings_smtptrace_error"].'</b></font><br>';
  }
}

function execute_command($command,$nr,$escalate= false,$secondstr = "") {
  global $connection,$clrf,$message;
  if (!is_array($nr)) { $nr = array($nr);}
  if (is_resource($connection)) {
    if ($command != "") {
	  if ($secondstr == "") {$secondstr = $command;}
      $message .= '<font color="#4E4E4E"><b>'.htmlspecialchars($secondstr)."</b></font><br>";
      fwrite($connection,$command.$clrf);
    }
    $result = get_my_data($connection,$clrf);
    if (!$escalate) { validate($result,$nr); }
	  else { return trim($result); }
  }
}

function cram_md5_response ($username,$password,$challenge) {
  $challenge=base64_decode($challenge);
  $hash=bin2hex(hmac_md5($challenge,$password));
  $response=base64_encode($username . " " . $hash) . "\r\n";
  return $response;
}

function hmac_md5($data, $key='') {
    if (!$key) {
      return pack('H*',md5($data));
    }
    $key = str_pad($key,64,chr(0x00));
    if (strlen($key) > 64) {
      $key = pack("H*",md5($key));
    }
    $k_ipad =  $key ^ str_repeat(chr(0x36), 64) ;
    $k_opad =  $key ^ str_repeat(chr(0x5c), 64) ;
    $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
    return $hmac;
}


?>