-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
439 lines (382 loc) · 14.9 KB
/
Copy pathindex.php
File metadata and controls
439 lines (382 loc) · 14.9 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* Akeeba Extract
* A cross-platform desktop application to extract Akeeba Backup archives (JPA, JPS, ZIP)
*
* @package akeeba-extract
* @copyright Copyright (c)2026 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/src/Bootstrap.php';
use Boson\Application;
use Boson\ApplicationCreateInfo;
use Boson\Component\Http\Response;
use Boson\Contracts\Uri\UriInterface;
use Boson\WebView\Api\Schemes\Event\SchemeRequestReceived;
use Boson\WebView\Event\WebViewNavigated;
use Boson\WebView\Event\WebViewNavigating;
use Boson\WebView\WebViewCreateInfo;
use Boson\Window\WindowCreateInfo;
/**
* Archive extensions this application opens directly. Multi-part pieces
* (.j01, .z01, …) are deliberately excluded: the engine finds them on its own
* once the main archive is selected.
*/
const AKEEBA_ARCHIVE_EXTENSIONS = ['jpa', 'jps', 'zip'];
/**
* Convert a dropped file's `file://` URI into a local filesystem path.
*
* Handles URL-encoded characters and the Windows `file:///C:/…` form where the
* path component carries a leading slash before the drive letter.
*/
function akeeba_file_uri_to_path(UriInterface $uri): ?string
{
$path = (string) $uri->path;
if ($path === '') {
return null;
}
$path = \rawurldecode($path);
// Windows: "/C:/dir/file.jpa" → "C:/dir/file.jpa"
if (\preg_match('#^/[A-Za-z]:#', $path) === 1) {
$path = \ltrim($path, '/');
}
return $path;
}
// ---------------------------------------------------------------------------
// Create the Boson application with a window sized to fit the form without scrollbars
// ---------------------------------------------------------------------------
$app = new Application(
info: new ApplicationCreateInfo(
name: 'akeeba-extract',
schemes: ['app'],
window: new WindowCreateInfo(
title: 'Akeeba Extract',
width: 620,
height: 620,
resizable: true,
webview: new WebViewCreateInfo(
contextMenu: false,
devTools: false,
storage: false,
),
),
),
);
// ---------------------------------------------------------------------------
// Register a custom "app://" scheme that serves files from public/
// The webview will load app://host/index.html (and app://host/app.css etc.)
// ---------------------------------------------------------------------------
$publicDir = __DIR__ . '/public';
$app->webview->addEventListener(
SchemeRequestReceived::class,
static function (SchemeRequestReceived $event) use ($publicDir): void {
// Path component of the URL, e.g. /index.html or /app.js
$path = $event->request->url->path ?? '/index.html';
// Normalise: strip leading slash, prevent directory traversal
$path = ltrim((string) $path, '/');
$path = str_replace(['..', "\0"], '', $path);
$filePath = $publicDir . '/' . ($path ?: 'index.html');
if (!is_readable($filePath)) {
$event->response = new Response(status: 404, body: '404 Not Found');
return;
}
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$mime = match ($ext) {
'html' => 'text/html; charset=utf-8',
'css' => 'text/css',
'js' => 'application/javascript',
'png' => 'image/png',
'jpg', 'jpeg' => 'image/jpeg',
'svg' => 'image/svg+xml',
default => 'application/octet-stream',
};
$event->response = new Response(
status: 200,
headers: ['Content-Type' => $mime],
body: file_get_contents($filePath),
);
}
);
// ---------------------------------------------------------------------------
// Drag-and-drop support.
//
// Boson/saucer exposes no native file-drop event, but when a file is dropped
// onto the WebView the WebView tries to NAVIGATE to that file's file:// URL.
// We intercept that navigation: cancel it (we never want to leave the UI) and
// feed the dropped file's path to the front-end exactly as if it had been
// chosen with the picker. The JS side validates the extension and shows a
// friendly message for anything that is not a .jpa/.jps/.zip archive.
// ---------------------------------------------------------------------------
$app->webview->addEventListener(
WebViewNavigating::class,
static function (WebViewNavigating $event) use ($app): void {
$scheme = $event->url->scheme !== null
? strtolower((string) $event->url->scheme)
: '';
// Let the app:// UI (and anything that is not a dropped file) proceed.
if ($scheme !== 'file') {
return;
}
// Never navigate away from the UI to show a raw file.
$event->cancel();
$path = akeeba_file_uri_to_path($event->url);
if ($path === null || $path === '') {
return;
}
// scripts->eval() injects JS into the WebView (this is Boson's API for
// calling into the page, NOT PHP eval). The path is serialised with
// json_encode(), so it is embedded as a safely-escaped JS string
// literal — no script injection is possible from the file name.
$app->webview->scripts->eval(
'window.__akeebaApplyArchive && window.__akeebaApplyArchive('
. json_encode($path, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
. ');'
);
}
);
// Safety net: if a file:// navigation ever slips through (cancel not honoured
// on some platform), bounce straight back to the UI instead of stranding the
// user on a raw file view.
$app->webview->addEventListener(
WebViewNavigated::class,
static function (WebViewNavigated $event) use ($app): void {
$scheme = $event->url->scheme !== null
? strtolower((string) $event->url->scheme)
: '';
if ($scheme === 'file') {
$app->webview->url = 'app://host/index.html';
}
}
);
// ---------------------------------------------------------------------------
// Interface language
//
// Resolve the UI language (stored override → OS locale → en-GB) and load the
// matching catalogue into the engine's AKText table, so engine error/warning
// messages come out translated. The same catalogue is handed to the front-end
// as window.__akeebaLang via a document-creation script (scripts->preload runs
// BEFORE app.js, so the UI never flashes untranslated keys).
// ---------------------------------------------------------------------------
$language = new \Akeeba\Extract\I18n\LanguageService(
new \Akeeba\Extract\Settings(),
__DIR__
);
$language->load();
$app->webview->scripts->preload(
'window.__akeebaLang = '
. json_encode($language->catalog(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
. ';'
);
// Navigate to the UI entry point via the custom scheme
$app->webview->url = 'app://host/index.html';
// ---------------------------------------------------------------------------
// Instantiate the extraction service
// ---------------------------------------------------------------------------
$service = new \Akeeba\Extract\ExtractorService();
// ---------------------------------------------------------------------------
// Register JS ↔ PHP bindings
//
// Dot notation ("extractor.begin" etc.) is supported by Boson 0.19's
// WebViewContextPacker — it creates nested window objects automatically.
//
// Every binding body is wrapped in try/catch so a thrown exception is
// returned as a structured error rather than breaking the JS Promise.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Command-line / file-association support.
//
// When the app is launched with an archive path as an argument — e.g. by a
// Windows/Linux file association ("akeeba-extract /path/to/backup.jpa") — pick
// the first readable argument that is a supported archive and offer it to the
// UI through the initialArchive() binding, which the front-end calls on load.
// ---------------------------------------------------------------------------
$initialArchive = null;
foreach (array_slice($_SERVER['argv'] ?? [], 1) as $arg) {
if (!is_string($arg) || $arg === '' || $arg[0] === '-') {
continue; // skip flags / empty values
}
$real = realpath($arg);
if ($real === false || !is_file($real) || !is_readable($real)) {
continue;
}
if (in_array(strtolower(pathinfo($real, PATHINFO_EXTENSION)), AKEEBA_ARCHIVE_EXTENSIONS, true)) {
$initialArchive = $real;
break;
}
}
// initialArchive() → path passed on the command line, or null
$app->webview->bindings->bind(
'initialArchive',
static function () use ($initialArchive): ?string {
return $initialArchive;
}
);
// pickArchive() → native file-open dialog; returns path string or null
$app->webview->bindings->bind(
'pickArchive',
static function () use ($app): ?string {
try {
return $app->dialog->selectFile(null, ['*.jpa', '*.jps', '*.zip']);
} catch (\Throwable $e) {
return null;
}
}
);
// pickOutputDir(start) → native folder-picker; returns path or null
$app->webview->bindings->bind(
'pickOutputDir',
static function (?string $start = null) use ($app): ?string {
try {
$start = (is_string($start) && $start !== '') ? $start : null;
return $app->dialog->selectDirectory($start);
} catch (\Throwable $e) {
return null;
}
}
);
// defaultDir(archive) → dirname of the archive (helper for the UI)
$app->webview->bindings->bind(
'defaultDir',
static function (string $archive): ?string {
try {
$dir = dirname($archive);
return ($dir !== '' && $dir !== '.') ? $dir : null;
} catch (\Throwable $e) {
return null;
}
}
);
// extractor.begin(archive, dest, password, extractList, ignoreErrors) → {ok: bool, error: string}
// extractList is an optional newline/comma-separated list of glob patterns
// selecting which files to extract; empty extracts everything.
// ignoreErrors, when true, makes the engine skip files it cannot write (and
// corrupt entry headers) instead of aborting — each one surfaces as a warning.
$app->webview->bindings->bind(
'extractor.begin',
static function (string $archive, string $dest, ?string $password = null, ?string $extractList = null, bool $ignoreErrors = false) use ($service): array {
try {
$service->begin($archive, $dest, $password, $extractList, false, $ignoreErrors);
return ['ok' => true, 'error' => ''];
} catch (\Throwable $e) {
return ['ok' => false, 'error' => $e->getMessage()];
}
}
);
// extractor.beginScan(archive, password) → {ok: bool, error: string}
// Starts a dry-run pass over the archive. Drive it with extractor.step() exactly
// like a normal extraction; when it reports done, read the result with
// extractor.fileList(). Nothing is written to disk.
$app->webview->bindings->bind(
'extractor.beginScan',
static function (string $archive, ?string $password = null) use ($service): array {
try {
$service->begin($archive, '', $password, null, true);
return ['ok' => true, 'error' => ''];
} catch (\Throwable $e) {
return ['ok' => false, 'error' => $e->getMessage()];
}
}
);
// extractor.fileList() → [{path: string, type: 'file'|'dir'|'link'}, …]
// The entries gathered by the most recent dry-run scan.
$app->webview->bindings->bind(
'extractor.fileList',
static function () use ($service): array {
try {
return $service->fileList();
} catch (\Throwable) {
return [];
}
}
);
// extractor.step() → the progress array from ExtractorService::step()
$app->webview->bindings->bind(
'extractor.step',
static function () use ($service): array {
try {
return $service->step();
} catch (\Throwable $e) {
return [
'percent' => 0,
'files' => 0,
'bytesIn' => 0.0,
'bytesOut' => 0.0,
'totalBytes' => 0.0,
'done' => true,
'error' => $e->getMessage(),
'warnings' => [],
];
}
}
);
// extractor.cancel() → void (no return value needed)
$app->webview->bindings->bind(
'extractor.cancel',
static function () use ($service): void {
try {
$service->cancel();
} catch (\Throwable) {
// swallow
}
}
);
// openFolder(path) → open the folder in the native file manager
$app->webview->bindings->bind(
'openFolder',
static function (string $path) use ($app): void {
try {
$app->dialog->open($path);
} catch (\Throwable) {
// swallow
}
}
);
// update.check() → {available, version, infoURL, download}, best-effort;
// swallows any failure so a broken/offline update check never surfaces to the UI.
$app->webview->bindings->bind(
'update.check',
static function (): array {
try {
$svc = new \Akeeba\Extract\UpdateService(
\Akeeba\Extract\App::VERSION,
\Akeeba\Extract\Paths::updatesFile()
);
return $svc->status();
} catch (\Throwable) {
return ['available' => false, 'version' => null, 'infoURL' => null, 'download' => null];
}
}
);
// openUrl(url) → open a URL in the user's default web browser (e.g. the
// release page from the update banner). Best-effort; swallows errors.
$app->webview->bindings->bind(
'openUrl',
static function (string $url) use ($app): void {
try {
$app->dialog->open($url);
} catch (\Throwable) {
// swallow
}
}
);
// i18n.setLanguage(tag) → persist the interface-language override ("auto" for
// automatic OS detection, or a shipped tag such as "el-GR") and return the fresh
// catalogue so the UI can re-translate itself immediately. Always returns a
// catalogue, even on error, so the picker stays consistent.
$app->webview->bindings->bind(
'i18n.setLanguage',
static function (string $tag) use ($language): array {
try {
$language->setOverride($tag);
} catch (\Throwable) {
// fall through and return the current catalogue unchanged
}
return $language->catalog();
}
);
// ---------------------------------------------------------------------------
// Start the blocking event loop
// ---------------------------------------------------------------------------
$app->run();