Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions admin/admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #1f2933, #3a4a5a);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
color: #333;
}

.card {
background: white;
padding: 30px;
border-radius: 12px;
width: 900px;
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}

h1 {
text-align: center;
margin-top: 0;
}

table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}

th {
background: #4CAF50;
color: white;
padding: 10px;
}

td {
padding: 10px;
border-bottom: 1px solid #ddd;
}

.inactive {
background: #eee;
color: #888;
}

.button {
background: #4CAF50;
color: white;
padding: 8px 14px;
border-radius: 6px;
text-decoration: none;
}

.button.red {
background: #d9534f;
}

.test-result {
margin-left: 10px;
font-weight: bold;
}

.form-row {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}

.form-row input[type="text"] {
width: 260px; /* pick what looks good */
}

form input[type="text"],
form input[type="url"] {
width: 100%;
margin-bottom: 10px;
}

.form-row input[type="text"] {
width: auto;
margin-bottom: 0;
}

.form-row input.printer-name {
width: 180px;
}

.form-row input[name="url"] {
width: 200px;
}

.form-row input[name="apiKey"] {
width: 260px;
}
58 changes: 58 additions & 0 deletions admin/auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
session_start();

$password = '{{ 3dps_auth_password }}';
$ip_allowlist = explode(',', '{{ 3dps_allowed_ips }}');
$ip_allowlist = array_map('trim', $ip_allowlist);
$client_ip = $_SERVER['REMOTE_ADDR'];

function ip_allowed($client_ip, $allowlist) {

foreach ($allowlist as $rule) {

// CIDR
if (strpos($rule, '/') !== false) {
[$subnet, $mask] = explode('/', $rule);

if ((ip2long($client_ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
return true;
}
}

// exact match
if ($client_ip === $rule) {
return true;
}

// prefix match (partial IP like 10.0.0.)
if (str_ends_with($rule, '.') && str_starts_with($client_ip, $rule)) {
return true;
}
}

return false;
}

if (ip_allowed($client_ip, $ip_allowlist)) {
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit;
}else {
die('Access denied (not on local network)');
}


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_POST['password'] === $password) {
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit;
}
}

?>
<form method="post">
<h2>Admin Login</h2>
<input type="password" name="password" placeholder="Password">
<button type="submit">Login</button>
</form>
16 changes: 16 additions & 0 deletions admin/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require 'protect.php';

$file = __DIR__ . '/../../private/printers.json';
$printers = json_decode(file_get_contents($file), true);
$cacheFile = '/tmp/printer_data_cache.json'; // Set the cache file path

$id = $_GET['id'];
unset($printers[$id]);

file_put_contents($file, json_encode(array_values($printers), JSON_PRETTY_PRINT));
if (file_exists($cacheFile)) {
unlink($cacheFile);
}

header('Location: index.php');
152 changes: 152 additions & 0 deletions admin/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
require 'protect.php';

$file = __DIR__ . '/../../private/printers.json';
$printers = json_decode(file_get_contents($file), true);
$cacheFile = '/tmp/printer_data_cache.json'; // Set the cache file path

$id = $_GET['id'] ?? null;

$printer = $printers[$id] ?? [
'printerName' => '',
'url' => '',
'apiKey' => '',
'active' => true
];

function getPrinterName($url, $apiKey) {

$base = rtrim($url, '/');

$opts = [
"http" => [
"method" => "GET",
"header" => "X-Api-Key: $apiKey\r\n",
"timeout" => 5
]
];

$context = stream_context_create($opts);
$json = @file_get_contents($base . '/api/settings', false, $context);


if ($json === false) {
error_log("API FAILED: " . $base . "/api/settings");
return '';
}

$data = json_decode($json, true);

return $data['appearance']['name'] ?? '';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

// ALWAYS take values from the form first
$url = $_POST['url'] ?? '';
$apiKey = $_POST['apiKey'] ?? '';

// Fetch live name using the POSTed values
$printerName = getPrinterName($url, $apiKey);

// If editing and API failed → keep existing name
if ($id !== null && empty($printerName) && !empty($printers[$id]['printerName'])) {
$printerName = $printers[$id]['printerName'];
}

// If adding and API failed → stop
if ($id === null && empty($printerName)) {
die("Could not contact printer API.");
}

// Build data from POST (THIS IS CORRECT)
$data = [
'printerName' => $printerName,
'url' => $url,
'apiKey' => $apiKey,
'active' => isset($_POST['active'])
];

// Save
if ($id !== null) {
$printers[$id] = $data;
} else {
$printers[] = $data;
}

file_put_contents($file, json_encode($printers, JSON_PRETTY_PRINT));

if (file_exists($cacheFile)) {
unlink($cacheFile);
}

header('Location: index.php');
exit;
}
?>

<link rel="stylesheet" href="admin.css">

<div class="card">

<h1><?= $id !== null ? "Edit Printer" : "Add Printer" ?></h1>

<form method="post">

<div class="form-row">
<?php if ($id !== null && !empty($printer['printerName'])): ?>
<input class="printer-name" value="<?= htmlspecialchars($printer['printerName']) ?>" readonly>
<?php endif; ?>

<input type="text" name="url"
value="<?= htmlspecialchars($printer['url'] ?? '') ?>"
placeholder="Octoprint URL">

<input type="text" name="apiKey"
value="<?= htmlspecialchars($printer['apiKey'] ?? '') ?>"
placeholder="API Key">

<button type="button" id="test-connection">Test Connection</button>
<span id="test-result"></span>

<label>
<input type="checkbox" name="active" <?= $printer['active'] ? 'checked' : '' ?>>
Active
</label>
</div>

<br><br>

<button class="button">Save</button>

</form>

</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script>
$('#test-connection').click(function () {

let url = $('input[name="url"]').val();
let key = $('input[name="apiKey"]').val();

$('#test-result').text('Testing...');

$.post('test_printer.php', {
url: url,
apiKey: key
}, function (res) {

if (res.success) {
$('#test-result').html('<span style="color:green;">Succeeded</span>');
$('#printerName').val(res.name);
} else {
$('#test-result').html('<span style="color:red;">Failed</span>');
}

}, 'json');

});
</script>

Loading