Upload via GUI (34 Dateien)
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Sprachwahl der Verwaltung (Deutsch ist die Quellsprache, English kommt aus lang/en.json).
|
||||
*
|
||||
* Übersetzt wird die fertige Seite, so wie bei DeskHub selbst: Textstücke zwischen den HTML-Tags und die Attribute placeholder, title,
|
||||
* alt, aria-label und data-confirm. Ein Schlüssel ist der deutsche Text (Leerraum zählt nicht); "{}" steht für einen wechselnden Teil
|
||||
* und "{#}" für eine Zahl; in der Übersetzung stehen "{1}", "{2}" für die gefundenen Teile (oder "{}" / "{#}" der Reihe nach).
|
||||
* Was nicht übersetzt ist, bleibt deutsch. Inhalte in <code>, <pre>, <script> und <style> bleiben unverändert.
|
||||
*
|
||||
* Die Sprache steht in einem Cookie (site_lang); ?lang=en oder ?lang=de schaltet um.
|
||||
*/
|
||||
|
||||
const SITE_LANGS = ['de' => 'Deutsch', 'en' => 'English'];
|
||||
|
||||
/** Gewählte Sprache: ?lang= (wird im Cookie gemerkt), sonst das Cookie, sonst Deutsch. */
|
||||
function site_lang(): string
|
||||
{
|
||||
static $lang = null;
|
||||
if ($lang !== null) return $lang;
|
||||
$lang = 'de';
|
||||
$asked = (string) ($_GET['lang'] ?? '');
|
||||
if (isset(SITE_LANGS[$asked])) {
|
||||
$lang = $asked;
|
||||
if (!headers_sent()) {
|
||||
setcookie('site_lang', $asked, ['expires' => time() + 365 * 86400, 'path' => '/', 'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off', 'httponly' => true, 'samesite' => 'Strict']);
|
||||
}
|
||||
} elseif (isset(SITE_LANGS[(string) ($_COOKIE['site_lang'] ?? '')])) {
|
||||
$lang = (string) $_COOKIE['site_lang'];
|
||||
}
|
||||
return $lang;
|
||||
}
|
||||
|
||||
/** @return array{exact:array<string,string>,patterns:list<array{0:string,1:string}>} */
|
||||
function site_i18n_dictionary(string $lang): array
|
||||
{
|
||||
static $cache = [];
|
||||
if (isset($cache[$lang])) return $cache[$lang];
|
||||
$exact = [];
|
||||
$patterns = [];
|
||||
$file = dirname(__DIR__) . '/lang/' . $lang . '.json';
|
||||
$data = $lang !== 'de' && preg_match('/^[a-z]{2}$/', $lang) && is_file($file) ? json_decode((string) file_get_contents($file), true) : null;
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $de => $tr) {
|
||||
if (!is_string($de) || !is_string($tr)) continue;
|
||||
$key = site_i18n_norm($de);
|
||||
if (strpos($key, '{}') === false && strpos($key, '{#}') === false) {
|
||||
$exact[$key] = $tr;
|
||||
continue;
|
||||
}
|
||||
$quoted = preg_quote($key, '#');
|
||||
$regex = '#^' . str_replace(['\{\}', '\{\#\}'], ['(.+?)', '(\d+)'], $quoted) . '$#su';
|
||||
$patterns[] = [$regex, $tr];
|
||||
}
|
||||
// Längere (genauere) Muster zuerst
|
||||
usort($patterns, fn($a, $b) => strlen($b[0]) <=> strlen($a[0]));
|
||||
}
|
||||
return $cache[$lang] = ['exact' => $exact, 'patterns' => $patterns];
|
||||
}
|
||||
|
||||
function site_i18n_norm(string $s): string
|
||||
{
|
||||
return trim((string) preg_replace('/\s+/u', ' ', $s));
|
||||
}
|
||||
|
||||
/** Übersetzt einen (entkodierten) Text oder gibt null zurück, wenn es keine Übersetzung gibt. */
|
||||
function site_i18n_lookup(string $norm, array $dict, int $depth = 0): ?string
|
||||
{
|
||||
if ($norm === '') return null;
|
||||
if (isset($dict['exact'][$norm])) return $dict['exact'][$norm];
|
||||
foreach ($dict['patterns'] as [$regex, $tr]) {
|
||||
if (!preg_match($regex, $norm, $m)) continue;
|
||||
array_shift($m);
|
||||
// Gefundene Teile selbst übersetzen, wenn möglich (zum Beispiel der Seitenname im Titel)
|
||||
if ($depth < 2) foreach ($m as $i => $part) $m[$i] = site_i18n_lookup(site_i18n_norm($part), $dict, $depth + 1) ?? $part;
|
||||
if (preg_match('/\{\d+\}/', $tr)) {
|
||||
return (string) preg_replace_callback('/\{(\d+)\}/', fn($x) => $m[(int) $x[1] - 1] ?? '', $tr);
|
||||
}
|
||||
$i = 0;
|
||||
return (string) preg_replace_callback('/\{#?\}/', function () use (&$i, $m) { return $m[$i++] ?? ''; }, $tr);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Übersetzt eine fertige HTML-Seite. */
|
||||
function site_translate_html(string $html, string $lang): string
|
||||
{
|
||||
$dict = site_i18n_dictionary($lang);
|
||||
if (!$dict['exact'] && !$dict['patterns']) return $html;
|
||||
$h = static fn(string $s): string => htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
$parts = preg_split('/(<!--.*?-->|<[^<>]*>)/su', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
$skip = 0;
|
||||
$out = '';
|
||||
foreach ($parts as $i => $part) {
|
||||
if ($part === '') continue;
|
||||
if ($i % 2 === 1) {
|
||||
if (preg_match('/^<\s*(\/?)\s*(script|style|code|pre)\b/i', $part, $m)) {
|
||||
$skip = max(0, $skip + ($m[1] === '/' ? -1 : (substr($part, -2) === '/>' ? 0 : 1)));
|
||||
}
|
||||
if ($part[1] !== '!' && $part[1] !== '/') {
|
||||
$part = preg_replace_callback('/\b(placeholder|title|alt|aria-label|data-confirm)="([^"]*)"/i', function ($m) use ($dict, $h) {
|
||||
$t = site_i18n_lookup(site_i18n_norm(html_entity_decode($m[2], ENT_QUOTES | ENT_HTML5, 'UTF-8')), $dict);
|
||||
return $t === null ? $m[0] : $m[1] . '="' . $h($t) . '"';
|
||||
}, $part) ?? $part;
|
||||
}
|
||||
$out .= $part;
|
||||
continue;
|
||||
}
|
||||
if ($skip > 0) { $out .= $part; continue; }
|
||||
$decoded = html_entity_decode($part, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$norm = site_i18n_norm($decoded);
|
||||
$t = site_i18n_lookup($norm, $dict);
|
||||
if ($t === null) { $out .= $part; continue; }
|
||||
preg_match('/^\s*/u', $part, $lead);
|
||||
preg_match('/\s*$/u', $part, $trail);
|
||||
$out .= ($lead[0] ?? '') . htmlspecialchars($t, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . ($trail[0] ?? '');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** Startet die Übersetzung der folgenden Ausgabe (nur bei einer anderen Sprache als Deutsch). */
|
||||
function site_i18n_start(): void
|
||||
{
|
||||
static $started = false;
|
||||
if ($started) return;
|
||||
$lang = site_lang();
|
||||
if ($lang === 'de') return;
|
||||
$started = true;
|
||||
ob_start(fn(string $buffer) => site_translate_html($buffer, $lang));
|
||||
}
|
||||
|
||||
/** Umschalter für den Seitenkopf: die jeweils andere Sprache als Link (behält die übrigen Angaben der Adresse). */
|
||||
function site_lang_switcher(): string
|
||||
{
|
||||
$current = site_lang();
|
||||
$query = $_GET;
|
||||
$out = '';
|
||||
foreach (SITE_LANGS as $code => $name) {
|
||||
if ($code === $current) {
|
||||
$out .= '<span class="lang on" aria-current="true">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</span>';
|
||||
continue;
|
||||
}
|
||||
$query['lang'] = $code;
|
||||
$out .= '<a class="lang" href="?' . htmlspecialchars(http_build_query($query), ENT_QUOTES, 'UTF-8') . '" hreflang="' . $code . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>';
|
||||
}
|
||||
return '<span class="langs">' . $out . '</span>';
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
require_once __DIR__ . '/lib.php';
|
||||
require_once __DIR__ . '/i18n.php';
|
||||
|
||||
function site_headers(): void
|
||||
{
|
||||
@@ -17,6 +18,7 @@ function site_tabs(): array
|
||||
'overview' => ['Übersicht', '◧'],
|
||||
'packages' => ['Pakete', '▣'],
|
||||
'apps' => ['Apps', '⬇'],
|
||||
'server' => ['DeskHub-Update', '⟳'],
|
||||
'push' => ['Push-Dienst', '🔔'],
|
||||
'settings' => ['Einstellungen', '⚙'],
|
||||
];
|
||||
@@ -25,9 +27,10 @@ function site_tabs(): array
|
||||
function site_header(string $title, string $active = '', bool $nav = false): void
|
||||
{
|
||||
site_headers();
|
||||
site_i18n_start(); // Sprachwahl: übersetzt die folgende Ausgabe (Deutsch bleibt unverändert)
|
||||
$name = site_config()['site_name'];
|
||||
?><!doctype html>
|
||||
<html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<html lang="<?= site_h(site_lang()) ?>"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#0f6f6a">
|
||||
<title><?= site_h($title) ?> · <?= site_h($name) ?></title>
|
||||
<link rel="icon" href="assets/favicon.svg" type="image/svg+xml">
|
||||
@@ -45,6 +48,7 @@ function site_header(string $title, string $active = '', bool $nav = false): voi
|
||||
<?php endforeach; ?>
|
||||
</nav>
|
||||
<?php endif; ?>
|
||||
<?= site_lang_switcher() ?>
|
||||
</div></header>
|
||||
<main class="wrap">
|
||||
<?php
|
||||
|
||||
@@ -116,6 +116,7 @@ function site_app_platforms(): array
|
||||
'minecraft' => ['label' => 'Minecraft', 'what' => 'Plugin (JAR)', 'ext' => 'jar', 'magic' => 'PK', 'file' => 'DeskHub-Minecraft.jar', 'mime' => 'application/java-archive'],
|
||||
'wordpress' => ['label' => 'WordPress', 'what' => 'Plugin (ZIP)', 'ext' => 'zip', 'magic' => 'PK', 'file' => 'deskhub-tickets-wordpress.zip', 'mime' => 'application/zip'],
|
||||
'chrome' => ['label' => 'Chrome', 'what' => 'Erweiterung (ZIP)', 'ext' => 'zip', 'magic' => 'PK', 'file' => 'deskhub-chrome-erweiterung.zip', 'mime' => 'application/zip'],
|
||||
'server' => ['label' => 'DeskHub-Server', 'what' => 'System-Update (ZIP des Projekts)', 'ext' => 'zip', 'magic' => 'PK', 'file' => 'deskhub-update.zip', 'mime' => 'application/zip'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -142,6 +143,8 @@ function site_zip_app_version(string $platform, string $zipPath): ?string
|
||||
if ($platform === 'chrome' && preg_match('#^(?:[^/]+/)?manifest\.json$#', $name)) {
|
||||
$data = json_decode((string) $zip->getFromIndex($i), true);
|
||||
if (is_array($data) && isset($data['manifest_version'], $data['version'])) $found = (string) $data['version'];
|
||||
} elseif ($platform === 'server' && preg_match('#^(?:[^/]+/)?version\.php$#', $name)) {
|
||||
if (preg_match("/define\s*\(\s*'DESKHUB_VERSION'\s*,\s*'(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)'/", (string) $zip->getFromIndex($i, 8192), $m)) $found = $m[1];
|
||||
} elseif ($platform === 'wordpress' && preg_match('#\.php$#i', $name)) {
|
||||
$head = (string) $zip->getFromIndex($i, 8192);
|
||||
if (preg_match('/^[ \/*#@]*Plugin Name:/mi', $head) && preg_match('/^[ \/*#@]*Version:\s*([0-9][0-9A-Za-z.+-]*)/mi', $head, $m)) $found = $m[1];
|
||||
@@ -164,6 +167,25 @@ function site_app_release(string $platform): ?array
|
||||
return $rows[0];
|
||||
}
|
||||
|
||||
/** Signatur für das System-Update (Nachricht wie includes/update_security.php). Leer, wenn kein Signierschlüssel eingerichtet ist. */
|
||||
function site_sign_update(string $version, string $sha256): string
|
||||
{
|
||||
$key = site_signing_key();
|
||||
if (!$key) return '';
|
||||
openssl_sign("deskhub-update\n" . ltrim(trim($version), 'vV') . "\n" . strtolower($sha256), $sig, $key, OPENSSL_ALGO_SHA256);
|
||||
return base64_encode($sig);
|
||||
}
|
||||
|
||||
/** Alle Versionen einer Plattform, neueste zuerst. */
|
||||
function site_app_versions(string $platform): array
|
||||
{
|
||||
$stmt = site_db()->prepare('SELECT * FROM app_releases WHERE platform = ?');
|
||||
$stmt->execute([$platform]);
|
||||
$rows = $stmt->fetchAll();
|
||||
usort($rows, fn($a, $b) => version_compare($b['version'], $a['version']) ?: ((int) $b['id'] <=> (int) $a['id']));
|
||||
return $rows;
|
||||
}
|
||||
|
||||
function site_app_releases(): array
|
||||
{
|
||||
$rows = site_db()->query('SELECT * FROM app_releases')->fetchAll();
|
||||
@@ -181,10 +203,10 @@ function site_publish_app(string $platform, string $tmp, string $version, string
|
||||
if ($size < 1024 || $size > SITE_MAX_APP_BYTES) return ['ok' => false, 'error' => 'Die Datei muss zwischen 1 KB und 300 MB groß sein.'];
|
||||
$head = (string) file_get_contents($tmp, false, null, 0, 2);
|
||||
if ($head !== $kind['magic']) return ['ok' => false, 'error' => 'Dateityp passt nicht zur gewählten Plattform.'];
|
||||
if (in_array($platform, ['wordpress', 'chrome'], true)) {
|
||||
// Version steht im Paket (Plugin-Kopf bzw. manifest.json); eine eingetippte Nummer hat Vorrang
|
||||
if (in_array($platform, ['wordpress', 'chrome', 'server'], true)) {
|
||||
// Version steht im Paket (Plugin-Kopf, manifest.json bzw. version.php); eine eingetippte Nummer hat Vorrang
|
||||
$inZip = site_zip_app_version($platform, $tmp);
|
||||
if ($inZip === null) return ['ok' => false, 'error' => $platform === 'wordpress' ? 'Das ist kein WordPress-Plugin: In einer PHP-Datei fehlt der Kopf mit "Plugin Name:" und "Version:".' : 'Das ist keine Chrome-Erweiterung: manifest.json mit "version" fehlt.'];
|
||||
if ($inZip === null) return ['ok' => false, 'error' => ['wordpress' => 'Das ist kein WordPress-Plugin: In einer PHP-Datei fehlt der Kopf mit "Plugin Name:" und "Version:".', 'chrome' => 'Das ist keine Chrome-Erweiterung: manifest.json mit "version" fehlt.', 'server' => 'Das ist kein DeskHub-Paket: version.php mit DESKHUB_VERSION fehlt.'][$platform]];
|
||||
if ($version === '') $version = $inZip;
|
||||
}
|
||||
if (!preg_match('/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/', $version)) return ['ok' => false, 'error' => 'Bitte eine Versionsnummer wie 3.4.0 eingeben.'];
|
||||
|
||||
Reference in New Issue
Block a user