<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

include("auth.php");

global $db_type;
if($db_type != 'mysql'){
  include("../include/class.Pgsql.php");
}else{
  include("../include/class.Mysql.php");
}
error_reporting(E_ALL);

if(array_key_exists('maillist', $_REQUEST))
{
	$db =& get_db_connection();
	$maillist = $_REQUEST['maillist'] * 1;

	$res =& $db->Select ('SELECT majordomohome, majordomoml FROM admin');
	$admin = $res->nextrow();
	$HOME              = $admin["majordomohome"] . '/';

	$res = $db->Select('SELECT domains.id as domain_id, maillist.name as name FROM maillist INNER JOIN domains ON ( maillist.domain_id = domains.id ) where domains.kunde=\'' . $PHP_AUTH_USER . '\' AND maillist.id=' . $maillist);
	if(!$res || !($marr = $res->nextrow()))
	{
		die();	
	}
	
	
	$MAJORDOMO_CF      = $admin["majordomoml"] . '/' . $marr['domain_id'] . '/majordomo.cf';
	
	//$_SERVER['MAJORDOMO_CF'] = $MAJORDOMO_CF;
	//$_SERVER['HOME'] = $HOME;
	
	$cgi_envs = array(
	        'REMOTE_ADDR',
	        'REMOTE_HOST',
	        'SERVER_NAME',
	        'SERVER_PORT',
	        'SCRIPT_NAME',
	        'REQUEST_METHOD',
	        'QUERY_STRING',
	        'CONTENT_LENGTH',
	        'HTTP_COOKIE',
	        'HTTP_USER_AGENT',
	        'HTTP_REFERER',
	        //'MAJORDOMO_CF', 
	        //'HOME',
	);
	
	$env = "";
	
	if(is_array($_POST) && $_SERVER["REQUEST_METHOD"]=="POST")
	{
		if( array_key_exists('QUERY_STRING', $_SERVER) && $_SERVER['QUERY_STRING'])
		{
			$qa[] = $_SERVER['QUERY_STRING'];
		}
		
		foreach($_POST as $c => $v)	
		{
			$v = urlencode($v);
			//$c = urlencode($c);
			$qa[] = "$c=$v";
		}
		$_SERVER['QUERY_STRING'] = implode("&", $qa);
	}
	
	foreach($cgi_envs as $c)
	{
		if( array_key_exists($c, $_SERVER) && $_SERVER[$c])
		{
			$env .= " -env $c=".urlencode($_SERVER[$c]);
		}
	}
	$env .= " -env HOME=$HOME";
	$env .= " -env MAJORDOMO_CF=$MAJORDOMO_CF";
	$args = "majorcool.pl " . $admin["majordomoml"] . "/" . $marr['domain_id'] . "/majorcool_default.cf $env";
	

//echo $args;
	exec("$HOME/wrapper $args", $a, $e);
	if($e)
	{
		echo "error $e";
	}
	
	$p[0] =  '/' . str_replace('/',  '\/', preg_quote( $_SERVER['SCRIPT_NAME'] . '?')) . '/';
	$p[1] =  '/' . str_replace('/',  '\/', preg_quote( $_SERVER['SCRIPT_NAME'] . '"')) . '/';
	$p[2] =  '/\<option([^>]*)VALUE\="' . $marr['name'] . '"([^>]*)\>/iU';
	$r[0] = $_SERVER['REDIRECT_URL'] . '?maillist=' . $maillist . '&';
	$r[1] = $_SERVER['REDIRECT_URL'] . '?maillist=' . $maillist . '"';
	$r[2] = '<option$1VALUE="' . $marr['name'] . '" selected$2>';
	
	$flag = true;
	foreach ($a as $s)
	{
		if($flag && trim($s) == "")
		{
			$flag = false;	
		}
		if ($flag)
		{
			header(trim($s));	
		}
		else 
		{
			$s = preg_replace($p, $r, $s);
			echo $s . "\n";	
		}
		
	}
	
}


?>
