2018-10-31 16:44:34 +00:00
|
|
|
<?php
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-31 16:44:34 +00:00
|
|
|
$module_index = json_decode(file_get_contents("module_index.json"));
|
|
|
|
$module_list = [];
|
|
|
|
foreach($module_index as $module)
|
|
|
|
{
|
|
|
|
// If the module is optional, the module's id isn't present in the command line arguments, and the special 'all' module id wasn't passed in, skip it
|
|
|
|
if($module->optional &&
|
|
|
|
(
|
|
|
|
isset($argv) &&
|
|
|
|
strrpos(implode(" ", $argv), $module->id) === false &&
|
|
|
|
!in_array("all", $argv)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
continue;
|
2019-02-26 23:01:09 +00:00
|
|
|
$module_list[] = $module;
|
2018-10-31 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($_GET["modules"]))
|
|
|
|
$module_list = explode(",", $_GET["modules"]);
|
|
|
|
|
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\"");
|
|
|
|
}
|
|
|
|
|
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 12:24:11 +00:00
|
|
|
// We trim from the end here because of the __halt_compiler() directive
|
|
|
|
$core = rtrim(file_get_contents("core.php"));
|
2018-10-31 16:44:34 +00:00
|
|
|
$settings = file_get_contents("settings.fragment.php");
|
|
|
|
$settings = str_replace([ "<?php", "?>" ], "", $settings);
|
|
|
|
$core = str_replace([
|
|
|
|
"//{settings}",
|
|
|
|
"{version}",
|
|
|
|
"{commit}",
|
|
|
|
"{guiconfig}",
|
|
|
|
"{default-css}"
|
|
|
|
], [
|
|
|
|
$settings,
|
|
|
|
trim(file_get_contents("version")),
|
|
|
|
exec("git rev-parse HEAD"),
|
|
|
|
trim(file_get_contents("peppermint.guiconfig.json")),
|
|
|
|
trim(file_get_contents("theme_default.css"))
|
|
|
|
], $core);
|
|
|
|
|
|
|
|
$result = $core;
|
|
|
|
|
2019-02-26 23:01:09 +00:00
|
|
|
$extra_data_archive = new ZipArchive();
|
|
|
|
if($extra_data_archive->open("php://temp/maxmemory:".(5*1024*1024), ZipArchive::CREATE) !== true) {
|
|
|
|
http_response_code(503);
|
|
|
|
exit("Error: Failed to create temporary stream to store packing information");
|
|
|
|
}
|
|
|
|
|
2019-03-02 12:24:11 +00:00
|
|
|
// HACK! Determine the file descriptor of the ZipArchvie
|
|
|
|
$temp = fopen("php://memory", "w");
|
|
|
|
$archive_file_descriptor = $temp - 1;
|
|
|
|
fclose($temp);
|
|
|
|
|
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-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 12:24:11 +00:00
|
|
|
$archive_stream = fopen("php://fd/$archive_file_descriptor", "r");
|
|
|
|
|
|
|
|
$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 12:24:11 +00:00
|
|
|
fwrite($output_stream, $result);
|
|
|
|
stream_copy_to_stream($archive_stream, $output_stream);
|
|
|
|
|
2018-10-31 16:44:34 +00:00
|
|
|
?>
|