| Server IP : 188.114.96.3 / Your IP : 216.73.216.143 Web Server : Apache System : Linux sv058.home.net.pl 6.8.2 husak_dirty_pages_fix #1 SMP PREEMPT_DYNAMIC Tue Apr 2 22:06:51 CEST 2024 x86_64 User : ( 3194769) PHP Version : 8.5.12 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /polana/includes/ |
Upload File : |
<?php
declare(strict_types=1);
/**
* TKLM Translate V3: klient PHP -> https://translate.tklm.pl.
* Wywoluj TYLKO po stronie serwera (np. z panelu administracyjnego / zadania cron).
* Nigdy nie ujawniaj klucza w HTML, JavaScript, JSON ani przegladarce.
*/
final class TklmTranslateClient
{
private string $key;
private string $endpoint = 'https://translate.tklm.pl';
public function __construct(string $key)
{
if (!preg_match('/^[a-f0-9]{48}$/', $key)) {
throw new InvalidArgumentException('Nieprawidlowy klucz API TKLM');
}
if (!extension_loaded('curl')) {
throw new RuntimeException('Brak rozszerzenia cURL w PHP');
}
$this->key = $key;
}
public function languages(): array
{
return $this->request('/languages', null);
}
public function translate(string $text, string $source = 'pl', string $target = 'en'): array
{
$text = trim($text);
if ($text === '' || strlen($text) > 50000) {
throw new InvalidArgumentException('Tekst musi miec 1-50000 bajtow');
}
if (!preg_match('/^(auto|[a-z]{2,3}(?:-[A-Za-z0-9]+)?)$/', $source) ||
!preg_match('/^[a-z]{2,3}(?:-[A-Za-z0-9]+)?$/', $target)) {
throw new InvalidArgumentException('Niepoprawny kod jezyka');
}
return $this->request('/translate', [
'q' => $text, 'source' => $source, 'target' => $target, 'format' => 'text'
]);
}
public function detect(string $text): array
{
if (trim($text) === '' || strlen($text) > 50000) {
throw new InvalidArgumentException('Niepoprawna dlugosc tekstu');
}
return $this->request('/detect', ['q' => $text]);
}
private function request(string $path, ?array $data): array
{
$ch = curl_init($this->endpoint . $path);
if ($ch === false) {
throw new RuntimeException('Nie udalo sie uruchomic cURL');
}
$opts = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 120,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => [
'X-TKLM-Translate-Key: ' . $this->key,
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
],
];
if (defined('CURLOPT_PROTOCOLS_STR')) {
$opts[CURLOPT_PROTOCOLS_STR] = 'https';
} elseif (defined('CURLOPT_PROTOCOLS')) {
$opts[CURLOPT_PROTOCOLS] = CURLPROTO_HTTPS;
}
if ($data !== null) {
$opts[CURLOPT_POST] = true;
$opts[CURLOPT_POSTFIELDS] = http_build_query($data, '', '&', PHP_QUERY_RFC3986);
}
curl_setopt_array($ch, $opts);
$body = curl_exec($ch);
$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($body === false) {
$err = curl_error($ch);
curl_close($ch);
throw new RuntimeException('Polaczenie Translate nieudane: ' . $err);
}
curl_close($ch);
if ($code !== 200) {
throw new RuntimeException('API Translate zwrocilo HTTP ' . $code);
}
$json = json_decode($body, true);
if (!is_array($json)) {
throw new RuntimeException('Niepoprawny JSON z Translate');
}
return $json;
}
}