false, 'error' => 'Необходима авторизация.']); exit; } $currentNS = str_contains($ID, ':') ? substr($ID, 0, (int)strrpos($ID, ':')) : ''; if ($mediaManager->upload($currentNS)) { $uploadedName = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $_FILES['uploadfile']['name']); $nsPrefix = $currentNS ? $currentNS . ':' : ''; $wikiTag = '{{:' . $nsPrefix . $uploadedName . '|}}'; echo json_encode(['success' => true, 'tag' => $wikiTag]); exit; } else { echo json_encode(['success' => false, 'error' => 'Ошибка загрузки.']); exit; } } // 2. ИСПРАВЛЕНО: АСИНХРОННЫЙ API ЭНДПОИНТ ДЛЯ ЖИВОГО ПРЕДПРОСМОТРА (В самом верху до фильтрации) if ($ACT === 'api_preview' && $_SERVER['REQUEST_METHOD'] === 'POST') { header('Content-Type: text/html; charset=UTF-8'); $textToParse = isset($_POST['wikitext']) ? (string)$_POST['wikitext'] : ''; require_once WIKI_ROOT . 'src/Parser.php'; $parser = new Parser(); // Отдаем только чистый отрендеренный текст и намертво прерываем выполнение echo $parser->render($textToParse); exit; } // ========================================================================= // Обработка авторизации if ($ACT === 'login' && $_SERVER['REQUEST_METHOD'] === 'POST') { $username = isset($_POST['u']) ? (string)$_POST['u'] : ''; $password = isset($_POST['p']) ? (string)$_POST['p'] : ''; if (auth_login($username, $password)) { header('Location: ?id=' . urlencode($ID)); exit; } } // Обработка выхода if ($ACT === 'logout') { auth_logout(); header('Location: ?id=' . urlencode($ID)); exit; } // Сохранение вики-страницы if ($ACT === 'save' && $_SERVER['REQUEST_METHOD'] === 'POST') { if (!auth_is_logged()) { die('Ошибка доступа.'); } $textToSave = $_POST['wikitext'] ?? ''; if ($wikiStorage->save($ID, $textToSave)) { header('Location: ?id=' . urlencode($ID) . '&do=show'); exit; } } // Стандартная загрузка медиафайлов (из вкладки "Медиафайлы") if ($ACT === 'upload' && $_SERVER['REQUEST_METHOD'] === 'POST') { if (!auth_is_logged()) { die('Ошибка доступа.'); } $currentNS = str_contains($ID, ':') ? substr($ID, 0, (int)strrpos($ID, ':')) : ''; if ($mediaManager->upload($currentNS)) { header('Location: ?id=' . urlencode($ID) . '&do=media'); exit; } } // Стандартное удаление медиафайлов if ($ACT === 'delete') { if (!auth_is_logged()) { die('Ошибка доступа.'); } $currentNS = str_contains($ID, ':') ? substr($ID, 0, (int)strrpos($ID, ':')) : ''; $fileToDelete = isset($_GET['file']) ? (string)$_GET['file'] : ''; if (!empty($fileToDelete)) { $mediaManager->delete($currentNS, $fileToDelete); } header('Location: ?id=' . urlencode($ID) . '&do=media'); exit; } // Массив легальных команд (Добавлено действие api_upload) $allowed_actions = ['show', 'edit', 'save', 'media', 'upload', 'delete', 'login', 'api_upload', 'api_preview']; if (!in_array($ACT, $allowed_actions, true)) { $ACT = 'show'; } require_once WIKI_ROOT . 'src/Template.php'; if (file_exists(WIKI_TPL . 'main.php')) { include WIKI_TPL . 'main.php'; } else { die("Критическая ошибка движка: Главный файл шаблона не найден."); }