Bugfixes Telegram
This commit is contained in:
@@ -1,155 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
function telegramHttpPostJson(string $url, array $payload): array
|
||||
{
|
||||
$jsonPayload = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if ($jsonPayload === false) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'status_code' => 0,
|
||||
'body' => null,
|
||||
'error' => 'Die Telegram-Anfrage konnte nicht als JSON kodiert werden.',
|
||||
];
|
||||
}
|
||||
|
||||
if (DIRECTORY_SEPARATOR === '\\' && function_exists('shell_exec')) {
|
||||
$urlBase64 = base64_encode($url);
|
||||
$payloadBase64 = base64_encode($jsonPayload);
|
||||
$powerShellScript = str_replace(
|
||||
['__URL_BASE64__', '__PAYLOAD_BASE64__'],
|
||||
[$urlBase64, $payloadBase64],
|
||||
<<<'PS'
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$url = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('__URL_BASE64__'))
|
||||
$body = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('__PAYLOAD_BASE64__'))
|
||||
try {
|
||||
$response = Invoke-RestMethod -Method Post -Uri $url -ContentType 'application/json' -Body $body
|
||||
[PSCustomObject]@{
|
||||
ok = $true
|
||||
body = ($response | ConvertTo-Json -Depth 20 -Compress)
|
||||
error = ''
|
||||
} | ConvertTo-Json -Compress
|
||||
} catch {
|
||||
$stream = $null
|
||||
$responseBody = ''
|
||||
if ($_.Exception.Response) {
|
||||
try {
|
||||
$stream = $_.Exception.Response.GetResponseStream()
|
||||
if ($stream) {
|
||||
$reader = New-Object System.IO.StreamReader($stream)
|
||||
$responseBody = $reader.ReadToEnd()
|
||||
}
|
||||
} catch {
|
||||
} finally {
|
||||
if ($stream) {
|
||||
$stream.Dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
[PSCustomObject]@{
|
||||
ok = $false
|
||||
body = $responseBody
|
||||
error = $_.Exception.Message
|
||||
} | ConvertTo-Json -Compress
|
||||
}
|
||||
PS
|
||||
);
|
||||
$encodedCommandBinary = function_exists('iconv')
|
||||
? iconv('UTF-8', 'UTF-16LE', $powerShellScript)
|
||||
: $powerShellScript;
|
||||
$encodedCommand = base64_encode($encodedCommandBinary === false ? $powerShellScript : $encodedCommandBinary);
|
||||
$command = 'powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand ' . $encodedCommand;
|
||||
$rawOutput = shell_exec($command);
|
||||
|
||||
if ($rawOutput !== null && trim($rawOutput) !== '') {
|
||||
$decodedShell = json_decode(trim($rawOutput), true);
|
||||
if (is_array($decodedShell)) {
|
||||
return [
|
||||
'ok' => (bool) ($decodedShell['ok'] ?? false),
|
||||
'status_code' => 0,
|
||||
'body' => $decodedShell['body'] ?? null,
|
||||
'error' => $decodedShell['error'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($jsonPayload),
|
||||
],
|
||||
CURLOPT_POSTFIELDS => $jsonPayload,
|
||||
CURLOPT_TIMEOUT => 20,
|
||||
]);
|
||||
|
||||
$body = curl_exec($ch);
|
||||
$curlError = curl_error($ch);
|
||||
$statusCode = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
||||
|
||||
if ($body === false) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'status_code' => $statusCode,
|
||||
'body' => null,
|
||||
'error' => $curlError !== '' ? $curlError : 'Die Telegram-Anfrage ist fehlgeschlagen.',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'ok' => true,
|
||||
'status_code' => $statusCode,
|
||||
'body' => $body,
|
||||
'error' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => implode("\r\n", [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($jsonPayload),
|
||||
]),
|
||||
'content' => $jsonPayload,
|
||||
'timeout' => 20,
|
||||
'ignore_errors' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
$body = @file_get_contents($url, false, $context);
|
||||
$statusCode = 0;
|
||||
$responseHeaders = function_exists('http_get_last_response_headers')
|
||||
? http_get_last_response_headers()
|
||||
: ($http_response_header ?? null);
|
||||
|
||||
if (is_array($responseHeaders)) {
|
||||
foreach ($responseHeaders as $headerLine) {
|
||||
if (preg_match('/^HTTP\/\S+\s+(\d{3})\b/', $headerLine, $matches) === 1) {
|
||||
$statusCode = (int) $matches[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($body === false) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'status_code' => $statusCode,
|
||||
'body' => null,
|
||||
'error' => 'Die Telegram-Anfrage ist fehlgeschlagen. Auf diesem Server ist moeglicherweise weder cURL noch der HTTPS-Stream-Wrapper verfuegbar.',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'ok' => true,
|
||||
'status_code' => $statusCode,
|
||||
'body' => $body,
|
||||
'error' => null,
|
||||
];
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
function telegramServiceFooter(): string
|
||||
{
|
||||
return 'Ein Service des Astronomiemuseums der Sternwarte Sonneberg';
|
||||
}
|
||||
|
||||
function appendTelegramServiceFooter(string $text): string
|
||||
{
|
||||
$footer = telegramServiceFooter();
|
||||
$trimmedText = trim($text);
|
||||
|
||||
if ($trimmedText === '') {
|
||||
return $footer;
|
||||
}
|
||||
|
||||
if (str_contains($trimmedText, $footer)) {
|
||||
return $trimmedText;
|
||||
}
|
||||
|
||||
return $trimmedText . "\n\n" . $footer;
|
||||
}
|
||||
@@ -9,7 +9,7 @@ declare(strict_types=1);
|
||||
* und Dokumenten. Die Cron-Tasks muessen dadurch keine Request-Details kennen.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/http.php';
|
||||
require_once dirname(__DIR__, 2) . '/public/telegram/lib/http.php';
|
||||
|
||||
|
||||
function telegramHttpPostMultipart(string $url, array $fields, string $fileField, string $filePath, ?string $fileName = null): array
|
||||
|
||||
Reference in New Issue
Block a user