<?php
	$time = time();
	$message_erreur = "";
	$message_erreur2 = "";
	$access = 'on';
	require_once("../../system/database.php");
	
	$mysqli = @new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
	
	if (mysqli_connect_errno()) 
	{
		echo 1;
		die();
	}
	if(isset($_POST['password']) && isset($_POST['username'])){
			$password = $mysqli->real_escape_string(trim($_POST["password"]));
			$user_name = $mysqli->real_escape_string(trim($_POST["username"]));
			$password = sha1(str_rot13($password . $encryption));
			
			$exist = $mysqli->query("SELECT * FROM `users` WHERE `user_password` = '$password' AND `user_name` = '$user_name' AND `user_rank` > 4 ");
			if($exist->num_rows > 0){	
				$mysqli->query("ALTER TABLE `users` ADD ( temp_pass varchar(40) NOT NULL DEFAULT '0', temp_time varchar(10) NOT NULL DEFAULT '0', user_tumb varchar(100) NOT NULL DEFAULT 'default_avatar_tumb.png')");
				$mysqli->query("ALTER TABLE `chat` ADD avatar varchar(40) NOT NULL");
				$mysqli->query("ALTER TABLE `private` ADD avatar varchar(40) NOT NULL");
				$mysqli->query("ALTER TABLE `rooms` MODIFY room_name varchar(40)");
				$mysqli->query("ALTER TABLE `setting` ADD ( version varchar(4) NOT NULL DEFAULT '2', language varchar(20) NOT NULL DEFAULT 'English', ads_delay int(2) NOT NULL DEFAULT '10' )");
				$mysqli->query("ALTER TABLE `setting` ADD ( ads_time varchar(10) NOT NULL DEFAULT '0', ads_select int(1) NOT NULL DEFAULT '1', allow_ads int(1) NOT NULL DEFAULT '1', `ads_stop` varchar(1) NOT NULL DEFAULT '0', `ads_count` varchar(1) NOT NULL DEFAULT '1', `orientation` int(1) NOT NULL DEFAULT '1', `welcome` int(1) NOT NULL DEFAULT '0')");
				$mysqli->query("ALTER TABLE `setting` MODIFY ads_delay int(3)");
				$mysqli->query("UPDATE `setting` SET `ads_delay` = '30'");
				$mysqli->query("ALTER TABLE `users` ADD ( guest int(1) NOT NULL DEFAULT '0', verified int(1) NOT NULL DEFAULT '1', valid_key varchar(64) NOT NULL, user_ignore text NOT NULL)");
				$mysqli->query("ALTER TABLE `setting` ADD ( guest int(8) NOT NULL DEFAULT '1', allow_guest int(1) NOT NULL DEFAULT '0', guest_chat int(1) NOT NULL DEFAULT '0', guest_clear int(8) NOT NULL DEFAULT '3600', activation int(1) NOT NULL DEFAULT '0', cookie_ban int(1) NOT NULL DEFAULT '0', allow_email int(1) NOT NULL DEFAULT '1')");
				$mysqli->query("ALTER TABLE `users` ADD ( first_check int(10) NOT NULL DEFAULT '0', join_chat int(10) NOT NULL DEFAULT '0')");
				$mysqli->query("ALTER TABLE `setting` ADD ( `chat_speed` int(1) NOT NULL DEFAULT '2')");
				$mysqli->query("ALTER TABLE `rooms` ADD ( `access` int(1) NOT NULL DEFAULT '1')");
				$mysqli->query("INSERT INTO `theme` (name) VALUES ('Lite')");
				$mysqli->query("ALTER TABLE `setting` MODIFY ads_time int(10)");
				$mysqli->query("ALTER TABLE `setting` MODIFY ads_count int(1)");
				$mysqli->query("ALTER TABLE `setting` MODIFY ads_stop int(1)");
				$mysqli->query("ALTER TABLE `users` MODIFY user_join int(12)");
				$mysqli->query("ALTER TABLE `users` MODIFY last_action int(10)");
				$mysqli->query("ALTER TABLE `users` MODIFY mute_time int(12)");
				$mysqli->query("ALTER TABLE `users` MODIFY temp_time int(10)");
				$mysqli->query("ALTER TABLE `users` ADD email_count int(1) NOT NULL DEFAULT '0'");
				$mysqli->query("CREATE TABLE IF NOT EXISTS `addons` (
				  `id` int(10) NOT NULL AUTO_INCREMENT,
				  `name` varchar(50) NOT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				$mysqli->query("ALTER TABLE `private` ADD hunter_guest int(1) NOT NULL DEFAULT '0'");
				$mysqli->query("ALTER TABLE `setting` ADD silent_mode int(1) NOT NULL DEFAULT '0'");
				$mysqli->query("ALTER TABLE `setting` ADD show_topic int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE `setting` ADD global_sound int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE `users` ADD user_friends text NOT NULL");
				$mysqli->query("ALTER TABLE `setting` ADD private_style int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE `users` ADD user_friends text NOT NULL");
				$mysqli->query("UPDATE `setting` SET `version` = '50'");
				
				$mysqli->query("ALTER TABLE setting ADD ( 
				welcome_login_title varchar(40) NOT NULL DEFAULT 'Chat news',
				timezone varchar(60) NOT NULL DEFAULT 'America/Montreal',
				welcome_login varchar(300) NOT NULL DEFAULT 'Welcome to Boomchat you can change this message in your setting panel.',
				welcome_chat varchar(500) NOT NULL DEFAULT 'Welcome new member please be respectfull with other users and keep conversation clean enjoy your chat.',
				min_age int(2) NOT NULL DEFAULT '14',
				full_width int(1) NOT NULL DEFAULT '0',
				show_avatar int(1) NOT NULL DEFAULT '1',
				full_form int(1) NOT NULL DEFAULT '0',
				rules int(1) NOT NULL DEFAULT '0',
				allow_logs int(1) NOT NULL DEFAULT '1',
				full_sound int(1) NOT NULL DEFAULT '0'
				), CHARACTER SET utf8 COLLATE utf8_general_ci");
				
				$mysqli->query("ALTER TABLE users ADD ( 
				country varchar(100) NOT NULL,
				region varchar(100) NOT NULL,
				city varchar(100) NOT NULL
				), CHARACTER SET utf8 COLLATE utf8_general_ci");
				
				$mysqli->query("UPDATE users SET user_sound = '2' WHERE user_id > 0");
				$mysqli->query("ALTER TABLE users MODIFY COLUMN user_sound int(1) NOT NULL DEFAULT '2'");
				$mysqli->query("ALTER TABLE setting ADD rtl int(1) NOT NULL DEFAULT '0'");
				$mysqli->query("ALTER TABLE setting ADD allow_colors int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE setting ADD allow_avatar int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE users ADD count int(10) NOT NULL DEFAULT '0'");
				$mysqli->query("UPDATE setting SET allow_avatar = '1' WHERE id = 1");
				$mysqli->query("UPDATE setting SET allow_image = '4' WHERE id = 1");
				
				$mysqli->query("CREATE TABLE IF NOT EXISTS `friends` (
				  `id` int(10) NOT NULL AUTO_INCREMENT,
				  `hunter` varchar(50) NOT NULL,
				  `target` varchar(50) NOT NULL,
				  `status` int(1) NOT NULL DEFAULT 1,
				  PRIMARY KEY (`id`)
				) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci AUTO_INCREMENT=1");
				
				$mysqli->query("CREATE TABLE IF NOT EXISTS `player` (
				  `id` int(10) NOT NULL AUTO_INCREMENT,
				  `use_player` int(1) NOT NULL DEFAULT '0',
				  `player_status` int(1) NOT NULL DEFAULT '1',
				  `player_url` varchar(200) NOT NULL DEFAULT '',
				  PRIMARY KEY (`id`)
				) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci AUTO_INCREMENT=1");

				
				$mysqli->query("ALTER TABLE setting ADD allow_friend int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE setting ADD allow_ignore int(1) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE setting ADD allow_username int(1) NOT NULL DEFAULT '2'");
				$mysqli->query("ALTER TABLE setting ADD custom_count int(1) NOT NULL DEFAULT '0'");
				$mysqli->query("ALTER TABLE setting ADD custom1 varchar(50) NOT NULL DEFAULT 'Custom1'");
				$mysqli->query("ALTER TABLE setting ADD custom2 varchar(50) NOT NULL DEFAULT 'Custom2'");
				
				$mysqli->query("ALTER TABLE users ADD custom1 varchar(150) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD custom2 varchar(150) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD old_name varchar(30) NOT NULL DEFAULT 'e'");
				$mysqli->query("ALTER TABLE users ADD session_id int(10) NOT NULL DEFAULT '1'");
				$mysqli->query("ALTER TABLE users ADD facebook varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD twitter varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD pinterest varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD google varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD youtube varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD instagram varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD linkedin varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD tumblr varchar(120) NOT NULL DEFAULT ''");
				$mysqli->query("ALTER TABLE users ADD flickr varchar(120) NOT NULL DEFAULT ''");
				
				
				$mysqli->query("UPDATE setting SET version = '60' WHERE id = 1");
				
				$mysqli->query("INSERT INTO `player` (id) VALUES ('1')");
				
				echo "<div id=\"container_response\"><h2 class=\"success_h2\">Update completed with success</h2><a href=\"../index.php\" class=\"success_a\">Click here to go back to your chat</a></div>";
				die();
			}
			else {
				echo 3;
			}
	}
	else {
		echo 2;
	}

?>