<?php
require_once(dirname(__FILE__).'/../../upgrade.php');
require_once(dirname(__FILE__).'/../../api/class.user.php');
class create_admin_user extends UPGRADE
{
	/**
	* IsAlreadyUpgraded
	* Do we need to run this classes upgrade script or not ?
	*
	* @return bool true if the upgrade has already run, otherwise false
	*/
	function IsAlreadyUpgraded()
	{
		$query = "SELECT * FROM ".$GLOBALS['tablePrefix']."users WHERE pk_userId = 1";
		$result = $GLOBALS['AKB_DB']->Query($query);
		return ($GLOBALS['AKB_DB']->CountResult($result) == 1);
	}

	/**
	* Upgrade
	* Do the actual upgrade of this component. If this is successful all
	* subsequent calls to IsAlreadyUpgraded should return true
	*
	* @return void
	*/
	function Upgrade()
	{
		$query = "INSERT INTO ".$GLOBALS['tablePrefix']."users (userName, userPass, userStatus, userRoot, userEmail) values ('".$GLOBALS['AKB_DB']->Quote($GLOBALS['adminUser'])."', '".md5($GLOBALS['adminPass'])."', 1, 1, '".$GLOBALS['AKB_DB']->Quote($GLOBALS['contactEmail'])."')";
		$newid = $GLOBALS['AKB_DB']->Query($query);

		// Add permissions for the user (101-122)
		for ($i = 101; $i < 122; $i++) {
			$query = 'INSERT INTO '.$GLOBALS['tablePrefix'].'permissions (permUserId, permPermissionId) VALUES ('.$newid.', '.$i.')';
			$GLOBALS['AKB_DB']->Query($query);
		}
	}

}
?>
