$os->$branch->version)?$options->$os->$branch->version:""); $last_update = (isset($options->$os->$branch->date )?$options->$os->$branch->date:0); if( !is_numeric($current_version) ) $current_version = (($version)?$version:1); switch($os){ case 'win': $osdir='tsetup/'; $osfile = 'tupdate'; break; // $osdir='tsetup/tupdate'; break; case 'linux': $osdir='tlinux/'; $osfile = 'tlinuxupd'; break; // $osdir='tlinux/tlinuxupd'; break; case 'mac': $osdir='tmac/'; $osfile = 'tmacupd'; break; // $osdir='tmac/tmacupd'; break; default: // Maybe it's a good idea to default some of the above??? idk... echo "Error: no valid OS given."; exit(); break; } // Testing if we have new updates on tdesktop.com if($version=="" || ($last_update+15*60)$os->$branch->date = time(); save($options); $query = ['version' => $current_version, $branch => 1]; $query = http_build_query($query,'','&',PHP_QUERY_RFC3986); $url = "http://tdesktop.com/$os/tupdates/current?$query"; if( $str = file_get_contents($url) ){ if( preg_match("/([0-9]+) : (.*)/", $str, $matches) ) $version = $matches[1]; else {echo $str; exit;} // Seems we have a new version? $options->$os->$branch->response = $str; save($options); if( !file_exists($os) ) mkdir($os); if( !file_exists("$os/$osfile$version") ){ // Yeah, it seems we have a new version to download. if($beta) {echo $str; exit();} // I can't download private beta T-T if( isset($matches[2]) ){ if( !file_exists("$os/locked") ){ // Yeey no other instance is downloading to the dir. // Making sure no one will download anyting while i am downloading file_put_contents("$os/locked", "Delete this file if needed"); if( $file_content = file_get_contents($matches[2]) ){ // Downloading a new version, this may take some time... file_put_contents("$os/$osfile$version", $file_content); $options->$os->$branch->version = $version; save($options); unlink("$os/locked"); }else{ // Cant download from tdesktop.com, we forward them to the official link unlink("$os/locked"); echo $str; exit(); } }else{ // Downloading to the server. Getting the last version until finished $version = $options->$os->$branch->version; } } }else{ $options->$os->$branch->version = $version; save($options); } } } if( $official ) { // Sending the last response known from the official servers echo $options->$os->$branch->response; exit(); } if( !file_exists("$os/$osfile$version") ){ // Error: File not found in our server, redirecting to the official server echo "$version : http://updates.tdesktop.com/$osdir/$osfile$version"; exit(); } // Sending them to our server, we have this version!! echo "$version : http://{$_SERVER["HTTP_HOST"]}/$os/$osfile$version"; exit(); } // If someone asks for a file diferent than the above if( $path = realpath(".".$_SERVER["REQUEST_URI"]) && // If its a valid path and strpos($path, __DIR__) !== false && // It's contained on the root path and is_file($path) && // Is a file and strpos(basename($path), ".") == false // filename doesen't contain a "." ) return false; // Return the given file // If all the above fails... redirect to the telegram.org webpage header("Location: https://desktop.telegram.org/"); exit(); // Just to make things shorter function save($options){ file_put_contents("options.json", json_encode($options,JSON_PRETTY_PRINT)); }