Telegram neu organisiert

This commit is contained in:
Eskimue
2026-04-08 13:14:40 +02:00
parent 7dcefbe215
commit 19c19f0444
6 changed files with 161 additions and 319 deletions
+1 -118
View File
@@ -9,123 +9,7 @@ declare(strict_types=1);
* und Dokumenten. Die Cron-Tasks muessen dadurch keine Request-Details kennen.
*/
function telegramHttpPostJson(string $url, array $payload): array
{
$jsonPayload = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if ($jsonPayload === false) {
return [
'ok' => false,
'error' => 'Die Telegram-Anfrage konnte nicht als JSON kodiert werden.',
'body' => null,
];
}
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),
'error' => $decodedShell['error'] ?? null,
'body' => $decodedShell['body'] ?? 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);
$error = curl_error($ch);
curl_close($ch);
return [
'ok' => $body !== false,
'error' => $body === false ? $error : null,
'body' => $body !== false ? $body : 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);
return [
'ok' => $body !== false,
'error' => $body === false ? 'HTTP-Request fehlgeschlagen.' : null,
'body' => $body !== false ? $body : null,
];
}
require_once __DIR__ . '/http.php';
function telegramHttpPostMultipart(string $url, array $fields, string $fileField, string $filePath, ?string $fileName = null): array
@@ -217,7 +101,6 @@ PS
$body = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return [
'ok' => $body !== false,