<?php
// +---------------------------------------------+
// |     Copyright  2007 - 2009 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

function isFormValid()
{
	global $error;
	$validInfos = true;
	foreach ($error as $anError)
		if ($anError != '')
			$validInfos = false;
	return $validInfos;
}

function isPasswdAdmin ($passwd)
{
    $size = 5;
	return preg_match('/^[.a-z_0-9-]{'.$size.',32}$/', $passwd);
}

function passwdGen($length = 8)
{
	$str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	for ($i = 0, $passwd = ''; $i < $length; $i++)
		$passwd .= substr($str, mt_rand(0, strlen($str) - 1), 1);
	return $passwd;
}

function isEmail($email)
{
	return preg_match("/^[a-z0-9]+[.a-z0-9_-]*@[a-z0-9]+[.a-z0-9_-]*\.[a-z0-9]+$/i", $email);
}
// Check each POST data...

$error = array();
	

$error['infosName'] = '';
$error['infosPassword'] = '';
$error['infosPasswordRepeat'] = '';
$error['infosEmail'] = '';
	
if(!isset($_GET['infosName']) OR empty($_GET['infosName']))
	$error['infosName'] = '38';

if(!isset($_GET['infosPassword'])
	OR empty($_GET['infosPassword'])
	OR !isset($_GET['infosPasswordRepeat'])
	OR empty($_GET['infosPasswordRepeat']))
	$error['infosPassword'] = '38';
else if (!isPasswdAdmin ($_GET['infosPassword']))
	$error['infosPassword'] = '12';
	
if($error['infosPassword'] == '' AND $_GET['infosPassword'] != $_GET['infosPasswordRepeat'])
	$error['infosPasswordRepeat'] = '2';
	
	
if(!isset($_GET['infosEmail']) OR empty($_GET['infosEmail']))
	$error['infosEmail'] = '38';
else if(!isEmail ($_GET['infosEmail']))
	$error['infosEmail'] = '3';


//Insert configuration parameters into the database
//
if (isFormValid())
{
$servername      = trim($_GET['server']);
$dbname          = trim($_GET['name']);
$dbusername      = trim($_GET['login']);
$dbpassword      = trim($_GET['password']);
$tableprefix     = strtolower(trim($_GET['tablePrefix']));
$alltableprefix     = substr($tableprefix, 0, -1);
$entableprefix   = $alltableprefix . 'en_';

$configfile="<"."?php

\$dbtype      = 'mysql';

\$servername  = '$servername';
\$dbname      = '$dbname';
\$dbusername  = '$dbusername';
\$dbpassword  = '$dbpassword';

define('CWSINSTALLED', true);
define('TABLE_PREFIX_ALL', '$alltableprefix');

define('WEB_COOKIE_KEY', '".passwdGen(12)."');

?".">";

// write the config file
$filenum = fopen (INSTALL_PATH."/../includes/config.php","w");
ftruncate($filenum, 0);
fwrite($filenum, $configfile);
fclose($filenum);

	
	
	$username      = trim($_GET['infosName']);
	$password      = trim($_GET['infosPassword']);
	$email         = trim($_GET['infosEmail']);
	$smtpSrv       = trim($_GET['smtpSrv']);
	$smtpLogin     = trim($_GET['smtpLogin']);
	$smtpPassword  = trim($_GET['smtpPassword']);
	$smtpPort      = trim($_GET['smtpPort']);
	$smtpFrom      = trim($_GET['smtpFrom']);

	
	include(INSTALL_PATH.'/../includes/db/mysql.php');
	$DB = new DB;
	
	$DB->database = $dbname;
	$DB->server   = $servername;
	$DB->user     = $dbusername;
	$DB->password = $dbpassword;
	
	$DB->connect();

    $DB->query("INSERT INTO " . $tableprefix . "users (username, usergroupid, password, email, activated, joindate) VALUES('$username', '1', '".md5($password)."', '$email', 1, '".time()."') ");

    $DB->query("INSERT INTO " . $entableprefix . "users (username, usergroupid, password, email, activated, joindate) VALUES('$username', '1', '".md5($password)."', '$email', 1, '".time()."') ");

    if(!empty($smtpSrv)){
		$DB->query("UPDATE  " . $tableprefix . "mainsettings SET value = '$smtpSrv' WHERE varname = 'email_smtp_host'");
		$DB->query("UPDATE  " . $entableprefix . "mainsettings SET value = '$smtpSrv' WHERE varname = 'email_smtp_host'");
	}
    if(!empty($smtpPort)){
		$DB->query("UPDATE  " . $tableprefix . "mainsettings SET value = '$smtpPort' WHERE varname = 'email_smtp_port'");
		$DB->query("UPDATE  " . $entableprefix . "mainsettings SET value = '$smtpPort' WHERE varname = 'email_smtp_port'");
	}
    if(!empty($smtpLogin)){
		$DB->query("UPDATE  " . $tableprefix . "mainsettings SET value = '$smtpLogin' WHERE varname = 'email_smtp_user'");
		$DB->query("UPDATE  " . $entableprefix . "mainsettings SET value = '$smtpLogin' WHERE varname = 'email_smtp_user'");
	}
    if(!empty($smtpPassword)){
		$DB->query("UPDATE  " . $tableprefix . "mainsettings SET value = '$smtpPassword' WHERE varname = 'email_smtp_pass'");
		$DB->query("UPDATE  " . $entableprefix . "mainsettings SET value = '$smtpPassword' WHERE varname = 'email_smtp_pass'");
	}
    if(!empty($smtpFrom)){
		$DB->query("UPDATE  " . $tableprefix . "mainsettings SET value = '$smtpFrom' WHERE varname = 'email_smtp_from'");
		$DB->query("UPDATE  " . $entableprefix . "mainsettings SET value = '$smtpFrom' WHERE varname = 'email_smtp_from'");
	}
	
    $DB->query("UPDATE  " . $tableprefix . "mainsettings SET value = '$email' WHERE varname = 'technicalemail'");
	
	$DB->query("UPDATE  " . $tableprefix . "modulesettings SET value = '$email' WHERE title = 'تص¼‏دن' AND moduleid = 6");
	
    $DB->query("UPDATE  " . $entableprefix  . "mainsettings SET value = '$email' WHERE varname = 'technicalemail'");
	
	$DB->query("UPDATE  " . $entableprefix . "modulesettings SET value = '$email' WHERE title = 'تص¼‏دن' AND moduleid = 6");

	$DB->close();

}


// Building XML Response//

echo '<weencompanyConfig>'."\n";
foreach ($error AS $key => $line)
	echo '<field id="'.$key.'" result="'.( $line != "" ? 'fail' : 'ok').'" error="'.$line.'" />'."\n";
echo '</weencompanyConfig>';

?>
