<?php
if (!defined('IN_SITE'))
{
		die('Hacking attempt!');
}
class New_Install
{
		function New_Install($data)
		{
				global $Template;
				$Template->set_files(array("header" => "header.tpl", "error" => "install_error.tpl", "success" => "install_success.tpl", "refresh" => "install_refresh.tpl", "footer" => "footer.tpl", ));
				$Template->show("header");
				echo '<center><table border="0" cellpadding="0"><tr><td align="left">';
				$this->update_system_files($data);
				$this->import_sql($data);
				$this->create_admin($data);
				echo "<br><br><font class=search>SoSo News Express has been successfully installed!</font>";
				$Template->show("success");
				echo '</td></tr></table></center>';
				$Template->show("footer");
		}
		function update_system_files($data)
		{
				global $Func;
				echo "<font class=title2>Checking your data .... </font><font class=search>[OK]</font><br><br>";
				if (!$Func->write_config_file($data))
				{
						$this->halt("<font class=title2>Updating config.php .... </font><font class=search>[ERROR] - You must chmod 777 the folder in which you are installing the script. Ex: public_html, public_html/news, httpdocs,...</font>");
				}
				echo "<font class=title2>Updating config.php .... </font><font class=search>[OK]</font><br><br>";
				if (!$Func->write_htaccess_file($data))
				{
						$this->halt("<font class=title2>Updating .htaccess .... </font><font class=search>[ERROR] - You must chmod 777 the folder in which you are installing the script. Ex: public_html, public_html/news, httpdocs,...</font>");
				}
				echo "<font class=title2>Updating .htaccess .... </font><font class=search>[OK]</font><br><br>";
		}
		function import_sql($data)
		{
				global $Template, $DB, $Func;
				if (!$DB->sverconn_id)
				{
						if (!$DB->connect())
						{
								$this->halt("<font class=title2>Connecting database server .... </font><font class=search>[ERROR]</font>");
						}
						echo "<font class=title2>Connecting to DB server [" . $data['host_name'] . "] .... </font><font class=search>[OK]</font><br><br>";
				}
				if (!$DB->selectdb($data['db_name']))
				{
						echo "<font class=title2>Selecting DB name [" . $data['db_name'] . "] .... </font><font class=search>[NOT EXIST DB]</font><br>";
						if (!$DB->createdb($data['db_name']))
						{
								$this->halt("<font class=title2>Creating database name [" . $data['db_name'] . "] .... </font><font class=search>[ERROR]</font>");
						}
						echo "<font class=title2>Creating DB name [" . $data['db_name'] . "] .... </font><font class=search>[OK]</font><br>";
						if (!$DB->selectdb($data['db_name']))
						{
								$this->halt("<font class=title2>Selecting database name [" . $data['db_name'] . "] .... </font><font class=search>[ERROR]</font>");
						}
				}
				echo "<font class=title2>Selecting DB name [" . $data['db_name'] . "] .... </font><font class=search>[OK]</font><br><br>";
				$sql = str_replace("{PREFIX}_", $data['tbl_prefix'], implode('', file('./db/full_install.sql')));
				$sql .= str_replace("{PREFIX}_", $data['tbl_prefix'], implode('', file('./db/mod_country_code.sql')));
				$sql = $Func->remove_remarks($sql);
				$sql_info = $Func->compile_sql($sql);
				echo "<font class=title2>Creating tables ............. </font><br>";
				$tbl_insert = "";
				$rows = 0;
				$count = sizeof($sql_info);
				for ($i = 0; $i < $count; $i++)
				{
						$sql_query = trim($sql_info[$i]);
						if (empty($sql_query) || (substr($sql_query, 0, 1) == '#'))
						{
								continue;
						}
						if (preg_match('/CREATE TABLE (.*?)\((.*?)/i', $sql_query, $match))
						{
								$tbl_name = $match[1];
						}
						else
						{
								$tbl_name = "";
								if (preg_match('/INSERT INTO (.*?)\((.*?)/i', $sql_query, $match))
								{
										$tbl_insert = $match[1];
								}
								else
								{
										$tbl_insert = "";
								}
						}
						if (!$DB->query($sql_query))
						{
								if ($tbl_insert == "")
								{
										$this->halt("<font class=title2>Creating table [" . $tbl_name . "].... </font><font class=search>[ERROR]</font>");
								}
								else
								{
										$this->halt("<font class=title2>Inserting data to table [" . $tbl_insert . "] .... </font><font class=search>[ERROR]</font>");
								}
						}
						if ($tbl_name != "")
						{
								echo "<font class=textbody>Creating table [" . $tbl_name . "] .... </font><font class=search2>[OK]</font><br>";
						}
				}
				echo "<br><font class=title2>Importing setting data.... </font>";
				if (!$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('site_name','" . $data['site_name'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('site_slogan','" . $data['site_slogan'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('site_url','" . $data['site_url'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('site_path','" . $data['site_path'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('short_url_enabled','" . $data['use_short_url'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('short_url_sep','" . $data['url_sep'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('cookie_domain','" . $data['cookie_domain'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('cookie_path','" . $data['cookie_path'] . "')") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "config(config_name,config_value) VALUES('admin_email','" . $data['admin_email'] . "')"))
				{
						$this->halt("<font class=title2>Inserting data to " . $data['tbl_prefix'] . "config table.... </font><font class=search>[ERROR]</font>");
				}
				echo "<font class=search>[OK]</font><br>";
		}
		function create_admin($data)
		{
				global $Template, $DB, $Func;
				if (!$DB->sverconn_id)
				{
						if (!$DB->connect())
						{
								$this->halt("<font class=title2>Connecting database server .... </font><font class=search>[ERROR]</font>");
						}
						echo "<font class=title2>Connecting to DB server [" . $data['host_name'] . "] .... </font><font class=search>[OK]</font><br><br>";
				}
				echo "<br><font class=title2>Creating Admin Account.... </font>";
				if (!$DB->query("INSERT INTO " . $data['tbl_prefix'] . "user_group(group_name, group_desc, group_level, user_counter) VALUES('Admin', 'System Administrator', 1, 1)") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "user(username, user_password, user_email, user_timezone, user_last_login, user_ip, user_template, user_language, article_counter, enabled) VALUES('" . $data['username'] . "', '" . md5($data['password']) . "', '" . $data['admin_email'] . "', '20', '0', '', '', 'English', '30', 1)") || !$DB->query("INSERT INTO " . $data['tbl_prefix'] . "user_group_ids(user_id, group_id) VALUES(1, 1)"))
				{
						$this->halt("<font class=title2>Creating Admin Account.... </font><font class=search>[ERROR]</font>");
				}
				echo "<font class=search>[OK]</font>";
				$DB->close();
		}
		function halt($msg)
		{
				global $Template;
				echo $msg, " <font class=date> - ", @mysql_error(), "</font>";
				$Template->show("error");
				$Template->show("footer");
				die();
		}
}
?>
