2018-10-31 16:44:34 +00:00
|
|
|
<?php
|
|
|
|
|
2019-08-29 19:40:56 +00:00
|
|
|
// If this script is called directly, it must be because of the downloader.
|
|
|
|
// If this script is to be called directly, it MUST have been called previously
|
|
|
|
// via build.php at least once first.
|
|
|
|
if(!isset($paths)) {
|
|
|
|
$paths = new stdClass();
|
|
|
|
$paths->extra_data_directory = "build/._extra_data";
|
|
|
|
}
|
2020-05-09 14:38:39 +00:00
|
|
|
$theme_id = getenv("PEPPERMINT_THEME");
|
|
|
|
if(strlen($theme_id) == 0) $theme_id = "default";
|
2019-08-28 23:14:10 +00:00
|
|
|
|
2019-08-28 22:55:45 +00:00
|
|
|
if(isset($_GET["determine-latest-version"])) {
|
|
|
|
header("content-type: application/json");
|
|
|
|
exit(json_encode([
|
|
|
|
"latest_version" => trim(file_get_contents("https://raw.githubusercontent.com/sbrl/Pepperminty-Wiki/master/version")),
|
|
|
|
"local_version" => trim(file_get_contents("version"))
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2019-03-02 12:24:11 +00:00
|
|
|
/**
|
|
|
|
* Logs a string to stdout, but only on the CLI.
|
|
|
|
* @param string $line The line to log.
|
|
|
|
*/
|
2019-03-02 13:32:12 +00:00
|
|
|
function log_str(string $line) {
|
2019-03-02 12:24:11 +00:00
|
|
|
if(php_sapi_name() == "cli")
|
|
|
|
echo($line);
|
|
|
|
//else error_log($line);
|
2018-10-31 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 23:32:10 +00:00
|
|
|
/*
|
|
|
|
███ ███ ██████ ██████ ██ ██ ██ ███████
|
|
|
|
████ ████ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██ ████ ██ ██ ██ ██ ██ ██ ██ ██ █████
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██ ██ ██████ ██████ ██████ ███████ ███████
|
|
|
|
|
|
|
|
██ ██████ █████ ██████ ██ ███ ██ ██████
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
|
|
|
|
██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ███
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
███████ ██████ ██ ██ ██████ ██ ██ ████ ██████
|
|
|
|
*/
|
2020-05-09 14:38:39 +00:00
|
|
|
log_str("Building with theme '$theme_id'.\n");
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("*** Beginning main build sequence ***\n");
|
|
|
|
log_str("Reading in module index...\n");
|
2019-03-02 12:24:11 +00:00
|
|
|
|
2019-08-28 23:14:10 +00:00
|
|
|
if(isset($_GET["modules"]))
|
|
|
|
$requested_modules = explode(",", $_GET["modules"]);
|
|
|
|
|
2018-10-31 16:44:34 +00:00
|
|
|
$module_index = json_decode(file_get_contents("module_index.json"));
|
|
|
|
$module_list = [];
|
2019-08-28 23:14:10 +00:00
|
|
|
foreach($module_index as $module) {
|
|
|
|
/*
|
|
|
|
* If:
|
|
|
|
* - The module is optional
|
|
|
|
* - ...AND we're on the command line
|
|
|
|
* - ...AND the module isn't specified in the CLI arguments
|
|
|
|
* - ...AND the special keyword "all" isn't specified in the CLI arguments
|
|
|
|
* ....then skip it.
|
|
|
|
*/
|
|
|
|
if(
|
|
|
|
php_sapi_name() == "cli" &&
|
|
|
|
$module->optional &&
|
2018-10-31 16:44:34 +00:00
|
|
|
(
|
|
|
|
isset($argv) &&
|
|
|
|
strrpos(implode(" ", $argv), $module->id) === false &&
|
|
|
|
!in_array("all", $argv)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
continue;
|
2019-08-28 23:14:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If:
|
|
|
|
* - We're NOT on the command line
|
|
|
|
* - ...AND the module isn't requested
|
|
|
|
* ...then skip it.
|
|
|
|
*/
|
|
|
|
if(php_sapi_name() != "cli" && !in_array($module->id, $requested_modules))
|
|
|
|
continue;
|
|
|
|
|
2019-02-26 23:01:09 +00:00
|
|
|
$module_list[] = $module;
|
2018-10-31 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 23:32:10 +00:00
|
|
|
/*
|
|
|
|
██████ ███████ ██████ ███████ ███ ██ ██████ ███████ ███ ██ ██████ ██ ██
|
|
|
|
██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ████ ██ ██ ██ ██
|
|
|
|
██ ██ █████ ██████ █████ ██ ██ ██ ██ ██ █████ ██ ██ ██ ██ ████
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██████ ███████ ██ ███████ ██ ████ ██████ ███████ ██ ████ ██████ ██
|
|
|
|
|
|
|
|
███████ ██████ █████ ███ ██ ███ ██ ██ ███ ██ ██████
|
|
|
|
██ ██ ██ ██ ████ ██ ████ ██ ██ ████ ██ ██
|
|
|
|
███████ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
███████ ██████ ██ ██ ██ ████ ██ ████ ██ ██ ████ ██████
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
function module_list_search(array $list, string $id) : bool {
|
|
|
|
foreach($list as $item) {
|
|
|
|
if($item->id === $id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
function module_list_find(array $list, string $id) {
|
|
|
|
foreach($list as $item) {
|
|
|
|
if($item->id === $id)
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_str("Scanning for dependencies...\n");
|
|
|
|
$module_count = count($module_list);
|
|
|
|
for($i = 0; $i < $module_count; $i++) {
|
|
|
|
foreach($module_list[$i]->depends as $dependency) {
|
2020-03-15 17:54:27 +00:00
|
|
|
log_str("scanning {$module_list[$i]->id}: $dependency\n");
|
2020-03-11 23:32:10 +00:00
|
|
|
if(!module_list_search($module_list, $dependency)) {
|
2020-03-15 17:54:27 +00:00
|
|
|
log_str("Adding missing dependency $dependency for {$module_list[$i]->id}\n\n");
|
2020-03-11 23:32:10 +00:00
|
|
|
$missing_dependency = module_list_find($module_index, $dependency);
|
|
|
|
if($missing_dependency == null) {
|
|
|
|
if(php_sapi_name() != "cli") header("content-type: text/plain");
|
|
|
|
echo("Error: {$module_list[$i]->id} requires $dependency as a dependency, but it couldn't be found in the module index. This looks like a bug.\n");
|
|
|
|
if(php_sapi_name() == "cli") exit(2);
|
|
|
|
else exit();
|
|
|
|
}
|
|
|
|
$module_list[] = $missing_dependency;
|
|
|
|
$module_count++;
|
|
|
|
}
|
2020-03-15 17:54:27 +00:00
|
|
|
else {
|
|
|
|
log_str("present, no action needed\n");
|
|
|
|
}
|
2020-03-11 23:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 12:24:11 +00:00
|
|
|
if(php_sapi_name() != "cli") {
|
2018-10-31 16:44:34 +00:00
|
|
|
header("content-type: text/php");
|
|
|
|
header("content-disposition: attachment; filename=\"index.php\"");
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:32:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
██████ ██████ ██████ ███████
|
|
|
|
██ ██ ██ ██ ██ ██
|
|
|
|
██ ██ ██ ██████ █████
|
|
|
|
██ ██ ██ ██ ██ ██
|
|
|
|
██████ ██████ ██ ██ ███████
|
|
|
|
*/
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("Reading in core files...\n");
|
2018-10-31 16:44:34 +00:00
|
|
|
|
2019-03-02 21:13:41 +00:00
|
|
|
$core_files_list = glob("core/*.php"); natsort($core_files_list);
|
|
|
|
|
|
|
|
$core = "<?php\n";
|
|
|
|
foreach($core_files_list as $core_filename)
|
|
|
|
$core .= str_replace([ "<?php", "?>" ], "", file_get_contents($core_filename));
|
|
|
|
|
|
|
|
|
2018-10-31 16:44:34 +00:00
|
|
|
$core = str_replace([
|
|
|
|
"{version}",
|
|
|
|
"{commit}",
|
|
|
|
"{guiconfig}",
|
|
|
|
"{default-css}"
|
|
|
|
], [
|
|
|
|
trim(file_get_contents("version")),
|
|
|
|
exec("git rev-parse HEAD"),
|
|
|
|
trim(file_get_contents("peppermint.guiconfig.json")),
|
2019-11-04 00:52:34 +00:00
|
|
|
trim(file_get_contents("themes/$theme_id/theme.css"))
|
2018-10-31 16:44:34 +00:00
|
|
|
], $core);
|
|
|
|
|
|
|
|
$result = $core;
|
|
|
|
|
2020-03-11 23:32:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
██████ █████ ██████ ██ ██ ███████ ██████
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██████ ███████ ██ █████ █████ ██████
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██ ██ ██ ██████ ██ ██ ███████ ██ ██
|
|
|
|
*/
|
|
|
|
|
2019-02-26 23:01:09 +00:00
|
|
|
$extra_data_archive = new ZipArchive();
|
2019-03-02 16:45:34 +00:00
|
|
|
// Use dev/shm if possible (it's *always* in memory). PHP will default to the system's temporary directory if it's not available
|
|
|
|
$temp_filename = tempnam("/dev/shm", "pepperminty-wiki-pack");
|
|
|
|
if($extra_data_archive->open($temp_filename, ZipArchive::CREATE) !== true) {
|
2019-02-26 23:01:09 +00:00
|
|
|
http_response_code(503);
|
|
|
|
exit("Error: Failed to create temporary stream to store packing information");
|
|
|
|
}
|
|
|
|
|
2019-02-26 19:48:04 +00:00
|
|
|
$module_list_count = count($module_list);
|
|
|
|
$i = 1;
|
2019-02-26 23:01:09 +00:00
|
|
|
foreach($module_list as $module)
|
2018-10-31 16:44:34 +00:00
|
|
|
{
|
2019-02-26 23:01:09 +00:00
|
|
|
if($module->id == "") continue;
|
2018-10-31 16:44:34 +00:00
|
|
|
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("[$i / $module_list_count] Adding $module->id \r");
|
2018-10-31 16:44:34 +00:00
|
|
|
|
2019-02-26 23:01:09 +00:00
|
|
|
$module_filepath = "modules/" . preg_replace("[^a-zA-Z0-9\-]", "", $module->id) . ".php";
|
2018-10-31 16:44:34 +00:00
|
|
|
|
2019-03-02 13:32:12 +00:00
|
|
|
//log_str("id: $module->id | filepath: $module_filepath\n");
|
2018-10-31 16:44:34 +00:00
|
|
|
|
2019-02-26 19:48:04 +00:00
|
|
|
if(!file_exists($module_filepath)) {
|
2018-10-31 16:44:34 +00:00
|
|
|
http_response_code(400);
|
|
|
|
exit("Failed to load module with name: $module_filepath");
|
|
|
|
}
|
|
|
|
|
2019-02-26 23:01:09 +00:00
|
|
|
// Pack the module's source code
|
2018-10-31 16:44:34 +00:00
|
|
|
$modulecode = file_get_contents($module_filepath);
|
|
|
|
$modulecode = str_replace([ "<?php", "?>" ], "", $modulecode);
|
|
|
|
$result = str_replace(
|
|
|
|
"// %next_module% //",
|
|
|
|
"$modulecode\n// %next_module% //",
|
2019-02-26 19:48:04 +00:00
|
|
|
$result
|
|
|
|
);
|
|
|
|
|
2019-02-26 23:01:09 +00:00
|
|
|
|
2019-03-02 13:32:35 +00:00
|
|
|
// Pack the extra files that were downloaded in build.php
|
2019-02-26 23:01:09 +00:00
|
|
|
foreach($module->extra_data as $filepath_pack => $extra_data_item) {
|
|
|
|
if(is_string($extra_data_item)) {
|
|
|
|
// TODO: Test whether this works for urls. If not, then we'll need to implement a workaround
|
2019-08-29 19:40:56 +00:00
|
|
|
log_str("\n[pack] $paths->extra_data_directory/$module->id/$filepath_pack -> $module->id/$filepath_pack\n");
|
2019-03-02 13:32:12 +00:00
|
|
|
$extra_data_archive->addFile("$paths->extra_data_directory/$module->id/$filepath_pack", "$module->id/$filepath_pack");
|
2019-02-26 23:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 19:48:04 +00:00
|
|
|
$i++;
|
2018-10-31 16:44:34 +00:00
|
|
|
}
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("\n");
|
2018-10-31 16:44:34 +00:00
|
|
|
|
2019-03-02 16:45:34 +00:00
|
|
|
$extra_data_archive->close();
|
|
|
|
|
2020-03-11 23:32:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
███████ ██ ███ ██ ██ ███████ ██ ██ ███████ ██████
|
|
|
|
██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
█████ ██ ██ ██ ██ ██ ███████ ███████ █████ ██████
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
██ ██ ██ ████ ██ ███████ ██ ██ ███████ ██ ██
|
|
|
|
*/
|
|
|
|
|
2019-03-02 16:45:34 +00:00
|
|
|
$archive_stream = fopen($temp_filename, "r");
|
2019-03-02 12:24:11 +00:00
|
|
|
|
|
|
|
$output_stream = null;
|
|
|
|
if(php_sapi_name() == "cli") {
|
2019-02-26 19:48:04 +00:00
|
|
|
if(file_exists("build/index.php")) {
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("index.php already exists in the build folder, exiting\n");
|
2018-10-31 16:44:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-03-02 12:24:11 +00:00
|
|
|
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("Done. Saving to disk...");
|
2019-03-02 12:24:11 +00:00
|
|
|
$output_stream = fopen("build/index.php", "w");
|
2019-03-02 13:32:12 +00:00
|
|
|
log_str("complete!\n");
|
|
|
|
log_str("*** Build completed! ***\n");
|
2018-10-31 16:44:34 +00:00
|
|
|
}
|
2019-02-26 19:48:04 +00:00
|
|
|
else {
|
2019-03-02 12:24:11 +00:00
|
|
|
$output_stream = fopen("php://output", "w");
|
2018-10-31 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 16:47:22 +00:00
|
|
|
// Write the built code
|
2019-03-02 12:24:11 +00:00
|
|
|
fwrite($output_stream, $result);
|
2019-03-02 16:47:22 +00:00
|
|
|
// Write the delimiter
|
|
|
|
fwrite($output_stream, "__halt_compiler();");
|
|
|
|
// Write the extra data
|
2019-03-02 12:24:11 +00:00
|
|
|
stream_copy_to_stream($archive_stream, $output_stream);
|
|
|
|
|
2019-03-02 21:59:35 +00:00
|
|
|
// Cleanup
|
|
|
|
unlink($temp_filename);
|
2018-10-31 16:44:34 +00:00
|
|
|
?>
|