crispage/package.php
2024-09-06 22:19:41 -04:00

209 lines
4.8 KiB
PHP

<?php
/*
Crispage CMS
crispycat <cc@crispy.cat>
https://crispy.cat/software/crispage
Crispage is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
use \Crispage\Auth\CorePermissions;
use \Crispage\Request\Request;
use \Crispage\Extensions\Extension;
require_once __DIR__ . "/core/init.php";
define("IS_INSTALLED", file_exists(ROOT . "/config.php"));
error_reporting(\Crispage\ApplicationConfig::get("crispage.dev.error_level"));
require_once ROOT . "/core/app/Crispage.php";
$Crispage = new \Crispage();
$Crispage->request = Request::getRequest();
if (IS_INSTALLED) {
$Crispage->loadDatabase();
$Crispage->connectDatabase();
$Crispage->init();
$Crispage->auth->refreshSession();
if (!$Crispage->auth->userHasPermission(CorePermissions::MANAGE_EXTENSIONS)) {
http_response_code(401);
die("Unauthorized");
}
}
ob_start();
$Crispage->packages->log("Crispage package manager v" . VERSION, $Crispage->packages::L_INFO);
$count = $Crispage->cache->initializeCache();
$Crispage->packages->log("Cleared $count cache entries");
$mode = strval($Crispage->request->params["mode"] ?? "script");
switch ($mode) {
case "install": {
if (IS_INSTALLED) {
http_response_code(409);
ob_end_flush();
$Crispage->packages->log("Crispage is already installed", $Crispage->packages::L_FATAL);
break;
}
$core = new Extension(
"crispage.core", VERSION, "crispage.core",
"crispage.core", "package", []
);
define("PACKAGE", $core);
define("PACKAGE_PATH", $Crispage->packages->getPath($core));
$Crispage->packages->runScript($core, "install");
if ($Crispage->packages->status() != $Crispage->packages::SUCCESS)
http_response_code(500);
ob_end_flush();
break;
}
case "upload": {
if (!IS_INSTALLED) {
http_response_code(409);
ob_end_flush();
$Crispage->packages->log("Crispage is not installed", $Crispage->packages::L_FATAL);
break;
}
$package = $Crispage->request->files["package"] ?? [];
if (!$package) {
http_response_code(400);
ob_end_flush();
$Crispage->packages->log("No package uploaded", $Crispage->packages::L_FATAL);
break;
}
$ext = $Crispage->packages->upload($package);
define("PACKAGE", $ext);
if ($Crispage->packages->status() == $Crispage->packages::SUCCESS) {
$Crispage->extensions->register($ext);
define("PACKAGE_PATH", $Crispage->packages->getPath($ext));
?>
<form action="<?php echo WROOT; ?>/package.php" method="get">
<input type="hidden" name="mode" value="script" />
<input type="hidden" name="package" value="<?php echo $ext->id; ?>" />
<input type="hidden" name="script" value="install" />
<button type="submit">&rarr; Install package</button>
</form>
<?php
}
else http_response_code(500);
ob_end_flush();
break;
}
case "script": {
if (!IS_INSTALLED) {
http_response_code(409);
ob_end_flush();
$Crispage->packages->log("Crispage is not installed", $Crispage->packages::L_FATAL);
break;
}
$package = strval($Crispage->request->params["package"] ?? "default");
$script = strval($Crispage->request->params["script"] ?? "default");
$ext = $Crispage->extensions->get($package);
define("PACKAGE", $ext);
if (!$ext) {
http_response_code(404);
ob_end_flush();
$Crispage->packages->log("Package $package does not exist", $Crispage->packages::L_FATAL);
break;
}
define("PACKAGE_PATH", $Crispage->packages->getPath($ext));
$Crispage->packages->runScript($ext, $script, true);
if ($Crispage->packages->status() != $Crispage->packages::SUCCESS)
http_response_code(500);
ob_end_flush();
break;
}
default: {
http_response_code(400);
ob_end_flush();
die("Invalid mode");
}
}
if ($mode != "install") {
?>
<form action="<?php echo WROOT; ?>/backend" method="get">
<input type="hidden" name="route" value="extensions/list" />
<button type="submit">&larr; Extension List</button>
</form>
<?php
}
?>
<style>
body {
font-family: "Verdana", "Tahoma", sans-serif;
background: #ddd;
border: 2px solid #007;
padding: 8px;
margin: 8px auto;
max-width: 960px;
}
body:before {
content: "Crispage Package Manager";
display: block;
background: #007;
color: #fff;
padding: 6px;
margin: -8px -8px 4px;
}
button {
border: 2px outset;
background: #def;
}
button:hover, button:active {
background: #fed;
}
button:active {
border: 2px inset;
}
form label {
display: inline-block;
width: 200px;
}
form input {
width: 200px;
}
form input[type=checkbox], form input[type=radio] {
width: 16px;
margin-left: 180px;
}
</style>