<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

//======================================================================
// MemHT Portal Updated
//======================================================================

error_reporting(E_ALL);

//===========================================
//Database: Connect
//===========================================
require_once("../inc/inc_config.php");
require_once("../inc/inc_database.php");

$dblink = new database();
$dblink->connect();

//===========================================
//Timezone setting
//===========================================
$timezonerow = @$dblink->get_row("SELECT timezone FROM memht_config");
$siteConfig['timezone'] = intval($timezonerow['timezone']);
$tzNOW = "DATE_ADD(NOW(),INTERVAL ".$siteConfig['timezone']." HOUR)";
	
@include_once("../inc/inc_functions.php");
@include_once("../inc/inc_login.php");
include_once("update/template.php");

global $userid;

function isAuthTemp() {
	global $dblink;
	
	return true;
}

if (isAuthTemp()) {
	$step = (isset($_POST['step'])) ? intval($_POST['step']) : 1 ;
	
	function license() {
		?>
		<form id='form' name='form' method='post' action='update.php'>
		<div class='title'>License</div>
		<div class='spacer'></div>
		<div><iframe src="license.html" frameborder="0" scrolling="auto" class="license"></iframe></div>
		<div class='spacer'></div>
		<div align="right"><input type='submit' name='Submit' value='Accept' class='button'></div>
		<input type='hidden' name='step' value='2'>
		</form>
		<?php
	}
	
	function select() {
		global $dblink;
		
		//MemHT Portal version
		if ($row_info = @$dblink->get_row("SELECT * FROM memht_info")) {
			$memhtversion = intval($row_info['version']);
		} else if (file_exists("../inc/inc_version.php")) {
			include("../inc/inc_version.php");
		} else {
			$memhtversion = 0;
		}
		
		//Installer version
		include("version.php");
		$versarr = array();
		for ($i=0;$i<3;$i++) { $versarr[$i] = substr($installerversion,$i,1); }
		$installerversion_txt = implode(".",$versarr);
		
		//Remote version
		$lrfile = @fopen("http://www.memht.com/update/lastrelease.txt", "r");
		if ($lrfile) {
			$remoteversion = fgets($lrfile, 1024);
		} else {
			$remoteversion = 0;
		}
		@fclose($lrfile);
		$versarr = array();
		for ($i=0;$i<3;$i++) { $versarr[$i] = substr($remoteversion,$i,1); }
		$remoteversion_txt = implode(".",$versarr);
		
		//Installer status
		if ($remoteversion>0 AND $remoteversion>$installerversion) {
			$installer = 2; //Installer old
		} else if ($remoteversion>0 AND $remoteversion<=$installerversion) {
			$installer = 0; //Installer ok
		} else {
			$installer = 1; //Installer status unknown
		}
		
		//Initialization
		$proceed = true;
		
		//MemHT Portal version detected
		if ($memhtversion>0) {
			$versarr = array();
			if ($memhtversion>=340) {
				for ($i=0;$i<3;$i++) { $versarr[$i] = substr($memhtversion,$i,1); }
			} else {
				for ($i=0;$i<2;$i++) { $versarr[$i] = substr($memhtversion,$i,1); }
			}
			$memhtversion_txt = implode(".",$versarr);
			echo "<div class='info' style='margin-bottom:4px;'>The updater has detected your MemHT Portal version: <b>$memhtversion_txt</b><br><span style='font-size:9px;'><i>If it's wrong, select it manually (It's indicated in the administration homepage)</i></span></div>\n";

			switch ($installer) {
				case 2: //Installer old
					echo "<div class='error' style='margin-bottom:4px;'><b>This updater seems to be old!</b><span style='font-size:9px;'><i><br>The last MemHT Portal version is <b>$remoteversion_txt</b>, but this script can update your portal up to the <b>$installerversion_txt</b> version only, check <a href='http://www.memht.com' target='_blank' title='MemHT Portal'><b>memht.com</b></a> for new releases</i></span></div>\n";
				break;
				case 1: //Installer status unknown
					echo "<div class='warning' style='margin-bottom:4px;'><b>Remote version unknown!</b><span style='font-size:9px;'><i><br>This script can update your portal up to the <b>$installerversion_txt</b> version, check <a href='http://www.memht.com' target='_blank' title='MemHT Portal'><b>memht.com</b></a>, there might be new releases</i></span></div>\n";
				break;
			}
			if ($memhtversion>=$installerversion) {
				$proceed = false;
				echo "<div class='error' style='margin-bottom:4px;'><b>This updater is older than your MemHT Portal!</b><br><span style='font-size:9px;'><i>Check <a href='http://www.memht.com' target='_blank' title='MemHT Portal'><b>memht.com</b></a> for new releases</i></span></div>\n";
			}	
		} else {
			//MemHT Portal version unknown
			echo "<div class='warning' style='margin-bottom:4px;'>The updater cannot detect your MemHT Portal version<br><span style='font-size:9px;'><i>You have to select it manually (It's indicated in the administration homepage)</i></span></div>\n";
		}
		
		//Update version selector
		echo "<form id='form' name='form' method='post' action='update.php'>\n";
		echo "Your version: <select name='from' class='button'>\n";
			echo "<option value=''>Select your version</option>\n";
			$selected = ($memhtversion==400) ? " selected" : "" ; echo "<option value='400'{$selected}>4.0.0</option>\n";
			$selected = ($memhtversion==401) ? " selected" : "" ; echo "<option value='401'{$selected}>4.0.1</option>\n";
			$selected = ($memhtversion==402) ? " selected" : "" ; echo "<option value='402'{$selected}>4.0.2</option>\n";
		echo "</select>\n";
		echo " <input type='submit' name='Submit' value='Update to $installerversion_txt' class='button' onClick='javascript:printAlert();'>\n";
		echo "<input type='hidden' name='installerversion' value='$installerversion'>\n";
		echo "<input type='hidden' name='step' value='3'>\n";
		echo "</form>\n";
	}
	
	function update() {
		global $dblink;
		
		@set_time_limit(120);
		
		$from = (isset($_POST['from'])) ? intval($_POST['from']) : 0 ;
		$installerversion = (isset($_POST['installerversion'])) ? intval($_POST['installerversion']) : 0 ;
		
		if ($from>0) {
			if ($installerversion>0) {
				if ($from<$installerversion) {
					//---------
					$versions = array(400,401);
					foreach ($versions as $version) {
						if ($from<=$version) {
							include("update/update_from_{$version}.php");
						}
					}
					/*if ($dblink->get_num_noerr("SELECT version FROM memht_info")>0) {
						$dblink->query("UPDATE memht_info SET version='$installerversion'");
					}*/
					?>
					<div class='spacer'></div>
					<div class='box'><b>Attention: Delete the installation folder and files!</b></div>
					<div class='spacer'></div>
					<div align="right">
						<input type='button' name='button' value='Site home' class='button' onClick="window.location='../index.php';"> <input type='button' name='button' value='Administration' class='button' onClick="window.location='../admin.php';">
					</div>
					<?php
					//---------
				} else {
					echo "<div class='error'><b>This updater is older than the selected MemHT Portal version!</b><br><span style='font-size:9px;'><i>Check <a href='http://www.memht.com' target='_blank' title='MemHT Portal'><b>memht.com</b></a> for new releases</i></span></div>\n";
				}
			} else {
				echo "<div class='error'><b>Version info error</b><br><span style='font-size:9px;'><i>Try to restart the updater. If the error continues, contact the MemHT Portal support</i></span></div>\n";
			}
		} else {
			echo "<div class='error'><b>MemHT Portal version not selected</b><br><span style='font-size:9px;'><i>You have to select your portal version to continue (It's indicated in the administration homepage)</i></span></div>\n";
		}
	}
	
	//-----
	
	template_header($step);

	switch($step) {
		default:
		case 1: license(); break;
		case 2: select(); break;
		case 3: update(); break;
		case 4: finish(); break;
	}

	template_footer();
	
} else {
	die("<b>Access Denied!</b><br>You must be logged as administrator to execute this file");
}

//===========================================
//Database: Disconnect
//===========================================
$dblink->disconnect();

?>

<script type="text/javascript">
	function printAlert() {
		alert("This process needs some time to finish, do not stop the script execution");
	}
</script>