Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _content/developer/keymanweb/keyboards.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once _KEYMANCOM_INCLUDES . '/includes/template.php';
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

// Required
head([
Expand Down Expand Up @@ -110,7 +111,7 @@
<tbody>

<?php
$data = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . '/cloud/4.0/keyboards?languageidtype=bcp47&version='.$stable_version);
$data = Util::call_api_keyman_com('/cloud/4.0/keyboards?languageidtype=bcp47&version='.$stable_version, 'api.keyman.com-cloud_4.0_keyboards.json');
if($data === FALSE) {
// fallback if API is down, bad news anyway.
$data = file_get_contents('keyboards.txt');
Expand Down
3 changes: 2 additions & 1 deletion _content/developer/keymanweb/keymanweb-version.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
require_once _KEYMANCOM_INCLUDES . '/includes/servervars.php';

use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

function getKeymanWebHref()
{
$json = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . "/version/web");
$json = Util::call_api_keyman_com("/version/web", 'api.keyman.com-version_web.json');
if ($json) {
$json = json_decode($json);
}
Expand Down
3 changes: 2 additions & 1 deletion _content/downloads/all-versions/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once _KEYMANCOM_INCLUDES . '/includes/template.php';
require_once _KEYMANCOM_INCLUDES . '/includes/ui/downloads.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

// Required
head([
Expand All @@ -14,7 +15,7 @@
<h2 class="red underline large">Keyman downloads - all versions</h2>

<?php
$data = json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/all'));
$data = @json_decode(Util::call_downloads_keyman_com('/api/version/all', 'downloads.keyman.com-api_version_all.json'));

if ($data === NULL) {
die('Error decoding JSON data.');
Expand Down
7 changes: 3 additions & 4 deletions _content/downloads/releases/_version_downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once _KEYMANCOM_INCLUDES . '/includes/playstore.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Locale;
use Keyman\Site\com\keyman\Util;

Locale::definePageScope('LOCALE_DOWNLOADS', 'downloads');

Expand All @@ -31,8 +32,7 @@ function array_key_first(array $arr) {
}

// note: we currently ignore the tier parameter

$versions = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . "/api/version/2.0?targetVersion=$version"));
$versions = @json_decode(Util::call_downloads_keyman_com("/api/version/2.0?targetVersion=$version", 'downloads.keyman.com-api_version_2.0.json'));

if(empty($versions->android))
$tier = 'unknown';
Expand All @@ -47,8 +47,7 @@ function array_key_first(array $arr) {
$versionTier = $version . ($tier == 'stable' ? "" : "-$tier");



$versionsData = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . "/api/version/all"));
$versionsData = @json_decode(Util::call_downloads_keyman_com("/api/version/all", 'downloads.keyman.com-api_version_all.json'));
if (!$versionsData) {
die("Failed to retrieve or parse the API data.");
}
Expand Down
4 changes: 2 additions & 2 deletions _content/keyboards/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ protected static function LoadData() {

// Get Keyboard Metadata

$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . '/keyboard/' . rawurlencode(self::$id));
$s = Util::call_api_keyman_com('/keyboard/' . rawurlencode(self::$id), 'api.keyman.com-keyboard_sil_ipa.json');
if ($s === FALSE) {
// Will fail later in the script
self::$error .= error_get_last()['message'] . "\n";
Expand All @@ -396,7 +396,7 @@ protected static function LoadData() {

// Get Program Download Versions and URLs

$s = @file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/1.0');
$s = Util::call_downloads_keyman_com('/api/version/1.0', 'downloads.keyman.com-api_version_1.0.json');
if ($s === FALSE) {
// Will fail later in the script
self::$error .= error_get_last()['message'] . "\n";
Expand Down
3 changes: 2 additions & 1 deletion _content/keyboards/keyboard.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once _KEYMANCOM_INCLUDES . '/includes/servervars.php';
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

if(!isset($_REQUEST['id'])) {
header('HTTP/1.0 404 id parameter is required');
Expand All @@ -14,7 +15,7 @@
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

$kmw = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . "/cloud/4.0/keyboards/$id?version=$version&languageidtype=bcp47");
$kmw = Util::call_api_keyman_com("/cloud/4.0/keyboards/$id?version=$version&languageidtype=bcp47", 'api.keyman.com-cloud_4.0_keyboards_sil_ipa.json');
if($kmw === FALSE) {
header('HTTP/1.0 404 Keyboard not found');
exit;
Expand Down
3 changes: 2 additions & 1 deletion _content/keyboards/keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_once('./session.php');
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

if(isset($_REQUEST['legacy'])) {
$id = find_id_by_legacy(clean_id($_REQUEST['legacy']));
Expand All @@ -27,7 +28,7 @@ function clean_id($id) {
}

function find_id_by_legacy($legacy) {
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com.'/search/?q=k:legacy:'.rawurlencode($legacy));
$s = Util::call_api_keyman_com('/search/?q=k:legacy:'.rawurlencode($legacy), 'api.keyman.com-search.json');
if($s === FALSE) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion _content/keyboards/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Locale;
use Keyman\Site\com\keyman\Util;

Locale::definePageScope('LOCALE_KEYBOARDS_SHARE', 'keyboards/share');

Expand All @@ -24,7 +25,7 @@ function clean_id($id) {
}

function find_keyboard($id) {
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com.'/keyboard/'.$id);
$s = Util::call_api_keyman_com('/keyboard/'.$id, 'api.keyman.com-keyboard_sil_ipa.json');
if($s === FALSE) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion _includes/2020/KeymanDownloadVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Keyman\Site\com\keyman;

use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

class KeymanDownloadVersions {
private static $versions;
Expand All @@ -18,7 +19,7 @@ class KeymanDownloadVersions {

static function getDownloadUrls() {
if(empty(self::$versions))
self::$versions = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/2.0'));
self::$versions = @json_decode(Util::call_downloads_keyman_com('/api/version/2.0', 'downloads.keyman.com-api_version_2.0.json'));
return self::$versions;
}

Expand Down
3 changes: 2 additions & 1 deletion _includes/2020/KeymanWebHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Keyman\Site\com\keyman;
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Util;

class KeymanWebHost {
static function getKeymanWebUrlBase() {
$json = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com."/version/web");
$json = Util::call_api_keyman_com("/version/web", 'api.keyman.com-version_web.json');
if($json) {
$json = json_decode($json);
}
Expand Down
38 changes: 36 additions & 2 deletions _includes/2020/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ static function cdn($file) {
$cdn = false;
}
}
$use_cdn = KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_PRODUCTION ||
KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_STAGING ||
$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])) {
Expand All @@ -32,4 +32,38 @@ static function 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);
Comment thread
mcdurdin marked this conversation as resolved.
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);
}
}
4 changes: 4 additions & 0 deletions _includes/2020/templates/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ static function render($fields = []) {
echo "<script id='i18n_$domain' type='application/json'>[\n$localization\n]</script>\n";
}

echo "<script>\n";
KeymanHosts::Instance()->emitJavascriptConst();
echo "\n window.keymanHosts = keymanHosts;\n</script>";

array_unshift($fields->js,
Util::cdn('js/jquery1-11-1.min.js'),
Util::cdn('js/bowser.es5.2.9.0.min.js'),
Expand Down
6 changes: 2 additions & 4 deletions _includes/includes/ui/downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\com\keyman\Locale;
use Keyman\Site\com\keyman\Util;
Locale::definePageScope('LOCALE_DOWNLOADS', 'downloads');

$versions = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/2.0'));
//if($versions === NULL || $versions === FALSE) {
// echo "<p class='error'>WARNING: unable to retrieve latest versions of Keyman from download server</p>";
//}
$versions = @json_decode(Util::call_downloads_keyman_com('/api/version/2.0', 'downloads.keyman.com-api_version_2.0.json'));

function formatSizeUnits($bytes) {
if ($bytes >= 1073741824) {
Expand Down
9 changes: 5 additions & 4 deletions _includes/includes/ui/keyboard-details.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use \Keyman\Site\com\keyman\KeymanWebHost;
use \Keyman\Site\Common\KeymanHosts;
use \Keyman\Site\com\keyman\Locale;
use \Keyman\Site\com\keyman\Util;
use \Keyman\Site\com\keyman;

Locale::definePageScope('LOCALE_KEYBOARDS_DETAILS', 'keyboards/details');
Expand Down Expand Up @@ -192,10 +193,10 @@ protected static function LoadData() {
global $_m_Keyboards_Details;

self::$error = "";
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com. '/keyboard/' . rawurlencode(self::$id));
$s = Util::call_api_keyman_com('/keyboard/' . rawurlencode(self::$id), 'api.keyman.com-keyboard_sil_ipa.json');
if ($s === FALSE) {
// Will fail later in the script
self::$error .= error_get_last()['message'] . "\n";
self::$error .= "The keyboard was not found in the database\n"; //error_get_last()['message'] . "\n";
self::$title = $_m_Keyboards_Details("failed_to_load_keyboard_package", self::$id);
header('HTTP/1.0 404 Keyboard not found');
} else {
Expand Down Expand Up @@ -255,7 +256,7 @@ protected static function LoadData() {

self::$downloadCount = 0;
self::$totalDownloadCount = 0;
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com.'/search/2.0?q=k:id:' . rawurlencode(self::$id));
$s = Util::call_api_keyman_com('/search/2.0?q=k:id:' . rawurlencode(self::$id), 'api.keyman.com-search_2.0.json');
if ($s !== FALSE) {
$s = json_decode($s);
if(is_object($s) && is_array(($s->keyboards))) {
Expand Down Expand Up @@ -629,7 +630,7 @@ protected static function WriteKeyboardDetails() {
// TODO(lowpri): we could return this information in the API to avoid multiple
// round trip queries but that requires more changes to the API, docs, and
// schema.
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com.'/keyboard/' . rawurlencode($name));
$s = Util::call_api_keyman_com('/keyboard/' . rawurlencode($name), 'api.keyman.com-keyboard_sil_ipa.json');
if ($s === FALSE) {
echo "<span class='keyboard-unavailable' title='" .
$_m_Keyboards_Details("keyboard_not_available",
Expand Down
2 changes: 1 addition & 1 deletion cdn/dev/keyboard-search/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function wrapSearch(localCounter, updateHistory) {
return false;
}

var base = location.protocol+'//api.'+location.host; // this works on test sites as well as live, assuming we use the host pattern "keyman.com[.localhost]"
var base = window.keymanHosts.api;
var url = base+'/search/2.0?p='+page+'&q='+encodeURIComponent(stripCommonWords(q));

if(window.embed) {
Expand Down
2 changes: 1 addition & 1 deletion go/download/_download.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$WINDOWS_VERSION = $_REQUEST['version'];
$MAC_VERSION = $_REQUEST['version'];
} else {
$versions = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/2.0'));
$versions = @json_decode(Util::call_downloads_keyman_com('/api/version/2.0', 'downloads.keyman.com-api_version_2.0.json'));

$DEVELOPER_VERSION = $versions->developer->$TIER->version;
$WINDOWS_VERSION = $versions->windows->$TIER->version;
Expand Down
3 changes: 2 additions & 1 deletion go/package/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const DEBUG=0;

use Keyman\Site\com\keyman\KeymanComSentry;
use Keyman\Site\com\keyman\Util;
use Keyman\Site\com\keyman\Validation;
use Keyman\Site\Common\KeymanHosts;
use Keyman\Site\Common\JsonApiFailure;
Expand Down Expand Up @@ -42,7 +43,7 @@ public static function redirect_to_file($type, $id, $version, $platform, $tier,

if(empty($version)) {
// If version isn't provided, we'll query the live api for the version
$json = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . "/$type/" . rawurlencode($id));
$json = Util::call_api_keyman_com("/$type/" . rawurlencode($id), ($type == 'keyboard' ? 'api.keyman.com-keyboard_sil_ipa.json' : 'api.keyman.com-model_nrc.en.mtnt.json'));
if ($json !== FALSE) {
$json = json_decode($json);
}
Expand Down
Loading
Loading