This repository has been archived on 2024-08-28. You can view files and clone it, but cannot push or open issues or pull requests.
satori-services/public/translate.php
2022-07-04 00:07:38 +00:00

157 lines
4.7 KiB
PHP

<?php
ini_set('display_errors', 'on');
error_reporting(-1);
define('TL_FORMAT', strtolower($_GET['fmt'] ?? 'json'));
if (isset($_SERVER['HTTP_USER_AGENT'], $_GET['pretty']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/') !== false)
define('JSON_ARGS', JSON_PRETTY_PRINT);
else
define('JSON_ARGS', 0);
function out($arr, $mode) {
$output = '';
$mimeType = 'text/plain';
switch (TL_FORMAT) {
case 'xml':
$mimeType = 'application/xml';
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
switch ($mode) {
case 'list':
$type = get_class($arr[0]);
$output .= "<ArrayOf{$type}>";
foreach ($arr as $part) {
$output .= "<{$type} ";
foreach (get_object_vars($part) as $name => $value) {
$output .= "{$name}=\"{$value}\" ";
}
$output .= '/>';
}
$output .= "</ArrayOf{$type}>";
break;
case 'do':
$output .= "<Translation ";
foreach ($arr as $name => $value) {
$value = htmlspecialchars($value);
$output .= "{$name}=\"{$value}\" ";
}
$output .= '/>';
break;
}
break;
case 'json':
default:
$mimeType = 'application/json';
$output = json_encode($arr, JSON_ARGS);
break;
}
header("Content-Type: {$mimeType}; charset=utf-8");
die($output);
}
define('TRANSLATE_URL', 'https://translate.google.com/translate_a/single?ie=UTF-8&oe=UTF-8&multires=1&client=gtx&sl=%s&tl=%s&dt=t&q=%s');
define('CURL_USER_AGENT', 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36');
define('MAX_TRANSL_LENGTH', 630);
function curl_req($url)
{
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => CURL_USER_AGENT,
]);
return curl_exec($curl);
}
class Language {
public $code;
public $name;
public function __construct($code, $name)
{
$this->code = $code;
$this->name = $name;
}
public static function fromCode($code)
{
return new Language($code, locale_get_display_name($code));
}
}
$languages = [
'af', 'sq', 'ar', 'hy', 'az', 'eu', 'be', 'bn', 'bs',
'bg', 'ca', 'ny', 'co', 'hr', 'cs', 'da', 'nl', 'en',
'eo', 'tl', 'fi', 'fr', 'fy', 'gl', 'ka', 'de', 'el',
'gu', 'ht', 'ha', 'iw', 'hi', 'hu', 'is', 'ig', 'id',
'ga', 'ja', 'jw', 'kn', 'kk', 'km', 'ko', 'ku', 'ky',
'lo', 'la', 'lv', 'lt', 'lb', 'mk', 'mg', 'ms', 'ml',
'mt', 'mi', 'mr', 'mn', 'my', 'ne', 'no', 'ps', 'fa',
'pl', 'pt', 'ma', 'ro', 'ru', 'sm', 'gd', 'sr', 'st',
'sn', 'sd', 'si', 'sk', 'sl', 'so', 'es', 'su', 'sw',
'sv', 'tg', 'ta', 'te', 'th', 'tr', 'uk', 'ur', 'uz',
'vi', 'cy', 'xh', 'yi', 'yo', 'zu', 'it',
'ceb', 'haw', 'hmn',
'zh-cn', 'zh-tw',
];
$mode = $_GET['m'] ?? null;
switch ($mode)
{
case 'list':
$out = [];
foreach ($languages as $lang)
$out[] = Language::fromCode($lang);
out($out, $mode);
case 'do':
$from = strtolower($_GET['f'] ?? 'auto');
$to = strtolower($_GET['t'] ?? 'en');
if ($from !== 'auto' && !in_array($from, $languages))
out(['error' => 'The given origin language is invalid.'], $mode);
if (!in_array($to, $languages))
out(['error' => 'The given destination language is invalid.'], $mode);
$text = isset($_GET['z']) ? $_GET['z'] : file_get_contents('php://input');
$t_len = strlen($text);
if ($t_len < 1)
out(['error' => 'Nothing to translate.'], $mode);
if ($t_len > MAX_TRANSL_LENGTH)
out(['error' => 'Too much text, please split some up.'], $mode);
$google_txt = curl_req(sprintf(TRANSLATE_URL, $from, $to, rawurlencode($text)));
$google = json_decode($google_txt);
if (strlen($google_txt) < 1 || !$google)
out(['error' => 'Something happened.'], $mode);
$result = $google[0][0][0];
$original = $google[0][0][1];
$from = $google[2];
$from_text = locale_get_display_name($from);
$to_text = locale_get_display_name($to);
out(compact('from', 'from_text', 'to', 'to_text', 'original', 'result'), $mode);
}