<?php
/*###########################################################################*
 # Updater xLive CZ Copyright 2010, 2011 (c) wencaS                          #
 # Verze: 2.0 pro xtreamer                                                   #
 # Autor: wencaS                                                             #
 # Homepage: http://www.wencas.cz                                            #
 # Email: wenca.S@seznam.cz                                                  #
 # Licence: GNU                                                              #
 # Datum: 4.5. 2011                                                          #
 # ************************************************************************* #
 #                                                                           #
 # Tento program můžete šířit a/nebo upravovat podle podmínek                #
 # GNU General Public License, vydávané Free Software Foundation;            #
 # buď verze 2 této licence anebo (podle vašeho uvážení) kterékoli pozdější  #
 # verze.                                                                    #
 # Tento program je rozšiřován v naději, že bude užitečný, ale               #
 # BEZ JAKÉKOLIV ZÁRUKY, dokonce ani předpokládané                           #
 # Podívejte se na GNU General Public License pro další podrobnosti.         #
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA #
 # ------------------------------------------------------------------------- #
 # PROTOŽE JE LICENCE K POUŽÍVÁNÍ TOHOTO SOFTWARU ZDARMA, NEJSOU POSKYTOVÁNY #
 # ŽÁDNÉ ZÁRUKY. DRŽITELÉ AUTORSKÝCH PRÁV A/NEBO DALŠÍ STRANY POSKYTUJÍ      #
 # TENTO SOFTWARE "JAK JE" ("JAK STOJÍ A LEŽÍ"), BEZ ZÁRUK JAKÉHOKOLIV DRUHU,#
 # VČETNĚ ZÁRUK PRODEJNOSTI ČI POUŽITELNOSTI PRO JISTÝ KONKRÉTNÍ ÚČEL.       #
 # VEŠKERÝ RISK TÝKAJÍCÍ SE KVALITY ČI FUNKČNOSTI TOHOTO SOFTWARU JE NA VÁS. #
 # POKUD BY SE UKÁZALO, ŽE JE TENTO SOFTWARE VADNÝ, PŘEBÍRÁTE VEŠKEROU       #
 # ODPOVĚDNOST ZA VŠECHNY ŠKODY A S NIMI SPOJENÝ SERVIS.                     #
 #                                                                           #
 # V ŽÁDNÉM PŘÍPADĚ NEBUDOU DRŽITELÉ AUTORSKÝCH PRÁV A DALŠÍ STRANY          #
 # POSKYTUJÍCÍ TENTO SOFTWARE, JAK JE POVOLENO VÝŠE, ZODPOVĚDNÉ ZA VZNIKLÉ   #
 # ŠKODY, VČETNĚ OBYČEJNÝCH, SPECIELNÍCH, NÁHODNÝCH NEBO NÁSLEDNÝCH ŠKOD     #
 # VZNIKLÝCH POUŽITÍM NEBO NEMOŽNOSTÍ POUŽÍT TENTO SOFTWARE (VČETNĚ ZTRÁTY   #
 # DAT NEBO JEJICH NEADEKVÁTNÍHO POUŽITÍ NEBO ZTRÁTY ZPŮSOBENÉ VÁMI NEBO     #
 # TŘETÍ STRANOU NEBO CHYBOU TOHOTO SOFTWARU PŘI KOOPERACI S JINÝMI          #
 # PROGRAMY), A TO DOKONCE I TEHDY, POKUD DRUHÁ STRANA UPOZORNILA NA MOŽNOST #
 # TAKOVÝCH ŠKOD.                                                            #
 # ************************************************************************* #
 #
 # Changelog:
 # V 2.0
 #    + Oddělení cz xLive do extra složky xLiveCZ
 #    + Modifikace Xtreamering/index.php s vytvořením zálohy index_backup.php
 #
 # V 1.18 (nezveřejněno)
 #    + Možnost přeinstalace
 #    + Podpora xLive v. 10
 #
 # V 1.171
 #    + Instalace adult cs
 #    + Instalace archívu novy
 #    + Upraven vzhled instalace
 #
 # V 1.16
 #    - Oprava aktualizace Xtreamer CZ xLive Installer Updater
 #    + Každé spuštění přepíše existující log
 #
 # V 1.15
 #    - Odstraněno info a datumu spuštění z logu, kvůli chybě
 #      system's timezone settings
 #
 # V 1.14
 #    + Automatický live update z webu
 #
 # V 1.1
 #    Upraven odkaz na návod ruční instalace od killerman
 #
 # V 1.0
 #    První vydání
 *###########################################################################*/

$U_NAME = "MyxLive";
$U_VERSION = "2.0";
$U_AUTOR = "@ westerim";


$U_SCRIPT = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER["PHP_SELF"]); // SCRIPT_NAME
define("XLIVE", "/tmp/usbmounts/sda1/scripts/");
define("XTREAMERING", "http://".$_SERVER['HTTP_HOST']."/media/sda1/scripts/Xtreamering/index.php");

$downloadpath   = "/usr/down";
$extractpath    = "/usr/down/extract";
$installpath_SH = "/sbin/www";
$czechRSS       = XLIVE."MyxLive/category/czech.rss";
$archivyRSS     = XLIVE."MyxLive/category/archivy.rss";
$IndexXtremering= XLIVE."Xtreamering/index.php";

$SH_file        = "nova.sh";
$RT_file        = "support";
$SH_file_path   = $installpath_SH."/".$SH_file;
$xL_file_path   = XLIVE."MyxLive/version.txt";
$Update_URL     = "http://dl.dropbox.com/u/39481567/MyxLive/onemli/";
$xLive_URL      = $Update_URL."MyxLive_";
$xL_version     = $Update_URL."myxlive.txt";
$SH_version     = $Update_URL."versionsh.txt";
$RT_URL         = $Update_URL;
$installfile_RT = "/usr/bin/rtmpdump";

$install_emulat = "/sbin/www/incl";
$installpath_XX = "/sbin/www/incl/adultcsfunc";
$XXX_file_path  = XLIVE."MyxLive/category/adult_cs_php/version.txt";
$XXX_version    = "http://xtreamer.wencas.cz/updats/xxx/version.txt"; // "http://xtreamer.wencas.cz/updats/version.php";
$XX_download    = "http://xtreamer.wencas.cz/updats/xxx/adult_csCZ_";

$installnova    = "/sbin/www/incl/nova";
$nova_ver_curr  = XLIVE."MyxLive/category/nova/version.txt";
$nova_ver_avai  = "http://xtreamer.wencas.cz/updats/nova/version.txt"; // "http://xtreamer.wencas.cz/updats/version.php";
$nova_download  = "http://xtreamer.wencas.cz/updats/nova/novaCZ_";
$UP_version     = "http://xtreamer.wencas.cz/updats/updater/version.txt"; // "http://xtreamer.wencas.cz/updats/version.php";
$UP_download    = "http://xtreamer.wencas.cz/updats/updater/updater_";
$backup         = "";
$xxx_av         = "";
$xxx            = false;

$bull           = "&#8226;&#160;&#160;";
$res            = "";


if (!Exist_Fil_Dir(XLIVE."MyxLive")) {
	Create_Dir(XLIVE."MyxLive/install");
	Create_Dir(XLIVE."MyxLive/image");
	$cmd = "cp -f '".XLIVE."xLive/install/install.php' '".XLIVE."MyxLive/install/install.php'";
	Execute($cmd);
}
if (!Exist_Fil_Dir(XLIVE."Xtreamering/image/MyxLive.jpg")) DownloadFiles(XLIVE."Xtreamering/image/MyxLive.jpg", "http://www.westerim.v2hd.com/update/MyxLive.jpg");
if (!Exist_Fil_Dir(XLIVE."Xtreamering/image/updateBar.png")) DownloadFiles(XLIVE."Xtreamering/image/updateBar.png", "http://www.westerim.v2hd.com/update/updateBar.png");
if (!Exist_Fil_Dir(XLIVE."Xtreamering/image/MyxLive_menu_big.png")) DownloadFiles(XLIVE."Xtreamering/image/MyxLive_menu_big.png", "http://www.westerim.v2hd.com/update/MyxLive_menu_big.png");
if (!Exist_Fil_Dir(XLIVE."Xtreamering/image/MyxLive_menu_small.png")) DownloadFiles(XLIVE."Xtreamering/image/MyxLive_menu_small.png", "http://www.westerim.v2hd.com/update/MyxLive_menu_small.png");
EditIndexXtremering($IndexXtremering);


if (!Exist_Fil_Dir($downloadpath))   Create_Dir($downloadpath);
if (!Exist_Fil_Dir(XLIVE."MyxLive/themes/osd/myxlive-update.png")) DownloadFiles(XLIVE."MyxLive/themes/osd/myxlive-update.png", "http://www.westerim.v2hd.com/update/myxlive-update.png");
if (Exist_Fil_Dir("/sbin/www/adultcsfunc")) DelFiles("/sbin/www/adultcsfunc");

$action = $_GET["action"];
switch ($action) {
	case 'reinstall':
		if (Exist_Fil_Dir($xL_file_path))   DelFiles($xL_file_path);
		if (Exist_Fil_Dir($nova_ver_curr))  DelFiles($nova_ver_curr);
		if (Exist_Fil_Dir($SH_file_path))   DelFiles($SH_file_path);
		if (Exist_Fil_Dir($installfile_RT)) DelFiles($installfile_RT);
		if (Exist_Fil_Dir($XXX_file_path))  DelFiles($XXX_file_path);
		if (Exist_Fil_Dir(XLIVE."MyxLive/category/adult_cs_no.txt")) DelFiles(XLIVE."MyxLive/category/adult_cs_no.txt");

		$redirecturl = $U_SCRIPT."/install.php?header=no";
		header('Location: '.$redirecturl);
		die();
		break;

	case 'install':
	  $add = ">>";
	  $msg = "\n----------- Installation Message -----------\n";
	  if ($_GET["xL"] == "1") {
			DownloadFiles($downloadpath."/xLive.zip", $xLive_URL.$_GET["xlV"].".zip");
			if (Exist_Fil_Dir($downloadpath."/xLive.zip")) {
				DelFiles($xL_file_path);
				ExtractFiles($downloadpath."/xLive.zip", XLIVE);
				if (Exist_Fil_Dir($XXX_file_path)) EditczechRSS($czechRSS);
				if (Exist_Fil_Dir($nova_ver_curr)) EditarchivyRSS($archivyRSS);
				$msg .= "Download successfull: MyxLive_".$_GET["xlV"].".zip\n";
			} else {
				$msg .= "Download Error: MyxLive_".$_GET["xlV"].".zip\n";
			}
			if (Current_($xL_file_path) == $_GET["xlV"]) {
				$msg .= "Unpacking successfull: MyxLive_".$_GET["xlV"].".zip\n";
			} else {
				$msg .= "Error unpacking: MyxLive_".$_GET["xlV"].".zip\n";
			}
			DelFiles($downloadpath."/xLive.zip");
			if (!Exist_Fil_Dir($downloadpath."/xLive.zip")) {
				$msg .= "Deleted file successfull: xLive.zip\n\n";
			} else {
				$msg .= "Error deleting file: xLive_".$_GET["xlV"].".zip\n\n";
			}
		}

	  if ($_GET["nova"] == "1") {
			DownloadFiles ($downloadpath."/nova.zip", $nova_download.$_GET["novaV"].".zip");
			if (!Exist_Fil_Dir($installnova)) Create_Dir($installnova);
			if (Exist_Fil_Dir ($downloadpath."/nova.zip")) {
				DelFiles ($nova_ver_curr);
				ExtractFiles ($downloadpath."/nova.zip", XLIVE."MyxLive/");
				EditarchivyRSS($archivyRSS);
				$cmd  = "mv -f '".XLIVE."MyxLive/category/nova/func.php' '".$installnova."/func.php'\n";
				$cmd .= "mv -f '".XLIVE."MyxLive/category/nova/browseremulator.class.php' '".$install_emulat."/browseremulator.class.php'\n";
				Execute($cmd);
				$msg .= "OK Staženo nova_".$_GET["novaV"].".zip\n";
				$txtv = "Version ".$_GET["novaV"].",";
				CreateLogFile($txtv, $nova_ver_curr, ">");
			} else {
				$msg .= "Chyba stažení nova_".$_GET["novaV"].".zip\n";
			}
			if (Current_ ($nova_ver_curr) == $_GET["novaV"]) {
				$msg .= "OK Rozbalení nova_".$_GET["novaV"].".zip\n";
			} else {
				$msg .= "Chyba rozbalení nova_".$_GET["novaV"].".zip\n";
			}
			DelFiles ($downloadpath."/nova.zip");
			if (!Exist_Fil_Dir ($downloadpath."/nova.zip")) {
				$msg .= "OK Soubor smazán nova_".$_GET["novaV"].".zip\n\n";
			} else {
				$msg .= "Chyba smazání nova_".$_GET["novaV"].".zip\n\n";
			}
		}

	  if ($_GET["sh"] == "1") {
			DownloadFiles($SH_file_path, $Update_URL.$SH_file);
			if (Exist_Fil_Dir($SH_file_path)) {
				$current   = Current_($SH_file_path);
				$available = Available_($SH_version);
				if ($current == $available) {
					$msg .= "OK SH Soubor v $current nainstalován\n\n";
				} else {
					$msg .= "Chyba instalace SH souboru\n\n";
				}
			}
		}

	  if ($_GET["rt"] == "1") {
			DownloadFiles($installfile_RT, $Update_URL.$RT_file);
			if (Exist_Fil_Dir($SH_file_path)) {
				$msg .= "OK RT Soubor nainstalován\n\n";
			} else {
				$msg .= "Chyba instalace RT souboru\n\n";
			}
		}

	  if ($_GET["up"] == "1") {
			DownloadFiles($downloadpath."/updater.zip", $UP_download.$_GET["upV"].".zip");
			if (Exist_Fil_Dir($downloadpath."/updater.zip")) {
				ExtractFiles($downloadpath."/updater.zip", XLIVE."MyxLive/");
				$msg .= "OK Staženo updater_".$_GET["upV"].".zip\n";
			} else {
				$msg .= "Chyba stažení updater_".$_GET["upV"].".zip\n";
			}
			DelFiles($downloadpath."/updater.zip");
		}




	  echo xmlHeaderAf($xxx, $xxx_av);
	  echo"
			<item>
				<title>".$bull.$errors."Return to menu</title>
				<description>".$errors."</description>
				<link>".XTREAMERING."</link>
				<media:thumbnail url=\"".XLIVE."MyxLive/themes/osd/myxlive-update.png\" />
			</item>
			<item>
				<title>".$bull.$errors."Re-check the updates</title>
				<description>".$errors."</description>
				<link>".$U_SCRIPT."/install".$backup.".php?header=no</link>
				<media:thumbnail url=\"".XLIVE."MyxLive/themes/osd/myxlive-update.png\" />
			</item>";
	  echo xmlFooter();
	  CreateLogFile($msg, XLIVE."MyxLive/install/update.log", $add);
		break;

	default:
		if (Exist_Fil_Dir(XLIVE."MyxLive/install/install_back.php")) DelFiles(XLIVE."MyxLive/install/install_back.php");
	  $add  = ">";
	  $msg  = "\n$U_NAME v $U_VERSION by $U_AUTOR\n";
	  $msg .= "\n----------- VERSION CHECK -------------\n\n";
	  $available = Available_($UP_version."?n=upd");
	  $msg .= $U_NAME.": Installed version ".$U_VERSION."\n";
	  $num = 0;
	  $cur_v = $U_VERSION;
	  if (strpos($U_VERSION, "beta"))
	  	$cur_v = substr($U_VERSION, 0, strpos($U_VERSION, "beta")+strlen("beta"))-0.01;
		if ($cur_v < $available) {
		  $backup         = "_back";
			$cmd = "cp -f '".XLIVE."MyxLive/install/install.php' '".XLIVE."MyxLive/install/install".$backup.".php'";
			Execute($cmd);
			$msg .= $description = $U_NAME.": Available version $available\n";
			$descriptions = $description."\n";
			$params  = "&amp;up=1&amp;upV=$available";
			$num++;
	    $items = "
			<item>
				<title>".$bull."Download &amp; Install $U_NAME v $available</title>
				<description>".$description."</description>
				<link>".$U_SCRIPT."/install".$backup.".php?action=install&amp;up=1&amp;upV=$available</link>
			</item>";
		}

		$current   = Current_($xL_file_path);
		$available = Available_($xL_version);
	  $msg .= "MyxLive : Installed version $current\n";
		if ($current < $available) {
			$msg .= $description = "MyxLive : Available version $available\n";
			$descriptions .= $description."\n";
			$params .= "&amp;xL=1&amp;xlV=$available";
			$num++;
	    $items .= "
			<item>
				<title>".$bull."Download &amp; Install MyxLive v $available</title>
				<description>".$description."</description>
				<link>".$U_SCRIPT."/install".$backup.".php?action=install&amp;xL=1&amp;xlV=$available</link>
			</item>";
		}



		$current   = Current_($SH_file_path);
		$available = Available_($SH_version);
	  $msg .= "SH file : Installed Version $current\n";
		if ($current < $available) {
			$msg .= $description = "SH file : Accessible Version $available\n";
			$descriptions .= $description."\n";
			$params .= "&amp;sh=1";
			$num++;
	    $items .= "
			<item>
				<title>".$bull."Download &amp; Install file SH v $available</title>
				<description>".$description."</description>
				<link>".$U_SCRIPT."/install".$backup.".php?action=install&amp;sh=1</link>
			</item>";
		}
	  $msg .= "(Support files, to play some videos)\n";
		if (!Exist_Fil_Dir($installfile_RT)) {
			$msg .= $description = "Installs a support file required to play some videos.";
			$descriptions .= $description."\n";
			$params .= "&amp;rt=1";
			$num++;
	    $items .= "
			<item>
				<title>".$bull."Download &amp; Install support file</title>
				<description>".$description."</description>
				<link>".$U_SCRIPT."/install".$backup.".php?action=install&amp;rt=1</link>
			</item>";
		}


		$pocet = $num;




		if ($_GET["header"] == "no") {
			echo xmlHeaderAf($xxx, $xxx_av);
	  } else {
			echo xmlHeader($xxx, $xxx_av);
		}
	  if ($num != 0) {
	    if ($pocet > 1) {
				/*$item = "
			<item>
				<title>".$bull."Stáhnout &amp; Instalovat vše ($pocet aktualizace, NEinstaluje XXX + 18 !!!)</title>
				<description>".$descriptions."</description>
				<link>".$U_SCRIPT."/install".$backup.".php?action=install".$params."</link>
			</item>";*/
			}
			$item .= $items.$reinst;
	    $item .= "
			<item>
				<title>".$bull."I want to go back</title>
				<description></description>
				<link>".XTREAMERING."</link>
			</item>";
		} else {
			$msg .= "Found no available updates.\n";
	    $item = $reinst."
			<item>
				<title>".$bull."Everything okay, return to the menu</title>
				<description></description>
				<link>".XTREAMERING."</link>
			</item>";
		}
	  echo $item;
	  echo xmlFooter();
	  CreateLogFile($msg, XLIVE."MyxLive/install/update.log", $add);
		break;
}

function CreateLogFile($prnt, $path, $add) {
	$cmd = "echo '$prnt' $add '$path'\n";
	Execute($cmd);
}
function DelFiles($fil) {
// 	global $res;
	$cmd = "rm -f '$fil'\n";
	Execute($cmd);
// return;
}
function DownloadFiles($down_dir, $down_link) {
// 	global $res;
	$cmd = "wget '$down_link' -O '$down_dir'\n";
	$cmd .= "/bin/chmod 0755 '$down_dir'\n";
	Execute($cmd);
}
function ExtractFiles($downloadpath, $extractpath) {
// 	global $res;
	$cmd = "unzip -o '$downloadpath' -d '$extractpath'\n";
	Execute($cmd);
}

function Exist_Fil_Dir($name) {
	if (file_exists($name)) {
		return true;
	}
	return false;
}
function Create_Dir($name) {
// 	global $res;
	$cmd = "mkdir -p '$name'\n";
	$cmd .= "/bin/chmod 0755 '$name'\n";
	Execute($cmd);
	if (Exist_Fil_Dir($name)) {
		return true;
	}
	return false;
}

function Available_($file) {
	$ver_available = trim(file_get_contents($file));
	$X_s           = strpos($ver_available, "Version ")+8;
	$X_e           = strpos($ver_available, ",");
	$num_available = substr($ver_available, $X_s, $X_e-$X_s);
	return $num_available;
}
function Current_($file) {
  if (!Exist_Fil_Dir($file)) {
		$num_current = "0";
		return $num_current;
	}
	$ver_current = trim(file_get_contents($file));
	$X_s         = strpos($ver_current, "Version ")+8;
	$X_e         = strpos($ver_current, ",");
	if ($X_s != "") {
		$num_current  = substr($ver_current, $X_s, $X_e-$X_s);
	}
	if ($X_s =="") { $num_current = "0"; }
	return $num_current;
}
function Execute($cmd) {
	global $res;
// 	$cmd .= "sleep '1'";
	@exec($cmd,$res);
}

function EditarchivyRSS($archivyRSS) {
	$fp = fopen($archivyRSS, "r+");
	$ci=1;
	$line="";
	while ($ci>0) {
		$data = fgets($fp,128);
		If (!$data) {
			Break;
		}
		$line.=$data;
		$ci++;
	}
	$S     = 0;
	$E     = 0;
	$find  = "<title>Archiv NOVA</title>";
	$S     = strpos($line, $find, $S) + strlen($find);
	$find  = "<link>";
	$S     = strpos($line, $find, $S) + strlen($find);
	$partA = substr($line, 0, $S);
	$find  = "</link>";
	$S     = strpos($line, $find, $S);
	$partB = substr($line, $S);
	$line = $partA."http://127.0.0.1/media/sda1/scripts/MyxLive/category/nova/index.php".$partB;
	fwrite(fopen($archivyRSS, 'w'), $line);
	return;
}

function EditIndexXtremering($IndexXtremering) {
	if (Exist_Fil_Dir(XLIVE."MyxLive/versionxtreamering.xml")) {
		$available = file_get_contents(XLIVE."Xtreamering/version.xml");
		$find      = "<version>";
		$X_s       = strpos($available, $find) + strlen($find);
		$find      = "</version>";
		$X_e       = strpos($available, $find);
		$available = substr($available, $X_s, $X_e-$X_s) + 0;

		$curent = file_get_contents(XLIVE."MyxLive/versionxtreamering.xml");
		$find   = "<version>";
		$X_s    = strpos($curent, $find) + strlen($find);
		$find   = "</version>";
		$X_e    = strpos($curent, $find);
		$curent = substr($curent, $X_s, $X_e-$X_s) + 0;
		if ($curent >= $available) return;
	}
// if (version_compare($available, $curent."0.0.1", '>')) {
//     echo "available = $available<br>curent = $curent.0.0.1\n>";
// }

	$fp = fopen($IndexXtremering, "r+");
	$ci=1;
	$line="";
	while ($ci>0) {
		$data = fgets($fp,128);
		If (!$data) {	Break; }
		$line.=$data;
		$ci++;
	}

	$includestring = "//if (file_exists(\"".XLIVE."MyxLive/install/install.php\")) require_once(\"".XLIVE."MyxLive/install/install.php\");";
	if (strpos($line, $includestring)) return; // je uz upraveno?
	if (strpos($line, "if (file_exists(") && !strpos($line, "//if (file_exists(")) {
	  $line = str_replace("if (file_exists(", "//if (file_exists(", $line);
	} else {
		$find  = "?>";
		$S     = strpos($line, $find, strpos($line, $find) + strlen($find));
		$partA = substr($line, 0, $S);
		$partB = substr($line, $S);
		$line  = $partA."\t".$includestring."\n".$partB;
	}

/* prida updateBarCZ.png pro aktualizaci xLiveCZ */
	$includestring = "if(itemfile == \"upcz\"){
				\"/tmp/usbmounts/sda1/scripts/Xtreamering/image/updateBar.png\";
			} else";

	$find  = "if(itemfile != \"nil\"){";
	$S     = strpos($line, $find, $S);
	$partA = substr($line, 0, $S);
	$partB = substr($line, $S);
	$line  = $partA.$includestring."\n\t\t\t".$partB;

/* prida vazbu na <updateCZ> pro aktualizaci xLiveCZ */
	$includestring = "
			if(itemfile == \"upcz\"){
				fileName = \"/tmp/usbmounts/sda1/scripts/MyxLive/install/install.php\";
				if(CheckFile(fileName) == \"true\"){
					upUrl = \"http://localhost/media/sda1/scripts/MyxLive/install/install.php\";
					jumpToLink(\"updateCZ\");
				}
			} else\n";
	$find  = "if ( userInput == \"1\" )";
	$S     = strpos($line, $find, $S) + strlen($find);
	$find  = "{";
	$S     = strpos($line, $find, $S) + strlen($find);
	$partA = substr($line, 0, $S);
	$find  = "if(itemfile != \"nil\"){";
	$E     = strpos($line, $find, $S);
	$partB = substr($line, $E);
	$line  = $partA.$includestring."			".$partB;

/* prida tag <updateCZ> mezi </mediaDisplay> a <destination> */
	$includestring = "\n
<updateCZ>
	<link>
		<script>
			upUrl;
		</script>
	</link>
	<mediaDisplay name=\"threePartsView\" showHeader=\"no\" rowCount=\"3\" columnCount=\"4\" drawItemText=\"no\" menuBorderColor=\"0:0:0\" sideColorBottom=\"0:0:0\" sideColorTop=\"0:0:0\" itemImageXPC=\"10\" itemOffsetXPC=\"10\" backgroundColor=\"0:0:0\" sliding=\"yes\" idleImageXPC=\"45\" idleImageYPC=\"45\" idleImageWidthPC=\"8,6\" idleImageHeightPC=\"8\">
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy0.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy1.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy2.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy3.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy4.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy5.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy6.png</idleImage>
	</mediaDisplay>
</updateCZ>\n\n";
	$find  = "</mediaDisplay>";
	$S     = strpos($line, $find, $S) + strlen($find);
	$partA = substr($line, 0, $S);
	$find  = "<destination>";
	$E     = strpos($line, $find, $S);
	$partB = substr($line, $E);
	$line  = $partA.$includestring.$partB;

/* prida polozku pro CZ xLive pod </itemSize>*/
	$includestring = "\n
	<item>
		<title>MyxLive</title>
		<link>/tmp/usbmounts/sda1/scripts/MyxLive/category/czech.rss</link>
		<bg>/tmp/usbmounts/sda1/scripts/Xtreamering/image/MyxLive.jpg</bg>
		<file>upcz</file>
		<itemid>0</itemid>
		<mediaDisplay name=\"photoView\" showHeader=\"no\" rowCount=\"3\" columnCount=\"5\" drawItemText=\"no\" menuBorderColor=\"0:0:0\" sideColorBottom=\"0:0:0\" sideColorTop=\"0:0:0\" itemImageXPC=\"10\" itemOffsetXPC=\"10\" backgroundColor=\"0:0:0\" sliding=\"yes\" idleImageXPC=\"45\" idleImageYPC=\"45\" idleImageWidthPC=\"8,6\" idleImageHeightPC=\"8\">
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy0.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy1.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy2.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy3.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy4.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy5.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy6.png</idleImage>
			<onEnter>
				SwitchViewer(7);
			</onEnter>
		</mediaDisplay>
	</item>";
	$find  = "</itemSize>";
	$S     = strpos($line, $find, $S) + strlen($find);
	$partA = substr($line, 0, $S);
	$find  = "<title>MyxLive";
	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> z install.php*/
	$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 CZ xLive */
	$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 CZ xLive */
	$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;

	$cmd = "cp -f '".$IndexXtremering."' '".XLIVE."Xtreamering/index_backup.php'";
	Execute($cmd);

	$cmd = "cp -f '".XLIVE."Xtreamering/version.xml' '".XLIVE."MyxLive/versionxtreamering.xml'";
	Execute($cmd);
	if (!fwrite(fopen($IndexXtremering, 'w'), $line)) {
	  $line = str_replace("'", "\\\"", $line);
		CreateLogFile($line, $IndexXtremering, ">");
	}
	header('Location: '.$IndexXtremering);
	die();
	return;
}


function EditczechRSS($czechRSS) {
	$fp = fopen($czechRSS, "r+");
	$ci=1;
	$line="";
	while ($ci>0) {
		$data = fgets($fp,128);
		If (!$data) {	Break; }
		$line.=$data;
		$ci++;
	}
	$find  = "</text>"; // </mediaDisplay>
	$S     = strpos($line, $find) + strlen($find);
	$partA = substr($line, 0, $S);
	$find  = "</mediaDisplay>"; // </channel>
	$S     = strpos($line, $find);
	$find  = "<item><title>XXX + 18</title>";
	if (strpos($line, $find)) {
		$K    = strpos($line, $find);
	} else {
		$find = "</channel>";
		$K    = strpos($line, $find);
	}
	$partC = substr($line, $S, $K - $S);
	$partB = "
	<onUserInput>
		userInput = currentUserInput();
		if ( userInput == &quot;ENTR&quot; )
		{
			title = getItemInfo(&quot;title&quot;);
			writeStringToFile(\"/tmp/debug\", title);
			if (title == &quot;XXX + 18&quot;)
			{
				if(login() == &quot;true&quot;)
				{
					ret = \"true\";
				}
			}
		}
	</onUserInput>
";
	$partD = "<item><title>XXX + 18</title>
	<link>/tmp/usbmounts/sda1/scripts/MyxLive/category/adult_cs.rss</link>
	<media:thumbnail url=\"/tmp/usbmounts/sda1/scripts/MyxLive/image/adult_cs.jpg\"/>
	<mediaDisplay name=\"onePartView\" showDefaultInfo=\"no\" itemPerPage=\"8\" cornerRounding=\"no\" itemXPC=\"18\" itemWidthPC=\"22\" capWidthPC=\"0\" itemImageHeightPC=\"0\" showHeader=\"no\" itemOffsetXPC=\"5\" itemOffsetYPC=\"0\" itemGap=\"0\" itemImageGap=\"0\" menuBorderColor=\"0:0:0\" sideColorBottom=\"-1:-1:-1\" sideColorTop=\"-1:-1:-1\" itemImageXPC=\"0\" itemHeightPC=\"8\" backgroundColor=\"44:0:141\" focusBorderColor=\"255:0:0\" unFocusBorderColor=\"0:0:0\" idleImageXPC=\"46\" idleImageYPC=\"48\" idleImageWidthPC=\"8,6\" idleImageHeightPC=\"8\">
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads0.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads1.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads2.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads3.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads4.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads5.png</idleImage>
			<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/loads6.png</idleImage>
		<text offsetXPC=10 offsetYPC=6 widthPC=100 heightPC=6 fontSize=20 foregroundColor=250:0:0>
			XXX + 18
		</text>
		<backgroundDisplay>
			<image offsetXPC=0 offsetYPC=0 widthPC=100 heightPC=100>/tmp/usbmounts/sda1/scripts/MyxLive/backgrounds/pozadi_adult.jpg</image>
		</backgroundDisplay>
	</mediaDisplay>
</item>

</channel>
</rss>";
	$NewFile = $partA.$partB.$partC.$partD;
	fwrite(fopen($czechRSS, 'w'), $NewFile);
	return;
}
function xmlHeader($xxx, $xxx_av) {
	global $num, $U_NAME, $U_VERSION, $U_AUTOR, $U_SCRIPT;
	if ($num == 0) {
		$row = $num+1;
	} else {
		$row = $num+2;
	}
	$Header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>

<rss version=\"2.0\" xmlns:media=\"http://purl.org/dc/elements/1.1/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">

								
<mediaDisplay name=\"onePartView\" showDefaultInfo=\"no\" itemPerPage=\"8\" cornerRounding=\"no\" itemXPC=\"30\" itemYPC=\"30\" itemWidthPC=\"42\" capWidthPC=\"0\" itemImageHeightPC=\"0\" showHeader=\"no\" itemOffsetXPC=\"5\" itemOffsetYPC=\"0\" itemGap=\"0\" itemImageGap=\"0\" menuBorderColor=\"0:0:0\" sideColorBottom=\"-1:-1:-1\" sideColorTop=\"-1:-1:-1\" itemImageXPC=\"0\" itemHeightPC=\"8\" backgroundColor=\"0:0:0\" focusBorderColor=\"255:0:0\" unFocusBorderColor=\"0:0:0\" idleImageXPC=\"46\" idleImageYPC=\"48\" idleImageWidthPC=\"8,6\" idleImageHeightPC=\"8\">




<text align=\"center\" offsetXPC=\"0\" offsetYPC=\"0\" widthPC=\"100\" heightPC=\"20\" fontSize=\"30\" backgroundColor=\"139:155:85\" foregroundColor=\"255:255:255\">
		  <script>getPageInfo(\"pageTitle\");</script>
		</text>
		


		<text align=\"center\" redraw=\"yes\" lines=\"4\" offsetXPC=10 offsetYPC=80 widthPC=80 heightPC=15 fontSize=16 backgroundColor=139:155:85 foregroundColor=255:255:255>
			$U_NAME $U_VERSION by $U_AUTOR 
		</text>
								
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy0.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy1.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy2.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy3.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy4.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy5.png</idleImage>
		<idleImage>/tmp/usbmounts/sda1/scripts/MyxLive/image/busy6.png</idleImage>
<image offsetXPC=11 offsetYPC=3 widthPC=21 heightPC=16>
<script>channelImage;</script>

<widthPC>
<script>
          35 * screenYp / screenXp;
</script>
</widthPC>

<offsetXPC>
<script>
          3 + 61 * (1 - screenYp / screenXp) / 2;
</script>
</offsetXPC>
</image>	
		
		
		
		
		
		
		
		
		
		
		
		
		

	

	
	
	
	
	
	
	
	
	";

	if ($xxx) {
		$Header .= "
	<onUserInput>
		userInput = currentUserInput();
		if ( userInput == &quot;ENTR&quot; )
		{
			title = getItemInfo(&quot;title&quot;);
			writeStringToFile(\"/tmp/debug\", title);
			if (title == &quot;$xxx_av&quot;)
			{
				if(login() == &quot;true&quot;)
				{
					ret = \"true\";
				}
			}
		}
	</onUserInput>";
	}
	$Header .= "
	</mediaDisplay>
	
	<script>
channelImage = \"/tmp/usbmounts/sda1/scripts/MyxLive/themes/logo/myxtremer_logo.png\";
</script>
	
	<channel>
		<title></title>
		<link>$U_SCRIPT/install.php</link>
		
		
	
	
	<annotation><text offsetXPC=\"18\" offsetYPC=\"5\" widthPC=\"100\" heightPC=\"10\" fontSize=\"20\" backgroundColor=\"139:155:85\" foregroundColor=\"255:255:255\">
		$U_NAME $U_VERSION by $U_AUTOR 
	</text></annotation>
		
		
		
		";
	return $Header;
}
function xmlHeaderAf($xxx, $xxx_av) {
	global $U_NAME, $U_VERSION, $U_AUTOR, $U_SCRIPT;
	$Header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>

<rss version=\"2.0\" xmlns:media=\"http://purl.org/dc/elements/1.1/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
	<mediaDisplay>";
	if ($xxx) {
		$Header .= "
	<onUserInput>
		userInput = currentUserInput();
		if ( userInput == &quot;ENTR&quot; )
		{
			title = getItemInfo(&quot;title&quot;);
			writeStringToFile(\"/tmp/debug\", title);
			if (title == &quot;$xxx_av&quot;)
			{
				if(login() == &quot;true&quot;)
				{
					ret = \"true\";
				}
			}
		}
	</onUserInput>";
	}
	$Header .= "
	</mediaDisplay>
	<channel>
		<title>$U_NAME</title>
		<link>$U_SCRIPT/install.php</link>
		<annotation><text offsetXPC=\"18\" offsetYPC=\"5\" widthPC=\"100\" heightPC=\"10\" fontSize=\"20\" backgroundColor=\"139:155:85\" foregroundColor=\"255:255:255\">
		$U_NAME $U_VERSION by $U_AUTOR 
	</text></annotation>
		
		";
	return $Header;
}
function xmlFooter() {
	$Footer = "
	</channel>
</rss>";
	return $Footer;
}

?>