<?
// Сохранение настроек
if( isset( $_POST['ConfirmSetup'] ) ) {
	$CRMsn 				= $_POST["inputCRMsn"];
	$CRMusername 		= $_POST["inputCRMuname"];
	$CRMuserphone 		= $_POST["inputCRMuphone"];
	$CRMuseremail 		= $_POST["inputCRMumail"];
	$CRMadminpass 		= $_POST["inputCRMadminPass"];
	$CRMrules 			= $_POST["checkboxCRMrules"];
	$CRMuserword 		= $_POST["inputCRMword"];
	$CRMuserdate 		= $_POST["inputCRMdate"];
	
	$nCRMdateFrom 	= DateTime::createFromFormat( 'd.m.Y', date("d.m.Y") );
	$nCRMdateTo 	= DateTime::createFromFormat( 'd.m.Y', $CRMuserdate );
	
	if(preg_match("/[A-Z]{2}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}/i", $CRMsn) and $nCRMdateTo > $nCRMdateFrom){ 
		$doc = new DOMDocument;
		$doc->load($baseS);
		$xpath = new DOMXPath($doc);
		
		$nCRMusername 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("username")->item(0)->nodeValue=$CRMusername;
		$nCRMuserphone 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("userphone")->item(0)->nodeValue=$CRMuserphone;
		$nCRMuseremail 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("useremail")->item(0)->nodeValue=$CRMuseremail;
		$nCRMrules	 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("rules")->item(0)->nodeValue=$CRMrules;
		$nCRMsn 			= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("sn")->item(0)->nodeValue=$CRMsn;
		$nCRMuserword 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("userword")->item(0)->nodeValue=$CRMuserword;
		$nCRMuserdate 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("userdate")->item(0)->nodeValue=$CRMuserdate;
		$doc->save($baseS);
		
		$dom = new DOMDocument;
		$dom->load($baseU);
		$xpathm = new DOMXPath($dom);
		$UserAdminPass = $xpathm->query('/users/user[@login="admin"]')->item(0)->setAttribute('password', md5(md5($_SERVER["SERVER_NAME"]).md5($CRMadminpass).md5($folder)));
		$dom->save($baseU);
		
		$subject1 	= "$CRMname. Успешная регистрация системы";
		$message1 	= '
			<h2>Здравствуйте!</h2>
			<p>Поздравляем, Вы успешно зарегистрировали CRM систему '.$CRMname.' по адресу: <a href="http://'.$_SERVER["SERVER_NAME"].'/'.$folder.'">http://'.$_SERVER["SERVER_NAME"].'/'.$folder.'</a></p>
			<br/>
			<p>Данные входа главного администратора:</p>
			<p>Логин: <b>admin</b></p>
			<p>Пароль: <b>'. $CRMadminpass .'</b></p>
			<br/>
			<p>Хорошего дня и отличного настроения!</p>
		';
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=utf-8\r\n";
		$headers .= "From: no-reply@$_SERVER[SERVER_NAME]";
		mail($CRMuseremail, $subject1, $message1, $headers);

		$subject2 	= "$CRMname. Новая регистрация | SN: $CRMsn";
		$message2 	= '
			<h2>Здравствуйте!</h2>
			<p>Уведомляем Вас, что '. date("d-m-Y") .' в '. date("H:i:s") .' по адресу: <a href="http://'.$_SERVER["SERVER_NAME"].'/'.$folder.'">http://'.$_SERVER["SERVER_NAME"].'/'.$folder.'</a> была зафиксирована новая регистрация '.$CRMname.'</p>
			<br/>
			<p>S/N: <b>'. $nCRMsn .'</b></p>
			<p>Пользователь: <b>'. $nCRMusername .'</b></p>
			<p>Телефон: <b>'. $nCRMuserphone .'</b></p>
			<p>Email: <b>'. $nCRMuseremail .'</b></p>
			<br/>
			<p>Хорошего дня и отличного настроения!</p>
		';
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=utf-8\r\n";
		$headers .= "From: no-reply@$_SERVER[SERVER_NAME]";
		mail($CRMemail, $subject2, $message2, $headers);
		if(file_exists(ROOT . 'install.php')){rename(ROOT . 'install.php', ROOT . 's_'. md5(md5($_SERVER["SERVER_NAME"]).md5($folder)) . '.php');};
		header('Location:/' . $folder . '/admin/#settings');
		exit;
		
	}
}
?>