<?php
/*
	DevSaver Web Framework
	Copyright (c) 2002-2018 DevSaver. 
	All rights reserved.
		web:  www.devsaver.com
		mail: support@devsaver.com
*/

if (!defined("STPBase")) {
	die("This file can't be accessed directly!");
}




//check if admin already exists

//$admin = $db->QFetchArray("SELECT * FROM site_users WHERE user_login LIKE '{$_SESSION['install']['admin']['user']}'");

if (is_array($admin)) {

	$db->QueryUpdate(
		"site_users" , 
		array(
			"user_password"			=> md5($_SESSION["CMSinstall"]["admin"]["pass"]),			
			"user_email"			=> $_SESSION["CMSinstall"]["admin"]["email"],			
			"user_level"			=> "0",
			"user_protect_delete"	=> "1",			
		),
		"user_id={$admin[user_id]}"
	);

	$output[] = "Updated administrator `" . $_SESSION["CMSinstall"]["admin"]["user"] . "` password.";

} else {

	//insert new record
	$id = $db->QueryInsert(
		"site_users" , 
		array(
			"user_first_name"		=> "Administrator",
			"user_login"			=> $_SESSION["CMSinstall"]["admin"]["user"],			
			"user_password"			=> md5($_SESSION["CMSinstall"]["admin"]["pass"]),			
			"user_email"			=> $_SESSION["CMSinstall"]["admin"]["email"],			
			"user_level"			=> "0",
			"user_protect_delete"	=> "1",
			"user_log_create"		=> time(),
			"user_status"			=> "1",
			"user_groups"			=> "1",
			"user_status"			=> "1",			
		)
	);

	$output[] = "Added administrator `" . $_SESSION["CMSinstall"]["admin"]["user"] . "`.";

	//autologin user in admin 
	$user = $db->QFetchArray("SELECT * FROM site_users WHERE user_id={$id}");
	$user["acl"] = "superadmin";
			
	$_SESSION["store"]["minibase"]["user"] = 1;
	$_SESSION["store"]["minibase"]["raw"] = $user;

}



?>