<?PHP


class installer extends engine{

	var $call_class = array('File');

	//UPDATE

	function install(){

		$_POST = $this->trim($_POST);

		$connected = true;
		$connection = mysqli_connect($_POST['host'].($_POST['port'] && $_POST['port'] != 3306 ? ':'.$_POST['port'] : ''), $_POST['user'], $_POST['password']) or($connected = false);
		mysqli_select_db($connection, $_POST['database']) or($connected = false);

		if($connected == false){

			$this->error('database', 'cannot access to database');
		}
		else{

			$import_error = false;

			$q = mysqli_query($connection, "SHOW TABLES");
			while($r = mysqli_fetch_Assoc($q)){

				$table = $r['Tables_in_'.$_POST['database']];
				mysqli_query($connection, "DROP TABLE ".$table);
			}

			$exp = explode(';', str_replace('&nbsp;', ' ', file_get_contents(_RootFolder.'/d22341sd21782.sql')));
			foreach($exp as $v){

				$sql = ($v);
				if(strlen($sql) > 0)
					mysqli_query($connection, $sql) or $import_error .= mysqli_error($connection).'<br><br>'.$sql;
			}

			if($import_error == false){

				$mysqlError = false;
				$writeError = false;

				$content = file_get_contents(_RootFolder.'/inc/conf.php');

				$content = str_Replace("'DEFAULT_LANGUAGE'", "'".$_POST['default_language']."'", $content);
				$content = str_Replace("'DB_PREFIX'", "'".$_POST['prefix']."'", $content);
				$content = str_Replace("'DB_HOST'", "'".$_POST['host']."'", $content);
				$content = str_Replace("'DB_PORT'", "'".$_POST['port']."'", $content);
				$content = str_Replace("'DB_DATABASE'", "'".$_POST['database']."'", $content);
				$content = str_Replace("'DB_USER'", "'".$_POST['user']."'", $content);
				$content = str_Replace("'DB_PASSWORD'", "'".$_POST['password']."'", $content);
				$content = str_Replace("'DB_ERROR_REPORT_EMAIL'", "'".$_POST['error_report_email']."'", $content);
				$content = str_Replace("'DB_ERROR_SHOW_ERROR'", "false", $content);
				$content = str_Replace("'DB_MYADMIN'", "'".$_POST['phpmyadmin']."'", $content);

				file_put_contents(_RootFolder.'/inc/conf.php', $content) or $writeError = true;

				//superadmin and admin permissions
				$q = mysqli_query($connection, "SELECT id FROM roles WHERE type = 'SUPERADMIN' ");
				$row = mysqli_fetch_Assoc($q);
				$role_id = $row['id'];

				mysqli_query($connection, "INSERT INTO users (role_id, name, password, type) VALUES (".$role_id.", '".$_POST['admin_user']."', '".md5($_POST['admin_password'])."', 'SUPERADMIN')") or $mysqlError = mysqli_error($connection);
				$user_id = mysqli_insert_id($connection);

				mysqli_query($connection, "INSERT INTO clients (user_id, first_name) VALUES (".$user_id.", '".$_POST['admin_user']."')") or $mysqlError = mysqli_error($connection);
				$client_id = mysqli_insert_id($connection);

				mysqli_query($connection, "INSERT INTO client_details (client_id) VALUES (".$client_id.")") or $mysqlError = mysqli_error($connection);

				$q = mysqli_query($connection, "SELECT * FROM templates WHERE type = 'MODEL' ") or $mysqlError = mysqli_error($connection);
				while($row = mysqli_fetch_Assoc($q)){

					mysqli_query($connection, "INSERT INTO permissions (role_id, table_name, table_id, created_at, allow_access) VALUES (".$role_id.", 'templates', ".$row['id'].", NOW(), 1)") or $mysqlError = mysqli_error($connection);
				}

				//admin permissions
				$q = mysqli_query($connection, "SELECT id FROM roles WHERE type = 'ADMIN' ") or $mysqlError = mysqli_error($connection);
				$row = mysqli_fetch_Assoc($q);
				$role_id = $row['id'];

				$q = mysqli_query($connection, "SELECT * FROM templates WHERE type = 'MODEL' AND name IN('banner', 'content', 'dashboard', 'language', 'login', 'menu', 'messages', 'pages', 'settings', 'users') ") or $mysqlError = mysqli_error($connection);
				while($row = mysqli_fetch_Assoc($q)){

					mysqli_query($connection, "INSERT INTO permissions (role_id, table_name, table_id, created_at, allow_access) VALUES (".$role_id.", 'templates', ".$row['id'].", NOW(), 1)") or $mysqlError = mysqli_error($connection);
				}

				//not logged user permissions
				$q = mysqli_query($connection, "SELECT id FROM roles WHERE type = 'USER' ") or $mysqlError = mysqli_error($connection);
				$row = mysqli_fetch_Assoc($q);
				$role_id = $row['id'];
				$pageList = array('right_banner', 'top_offer_banner', 'login', 'public_menu', 'page', 'title', 'meta_description', 'meta_keywords');

				$q = mysqli_query($connection, "SELECT * FROM templates WHERE type = 'MODEL' AND name IN('banner', 'login', 'menu', 'pages', 'settings') ") or $mysqlError = mysqli_error($connection);
				while($row = mysqli_fetch_Assoc($q)){

					if(in_array($row['page'], $pageList))
						mysqli_query($connection, "INSERT INTO permissions (role_id, table_name, table_id, created_at, allow_access) VALUES (".$role_id.", 'templates', ".$row['id'].", NOW(), 1)") or $mysqlError = mysqli_error($connection);
				}



				//logged user permissions
				$q = mysqli_query($connection, "SELECT id FROM roles WHERE type = 'CLIENT' ") or $mysqlError = mysqli_error($connection);
				$row = mysqli_fetch_Assoc($q);
				$role_id = $row['id'];
				$pageList = array('right_banner', 'top_offer_banner', 'view_stat', 'login', 'logged', 'public_menu', 'page', 'title', 'meta_description', 'meta_keywords');

				$q = mysqli_query($connection, "SELECT * FROM templates WHERE type = 'MODEL' AND name IN('banner', 'login', 'menu', 'pages', 'settings') AND page_position LIKE '%public%' ") or $mysqlError = mysqli_error($connection);
				while($row = mysqli_fetch_Assoc($q)){

					if(in_array($row['page'], $pageList))
						mysqli_query($connection, "INSERT INTO permissions (role_id, table_name, table_id, created_at, allow_access) VALUES (".$role_id.", 'templates', ".$row['id'].", NOW(), 1)") or $mysqlError = mysqli_error($connection);
				}

				mysqli_query($connection, "INSERT INTO settings (name, value, created_at) VALUES ('installed', 'TRUE', NOW())") or $mysqlError = mysqli_error($connection);
				mysqli_close($connection);
				if($mysqlError == false && $writeError == false){

					unlink(_RootFolder.'/d22341sd21782.sql');
					sleep(3);//because cannot write conf file so fast as needed
					die($this->Redirect(_RootUrl.'/admin/?model=header_data&page=header_data&action=login&user='.$_POST['admin_user'].'&password='.md5($_POST['admin_password'])));
				}
				else{

					if($mysqlError == true)
						$this->error('mysql_error', $mysqlError);
					if($writeError == true)
						$this->error('write_error', 'Cannot overwrite inc/conf.php fail please change file permissions');
				}
			}
			else{

				$this->error('import_error', $import_error);
			}
		}
	}

}


?>