<?php namespace App\Controller;
use AdScore\Common\Definition\Signature;
use Symfony\Component\HttpFoundation\{Request, Response, JsonResponse};
use Symfony\Component\Routing\Annotation\Route;
use App\Component\{Notification, Siv};
use AdScore\Traffic\Utils\GeoIp;
use GuzzleHttp\{Client, RequestOptions};
use Psr\Log\LoggerInterface;
/**
* Environment and resources entrypoint
* @TODO: Conforms to 2019.11 ASAPI standards
*/
class SystemController extends Controller {
const CURRENCY_OVERRIDES = [
'USD' => ['priority' => 3, 'name' => 'United states dollar'],
'EUR' => ['priority' => 2],
'GBP' => ['priority' => 1]
];
/**
* @Route("/", name="default")
*/
public function defaultAction() {
return new JsonResponse(
['status' => 'ok',]
);
}
/**
* @Route("/list/environment", name="list_environment", methods="GET")
*/
public function listEnvironmentAction() {
$geoData = GeoIp::examine();
if (empty($geoData['geo.country']) && isset($_SERVER['HTTP_CF_IPCOUNTRY']))
$geoData['geo.country'] = $_SERVER['HTTP_CF_IPCOUNTRY'];
return new JsonResponse(
['status' => 'ok', 'country' => $geoData['geo.country'], 'time_zone' => $geoData['geo.timezone']]
);
}
/**
* @Route("/list/currency", name="list_currency", methods="GET")
*/
public function listCurrencyAction(LoggerInterface $logger) {
$client = new Client();
$response = $client->get('https://restcountries.com/v3.1/all');
$data = json_decode($response->getBody(), true);
$output = [];
$overrides = self::CURRENCY_OVERRIDES;
foreach ($data as $country) {
foreach (($country['currencies'] ?? []) as $currencyCode => $currency) {
$currency['code'] = $currencyCode;
if ( (!isset($currency['code']) || !isset($currency['name'])) || ($currency['code'] == '(none)') || ($currency['name'] == '(none)') || ($currency['name'][0] == '[') ) {
continue;
}
if (!isset($currency['symbol']) || ($currency['symbol'] == '(none)')) {
$currency['symbol'] = null;
}
if (isset($output[$currency['code']])) {
if (!in_array($country['cca2'], $output[$currency['code']]['countries'])) {
$output[$currency['code']]['countries'][] = $country['cca2'];
}
} else {
$output[$currency['code']] = [
'code' => $currency['code'],
'name' => mb_convert_case($overrides[$currency['code']]['name'] ?? $currency['name'], MB_CASE_TITLE, 'UTF-8'),
'symbol' => $currency['symbol'] ?? null,
'countries' => [$country['cca2']],
'priority' => $overrides[$currency['code']]['priority'] ?? 0
];
}
}
}
$output = array_values($output);
usort($output, function($a, $b) {
if ($a['priority'] !== $b['priority']) {
return ($b['priority'] - $a['priority']);
} else {
return strcmp($a['code'], $b['code']);
}
});
foreach ($output as &$item) {
unset($item['countries']);
unset($item['priority']);
}
return new JsonResponse(
['status' => 'ok', 'currency' => $output]
);
}
/**
* @Route("/list/time_zone", name="list_time_zone", methods="GET")
*/
public function listTimeZoneAction() {
return new JsonResponse(
['status' => 'ok', 'time_zone' => timezone_identifiers_list()]
);
}
/**
* @Route("/support/message", name="support_message", methods="POST")
*/
public function supportMessageAction(Request $request) {
$errors = [];
$data = $request->getContent();
if (Siv::jsonParseStrict($data, ['email', 'name', 'subject', 'content'], [], $errors)) {
Siv::email($data['email'], 'email', $errors);
Siv::notEmpty($data['name'], 'name', $errors);
Siv::notEmpty($data['subject'], 'subject', $errors);
Siv::notEmpty($data['content'], 'content', $errors);
}
if (!empty($errors))
return new JsonResponse(['status' => 'error', 'errors' => $errors], JsonResponse::HTTP_BAD_REQUEST);
Notification::sendSingle($data['subject'], $data['content'], [['name' => 'Support Form', 'email' => 'support@adscore.com']], ['name' => $data['name'], 'email' => $data['email']]);
return new JsonResponse(['status' => 'ok', 'message' => $data], JsonResponse::HTTP_OK);
}
/**
* @Route("/list/zone_fields", name="zone_list_fields", methods="GET")
*/
public function listZoneFieldsAction() {
return $this->jsonOkResponse(['fields' => Signature::getFieldsRef()]);
}
}