<?php
require("config.php");
require("includes/debug.php");
require("includes/functions.php");
require("includes/settings.php");

// 2.0 -> 2.0.1
function db20to201() {
	mysql_query("ALTER TABLE cmum_settings ADD osdlink TINYTEXT NOT NULL");
	mysql_query("ALTER TABLE cmum_settings CHANGE osdlink osdlink TINYTEXT NULL");
	mysql_query("UPDATE cmum_settings SET osdlink='0' WHERE id='1'");
	mysql_query("ALTER TABLE cmum_udb ADD customvalues TEXT NOT NULL AFTER usrgroup");
	mysql_query("ALTER TABLE cmum_udb CHANGE customvalues customvalues TEXT NULL");
	mysql_query("UPDATE cmum_settings SET addusercol='2' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET dbversion='2.0.1' WHERE id='1'");
}
// 2.0.1 -> 2.0.2
function db201to202() {
	mysql_query("ALTER TABLE cmum_settings ADD def_customvalues TEXT NOT NULL AFTER def_debug");
	mysql_query("ALTER TABLE cmum_settings CHANGE def_customvalues def_customvalues TEXT NULL");
	mysql_query("UPDATE cmum_settings SET dbversion='2.0.2' WHERE id='1'");
}
// 2.0.2 -> 2.1
function db202to21() {
	mysql_query("ALTER TABLE cmum_udb ADD ecmrate TINYTEXT NOT NULL AFTER customvalues");
	mysql_query("ALTER TABLE cmum_udb CHANGE ecmrate ecmrate TINYTEXT NULL");
	mysql_query("ALTER TABLE cmum_udb ADD startdate TINYTEXT NOT NULL AFTER ecmrate");
	mysql_query("ALTER TABLE cmum_udb CHANGE startdate startdate TINYTEXT NULL");
	mysql_query("ALTER TABLE cmum_udb ADD expiredate TINYTEXT NOT NULL AFTER startdate");
	mysql_query("ALTER TABLE cmum_udb CHANGE expiredate expiredate TINYTEXT NULL");
	mysql_query("ALTER TABLE cmum_settings DROP addusercol");
	mysql_query("ALTER TABLE cmum_settings ADD extracolumn TINYTEXT NOT NULL AFTER osdlink");
	mysql_query("ALTER TABLE cmum_settings CHANGE extracolumn extracolumn TINYTEXT NULL");
	mysql_query("UPDATE cmum_settings SET extracolumn='password' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET cmumversion='2.1' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET dbversion='2.1' WHERE id='1'");
}

// 2.1 -> 2.2
function db21to22() {
	mysql_query("ALTER TABLE cmum_settings ADD rndpasschr TEXT NOT NULL");
	mysql_query("ALTER TABLE cmum_settings CHANGE rndpasschr rndpasschr TEXT NULL");
	mysql_query("UPDATE cmum_settings SET rndpasschr='abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789' WHERE id='1'");
	mysql_query("ALTER TABLE cmum_settings ADD rndpasschrlength INT NOT NULL");
	mysql_query("ALTER TABLE cmum_settings CHANGE rndpasschrlength rndpasschrlength INT(11) NULL");
	mysql_query("UPDATE cmum_settings SET rndpasschrlength='8' WHERE id='1'");
	mysql_query("ALTER TABLE cmum_settings ADD orderbyfield TINYTEXT NOT NULL");
	mysql_query("ALTER TABLE cmum_settings CHANGE orderbyfield orderbyfield TINYTEXT NULL");
	mysql_query("UPDATE cmum_settings SET orderbyfield='name' WHERE id='1'");
	mysql_query("ALTER TABLE cmum_settings ADD orderbyorder TINYTEXT NOT NULL");
	mysql_query("ALTER TABLE cmum_settings CHANGE orderbyorder orderbyorder TINYTEXT NULL");
	mysql_query("UPDATE cmum_settings SET orderbyorder='asc' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET cmumversion='2.2' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET dbversion='2.2' WHERE id='1'");
}

// 2.2 -> 2.3
function db22to23() {
	$usrchsql = mysql_query("SELECT * FROM cmum_udb");
	$admchsql = mysql_query("SELECT * FROM cmum_admins");
		while($admdata=mysql_fetch_array($admchsql)) {
			$admid[$admdata["username"]] = $admdata["id"];
		}
			while($usrdata=mysql_fetch_array($usrchsql)) {
				if ($usrdata["addedby"] <> "") {
					if (isset($admid[$usrdata["addedby"]])) {
						mysql_query("UPDATE cmum_udb SET addedby='".$admid[$usrdata["addedby"]]."' WHERE id='".$usrdata["id"]."'");
					} else {
						mysql_query("UPDATE cmum_udb SET addedby='' WHERE id='".$usrdata["id"]."'");
					}
				}
				if ($usrdata["changedby"] <> "") {
					if (isset($admid[$usrdata["changedby"]])) {
						mysql_query("UPDATE cmum_udb SET changedby='".$admid[$usrdata["changedby"]]."' WHERE id='".$usrdata["id"]."'");
					} else {
						mysql_query("UPDATE cmum_udb SET changedby='' WHERE id='".$usrdata["id"]."'");
					}
				}
			}
	mysql_query("ALTER TABLE cmum_admins ADD level INT NOT NULL");
	mysql_query("ALTER TABLE cmum_admins CHANGE level level INT(11) NULL");
	mysql_query("ALTER TABLE cmum_admins ADD realname TINYTEXT NOT NULL AFTER password");
	mysql_query("ALTER TABLE cmum_admins CHANGE realname realname TINYTEXT NULL DEFAULT NULL");
	mysql_query("ALTER TABLE cmum_settings ADD def_ecmrate INT NOT NULL AFTER def_customvalues");
	mysql_query("ALTER TABLE cmum_settings CHANGE def_ecmrate def_ecmrate TINYTEXT NULL DEFAULT NULL");
	mysql_query("ALTER TABLE cmum_settings ADD cspconnchk INT NOT NULL AFTER cspsrv_protocol");
	mysql_query("ALTER TABLE cmum_settings CHANGE cspconnchk cspconnchk INT(11) NULL");
	mysql_query("UPDATE cmum_settings SET cspconnchk='1' WHERE id='1';");
	mysql_query("UPDATE cmum_settings SET cmumversion='2.3' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET dbversion='2.3' WHERE id='1'");
}

// 2.3 -> 2.3.1
function db23to231() {
	mysql_query("ALTER TABLE cmum_settings ADD genxmlkey TINYTEXT NOT NULL");
	mysql_query("ALTER TABLE cmum_settings CHANGE genxmlkey genxmlkey TINYTEXT NULL DEFAULT NULL");
	mysql_query("UPDATE cmum_settings SET cmumversion='2.3.1' WHERE id='1'");
	mysql_query("UPDATE cmum_settings SET dbversion='2.3.1' WHERE id='1'");
}

$latestdbver = "2.3.1";
$compupg = "false";

if (isset($_POST["sdbupg"]) and $_POST["sdbupg"] == "Yes, start upgrading!") {
	$connupg = @mysql_connect($dbhost,$dbuser,$dbpass) or sqlerror();
	mysql_select_db($dbname, $connupg);
		if ($_POST["currentdbver"] == "2.0") {
			db20to201();
			db201to202();
			db202to21();
			db21to22();
			db22to23();
			db23to231();
			optimizedb();
			$compupg = "true";
		}
		if ($_POST["currentdbver"] == "2.0.1") {
			db201to202();
			db202to21();
			db21to22();
			db22to23();
			db23to231();
			optimizedb();
			$compupg = "true";
		}
		if ($_POST["currentdbver"] == "2.0.2") {
			db202to21();
			db21to22();
			db22to23();
			db23to231();
			optimizedb();
			$compupg = "true";
		}
		if ($_POST["currentdbver"] == "2.1") {
			db21to22();
			db22to23();
			db23to231();
			optimizedb();
			$compupg = "true";
		}
		if ($_POST["currentdbver"] == "2.2") {
			db22to23();
			db23to231();
			optimizedb();
			$compupg = "true";
		}
		if ($_POST["currentdbver"] == "2.3") {
			db23to231();
			optimizedb();
			$compupg = "true";
		}
	$connupg = null; 
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=<?php print($charset); ?>" />
<title><?php print($longtitle); ?></title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="apple-touch-icon" href="favicon.png"> 
<link href="css/main.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="980" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD; text-align: left;">
  <tr bgcolor="#FFFFFF">
    <td height="22" colspan="6">&nbsp;</td>
  </tr>
  <tr bgcolor="#0066FF">
    <td width="15" bgcolor="#808080">&nbsp;</td>
    <td width="751" height="120" bgcolor="#808080">
    <font color="#FFFFFF"><div id="headertitle"><?php print($longtitle); ?> - Database Upgrade</div><br /><br />
	<?php
	$conn = @mysql_connect($dbhost,$dbuser,$dbpass) or sqlerror();
	mysql_select_db($dbname, $conn);
	$sql = mysql_query("SELECT * FROM cmum_settings WHERE id='1'") or sqlerror();
	$line=mysql_fetch_array($sql);
		$cmumversion = $line["cmumversion"];
		$dbversion = $line["dbversion"];
	$conn = null;
		print("CMUM Version: ".$cmumversion." | Database Version: ".$dbversion);
	?>
	</font>
	</td>
    <td width="15" bgcolor="#808080">&nbsp;</td>
  </tr>
    <tr bgcolor="#FFFFFF">
    <td height="18">&nbsp;</td>
    <td height="18">&nbsp;</td>
    <td height="18">&nbsp;</td>
  </tr>
</table>
<table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD; text-align: left;">
  <tr bgcolor="#FFFFFF">
    <td width="15" height="20">&nbsp;</td>
    <td height="20" colspan="4">
	<?
	if ($dbversion < $latestdbver and $compupg == "false") {
		print("Current database version: ".$dbversion." - Latest database version: ".$latestdbver."<br><br>Would you like to upgrade?<br><br>");
		print("<form action=\"upgrade.php\" method=\"post\"><input type=\"hidden\" name=\"currentdbver\" value=\"".$dbversion."\"><input type=\"submit\" name=\"sdbupg\" value=\"Yes, start upgrading!\" id=\"submit\"></form>   ");
	} elseif ($dbversion > $latestdbver and $compupg == "false") {
		print("You have a newer version of the database then this upgrade. No need to upgrade.<br><br>Please delete this file from your server!");
	} elseif ($dbversion == $latestdbver and $compupg == "false") {
		print("You allready have the latest database version. No need to upgrade.<br><br>Please delete this file from your server!");
	} elseif ($compupg == "true") {
		print("Atualizacao do banco de dados concluída com sucesso!<br><br>Por favor, apague este arquivo do seu servidor!");
	}
	?>
    </td>
    <td width="15" height="20">&nbsp;</td>
  </tr>
</table>
<table width="980" height="20" border="0" align="center" valign="top" cellpadding="0" cellspacing="0" style="border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left;">
    <tr bgcolor="#FFFFFF">
    <td height="25" width="780">&nbsp;</td>
  </tr>
</table>
<?php
require("includes/footer.php");
?>
</body>
</html>
