<?php
require_once(dirname(__FILE__).'/../../../upgrade.php');
class create_column_tokencode 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()
	{
		return $this->ColumnExists($GLOBALS['tablePrefix'].'users', 'tokencode');
	}

	/**
	* 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 = "ALTER TABLE `".$GLOBALS['tablePrefix']."users` ADD COLUMN `tokencode` varchar(128) NOT NULL default ''";
		$result = $GLOBALS['AKB_DB']->Query($query);
		
		$query = "SELECT userid, username, pass FROM ".$GLOBALS['tablePrefix']."users";
		$result = $GLOBALS['AKB_DB']->Query($query);
		while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
			$newTokenCode = generateTokenCode($row['username']);
			if (trim($row['pass']) != '@LDAP') {
				$newPassword = md5(md5($newTokenCode) . $row['pass']);
				$updateQuery = "UPDATE ".$GLOBALS['tablePrefix']."users SET tokencode = '".$newTokenCode."', pass = '".$newPassword."'  WHERE userid = '".$row['userid']."'";
			} else {
				$updateQuery = "UPDATE ".$GLOBALS['tablePrefix']."users SET tokencode = '".$newTokenCode."' WHERE userid = '".$row['userid']."'";
			}
			$updateResult = $GLOBALS['AKB_DB']->Query($updateQuery);
		}
		
	}
}
?>
