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
+155
View File
@@ -0,0 +1,155 @@
<?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,
];
}