157 lines
5.4 KiB
PHP
157 lines
5.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
if (!defined('IN_DA_PLUGIN') || IN_DA_PLUGIN !== true) {
|
|
http_response_code(403);
|
|
echo 'Forbidden';
|
|
exit;
|
|
}
|
|
|
|
if (!defined('PLUGIN_ACTION')) {
|
|
http_response_code(400);
|
|
echo 'Missing PLUGIN_ACTION';
|
|
exit;
|
|
}
|
|
|
|
define('PLUGIN_ROOT', dirname(__DIR__));
|
|
define('PLUGIN_EXEC_DIR', __DIR__);
|
|
define('PLUGIN_HANDLERS_DIR', __DIR__ . '/handlers');
|
|
|
|
$pluginErrorLog = PLUGIN_ROOT . '/error.log';
|
|
@ini_set('log_errors', '1');
|
|
@ini_set('error_log', $pluginErrorLog);
|
|
if (!is_file($pluginErrorLog)) {
|
|
@file_put_contents($pluginErrorLog, '');
|
|
}
|
|
|
|
register_shutdown_function(static function () use ($pluginErrorLog): void {
|
|
$last = error_get_last();
|
|
if ($last === null) {
|
|
return;
|
|
}
|
|
|
|
$fatalTypes = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
|
|
if (!in_array((int)$last['type'], $fatalTypes, true)) {
|
|
return;
|
|
}
|
|
|
|
$msg = sprintf(
|
|
"[%s] FATAL type=%d message=%s file=%s line=%d\n",
|
|
date('c'),
|
|
(int)$last['type'],
|
|
(string)$last['message'],
|
|
(string)$last['file'],
|
|
(int)$last['line']
|
|
);
|
|
@error_log($msg, 3, $pluginErrorLog);
|
|
});
|
|
|
|
try {
|
|
autoload_plugin_libs();
|
|
Http::bootstrapGlobals();
|
|
|
|
$settings = Settings::load(PLUGIN_ROOT . '/plugin-settings.conf');
|
|
AdminerRuntimeConfig::sync($settings, $pluginErrorLog);
|
|
$daUser = DirectAdminUser::fromEnvironment($settings);
|
|
|
|
if (!$daUser->hasPluginAccess()) {
|
|
http_response_code(403);
|
|
$langCode = $daUser->language();
|
|
$lang = Lang::load($daUser->language());
|
|
render_access_denied_message($lang, $daUser->skin(), $langCode);
|
|
exit;
|
|
}
|
|
|
|
$credentials = Settings::loadPostgresqlCredentials('/usr/local/directadmin/conf/postgresql.conf');
|
|
$pg = new PostgresService($credentials, $settings);
|
|
$pg->ensureMetadataSchema();
|
|
|
|
$secret = $settings->loadOrCreateSecret(PLUGIN_ROOT . '/data/secret.key');
|
|
$csrf = new CsrfGuard($secret, $daUser->username(), Http::server('SESSION_ID'));
|
|
|
|
$lang = Lang::load($daUser->language());
|
|
$ctx = new AppContext($daUser, $settings, $pg, $csrf, $lang);
|
|
|
|
$handlerFile = PLUGIN_HANDLERS_DIR . '/' . basename((string)PLUGIN_ACTION) . '.php';
|
|
if (!is_file($handlerFile)) {
|
|
throw new RuntimeException('Brak handlera dla akcji: ' . (string)PLUGIN_ACTION);
|
|
}
|
|
|
|
$handler = require $handlerFile;
|
|
if (!is_callable($handler)) {
|
|
throw new RuntimeException('Handler akcji nie jest wywoływalny: ' . (string)PLUGIN_ACTION);
|
|
}
|
|
|
|
$handler($ctx);
|
|
exit;
|
|
} catch (Throwable $e) {
|
|
$msg = sprintf(
|
|
"[%s] EXCEPTION class=%s message=%s file=%s line=%d action=%s\n",
|
|
date('c'),
|
|
get_class($e),
|
|
$e->getMessage(),
|
|
$e->getFile(),
|
|
$e->getLine(),
|
|
defined('PLUGIN_ACTION') ? (string)PLUGIN_ACTION : 'unknown'
|
|
);
|
|
@error_log($msg, 3, $pluginErrorLog);
|
|
@error_log("[stacktrace]\n" . $e->getTraceAsString() . "\n", 3, $pluginErrorLog);
|
|
|
|
http_response_code(500);
|
|
header('Content-Type: text/html; charset=UTF-8');
|
|
echo '<!doctype html><html><head><meta charset="utf-8"><title>Błąd pluginu</title></head><body>';
|
|
echo '<h1>Błąd pluginu PostgreSQL</h1>';
|
|
echo '<p>' . htmlspecialchars($e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</p>';
|
|
echo '<p>Sprawdź konfigurację i uprawnienia pluginu.</p>';
|
|
echo '</body></html>';
|
|
exit;
|
|
}
|
|
|
|
function autoload_plugin_libs(): void
|
|
{
|
|
$files = [
|
|
PLUGIN_EXEC_DIR . '/lib/Http.php',
|
|
PLUGIN_EXEC_DIR . '/lib/Settings.php',
|
|
PLUGIN_EXEC_DIR . '/lib/AdminerRuntimeConfig.php',
|
|
PLUGIN_EXEC_DIR . '/lib/DirectAdminUser.php',
|
|
PLUGIN_EXEC_DIR . '/lib/Lang.php',
|
|
PLUGIN_EXEC_DIR . '/lib/LocalizedException.php',
|
|
PLUGIN_EXEC_DIR . '/lib/CsrfGuard.php',
|
|
PLUGIN_EXEC_DIR . '/lib/NamePolicy.php',
|
|
PLUGIN_EXEC_DIR . '/lib/PostgresService.php',
|
|
PLUGIN_EXEC_DIR . '/lib/AdminerSso.php',
|
|
PLUGIN_EXEC_DIR . '/lib/BackupQueueService.php',
|
|
PLUGIN_EXEC_DIR . '/lib/FilePicker.php',
|
|
PLUGIN_EXEC_DIR . '/lib/AppContext.php',
|
|
];
|
|
|
|
foreach ($files as $file) {
|
|
require_once $file;
|
|
}
|
|
}
|
|
|
|
function render_access_denied_message(Lang $lang, string $skin, string $langCode): void
|
|
{
|
|
$path = PLUGIN_ROOT . '/user/' . strtolower(trim($skin)) . '.css';
|
|
if (!is_file($path)) {
|
|
$path = PLUGIN_ROOT . '/user/enhanced.css';
|
|
}
|
|
$css = is_file($path) ? (string)file_get_contents($path) : '';
|
|
|
|
$message = $lang->t('Bazy danych PostgreSQL nie są dostępne dla Twojego konta - skontaktuj się z pomocą techniczną aby uzyskać pomoc i więcej informacji');
|
|
$title = $lang->t('Błąd');
|
|
|
|
header('Content-Type: text/html; charset=UTF-8');
|
|
echo '<!doctype html><html lang="' . htmlspecialchars($langCode, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '"><head><meta charset="utf-8">';
|
|
echo '<meta name="viewport" content="width=device-width, initial-scale=1">';
|
|
echo '<title>' . htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</title>';
|
|
echo '<style>' . $css . '</style>';
|
|
echo '</head><body>';
|
|
echo '<div class="wrap">';
|
|
echo '<section class="main-section">';
|
|
echo '<div class="alert alert-err">' . htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</div>';
|
|
echo '</section>';
|
|
echo '</div>';
|
|
echo '</body></html>';
|
|
}
|