<?php
/*
 * Version: 1.0.25
 * Date: 26.02.2012 18:41:57
 * Autors: xLiveCZ team
 * Autori se zrikaji jakekoliv odpovednosti za pripadne skody zpusobene pouzivanim tohoto scriptu
 *
 */

define('HOST', 'http://'.$_SERVER['HTTP_HOST'].'/');                                 // http://myxtreamer/
define('INSTALLPHP_URL', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);      // http://myxtreamer/media/sda1/scripts/xLiveCZ/install/preinstall.php
define('INSTALLPHP_DIR', dirname(__FILE__).'/');                                     // /tmp/usbmounts/sda1/scripts/xLiveCZ/install/
define('INSTALL_URL_DIR', str_replace($_SERVER['HTTP_HOST'], 'localhost', current(explode('install/', INSTALLPHP_URL)).'install/'));  // http://localhost/media/sda1/scripts/xLiveCZ/install/
define('PROJECT_URL_DIR', str_replace($_SERVER['HTTP_HOST'], 'localhost', current(explode('install/', INSTALLPHP_URL)).''));  // http://localhost/media/sda1/scripts/~jmeno projektu~/

define('SCRIPTS_DIR',current(explode('scripts/', __FILE__)).'scripts/');             // /tmp/usbmounts/sda1/scripts/
define('XTREAMERING_INDEX', SCRIPTS_DIR.'Xtreamering/index.php');                    // /tmp/usbmounts/sda1/scripts/Xtreamering/index.php
define('XTREAMERING_DIR', SCRIPTS_DIR.'Xtreamering/');                               // /tmp/usbmounts/sda1/scripts/Xtreamering/
define('XTREAMERING_V_INFO', 'http://download.xtreamer.net/Firmware/APP/verInfo/Xtreamering.xml');

define('PROJECT_DIR', current(explode('install/',__FILE__)));                        // /tmp/usbmounts/sda1/scripts/xLiveCZ/
define('PROJECT_NAME', str_replace('/', '', next(explode('scripts/',PROJECT_DIR)))); // xLiveCZ

if (file_exists(XTREAMERING_INDEX))
	$xtreamering_instaled_v = @simplexml_load_file(XTREAMERING_DIR.'version.xml');
$xtreamering_avalable_v = @simplexml_load_file(XTREAMERING_V_INFO);
$xtreamering_index = @file_get_contents(XTREAMERING_INDEX);

$action      = "";
$disabled    = "";
$action_html = "";

if (isset($_POST["action"])) $action = $_POST["action"];

switch ($action) {
	case 'inject':
		if (!strpos($xtreamering_index, PROJECT_NAME)) {
			// stahnout obrazky do xtreameringu
			if (!file_exists(XTREAMERING_DIR."image/".PROJECT_NAME.".jpg"))            $cmd  = "wget 'http://xtr-updats.wencas.cz/images/".PROJECT_NAME.".jpg' -O '".XTREAMERING_DIR."image/".PROJECT_NAME.".jpg'\n";
			if (!file_exists(XTREAMERING_DIR."image/updateBarCZ.png"))        $cmd .= "wget 'http://xtr-updats.wencas.cz/images/updateBarCZ.png' -O '".XTREAMERING_DIR."image/updateBarCZ.png'\n";
			if (!file_exists(XTREAMERING_DIR."image/".PROJECT_NAME."_menu_big.png"))   $cmd .= "wget 'http://xtr-updats.wencas.cz/images/".PROJECT_NAME."_menu_big.png' -O '".XTREAMERING_DIR."image/".PROJECT_NAME."_menu_big.png'\n";
			if (!file_exists(XTREAMERING_DIR."image/".PROJECT_NAME."_menu_small.png")) $cmd .= "wget 'http://xtr-updats.wencas.cz/images/".PROJECT_NAME."_menu_small.png' -O '".XTREAMERING_DIR."image/".PROJECT_NAME."_menu_small.png'\n";
			shell_exec($cmd);

			if (EditIndexXtreamering($xtreamering_index)) {
				$msg_text = "Byla úspěšně přidána podpora pro ".PROJECT_NAME.".<br>Ověř funkčnost Xtreameringu, ".PROJECT_NAME." doinstaluješ tlačítkem 1 DO z hlavního menu Xtreameringu.<br><br>".PROJECT_NAME." team přeje příjemnou zábavu.";
			} else {
				$msg_err = "SORRY!<br>Soubor Xtreamering/index.php pravděpodobně nebyl modifikovaný!!!<br><br>Skus celý postup opakovat.";
			}
		} else {
			$msg_err = "Pravděpodobně se pokoušíš obnovit stránku, ačkoli jsi k tomu nebyl vyzván!<br><br>Modifikace souboru Xtreamering/index.php se jeví jako úspěšně dokončená!";
		}

		$disabled = "disabled";
		$action_html = "";

		echo HTML($msg_text, $msg_err, $disabled, $action_html, $xtreamering_instaled_v->version, $xtreamering_avalable_v->version, $xtreamering_index);
	break;

	case 'check':
		$action_html = "inject";
		$msg_text = "";

		if (file_exists(XTREAMERING_INDEX)) {
			$file_ind = pathinfo(XTREAMERING_INDEX);
			$index_backup = $file_ind['filename']."_backup_".@mt_rand().".php";
// 			if (file_exists(XTREAMERING_DIR.$index_backup)) {
// 				@unlink(XTREAMERING_DIR.$index_backup);
// 			}
			@copy(XTREAMERING_INDEX, XTREAMERING_DIR.$index_backup);
		}

		if (!file_exists(XTREAMERING_DIR.$index_backup))
			$msg_err .= "Soubor Xtreamering/index.php nebyl zálohovaný!!!<br>Zálohu potřebuješ v případě že jsi měl nějaké své vlastní úpravy v Xtreameringovym index.php.<br>Můžeš si soubor zálohovat sám, pokud ho potřebuješ. V opačném případě můžeš pokračovat kliknutím na tlačítko \"Next\".<br /><br />";

		$msg_text .= "Xtreamering je nainstalován v aktuální verzi a v dalším kroku bude jeho index.php upraven pro ".PROJECT_NAME.".<br><br>Pokračuj kliknutím na tlačítko \"Next\".";

		echo HTML($msg_text, $msg_err, $disabled, $action_html, $xtreamering_instaled_v->version, $xtreamering_avalable_v->version, $xtreamering_index);
	break;

	default:
		$action_html = "check";
		if (strpos($xtreamering_index, PROJECT_NAME)) {
			$msg_err = "";
			$msg_text = "Soubor Xtreamering/index.php už má přidanou podporu pro ".PROJECT_NAME.".";
			$disabled = "disabled";
			$action_html   = "check";
		}
		if ($disabled != "disabled"){
			if (file_exists(XTREAMERING_INDEX)) {
				$msg_text = "Soubor Xtreamering/index.php bude nyní zálohován.<br />Pro přidání ".PROJECT_NAME." do Xtreameringu pokračuj kliknutím na tlačítko \"Next\".";
			} else {
				$msg_text = "Xtreamering není ještě nainstalován? Nainstaluj nejdříve dálkovým ovladačem Xtreamering, potom obnov tuto stránku klávesou \"F5\".";
				$disabled = "disabled";
				$action_html = "default";
			}
		}
		echo HTML($msg_text, $msg_err, $disabled, $action_html, $xtreamering_instaled_v->version, $xtreamering_avalable_v->version, $xtreamering_index);
	break;
}


function EditIndexXtreamering($line) {
	/* prida vazbu na <update".PROJECT_NAME."> pro aktualizaci projektu */
	$includestring = "
			if(itemfile == \"".strtolower(PROJECT_NAME)."\"){
				fileName = \"".INSTALLPHP_DIR."install.php\";
				if(CheckFile(fileName) == \"true\"){
					url = \"".INSTALL_URL_DIR."install.php\";
					jumpToLink(\"destination\");
				}
			} else";
	$find  = "if ( userInput == \"1\" )";
	$S     = strpos($line, $find, $S) + strlen($find);
	$find  = "{";
	$S     = strpos($line, $find, $S) + strlen($find);
	$partA = substr($line, 0, $S);
	$partB = substr($line, $S);
	$line  = $partA.$includestring."			".$partB;


	/* prida polozku pro project na prvni pozici pod </itemSize>*/
	$includestring = "\n
	<item>
		<title>".PROJECT_NAME."</title>
		<bg>".XTREAMERING_DIR."image/".PROJECT_NAME.".jpg</bg>
		<file>".strtolower(PROJECT_NAME)."</file>
		<itemid>0</itemid>
		<onClick>
			<script>
				SwitchViewer(7);
				fileName = \"".PROJECT_DIR."category/czech.php\";
				if(CheckFile(fileName) == \"true\"){
					url = \"".PROJECT_URL_DIR."category/czech.php\";
					jumpToLink(\"destination\");
				}else{
					url = \"".INSTALL_URL_DIR."install.php\";
					jumpToLink(\"destination\");
				}
			</script>
		</onClick>
	</item>";
	$find  = "</itemSize>";
	$S     = strpos($line, $find, $S) + strlen($find);
	$partA = substr($line, 0, $S);
	$find  = "<title>".PROJECT_NAME."";
	if (strpos($line, $find, $S)) {
		$find  = "</item>";
		$S     = strpos($line, $find, $S) + strlen($find);
	}
	$partB = substr($line, $S);
	$line  = $partA.$includestring.$partB;

	/* zajisti nacitani ID polozek do <itemid> </itemid> */
	$Item_s = 0;
	$Item_e = 0;
	$i      = 0;
	$find   = "<item>";
	$find1  = "<itemid>";
	$find2  = "</itemid>";
	while (strpos($line, $find, $Item_e) > $Item_s) {
		$Item_s = strpos($line, $find, $Item_s) + strlen($find);
		$Item_s = strpos($line, $find1, $Item_s) + strlen($find1);
		$Item_e = strpos($line, $find2, $Item_s);
		$partA  = substr($line, 0, $Item_s);
		$partB  = substr($line, $Item_e);
		$line   = $partA.$i.$partB;
		$i++;
	}

	/* prida +1 v <mediaDisplay do rowCount="xx" pro pridanou polozku projektu */
	$find  = "rowCount=\"";
	$S_num = strpos($line, $find) + strlen($find);
	$partA = substr($line, 0, $S_num);
	$find  = "\"";
	$E_num = strpos($line, $find, $S_num);
	$numi_ = substr($line, $S_num, $E_num)+1; //zbytecne, skutecny pocet polozek je vlozen v $i
	$partB = substr($line, $E_num);
	$line  = $partA.$i.$partB;

	/* prida +1 do <itemSize> pro pridanou polozku projektu */
	$find  = "<itemSize>";
	$S_num = strpos($line, $find, $S_num) + strlen($find);
	$find  = "<script>";
	$S_num     = strpos($line, $find, $S_num) + strlen($find);
	$partA = substr($line, 0, $S_num);
	$find  = ";";
	$E_num     = strpos($line, $find, $S_num);
	$numid = substr($line, $S_num, $E_num)+1; //zbytecne, skutecny pocet polozek je vlozen v $i
	$partB = substr($line, $E_num);
	$line  = $partA."\n        ".$i.$partB;

	if (file_put_contents(XTREAMERING_INDEX, $line))
		return true;
	else
		return false;
}

function HTML($msg_text='', $msg_err='', $disabled='', $action_html='', $cur,$ava, $xtreamering_index) {
$out = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Preinstall</title>
		<style type="text/css">
		body {
			marginheight: 0px;
			marginwidth: 0px;
			leftmargin: 0px;
			topmargin: 0px;
			bgcolor: #012436;
		}
		table {
			cellspacing: 0px;
			cellpadding: 0px;
			border: 0px;
		}

		input[type="submit"] {
			width: 80px;
			font-size: 12px;
			font-weight: bold;
			border: solid 1px #012436;
			'.($disabled == "disabled" ? "color: #B3B3B3;
			background-color: #656565;" : "cursor: Pointer;
			color: #A7CEFF;
			background-image: url('".HOST."dlf/button_bg.jpg');").'
		}
		input[type="submit"]:hover { '.($disabled == "disabled" ? "color: #B3B3B3; background-color: #656565;" : "color: #D2E7FF; background-image: url('".HOST."dlf/button_bg.jpg');").' }
		</style>

		<link href="'.HOST.'dlf/styles.css" rel="stylesheet" type="text/css">
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
	</head>
	<body oncontextmenu=\'return false;\' onload=\'document.forms.preinstall.submit.focus()\'>
		<center>
			<table height="500" width="996">
				<tr>
					<td width="300">&nbsp;</td>
					<td width="620">&nbsp;<font face="arial" color="#FFFFFF" size="2"><b>V následujících krocích budou použita post data, neobnovuj stránku pokud k tomu nebudeš vyzván!!!<br><span style="color: #ED1C24; font-weight: bold;">Mohl by si tím přijít o automaticky vytvářené zálohy souborů!!!</span><br><br>Čti pozorně pokyny!!!</b></font></td>
					<td height="100" align="right" valign="bottom">
						<Img src="'.HOST.'dlf/mvix_logo.png" width="300" height="72" alt="logo"></td>
				</tr>
				<tr>
					<td width="350">&nbsp;</td>
					<td width="620" valign="top">
						<table width="540" height="100">
							<tr>
								<td><font face="arial" color="#FFFFFF" size="2"><b>Naistalovaná verze Xtreamering: '.(strpos($cur, "reinstall") ? "Připraveno k reinstalaci" : $cur).(!strpos($cur, "reinstall") && ((strpos($xtreamering_index, "updateCZ") || strpos($xtreamering_index, "upcz")) || (strpos($xtreamering_index, PROJECT_NAME))) ? " (editovaná)" : "").(!file_exists(XTREAMERING_INDEX) ? " Nenainstalováno" : "").'</b></font>
										<br><font face="arial" color="#FFFFFF" size="2"><b>Dostupná verze Xtreamering: '.$ava.'</b></font>
								</td>
							</tr>
						</table>
						<form name="preinstall" action="'.INSTALLPHP_URL.'" method="post">
						<table width=540 height="100">
							<tr>
								<td height="250">
									<span style="color: #FF0000; font-weight: bold;">'.$msg_err.'</span><br><span style="color: #FFFFFF; font-weight: bold;">'.$msg_text.'</span>
								</td>
							</tr>
							<tr>
								<td align="right">
									<input type="hidden" name="action" value="'.$action_html.'">
									<input type="submit" class="btn_2" name="submit" value="Next" '.$disabled.'>
								</td>
							</tr>
						</table>
						</form>
					</td>
					<td width="337" align="right" valign="middle">
						<img src="'.HOST.'dlf/pvr_img.png" width="337" height="250" alt="xtr"></td>
				</tr>
			</table>
			<table width="700">
				<tr><td height="4"></td>
				</tr>
				<tr>
					<td align="right" valign="top" style="border-top:solid 1px; border-top-color:#FFFFFF">
						<table width="900">
							<tr>
								<td width="20"></td>
								<td width="440" valign="middle"><a href="http://www.wencas.cz/xtreamer/79" target="_blank">Popis předinstalace '.PROJECT_NAME.'</a>
								</td>
								<td align=right>
									<img src="'.HOST.'dlf/footer.png" width="175" height="51" alt="footer">
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</center>
	</body>
</html>';
return $out;
}

?>