-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUtil.php
More file actions
69 lines (61 loc) · 2.57 KB
/
Copy pathUtil.php
File metadata and controls
69 lines (61 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace Keyman\Site\com\keyman;
use Keyman\Site\Common\KeymanHosts;
class Util {
static function Fail($message) {
echo $message;
//TODO: http header 500
exit;
}
static function cdn($file) {
global $cdn; // we'll continue to use this as global for now.
if(!isset($cdn)) {
if(!empty($_SERVER['DOCUMENT_ROOT']) && file_exists($_SERVER['DOCUMENT_ROOT'].'/cdn/deploy/cdn.php')) {
require_once($_SERVER['DOCUMENT_ROOT'].'/cdn/deploy/cdn.php');
} else {
$cdn = false;
}
}
$use_cdn = KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_PRODUCTION ||
KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_STAGING ||
(isset($_REQUEST['cdn']) && $_REQUEST['cdn'] == 'force');
if($use_cdn) {
if($cdn && isset($cdn['/'.$file])) {
return "/cdn/deploy{$cdn['/'.$file]}";
}
}
// TODO: log warning or error to sentry on missing files
return "/cdn/dev/{$file}";
}
private static function call_keyman_site($site, $url, $testFixture) {
if(KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_TEST) {
return file_get_contents(__DIR__ . '/../../tests/fixtures/' . $testFixture);
}
// curl library is more reliable than file_get_contents
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $site . $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 60);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'keyman.com/1.0');
$query = @curl_exec($curl_handle);
$http_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
if($http_code >= 200 && $http_code <= 299) {
// Can use this to generate fixtures when needed:
// $p = parse_url($site . $url);
// file_put_contents(__DIR__ . '/../../tests/fixtures/' . $site . '-' . str_replace('/', '_', $p['path']) . '.json', $query);
return $query;
} else {
@trigger_error("request to $site$url failed with $http_code");
return FALSE;
}
}
static function call_api_keyman_com($url, $testFixture) {
return Util::call_keyman_site(KeymanHosts::Instance()->SERVER_api_keyman_com, $url, $testFixture);
}
static function call_downloads_keyman_com($url, $testFixture) {
return Util::call_keyman_site(KeymanHosts::Instance()->SERVER_downloads_keyman_com, $url, $testFixture);
}
}