2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
global $utopia, $register, $request, $response, $projectDB, $project, $user, $audit;
|
|
|
|
|
2020-06-23 09:55:45 +12:00
|
|
|
use Appwrite\Database\Document;
|
2020-06-24 03:01:20 +12:00
|
|
|
use Appwrite\Utopia\Response;
|
2019-05-09 18:54:39 +12:00
|
|
|
use Utopia\App;
|
|
|
|
use Utopia\Locale\Locale;
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
|
2019-12-17 08:35:33 +13:00
|
|
|
include_once __DIR__ . '/../shared/api.php';
|
2019-11-30 07:26:06 +13:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
$utopia->get('/v1/locale')
|
|
|
|
->desc('Get User Locale')
|
|
|
|
->label('scope', 'locale.read')
|
2020-01-27 19:14:14 +13:00
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2020-01-31 05:18:46 +13:00
|
|
|
->label('sdk.method', 'get')
|
2019-10-08 20:09:35 +13:00
|
|
|
->label('sdk.description', '/docs/references/locale/get-locale.md')
|
2019-05-09 18:54:39 +12:00
|
|
|
->action(
|
2019-09-07 05:10:41 +12:00
|
|
|
function () use ($response, $request, $utopia) {
|
2019-12-17 08:35:33 +13:00
|
|
|
$eu = include __DIR__.'/../../config/eu.php';
|
|
|
|
$currencies = include __DIR__.'/../../config/currencies.php';
|
2020-01-24 08:46:03 +13:00
|
|
|
$reader = new Reader(__DIR__.'/../../db/DBIP/dbip-country-lite-2020-01.mmdb');
|
2019-09-07 05:10:41 +12:00
|
|
|
$output = [];
|
|
|
|
$ip = $request->getIP();
|
|
|
|
$time = (60 * 60 * 24 * 45); // 45 days cache
|
|
|
|
$countries = Locale::getText('countries');
|
2019-05-09 18:54:39 +12:00
|
|
|
$continents = Locale::getText('continents');
|
|
|
|
|
2020-06-19 11:07:41 +12:00
|
|
|
if (App::MODE_TYPE_PRODUCTION !== $utopia->getMode()) {
|
2019-09-07 05:10:41 +12:00
|
|
|
$ip = '79.177.241.94';
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
$output['ip'] = $ip;
|
|
|
|
|
|
|
|
$currency = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$record = $reader->country($ip);
|
|
|
|
$output['countryCode'] = $record->country->isoCode;
|
|
|
|
$output['country'] = (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : Locale::getText('locale.country.unknown');
|
|
|
|
//$output['countryTimeZone'] = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $record->country->isoCode);
|
|
|
|
$output['continent'] = (isset($continents[$record->continent->code])) ? $continents[$record->continent->code] : Locale::getText('locale.country.unknown');
|
|
|
|
$output['continentCode'] = $record->continent->code;
|
2020-06-20 23:20:49 +12:00
|
|
|
$output['eu'] = (\in_array($record->country->isoCode, $eu)) ? true : false;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-23 09:55:45 +12:00
|
|
|
foreach ($currencies as $element) {
|
2020-06-20 23:20:49 +12:00
|
|
|
if (isset($element['locations']) && isset($element['code']) && \in_array($record->country->isoCode, $element['locations'])) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$currency = $element['code'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$output['currency'] = $currency;
|
2019-09-07 05:10:41 +12:00
|
|
|
} catch (\Exception $e) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$output['countryCode'] = '--';
|
|
|
|
$output['country'] = Locale::getText('locale.country.unknown');
|
|
|
|
$output['continent'] = Locale::getText('locale.country.unknown');
|
|
|
|
$output['continentCode'] = '--';
|
|
|
|
$output['eu'] = false;
|
|
|
|
$output['currency'] = $currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response
|
2019-09-07 05:10:41 +12:00
|
|
|
->addHeader('Cache-Control', 'public, max-age='.$time)
|
2020-06-20 23:20:49 +12:00
|
|
|
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $time).' GMT') // 45 days cache
|
2020-06-23 09:55:45 +12:00
|
|
|
;
|
|
|
|
|
2020-06-24 03:01:20 +12:00
|
|
|
$response->dynamic(new Document($output), Response::MODEL_LOCALE);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$utopia->get('/v1/locale/countries')
|
|
|
|
->desc('List Countries')
|
|
|
|
->label('scope', 'locale.read')
|
2020-01-27 19:14:14 +13:00
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
|
|
|
->label('sdk.method', 'getCountries')
|
2019-10-09 21:40:02 +13:00
|
|
|
->label('sdk.description', '/docs/references/locale/get-countries.md')
|
2019-05-09 18:54:39 +12:00
|
|
|
->action(
|
2019-10-21 21:14:32 +13:00
|
|
|
function () use ($response) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$list = Locale::getText('countries'); /* @var $list array */
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\asort($list);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
$response->json($list);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$utopia->get('/v1/locale/countries/eu')
|
|
|
|
->desc('List EU Countries')
|
|
|
|
->label('scope', 'locale.read')
|
2020-01-27 19:14:14 +13:00
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
|
|
|
->label('sdk.method', 'getCountriesEU')
|
2019-10-08 20:09:35 +13:00
|
|
|
->label('sdk.description', '/docs/references/locale/get-countries-eu.md')
|
2019-05-09 18:54:39 +12:00
|
|
|
->action(
|
2019-09-07 05:10:41 +12:00
|
|
|
function () use ($response) {
|
|
|
|
$countries = Locale::getText('countries'); /* @var $countries array */
|
2019-12-17 08:35:33 +13:00
|
|
|
$eu = include __DIR__.'/../../config/eu.php';
|
2019-09-07 05:10:41 +12:00
|
|
|
$list = [];
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
foreach ($eu as $code) {
|
2020-06-20 23:20:49 +12:00
|
|
|
if (\array_key_exists($code, $countries)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$list[$code] = $countries[$code];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\asort($list);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
$response->json($list);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$utopia->get('/v1/locale/countries/phones')
|
|
|
|
->desc('List Countries Phone Codes')
|
|
|
|
->label('scope', 'locale.read')
|
2020-01-27 19:14:14 +13:00
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
|
|
|
->label('sdk.method', 'getCountriesPhones')
|
2019-10-08 20:09:35 +13:00
|
|
|
->label('sdk.description', '/docs/references/locale/get-countries-phones.md')
|
2019-05-09 18:54:39 +12:00
|
|
|
->action(
|
2019-09-07 05:10:41 +12:00
|
|
|
function () use ($response) {
|
2019-12-17 08:35:33 +13:00
|
|
|
$list = include __DIR__.'/../../config/phones.php'; /* @var $list array */
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:10:41 +12:00
|
|
|
$countries = Locale::getText('countries'); /* @var $countries array */
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
foreach ($list as $code => $name) {
|
2020-06-20 23:20:49 +12:00
|
|
|
if (\array_key_exists($code, $countries)) {
|
2020-03-30 09:10:33 +13:00
|
|
|
$list[$code] = '+'.$list[$code];
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\asort($list);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
$response->json($list);
|
|
|
|
}
|
2019-08-19 22:31:06 +12:00
|
|
|
);
|
|
|
|
|
2019-10-21 21:14:32 +13:00
|
|
|
$utopia->get('/v1/locale/continents')
|
2020-04-13 20:18:09 +12:00
|
|
|
->desc('List Continents')
|
2019-10-21 21:14:32 +13:00
|
|
|
->label('scope', 'locale.read')
|
2020-01-27 19:14:14 +13:00
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
2019-10-21 21:14:32 +13:00
|
|
|
->label('sdk.namespace', 'locale')
|
|
|
|
->label('sdk.method', 'getContinents')
|
|
|
|
->label('sdk.description', '/docs/references/locale/get-continents.md')
|
|
|
|
->action(
|
|
|
|
function () use ($response) {
|
|
|
|
$list = Locale::getText('continents'); /* @var $list array */
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\asort($list);
|
2019-10-21 21:14:32 +13:00
|
|
|
|
|
|
|
$response->json($list);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2019-08-19 22:31:06 +12:00
|
|
|
$utopia->get('/v1/locale/currencies')
|
2019-11-10 18:24:51 +13:00
|
|
|
->desc('List Currencies')
|
2019-08-19 22:31:06 +12:00
|
|
|
->label('scope', 'locale.read')
|
2020-01-27 19:14:14 +13:00
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
2019-08-19 22:31:06 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
|
|
|
->label('sdk.method', 'getCurrencies')
|
2019-10-08 20:09:35 +13:00
|
|
|
->label('sdk.description', '/docs/references/locale/get-currencies.md')
|
2019-08-19 22:31:06 +12:00
|
|
|
->action(
|
2019-09-07 05:10:41 +12:00
|
|
|
function () use ($response) {
|
2019-12-17 08:35:33 +13:00
|
|
|
$currencies = include __DIR__.'/../../config/currencies.php';
|
2019-08-19 22:31:06 +12:00
|
|
|
|
|
|
|
$response->json($currencies);
|
|
|
|
}
|
2019-09-07 05:10:41 +12:00
|
|
|
);
|
2020-05-25 00:20:32 +12:00
|
|
|
|
|
|
|
|
|
|
|
$utopia->get('/v1/locale/languages')
|
|
|
|
->desc('List Languages')
|
|
|
|
->label('scope', 'locale.read')
|
|
|
|
->label('sdk.platform', [APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER])
|
|
|
|
->label('sdk.namespace', 'locale')
|
|
|
|
->label('sdk.method', 'getLanguages')
|
|
|
|
->label('sdk.description', '/docs/references/locale/get-languages.md')
|
|
|
|
->action(
|
|
|
|
function () use ($response) {
|
2020-05-25 00:31:48 +12:00
|
|
|
$languages = include __DIR__.'/../../config/languages.php';
|
2020-05-25 00:20:32 +12:00
|
|
|
|
2020-05-25 00:31:48 +12:00
|
|
|
$response->json($languages);
|
2020-05-25 00:20:32 +12:00
|
|
|
}
|
|
|
|
);
|