|
| 1 | +<?php |
| 2 | + declare(strict_types=1); |
| 3 | + |
| 4 | + namespace Keyman\Site\com\keyman; |
| 5 | + |
| 6 | + require __DIR__ . '/../../_includes/autoload.php'; |
| 7 | + |
| 8 | + const DEBUG=0; |
| 9 | + |
| 10 | + use Keyman\Site\com\keyman\KeymanComSentry; |
| 11 | + use Keyman\Site\com\keyman\Validation; |
| 12 | + use Keyman\Site\Common\KeymanHosts; |
| 13 | + use Keyman\Site\Common\JsonApiFailure; |
| 14 | + |
| 15 | + $env = getenv(); |
| 16 | + KeymanComSentry::init(); |
| 17 | + |
| 18 | + AppDownloadPage::redirect_to_file( |
| 19 | + isset($_REQUEST['url']) ? $_REQUEST['url'] : null, |
| 20 | + isset($_REQUEST['product']) ? $_REQUEST['product'] : null, |
| 21 | + isset($_REQUEST['version']) ? $_REQUEST['version'] : null, |
| 22 | + isset($_REQUEST['tier']) ? $_REQUEST['tier'] : null, |
| 23 | + // TODO: should we? or just leave it encoded in URL for now? |
| 24 | + // isset($_REQUEST['bcp47']) ? $_REQUEST['bcp47'] : null, |
| 25 | + // isset($_REQUEST['update']) ? $_REQUEST['update'] : null |
| 26 | + ); |
| 27 | + |
| 28 | + class AppDownloadPage { |
| 29 | + |
| 30 | + public static function redirect_to_file($url, $product, $version, $tier) { |
| 31 | + if(empty($url)) { |
| 32 | + JsonApiFailure::InvalidParameters("url"); |
| 33 | + } |
| 34 | + |
| 35 | + if(empty($product)) { |
| 36 | + $product = 'unknown'; |
| 37 | + } |
| 38 | + |
| 39 | + if(empty($version)) { |
| 40 | + $version = '0.0'; |
| 41 | + } |
| 42 | + |
| 43 | + if(empty($tier)) { |
| 44 | + $tier = 'stable'; |
| 45 | + } |
| 46 | + |
| 47 | + $tier = Validation::validate_tier($tier, 'stable'); |
| 48 | + |
| 49 | + $url_e = htmlentities($url); |
| 50 | + |
| 51 | + if(DEBUG) { |
| 52 | + header('Content-Type: text/plain'); |
| 53 | + } |
| 54 | + |
| 55 | + if(KeymanHosts::Instance()->Tier() !== KeymanHosts::TIER_TEST) { |
| 56 | + self::report_app_download_event($product, $version, $tier); |
| 57 | + |
| 58 | + if(DEBUG) { |
| 59 | + echo "\n\nLocation: $url\n"; |
| 60 | + exit; |
| 61 | + } |
| 62 | + |
| 63 | + // We don't do a redirect for Test tier because a test instance of the |
| 64 | + // downloads server is not available and so it gives us an error |
| 65 | + header("HTTP/1.1 302 Found"); |
| 66 | + header("Cache-Control: no-store"); |
| 67 | + header("Location: $url"); |
| 68 | + } |
| 69 | + |
| 70 | + echo "<a href='$url_e'>Download Link</a>"; |
| 71 | + } |
| 72 | + |
| 73 | + private static function report_app_download_event($product, $version, $tier) { |
| 74 | + global $env; |
| 75 | + $url = KeymanHosts::Instance()->SERVER_api_keyman_com . "/app-downloads-increment/".rawurlencode($product). |
| 76 | + "/".rawurlencode($version)."/".rawurlencode($tier); |
| 77 | + |
| 78 | + if(KeymanHosts::Instance()->Tier() !== KeymanHosts::TIER_TEST) { |
| 79 | + if(KeymanHosts::Instance()->Tier() === KeymanHosts::TIER_DEVELOPMENT) |
| 80 | + $key = 'local'; |
| 81 | + else |
| 82 | + $key = $env['API_KEYMAN_COM_INCREMENT_DOWNLOAD_KEY']; |
| 83 | + |
| 84 | + $c = curl_init($url); |
| 85 | + curl_setopt($c, CURLOPT_HEADER, 0); |
| 86 | + curl_setopt($c, CURLOPT_POSTFIELDS, 'key='.rawurlencode($key)); |
| 87 | + curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE); |
| 88 | + curl_setopt($c, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); |
| 89 | + $data = curl_exec($c); |
| 90 | + curl_close($c); |
| 91 | + |
| 92 | + if(DEBUG) { |
| 93 | + var_dump("app-downloads-increment ($url):",$data); |
| 94 | + } |
| 95 | + } else |
| 96 | + $data = TRUE; |
| 97 | + return $data !== FALSE; |
| 98 | + } |
| 99 | + } |
0 commit comments