<?php
/** 
* @package	uForum 
* @file		install/index.php
* @version	$Id$
* @copyright	2007-2010 (c) PioDer <pioder@wp.pl>
* @link    		 		 	http://www.pioder.pl/
* @license	see LICENSE.txt
**/
define('IN_uF',true);
require('./../config.php');
require('./../includes/constants.php');
require('./../includes/errors.php');
require('./../includes/db.php');
require('./functions_add.php');
require('./../admin/check_script_data.php');

if (ini_get('short_open_tag')==0)
{
	die('<font color="red" size="4">Please enable <b>short_open_tag</b> in PHP configuration. Aborted!</font>'); 
}

if(get_magic_quotes_gpc())
{
	die('<font color="red" size="4">Please disable <b>magic_quotes_gpc</b> in PHP configuration. Aborted!</font>'); 
}

if(ini_get('register_globals'))
{
	die('<font color="red" size="4">Please disable <b>register_globals</b> in PHP configuration. Aborted!</font>');
}
$count =0;
for($i=0; $i<count($script_files);$i++)
{
	$actual = check_size_md5($script_files[$i]);
	$rule = $size_md5[$script_files[$i]];
	if ($actual!=$rule)
	{
		$count +=1;
	}
}
if (UF_INSTALLED)
{
	header('Location: ../index.php');
}
if (!isset($_GET['page']))
{
	header('Location: index.php?page=step1');
}
if(!isset($_COOKIE['lang']))
{
	$_COOKIE['lang']='Polish';
}
if (@is_file('./../lngs/'.$_COOKIE['lang'].'/install.php'))
{
	require('./../lngs/'.$_COOKIE['lang'].'/install.php');
}
$header = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>&micro;Forum Installer</title>
<style type="text/css">
body
{
font-family: Verdana;
font-color: black;
font-size: 11px;
}
</style>
</head>
<body class="body">
<center>
<div align="center" style="width: 748px; background-color: #EBEBEB">
<table class="maintable" width="748">
<tr><td align="center" colspan="2"><font face="Verdana" style="font-size: 24pt"><b>'.$lng['install_header'].'</b></font></td></tr>';
switch($_GET['page'])
{
	case 'step1':
	{
   		if (isset($_POST['lang']))
   		{
       			setcookie('lang',$_POST['lang'], $_SERVER['REQUEST_TIME']+360000000);
       			header('Location: index.php?page=step2');
		}
		else
		{
  			echo $header.'
    			  <tr><td width="748" height="400" align="center">
			  <span class="fstandard"><b>Select a language:</b>
			  <form action="index.php?page=step1" method="POST">
   			  <select style="width: 150px" onchange="this.form.submit()" name="lang">
			  <option value="Polish">&nbsp;</option>
			  '.AddLangs(false).'
			  </select></td></tr></table></div></center></body></html>';
	   	}
     		break;
	}
	case 'step2':
	{
   		if (isset($_POST['accept']))
   		{
       			header('Location: index.php?page=step3');
		}
		else
		{
	 		echo $header.'
  			 <tr><td align="center"><span class="fstandard"><b>'.$lng['licence'].'</b></td></tr>
    			  <tr><td width="748" align="center">
			  <form action="index.php?page=step2" method="POST">
			  <iframe width="748" height="500" src="licence.html">
			  Sorry, you browser doesn\'t support frames</iframe>
			     <input type="submit" class="fbutton" value="'.$lng['accept_licence'].'"
			  <input type="hidden" name="accept">
			  </td></tr></table></div></body></html>';
	  	}
		break;
	}
	case 'step3':
	{
		if (isset($_POST['next']))
   		{
       			header('Location: index.php?page=step4');
		}
		else
		{
			$bad = 0;
			if (!@is_writable($folders[0])) { $bad =+1; }
			if (!@is_writable($folders[1])) { $bad =+1; }
			if (!@is_writable($folders[2])) { $bad =+1; }
			if (phpversion()<'4.3.10') { $bad=+1; }
			if (mysql_get_client_info()<'4.1') { $bad =+1; }
			if ($count>1) { $bad =+1; } 
			echo $header;
			$skin=array(
			'L.name'=>$lng['name'],
			'L.value'=>$lng['value'],
			'L.php_version'=>$lng['php_version'],
			'PHP.version'=>phpversion(),
			'PHP.value' => (phpversion()>='5.0.7')  ? '<font color="green">OK</font>' : ' <font color="red">Error - No OK</font>',
			'mySQL.version'=>mysql_get_client_info(),
			'L.mySQL_version'=>$lng['mysql_version'],
			'mySQL.value'=>(mysql_get_client_info()>='4.1')  ? '<font color="green">OK</font>' : ' <font color="red">Error - No OK</font>',
			'L.cat_name'=>$lng['catalog'],
			'cat1.name'=> $folders[0],
			'cat1.value'=> (@is_writable($folders[0])) ? '<font color="green">'.$lng['is_writable'].' - OK</font>' : '<font color="red">'.$lng['not_writable'].' Error - No OK</font>',
			'cat2.name'=> $folders[1],
			'cat2.value'=> (@is_writable($folders[1])) ? '<font color="green">'.$lng['is_writable'].' - OK</font>' : '<font color="red">'.$lng['not_writable'].' Error - No OK</font>',
			'cat3.name'=> $folders[2],
			'cat3.value'=> (@is_writable($folders[2])) ? '<font color="green">'.$lng['is_writable'].' - OK</font>' : '<font color="red">'.$lng['not_writable'].' Error - No OK</font>',
			'successfully' => ($count <1) ? '<font color="green">'.$lng['files_is_good'].'  ('.count($script_files).')</font>' : '<font color="red">'.$lng['files_not_good'].'  ('.$count.')</font>'
			);
			require('./tpls/install_check_script.tpl');
			for($i=0; $i<count($script_files);$i++)
			{
				$actual = check_size_md5($script_files[$i]);
				$rule = $size_md5[$script_files[$i]];
				if ($actual!=$rule)
				{
					$skin=array(
					'L.sum_actual'=>$lng['actual_sum'],
					'sum_actual'=>$actual,
					'L.invalid_md5sum'=>$lng['invalid_md5sum'].' ('.substr($script_files[$i], strrpos($script_files[$i], '/') + 1, strlen($script_files[$i])).')'
					);
					require('./tpls/invalidfile_body.tpl');
				}
			}
			echo '<form action="index.php?page=step3" method="POST"><input type="hidden" name="next">';
			if ($bad>0)
			{
				echo '<input type="button" class="fbutton" value="'.$lng['no_next_page'].'"><br>';
				echo '<a href="index.php?page=step4">'.$lng['click_next'].'</a>';
			}
			else
			{
				echo '<input type="submit" class="fbutton" value="'.$lng['next_page'].'">';
			}
			echo '</form>';
  		}
  		break;
		
	}
	case 'step4':
	{
		if (isset($_POST['mysql_host'],$_POST['database'],$_POST['mysql_prefix'],$_POST['mysql_user'],$_POST['mysql_pass']))
		{
			$open = @fopen('../config.php','a+');
			$config_file = @fread($open,@filesize('../config.php'));
			$config_file = str_replace('%host%',strip_tags($_POST['mysql_host']),$config_file);
			$config_file = str_replace('%user%',strip_tags($_POST['mysql_user']),$config_file);
			$config_file = str_replace('%pass%',strip_tags($_POST['mysql_pass']),$config_file);
			$config_file = str_replace('%dbname%',strip_tags($_POST['database']),$config_file);
			$config_file = str_replace('%dbprefix%',strip_tags($_POST['mysql_prefix']),$config_file);
			$connect = @mysql_connect($_POST['mysql_host'],$_POST['mysql_user'],$_POST['mysql_pass']) or message_die(GENERAL,'Could not connect to database server. Error with installation!',@mysql_error());
			@fwrite(@fopen('../config.php','w'),$config_file) or message_die(GENERAL,'Could not write config file. Error with installation!','');
			@mysql_query("SET NAMES 'utf8'",$connect);
			@mysql_select_db($_POST['database'],$connect) or message_die(GENERAL,'Could not connect to database. Error with installation!',@mysql_error());
			require('./../includes/sql_parse.php');
			$sql = @fread(@fopen('uf_tables.sql','r'),@filesize('uf_tables.sql'));
			$sql = preg_replace('/uf_/', $_POST['mysql_prefix'], $sql);
			$sql = remove_remarks($sql);
			$sql = split_sql_file($sql);
			for ($i=0;$i<count($sql);$i++)
			{
				DataBase::sql_query($sql[$i],'ERROR','Error of SQL Query: '.$sql[$i].' Error with install script!');
			}
			DataBase::sql_query("UPDATE `".$_POST['mysql_prefix']."config` SET `value`='".$_SERVER['REQUEST_TIME']."' WHERE `name`='date_install'",GENERAL,'Could not update date install information');
			$patch = substr('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],0 ,-strlen('install/index.php?page=step4'));
			DataBase::sql_query("UPDATE `".$_POST['mysql_prefix']."config` SET `value`='".$patch."' WHERE `name`='forumpatch'",GENERAL,'Could not update date install information');
			header('Location: index.php?page=step5');
			
			
		}
		else
		{
			if (!isset($_POST['database'])) { $_POST['database']=''; }	
			if (!isset($_POST['mysql_user'])) { $_POST['mysql_user']=''; }
			if (!isset($_POST['mysql_pass'])) { $_POST['mysql_pass']=''; }
		}
		echo $header;
		$skin = array(
		'L.mysql_host'=>$lng['mysql_host'],
		'L.mysql_user'=>$lng['mysql_user'],
		'L.database'=>$lng['db_name'],
		'L.mysql_pass'=>$lng['mysql_pass'],
		'L.mysql_prefix'=>$lng['mysql_prefix'],
		'L.submit'=>$lng['start_install'],
		);
		require('./tpls/install_step4.tpl');
		break;
	}
	case 'step5':
	{
		echo $header.'<tr><td class="fitem" align="center"><span class="fstandard">'.$lng['install_complete'].'</span></td></tr></table><meta http-equiv="refresh" content="1.5; url=index.php?page=step6"></body></html>';
		break;
	}
	case 'step6':
	{
   		DataBase::db_connect();
		if (isset($_POST['user_name'],$_POST['password'],$_POST['retype_password'],$_POST['email']))
   		{
       			if ($_POST['password']==$_POST['retype_password'])
       			{
	           		if (ereg("^.+@.+\..+$", $_POST['email']))
				{
          				$sql = "UPDATE `".USERS_TABLE."` SET
		  			`nick`='".htmlspecialchars($_POST['user_name'])."',
		  			`pass`='".md5(htmlspecialchars($_POST['password']))."',
		  			`email`='".htmlspecialchars($_POST['email'])."',
		  			`lang`= '".$_COOKIE['lang']."',
		  			`regdate`='".$_SERVER['REQUEST_TIME']."',
		  			`lastvisit`='".$_SERVER['REQUEST_TIME']."'
		  			WHERE `u_id`='1';";
		  			DataBase::sql_query($sql,CRITICAL,'Could not update user. Error with install script!');
		  			$sql = "UPDATE `".POSTS_TABLE."` SET `ptime`='".$_SERVER['REQUEST_TIME']."' WHERE `p_id`='1'";
		  			DataBase::sql_query($sql,CRITICAL,'Could not update post. Error with install script!');
		  			$sql = "UPDATE `".TOPICS_TABLE."` SET `lastpost_time`='".$_SERVER['REQUEST_TIME']."' WHERE `t_id`='1'";
		  			DataBase::sql_query($sql,CRITICAL,'Could not update topic. Error with install script!');
		  			header('Location: index.php?page=step7');
				}
				else
				{
		    			alert($lng['invalid_email']);
		  		}
			}
			else
			{
	    			alert($lng['invalid_passwords']);
			}
		}
		else
		{
    			if (!isset($_POST['user_name'])) { $_POST['user_name'] = ''; }
    			if (!isset($_POST['email'])) { $_POST['email'] = ''; }
			$_POST['password'] = '';
			$_POST['retype_password'] = '';
		}
		echo $header;
		$skin = array(
		'L.user_name'=>$lng['user_name'],
		'L.password'=>$lng['password'],
		'L.retype_password'=>$lng['retype_password'],
		'L.email'=>$lng['email'],
		'L.submit'=>$lng['save']
		);
		require('./tpls/install_step6.tpl');
		break;
	}
	case 'step7':
	{
		$open = @fopen('../config.php','a+');
		$config_file = @fread($open,@filesize('../config.php'));
		$config_file = str_replace('false','true',$config_file);
		@fwrite(@fopen('../config.php','w'),$config_file) or message_die(GENERAL,'Could not write config file. Error with installation!','');
		echo $header.'<tr><td class="fitem" align="center"><span class="fstandard">'.$lng['info_2'].'</span></td></tr></table><meta http-equiv="refresh" content="4; url=../index.php"></body></html>';
		break;
	}
}
?>