devsuite/cds/actions/Crispage/DevSuite/PackagerAction.php

134 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2024-05-27 00:44:14 -04:00
<?php
namespace Crispage\DevSuite;
defined("ROOT") or die();
use \Crispage\Auth\CorePermissions;
use \Crispage\ApplicationConfig;
use \Crispage\Utils\URIUtils;
class PackagerAction extends \Crispage\Framework\Action {
public static function getExtensionInfo(): array {
return [
"id" => "crispage.devsuite.actions.packager",
"version" => VERSION,
"package" => "crispage.devsuite"
];
}
public function __construct(\Crispage $app, array $data) {
parent::__construct($app, $data);
}
public function run(): void {
$this->app->page->data["title"] = $this->app->i18n->translate("{%CDS_PACKAGER}");
if (!$this->app->auth->userHasPermission(
CorePermissions::MANAGE_EXTENSIONS
)) {
$this->app->page->setPersistentMessage(
"unauthorized", "Unauthorized", "danger"
);
$this->app->page->actionFinished();
};
$mode = strval($this->app->request->params["mode"] ?? "");
$package = strval(
$this->app->request->params["package"]
?? ApplicationConfig::get("cds.default_info")["id"]
);
switch ($mode) {
case "build": {
$version = strval(
$this->app->request->params["version"]
?? ApplicationConfig::get("cds.default_info")["version"]
);
$pdata = parse_ini_string(
strval($this->app->request->params["pdata"] ?? ""),
false, \INI_SCANNER_TYPED
);
$exts = $this->app->request->params["exts"] ?? [];
if (!is_array($exts)) $exts = [];
$exts = array_filter(
array_map([$this->app->extensions, "get"], $exts)
);
$cdirs = $this->app->request->params["cdirs"] ?? [];
if (!is_array($cdirs)) $cdirs = [];
$files = explode("\n", strval(
$this->app->request->params["files"] ?? ""
));
$dscripts = boolval($this->app->request->params["dscripts"] ?? "0");
$dplugins = boolval($this->app->request->params["dplugins"] ?? "0");
$ppath = $this->app->cds->packager->buildPackage(
$package, ["version" => $version],
$pdata, $exts, $cdirs, $files, $dscripts, $dplugins
);
$arclink = URIUtils::iuri("/backend", [
"route" => "cds_packager",
"mode" => "archive",
"package" => $package
]);
$this->app->page->data["messages"]["success"] = [
"color" => "success",
"content" => $this->app->i18n->translate("{%CDS_PACKAGE_SUCCESS}", $ppath, $arclink)
];
break;
}
case "archive": {
$path = $this->app->cds->packager->targzPackage($package);
if (!file_exists($path)) {
$this->app->data->messages["nofile"] = [
"color" => "danger",
"content" => $this->app->translate("{%FILE_DOES_NOT_EXIST}")
];
break;
}
$this->app->page->stopBuffering(false);
$this->app->page->template->setLayout("api", "default");
header("Content-Type: application/x-compressed-tar");
header("Content-Length: " . filesize($path));
header("Content-Disposition: attachment; filename=\"" . basename($path) . "\"");
header("Cache-Control: must-revalidate");
header("Expires: 0");
readfile($path);
$this->app->page->actionFinished(false);
$this->app->cds->disableLogPrint();
return;
}
}
$dirs = [["/user/etc", "etc"]];
foreach (ApplicationConfig::get("crispage.paths.asset") as $path)
$dirs[] = [$path, "asset"];
foreach (ApplicationConfig::get("crispage.paths.action") as $path)
$dirs[] = [$path, "action"];
foreach (ApplicationConfig::get("crispage.paths.component") as $path)
$dirs[] = [$path, "component"];
foreach (ApplicationConfig::get("crispage.paths.plugin") as $path)
$dirs[] = [$path, "plugin"];
foreach (ApplicationConfig::get("crispage.paths.template") as $path)
$dirs[] = [$path, "template"];
foreach (ApplicationConfig::get("crispage.paths.translation") as $path)
$dirs[] = [$path, "translation"];
$com_main = $this->app->page->createComponent(
"\\Crispage\\DevSuite\\PackagerComponent",
[
"extensions" => $this->app->cds->getExtensions(),
"codedirs" => $dirs
]
);
$this->app->page->setMainComponent($com_main);
$this->app->page->actionFinished();
}
}
?>