<?php
/*
 * ##################################################################
 * Created on: 10-Apr-06
 * File name:  auto-upgrade.php 
 * ##################################################################
 * This code is a sole property of netenberg.com 
 * ##################################################################
 */
require_once realpath(dirname(__FILE__)) . "/" . "init.php";
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
	echo "This feature is not supported in Windows ";
	exit;
}
for ($i= 1; $i < $argc; $i++)
{
	parse_str($argv[$i]);
}
$arguments  = compact('encode', 'force');
$force  = (isset ($arguments['force']) && trim($arguments['force']) == 'uncheck') ? 1 : 0;
$uname  = `uname`;
$FREEBSD= eregi("freebsd", strtolower($uname)) ? 1 : 0;
$BASE   = realpath(dirname(__FILE__)) . "/";
$PARENT = realpath(dirname(__FILE__) . "/../") . "/";
$licensing_server_array     = array();
$licensing_server_array[0]  = "netenberg.com";
require_once dirname(__FILE__) . "/system/config/licensing_servers.php";
require_once dirname(__FILE__) . "/system/config/version.php";
$current_version    = $ALPversion;
$latest_version     = getLatestVersionNo("http://" . $licensing_server_array[0] . "/files/accountlab_plus/free/version.txt");
$current_version_array  = splitVersionNo($current_version);
$latest_version_array   = splitVersionNo($latest_version);
$file_name  = (isset ($arguments['encode']) && strtolower($arguments['encode']) == "ioncube") ? "accountlab_plus_IonCube.tgz" : "accountlab_plus_Zend.tgz";
$tgz_file   = "http://files.betaservant.com/files/free/" . $file_name;
$cmds           = array ();
$cmds['mkdir']  = "";
$cmds['tar']    = "";
$cmds['cp']     = "";
$cmds['rm']     = "";
$cmds['mysqldump']= "";
if (!$force)
{
	if (!checkCmds($cmds))
		exit;
}
if ($current_version != $latest_version && ($latest_version_array[0] > $current_version_array[0] || ($latest_version_array[0] == $current_version_array[0] && $latest_version_array[1] > $current_version_array[1])))
{
	echo "Starting AccountLab plus automatic upgrade utility \n\r";
	echo "====================================================\n\r";
	echo "Current installation is version : " . $current_version . "\n\rLastest version is : " . $latest_version . "\n\r";
	echo "Downloading latest accountlab plus files ...\n\r";
	$download_complete= download($tgz_file, $PARENT . $file_name);
	if (!is_readable($PARENT . $file_name))
	{
		echo "Error: The downloaded file is not readable!\n\r";
		exit;
	}
	if ($download_complete)
	{
		echo "File saved as: ";
		echo $PARENT . $file_name . "\n\r";
		require_once $BASE . "elements/default/sysvar/db.php";
		$commands1[]= "echo \"Extracting new AccountLab Plus files ... \"";
		$commands1[]= "mkdir -p " . $PARENT . "alpnew_temporary/";
		$commands1[]= "tar -xzpf " . $PARENT . $file_name . " -C " . $PARENT . "alpnew_temporary/";
		$commands1[]= "echo \"Creating backup archive of existing installation ...\"";
		$commands1[]= "echo \"Dumping mysql database...\"";
		$commands1[]= "mysqldump -Q --add-drop-table -c -u " . $db_user . " -p" . $db_pass . " -h " . $db_host . " " . $db_name . " > " . $db_name . time() . ".sql";
		$commands1[]= "echo \"Creating backup...\"";
		$commands1[]= "mkdir -p " . $PARENT . "alpbkp_temporary/";
		$commands1[]= (($FREEBSD) ? "cp -rfp " : "cp -rfpd ") . $BASE . "* " . $PARENT . "alpbkp_temporary/";
		$commands1[]= "tar -czpf " . $PARENT . "accountlab_plus_" . str_replace(" ", "_", $current_version) . "_" . time() . "_bkp.tgz " . $PARENT . "alpbkp_temporary/";
		$commands2[]= "echo \"Restoring db.php ...\"";
		$commands2[]= "cp " . $PARENT . "alpbkp_temporary/elements/default/sysvar/db.php " . $BASE . "elements/default/sysvar/";
		$commands2[]= "chmod 644 " . $BASE . "elements/default/sysvar/db.php";
		$commands1[]= "echo \"Coping new AccountLab Plus files to install directory ...\"";
		$commands1[]= (($FREEBSD) ? "cp -rfp " : "cp -rfpd ") . $PARENT . "alpnew_temporary/accountlab_plus/* " . $BASE;
		$commands3[]= "echo \"Removing temporary files ...\"";
		$commands3[]= "rm -rf " . $PARENT . "alpbkp_temporary/";
		$commands3[]= "rm -rf " . $PARENT . "alpnew_temporary/";
		$commands3[]= "rm " . $PARENT . $file_name;
		$commands3[]= "rm " . $BASE . "/install.php";
		echo "Running unix commands...\n\r";
		echo "====================================================\n\r";
		foreach ($commands1 as $c1)
		{
			echo `$c1`;
		}
		foreach ($commands2 as $c2)
		{
			echo `$c2`;
		}
		echo "====================================================\n\r";
		echo "New files for AccountLab Plus " . $latest_version . " has been copied.\n\r\n\r";
		foreach ($commands3 as $c3)
		{
			echo `$c3`;
		}
		echo "Initiating Upgrade process...\n\r";
        error_reporting(0);
        require_once realpath(dirname(__FILE__)) . "/" . "init.php";
        $just_upgraded = $BL->installer->checkandupgrade();
        echo "\n\r#################################################################################\n\r";
        if($just_upgraded=="OK")
        {
    		echo "Database Upgrade Complete.";
        }
        elseif($just_upgraded=="NO_CONNECT")
        {
        	echo "Please access your AccountLab Plus admin backend from a browser, to complete the upgrade process.";
        }
        else
        {
        	echo "Database Upgrade Failed.";
        }
        echo "\n\r#################################################################################\n\r";
		exit;
	}
}
function splitVersionNo($version_no)
{
	if (ereg("r", $version_no))
	{
		$temp= explode("r", $version_no, 2);
		$version[0]= trim($temp[0]);
		$version[1]= trim($temp[1]);
	}
	else
	{
		$version[0]= trim($version_no);
		$version[1]= 0;
	}
	return $version;
}
function checkCmds($commands)
{
	$return= true;
	$msg= "\n\rFollowing shell commands are not installed, or may not have permissions to execute.\n\r";
	foreach ($commands as $k => $v)
	{
		$command_file= "";
		$t= "whereis " . $k;
		$str= `$t`;
		$str= trim($str);
		$p1= explode(" ", $str);
		foreach ($p1 as $p2)
		{
			$p2= trim($p2);
			$p3= explode("/", $p2);
			$c= count($p3);
			$p4= $p3[($c -1)];
			if (trim($p4) == $k)
			{
				$command_file= $p2;
			}
		}
		if (!is_executable($command_file) && !is_executable($k))
		{
			$msg .= "Command: '" . $k . "' is not executable\n\r";
			$return= false;
		}
	}
	if (!$return)
	{
		echo $msg . "\n\r";
		exit;
	}
	return $return;
}
function checkZend($file)
{
	$fp= fopen($file, "ab+");
	$str= substr(fgets($fp, 5), 0, 4);
	fclose($fp);
	return ($str == "Zend") ? true : false;
}
function getLatestVersionNo($filename)
{
	$fd= fopen($filename, "r");
	if (!$fd)
	{
		echo "Error: Can not open a connection to AccountLab Plus official server!\n\r";
		exit;
	}
	$content= fread($fd, 1024);
	fclose($fd);
	if (empty ($content))
	{
		echo "Error: Latest version number can not be resolved!\n\r";
		exit;
	}
	return $content;
}
function download($file_source, $file_target)
{
	$rh= fopen($file_source, 'rb');
	$wh= fopen($file_target, 'wb');
	if ($rh === false || $wh === false)
	{
		if ($rh === false)
			echo "\n\rDownload error: Cannot read from file (" . $file_source . ")\n\r";
		else
			echo "\n\rDownload error: Cannot write to file (" . $file_target . ")\n\r";
		return false;
	}
	$i= 0;
	$j= array (
		"|",
		"/",
		"-",
		"\\"
	);
	while (!feof($rh))
	{
		if (fwrite($wh, fread($rh, 1024)) === FALSE)
		{
			echo "\n\rDownload error: Cannot write to file (" . $file_target . ")\n\r";
			return false;
		}
		$i++;
		$l= $i -4 * floor($i / 4);
		echo "( " . $j[$l] . " )" . $i . " Kilo bytes downloaded so far" . "\r";
	}
	fclose($rh);
	fclose($wh);
	echo "\n\rDownload complete. \n\r";
	return true;
}
exit;
?>