-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
pack.php
272 lines (230 loc) · 10.3 KB
/
pack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// 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";
}
$theme_id = getenv("PEPPERMINT_THEME");
if(strlen($theme_id) == 0) $theme_id = "default";
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"))
]));
}
/**
* Logs a string to stdout, but only on the CLI.
* @param string $line The line to log.
*/
function log_str(string $line) {
if(php_sapi_name() == "cli")
echo($line);
//else error_log($line);
}
/*
███ ███ ██████ ██████ ██ ██ ██ ███████
████ ████ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██ ██ ██ ██ ██ █████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██████ ██████ ███████ ███████
██ ██████ █████ ██████ ██ ███ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██████ ██ ██ ██████ ██ ██ ████ ██████
*/
log_str("Building with theme '$theme_id'.\n");
log_str("*** Beginning main build sequence ***\n");
log_str("Reading in module index...\n");
if(isset($_GET["modules"]))
$requested_modules = explode(",", $_GET["modules"]);
$module_index = json_decode(file_get_contents("module_index.json"));
$module_list = [];
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 &&
(
isset($argv) &&
strrpos(implode(" ", $argv), $module->id) === false &&
!in_array("all", $argv)
)
)
continue;
/*
* 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;
$module_list[] = $module;
}
/*
██████ ███████ ██████ ███████ ███ ██ ██████ ███████ ███ ██ ██████ ██ ██
██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ████ ██ ██ ██ ██
██ ██ █████ ██████ █████ ██ ██ ██ ██ ██ █████ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ ███████ ██ ████ ██████ ███████ ██ ████ ██████ ██
███████ ██████ █████ ███ ██ ███ ██ ██ ███ ██ ██████
██ ██ ██ ██ ████ ██ ████ ██ ██ ████ ██ ██
███████ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██████ ██ ██ ██ ████ ██ ████ ██ ██ ████ ██████
*/
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) {
log_str("scanning {$module_list[$i]->id}: $dependency\n");
if(!module_list_search($module_list, $dependency)) {
log_str("Adding missing dependency $dependency for {$module_list[$i]->id}\n\n");
$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++;
}
else {
log_str("present, no action needed\n");
}
}
}
if(php_sapi_name() != "cli") {
header("content-type: text/php");
header("content-disposition: attachment; filename=\"index.php\"");
}
/*
██████ ██████ ██████ ███████
██ ██ ██ ██ ██ ██
██ ██ ██ ██████ █████
██ ██ ██ ██ ██ ██
██████ ██████ ██ ██ ███████
*/
log_str("Reading in core files...\n");
$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));
$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")),
trim(file_get_contents("themes/$theme_id/theme.css"))
], $core);
$result = $core;
/*
██████ █████ ██████ ██ ██ ███████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ █████ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██████ ██ ██ ███████ ██ ██
*/
$extra_data_archive = new ZipArchive();
// 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) {
http_response_code(503);
exit("Error: Failed to create temporary stream to store packing information");
}
$module_list_count = count($module_list);
$i = 1;
foreach($module_list as $module)
{
if($module->id == "") continue;
log_str("[$i / $module_list_count] Adding $module->id \r");
$module_filepath = "modules/" . preg_replace("[^a-zA-Z0-9\-]", "", $module->id) . ".php";
//log_str("id: $module->id | filepath: $module_filepath\n");
if(!file_exists($module_filepath)) {
http_response_code(400);
exit("Failed to load module with name: $module_filepath");
}
// Pack the module's source code
$modulecode = file_get_contents($module_filepath);
$modulecode = str_replace([ "<?php", "?>" ], "", $modulecode);
$result = str_replace(
"// %next_module% //",
"$modulecode\n// %next_module% //",
$result
);
// Pack the extra files that were downloaded in build.php
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
log_str("\n[pack] $paths->extra_data_directory/$module->id/$filepath_pack -> $module->id/$filepath_pack\n");
$extra_data_archive->addFile("$paths->extra_data_directory/$module->id/$filepath_pack", "$module->id/$filepath_pack");
}
}
$i++;
}
log_str("\n");
$extra_data_archive->close();
/*
███████ ██ ███ ██ ██ ███████ ██ ██ ███████ ██████
██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██
█████ ██ ██ ██ ██ ██ ███████ ███████ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ███████ ██ ██ ███████ ██ ██
*/
$archive_stream = fopen($temp_filename, "r");
$output_stream = null;
if(php_sapi_name() == "cli") {
if(file_exists("build/index.php")) {
log_str("index.php already exists in the build folder, exiting\n");
exit(1);
}
log_str("Done. Saving to disk...");
$output_stream = fopen("build/index.php", "w");
log_str("complete!\n");
log_str("*** Build completed! ***\n");
}
else {
$output_stream = fopen("php://output", "w");
}
// Write the built code
fwrite($output_stream, $result);
// Write the delimiter
fwrite($output_stream, "__halt_compiler();");
// Write the extra data
stream_copy_to_stream($archive_stream, $output_stream);
// Cleanup
unlink($temp_filename);
?>