<commit_before><?php
class SpotUserUpgrader {
	private $_db;
	private $_settings;

	function __construct(SpotDb $db, SpotSettings $settings) {
		$this->_db = $db;
		$this->_settings = $settings;
	} # ctor

	function update() {
		$this->createSecurityGroups();
		$this->createAnonymous();
		$this->createAdmin('admin', 'user', 'admin', '<EMAIL>');
		
		$this->updateUserPreferences();
		$this->updateSecurityGroups(false);

		/* Reset the users' group membership */
		if ($this->_settings->get('securityversion') < 0.27) {
			$this->resetUserGroupMembership($this->_settings->get('systemtype'));
		} # if

		$this->updateUserFilters(false);
		$this->updateSecurityVersion();
	} # update()

	/*
	 * Creates the anonymous user
	 */
	function createAnonymous() {
		# if we already have an anonymous user, exit
		$anonUser = $this->_db->getUser(1);
		if ($anonUser !== false) {
			if ($anonUser['username'] != 'anonymous') {
				throw new Exception("Anonymous user is not anonymous anymore. Database logical corrupted, unable to continue");
			} # if
			
			return ;
		} # if

		# DB connection
		$dbCon = $this->_db->getDbHandle();

		# Create an Spotweb API key. It cannot be used but should not be empty
		$apikey = md5('anonymous');
		
		# Create the dummy 'anonymous' user
		$anonymous_user = array(
			# 'userid'		=> 1,		<= Must be 1 for the anonymous user
			'username'		=> 'anonymous',
			'firstname'		=> 'Jane',
			'passhash'		=> '',
			'lastname'		=> 'Doe',
			'mail'			=> '<EMAIL>',
			'apikey'		=> $apikey,
			'lastlogin'		=> 0,
			'lastvisit'		=> 0,
			'deleted'		=> 0);
		$this->_db->addUser($anonymous_user);

		# Manually update the userid so we can be sure anonymous == userid 1
		$currentId = $dbCon->singleQuery("SELECT id FROM users WHERE username = 'anonymous'");
		$dbCon->exec("UPDATE users SET id = 1 WHERE username = 'anonymous'");
		$dbCon->exec("UPDATE usersettings SET userid = 1 WHERE userid = '%s'", Array( (int) $currentId));
	} # createAnonymous

	/*
	 * Create the admin user 
	 */
	function createAdmin($firstName, $lastName, $password, $mail) {
		# if we already have an admin user, exit
		$adminUser = $this->_db->getUser(2);
		if ($adminUser !== false) {
			return ;
		} # if
		
		# DB connection
		$dbCon = $this->_db->getDbHandle();

		# Retrieve the password salt from the settings
		$passSalt = $this->_settings->get('pass_salt');
		
		# calculate the password salt for the admin user
		$adminPwdHash = sha1(strrev(substr($passSalt, 1, 3)) . $password . $passSalt);
		
		# Create an Spotweb API key. It cannot be used but should not be empty
		$apikey = md5('admin');
		
		# Create the dummy 'admin' user
		$admin_user = array(
			# 'userid'		=> 2,		
			'username'	=> 'admin',
			'firstname'	=> $firstName,
			'lastname'		=> $lastName,
			'passhash'	=> $<PASSWORD>,
			'mail'		=> $mail,
			'apikey'		=> $apikey,
			'lastlogin'		=> 0,
			'lastvisit'		=> 0,
			'deleted'		=> 0);
		$this->_db->addUser($admin_user);

		# Manually update the userid so we can be sure admin == userid 2
		$currentId = $dbCon->singleQuery("SELECT id FROM users WHERE username = 'admin'");
		$dbCon->exec("UPDATE users SET id = 2 WHERE username = 'admin'");
		$dbCon->exec("UPDATE usersettings SET userid = 2 WHERE userid = '%s'", Array( (int) $currentId));
	} # createAdmin

	/*
	 * Update all users preferences
	 */
	function resetUserGroupMembership($systemType) {
		# DB connection
		$dbCon = $this->_db->getDbHandle();

		$userList = $this->_db->getUserList();

		# loop through every user and fix it 
		foreach($userList as $user) {
			/*
			 * Remove current group membership
			 */
			$dbCon->rawExec("DELETE FROM usergroups WHERE userid = " . $user['userid']);
			
			/* 
			 * Actually update the group membership, depending
			 * on what kind of user this is
			 */
			if ($user['userid'] == 1) {
				# Anonymous user
				if ($systemType == 'shared') {
					/* Grant the group with only logon rights */
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(1, 1, 1)");
				} else {
					/* Grant the group with the view permissions */
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(1, 2, 1)");
				} # else
			} elseif (($user['userid'] == 2) || ($user['userid'] == $this->_settings->get('custom_admin_userid'))) {
				# Admin user
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(2, 2, 1)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(2, 3, 2)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(2, 4, 3)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(2, 5, 4)");
			} else {
				# Grant the regular users all the necessary security groups
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 2, 1)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 3, 2)");

				# For a shared and single system, all users are trusted
				if (($systemType == 'shared') || ($systemType == 'single')) {
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 4, 3)");
				} # if

				# For a single system, all users are administrators as well (there should only be one additional user)
				if ($systemType == 'single') {
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 5, 4)");
				} # if<commit_msg>Fix a bug in the installer where the built-in account was incorrectly given admin rights twice causing an duplicate key exception and hence a not working installer.<commit_after><?php
class SpotUserUpgrader {
	private $_db;
	private $_settings;

	function __construct(SpotDb $db, SpotSettings $settings) {
		$this->_db = $db;
		$this->_settings = $settings;
	} # ctor

	function update() {
		$this->createSecurityGroups();
		$this->createAnonymous();
		$this->createAdmin('admin', 'user', 'admin', '<EMAIL>');
		
		$this->updateUserPreferences();
		$this->updateSecurityGroups(false);

		/* Reset the users' group membership */
		if ($this->_settings->get('securityversion') < 0.27) {
			$this->resetUserGroupMembership($this->_settings->get('systemtype'));
		} # if

		$this->updateUserFilters(false);
		$this->updateSecurityVersion();
	} # update()

	/*
	 * Creates the anonymous user
	 */
	function createAnonymous() {
		# if we already have an anonymous user, exit
		$anonUser = $this->_db->getUser(1);
		if ($anonUser !== false) {
			if ($anonUser['username'] != 'anonymous') {
				throw new Exception("Anonymous user is not anonymous anymore. Database logical corrupted, unable to continue");
			} # if
			
			return ;
		} # if

		# DB connection
		$dbCon = $this->_db->getDbHandle();

		# Create an Spotweb API key. It cannot be used but should not be empty
		$apikey = md5('anonymous');
		
		# Create the dummy 'anonymous' user
		$anonymous_user = array(
			# 'userid'		=> 1,		<= Must be 1 for the anonymous user
			'username'		=> 'anonymous',
			'firstname'		=> 'Jane',
			'passhash'		=> '',
			'lastname'		=> 'Doe',
			'mail'			=> '<EMAIL>',
			'apikey'		=> $apikey,
			'lastlogin'		=> 0,
			'lastvisit'		=> 0,
			'deleted'		=> 0);
		$this->_db->addUser($anonymous_user);

		# Manually update the userid so we can be sure anonymous == userid 1
		$currentId = $dbCon->singleQuery("SELECT id FROM users WHERE username = 'anonymous'");
		$dbCon->exec("UPDATE users SET id = 1 WHERE username = 'anonymous'");
		$dbCon->exec("UPDATE usersettings SET userid = 1 WHERE userid = '%s'", Array( (int) $currentId));
	} # createAnonymous

	/*
	 * Create the admin user 
	 */
	function createAdmin($firstName, $lastName, $password, $mail) {
		# if we already have an admin user, exit
		$adminUser = $this->_db->getUser(2);
		if ($adminUser !== false) {
			return ;
		} # if
		
		# DB connection
		$dbCon = $this->_db->getDbHandle();

		# Retrieve the password salt from the settings
		$passSalt = $this->_settings->get('pass_salt');
		
		# calculate the password salt for the admin user
		$adminPwdHash = sha1(strrev(substr($passSalt, 1, 3)) . $password . $passSalt);
		
		# Create an Spotweb API key. It cannot be used but should not be empty
		$apikey = md5('admin');
		
		# Create the dummy 'admin' user
		$admin_user = array(
			# 'userid'		=> 2,		
			'username'	=> 'admin',
			'firstname'	=> $firstName,
			'lastname'		=> $lastName,
			'passhash'	=> $<PASSWORD>,
			'mail'		=> $mail,
			'apikey'		=> $apikey,
			'lastlogin'		=> 0,
			'lastvisit'		=> 0,
			'deleted'		=> 0);
		$this->_db->addUser($admin_user);

		# Manually update the userid so we can be sure admin == userid 2
		$currentId = $dbCon->singleQuery("SELECT id FROM users WHERE username = 'admin'");
		$dbCon->exec("UPDATE users SET id = 2 WHERE username = 'admin'");
		$dbCon->exec("UPDATE usersettings SET userid = 2 WHERE userid = '%s'", Array( (int) $currentId));
	} # createAdmin

	/*
	 * Update all users preferences
	 */
	function resetUserGroupMembership($systemType) {
		# DB connection
		$dbCon = $this->_db->getDbHandle();

		$userList = $this->_db->getUserList();

		# loop through every user and fix it 
		foreach($userList as $user) {
			/*
			 * Remove current group membership
			 */
			$dbCon->rawExec("DELETE FROM usergroups WHERE userid = " . $user['userid']);
			
			/* 
			 * Actually update the group membership, depending
			 * on what kind of user this is
			 */
			if ($user['userid'] == 1) {
				# Anonymous user
				if ($systemType == 'shared') {
					/* Grant the group with only logon rights */
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(1, 1, 1)");
				} else {
					/* Grant the group with the view permissions */
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(1, 2, 1)");
				} # else
			} elseif (($user['userid'] == 2) || ($user['userid'] == $this->_settings->get('custom_admin_userid'))) {
				# Admin user
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 2, 1)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 3, 2)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 4, 3)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 5, 4)");
			} else {
				# Grant the regular users all the necessary security groups
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 2, 1)");
				$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 3, 2)");

				# For a shared and single system, all users are trusted
				if (($systemType == 'shared') || ($systemType == 'single')) {
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 4, 3)");
				} # if

				# For a single system, all users are administrators as well (there should only be one additional user)
				if ($systemType == 'single') {
					$dbCon->rawExec("INSERT INTO usergroups(userid,groupid,prio) VALUES(" . $user['userid'] . ", 5, 4)");
				} # if
{'fraction_non_alphanumeric': 0.16635194676737702, 'fraction_numerical': 0.009171837065012138, 'mean_word_length': 3.698774820447824, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1969659', 'n_tokens_mistral': 4268, 'n_tokens_neox': 3628, 'n_words': 1023}