This repository was archived by the owner on Aug 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
102 lines (91 loc) · 3.12 KB
/
Copy pathapi.php
File metadata and controls
102 lines (91 loc) · 3.12 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/app/ConfigLoader.php';
$isHttps = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (isset($_SERVER['SERVER_PORT']) && (string)$_SERVER['SERVER_PORT'] === '443'));
header('X-Frame-Options: DENY');
header('X-Content-Type-Options: nosniff');
header('Referrer-Policy: no-referrer');
header("Content-Security-Policy: default-src 'none'");
$config = loadConfig();
if (isWeakApiToken($config['api_token'] ?? null)) {
error_log('WebPrint: refusing to serve /api, api_token is empty/default/too short');
jsonOut(503, ['success' => false, 'message' => 'Service misconfigured']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
jsonOut(405, ['success' => false, 'message' => 'Method not allowed']);
exit;
}
if (!isValidBearerAuth($config)) {
jsonOut(401, ['success' => false, 'message' => 'Missing or invalid token']);
exit;
}
$printers = getValidatedPrinters($config);
$defaultPrinter = (string)($config['printer_name'] ?? '');
if (!array_key_exists($defaultPrinter, $printers) && !empty($printers)) {
$defaultPrinter = array_key_first($printers);
}
$selectedPrinter = $defaultPrinter;
if (isset($_POST['printer'])) {
$p = (string)$_POST['printer'];
if ($p !== '' && array_key_exists($p, $printers)) {
$selectedPrinter = $p;
} else {
jsonOut(400, ['success' => false, 'message' => 'Unknown printer']);
exit;
}
}
if ($selectedPrinter === '') {
jsonOut(400, ['success' => false, 'message' => 'Printer not configured']);
exit;
}
if (!isset($_FILES['file'])) {
jsonOut(400, ['success' => false, 'message' => 'No file uploaded']);
exit;
}
require_once __DIR__ . '/app/UploadHandler.php';
require_once __DIR__ . '/app/JobStore.php';
$originalName = sanitizeJobText((string)($_FILES['file']['name'] ?? ''), 150);
$upload = handleUpload($_FILES['file'], $config);
if (!$upload['ok']) {
$httpStatus = match($upload['error']) {
'Upload error' => 400,
'File too large' => 413,
'Unsupported media type' => 415,
default => 500,
};
addJob([
'source' => 'api',
'printer' => $selectedPrinter,
'filename' => $originalName,
'status' => 'rejected',
'job_id' => null,
'message' => $upload['error'],
]);
jsonOut($httpStatus, ['success' => false, 'message' => $upload['error']]);
exit;
}
$dest = $upload['path'];
require_once __DIR__ . '/app/PrinterService.php';
$service = new PrinterService();
$result = $service->printPdf($dest, $selectedPrinter);
if (!@unlink($dest)) {
error_log('WebPrint: failed to delete tmp file ' . $dest);
}
addJob([
'source' => 'api',
'printer' => $selectedPrinter,
'filename' => $originalName,
'status' => $result['success'] ? 'sent' : 'failed',
'job_id' => $result['job_id'],
'message' => $result['message'],
]);
if (!$result['success']) {
jsonOut(502, ['success' => false, 'message' => $result['message']]);
exit;
}
jsonOut(200, [
'success' => true,
'message' => $result['message'],
'job_id' => $result['job_id'],
]);