Last active
April 25, 2016 03:37
-
-
Save FabianPastor/848a4a3ab49cf08d5caad4e3b86a700b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
if($_SERVER['USER']!="root"){ | |
echo "Debes ser Root, ejecuta con sudo".PHP_EOL; | |
exit(1); | |
} | |
$option=new Options(); | |
//TODO: Force Download Version | |
$fversion=false; | |
if($vkey=array_search("-fv", $argv)){ | |
$fversion=$argv[($vkey+1)]; | |
} | |
//TODO: Force OS | |
$fos=false; | |
if($vkey=array_search("-fo", $argv)){ | |
$fos=$argv[($vkey+1)]; | |
} | |
echo "Iniciando Actualizador".PHP_EOL; | |
//TODO: Force url | |
$resp=file_get_contents($option->url()); | |
list($new,$url)=explode(" : ",$resp); | |
$new=trim($new); | |
$url=trim($url); | |
if($fversion){ | |
$url=str_replace($new, $fversion, $url); | |
$new=$fversion; | |
} | |
$urlparts=explode("/",$url); | |
$newfile=array_pop($urlparts); | |
if($new>=$option->ver){ | |
//Si no existe la nueva versión ya descargada se procede a descargar | |
if(!file_exists("./tdesktop/".$option->updateDir().$newfile)){ | |
echo "Descargando versión $new en $newfile".PHP_EOL; | |
//Crea el archivo current con la información de la nueva versión | |
//redireccionando a localhost en vez de a updates.tdesktop.com | |
file_put_contents("./tdesktop/{$option->os}/tupdates/current", | |
str_replace("updates.tdesktop.com","127.0.0.1",$resp)); | |
//Descarga del archivo desde los servidores. | |
if($archivo=file_get_contents($url)){ | |
file_put_contents("./tdesktop/".$option->updateDir().$newfile, $archivo); | |
}else{ | |
exit(2); | |
} | |
//Actualizamos opciones y guardamos el archivo de conf. | |
$option->ver=$new; | |
$option->save(); | |
} | |
//Se guarda el contenido de /etc/hosts para poder recuperarlo | |
$hosts=file_get_contents("/etc/hosts"); | |
//Se agrega la entrada para hoockear tdesktop.com | |
file_put_contents("/etc/hosts","127.0.0.1\ttdesktop.com".PHP_EOL,FILE_APPEND); | |
//Ejecutamos servidor web en una ventana externa para poder interrumpit | |
//el proceso sin interrumpir todo este script. | |
exec('xterm -e "php -S 127.0.0.1:80 -t ./tdesktop/"'); | |
//Quitamos el hoock a tdesktop. | |
file_put_contents("/etc/hosts",$hosts); | |
}else{ | |
//Nada que hacer | |
echo "La versión que tienes es Mayor".PHP_EOL; | |
} | |
exit(0); | |
class Options{ | |
private $file="Updater.json"; | |
public $os="linux"; //"win" | |
public $ver=9040; | |
public $dev=1; | |
public function __construct($obj=false){ | |
$this->load($obj); | |
$this->createDirs(); | |
} | |
public function uri($url=false){ | |
if($url!==false) return $url; | |
return "http://tdesktop.com/{$this->os}/tupdates/current"; | |
} | |
public function url($url=false,$get_http=false){ | |
if(!is_array($get_http)) | |
$get_http=array( | |
"version" => $this->ver, | |
"dev" => $this->dev | |
); | |
$args=http_build_query($get_http,'','&',PHP_QUERY_RFC3986); | |
return $this->uri($url)."?".$args; | |
} | |
public function save(){ | |
file_put_contents($this->file, json_encode($this,JSON_PRETTY_PRINT)); | |
} | |
public function load($obj=false){ | |
if($obj==false) | |
if(file_exists($this->file)) | |
$obj=json_decode(file_get_contents($this->file)); | |
if(isset($obj->os)) | |
$this->os=$obj->os; | |
if(isset($obj->ver)) | |
$this->ver=$obj->ver; | |
if(isset($obj->dev)) | |
$this->dev=$obj->dev; | |
} | |
public function createDirs(){ | |
if(!file_exists("./tdesktop/")) mkdir("./tdesktop/"); | |
if(!file_exists("./tdesktop/".$this->updateDir())) mkdir("./tdesktop/".$this->updateDir()); | |
if(!file_exists("./tdesktop/{$this->os}/")) mkdir("./tdesktop/{$this->os}/"); | |
if(!file_exists("./tdesktop/{$this->os}/tupdates/")) mkdir("./tdesktop/{$this->os}/tupdates/"); | |
} | |
public function updateDir(){ | |
if($this->os=="linux"){ | |
return "tlinux/"; | |
}elseif($this->os=="win"){ | |
return "tsetup/"; | |
}else{ | |
$this->os="linux"; | |
return "tlinux/"; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment