Created
June 28, 2021 05:38
-
-
Save moonexpr/b54aa78bd04e4c458846f8a2621e957b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| if (!isset($_GET['q'])) | |
| throw new Exception('Request is missing query parameter \'q\''); | |
| const SERVER_MAP = [ | |
| 100 => 'eu1', | |
| 110 => 'eu2', | |
| 120 => 'eu3', | |
| 200 => 'vin', | |
| 300 => 'la', | |
| 400 => 'chi', | |
| 410 => 'chi2', | |
| 420 => 'chi3', | |
| 500 => 'brz', | |
| 600 => 'aus', | |
| 700 => 'sgp' | |
| ]; | |
| function demoReference($query) { | |
| $regex = '/stvdemos\/(([0-9]+)-[0-9_\-a-z]+\.dem)/'; | |
| preg_match($regex, $query, $groups, PREG_OFFSET_CAPTURE, 0); | |
| if (sizeof($groups) != 3) | |
| throw new Exception('The demo reference is malformed, reference format is "stvdemos/{serverId}-{timestamp}-{map}.dem".'); | |
| return [ | |
| intval($groups[2][0]), | |
| $groups[1][0], | |
| ]; | |
| } | |
| function demoUrl($serverId, $fileName) { | |
| foreach( [intdiv($serverId, 10)*10, intdiv($serverId, 100)*100] as $providerId) | |
| { | |
| if ($provider = SERVER_MAP[$providerId]) | |
| { | |
| return "//$provider.creators.tf/demos/$serverId/$fileName"; | |
| } | |
| } | |
| throw new Exception('The demo reference does not map to any known provider.'); | |
| } | |
| try { | |
| $query = $_GET['q']; | |
| if (isset($_GET['base64']) && $_GET['base64'] != '0') { | |
| $query = base64_decode($query); | |
| } | |
| [$serverId, $fileName] = demoReference($query); | |
| $url = demoUrl($serverId, $fileName); | |
| header("HTTP/2 303 See Other"); | |
| header("Location: $url"); | |
| } catch (Exception $e) { | |
| header('HTTP/2 404 Not Found'); | |
| echo "<h1>Not Found</h1>"; | |
| echo "The demo that you have requested could not be found."; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment