Last active
January 31, 2017 11:47
-
-
Save FabianPastor/e0ab2202da058a44e6e319725f2c7b24 to your computer and use it in GitHub Desktop.
Route file for mirroring telegram desktop update system
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
<?php | |
/************************ | |
** Author: FabianPastor | |
** Description: Simple route file for mirroring telegram desktop update system | |
** | |
** You can test it in local by using the PHP integrated Webserver: | |
** $ php -S localhost:8080 index.php | |
** Also if you want to use it on Apache2 make sure you have mod_rewrite enabled. | |
** This would be the content for the .htaccess | |
DirectorySlash Off | |
Options FollowSymLinks Indexes | |
DirectoryIndex index.php | |
RewriteEngine on | |
RewriteCond %{REQUEST_FILENAME} -d | |
RewriteRule ^.*$ - [L] | |
RewriteCond %{REQUEST_FILENAME} -f | |
RewriteRule ^.*$ - [L] | |
RewriteRule ^.*$ index.php [L] | |
** Note: I assume you have a basic knowledge of apache and know how to enable the use of | |
** .htaccess, enable mod_rewrite and so on. Don't ask me how to do things. | |
************************/ | |
$options = json_decode(file_get_contents("options.json")); | |
// if(json_last_error()!==JSON_ERROR_NONE){ echo "Json Error: ".json_last_error_msg().PHP_EOL; exit();} | |
// If we recive a query for testing if there is a new update | |
if( preg_match("/\/([^\/]+)\/tupdates\/current\??(.*)/", $_SERVER["REQUEST_URI"], $matches) ){ | |
// Initializating variables and get the default values if no valid args are given | |
$os = $matches[1]; | |
parse_str($matches[2], $args); | |
$current_version=false; $alpha=false; $beta=false; $current=true; $official=false; | |
if( isset($args['version' ]) ) $current_version = $args['version']; | |
if( isset($args['alpha' ]) ) $alpha = true; | |
if( isset($args['beta' ]) ) $beta = true; | |
if( isset($args['official' ]) ) $official = true; | |
if($beta||$alpha) $current=false; | |
$branch = ($current?'stable':($alpha?'alpha':'beta')); | |
$version = (isset($options->$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)<time() ){ | |
$options->$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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment