Add CuituNet Intra customer management CMS
Password-protected intranet for managing fiber internet customers: - Customer table (company, address, speed, price) - Click row to view full details (contact & billing info) - Add, edit, delete customers - Search and sortable columns - Total billing summary - PHP + vanilla JS + JSON storage Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
153
api.php
Normal file
153
api.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
define('ADMIN_PASSWORD', 'cuitunet2024');
|
||||
define('DATA_FILE', __DIR__ . '/data/customers.json');
|
||||
|
||||
// Varmista data-kansio
|
||||
if (!file_exists(__DIR__ . '/data')) {
|
||||
mkdir(__DIR__ . '/data', 0755, true);
|
||||
}
|
||||
if (!file_exists(DATA_FILE)) {
|
||||
file_put_contents(DATA_FILE, '[]');
|
||||
}
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$action = $_GET['action'] ?? '';
|
||||
|
||||
// Auth-tarkistus (paitsi login)
|
||||
function requireAuth() {
|
||||
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Kirjaudu sisään']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function loadCustomers(): array {
|
||||
$data = file_get_contents(DATA_FILE);
|
||||
return json_decode($data, true) ?: [];
|
||||
}
|
||||
|
||||
function saveCustomers(array $customers): void {
|
||||
file_put_contents(DATA_FILE, json_encode($customers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
function generateId(): string {
|
||||
return bin2hex(random_bytes(8));
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'login':
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$password = $input['password'] ?? '';
|
||||
if ($password === ADMIN_PASSWORD) {
|
||||
$_SESSION['authenticated'] = true;
|
||||
echo json_encode(['success' => true]);
|
||||
} else {
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Väärä salasana']);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'logout':
|
||||
session_destroy();
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
|
||||
case 'check_auth':
|
||||
echo json_encode(['authenticated' => isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true]);
|
||||
break;
|
||||
|
||||
case 'customers':
|
||||
requireAuth();
|
||||
if ($method === 'GET') {
|
||||
$customers = loadCustomers();
|
||||
echo json_encode($customers);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'customer':
|
||||
requireAuth();
|
||||
if ($method === 'POST') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$customers = loadCustomers();
|
||||
$customer = [
|
||||
'id' => generateId(),
|
||||
'yritys' => trim($input['yritys'] ?? ''),
|
||||
'asennusosoite' => trim($input['asennusosoite'] ?? ''),
|
||||
'liittymanopeus' => trim($input['liittymanopeus'] ?? ''),
|
||||
'hinta' => floatval($input['hinta'] ?? 0),
|
||||
'yhteyshenkilö' => trim($input['yhteyshenkilö'] ?? ''),
|
||||
'puhelin' => trim($input['puhelin'] ?? ''),
|
||||
'sahkoposti' => trim($input['sahkoposti'] ?? ''),
|
||||
'laskutusosoite' => trim($input['laskutusosoite'] ?? ''),
|
||||
'laskutussahkoposti' => trim($input['laskutussahkoposti'] ?? ''),
|
||||
'ytunnus' => trim($input['ytunnus'] ?? ''),
|
||||
'lisatiedot' => trim($input['lisatiedot'] ?? ''),
|
||||
'luotu' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
if (empty($customer['yritys'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Yrityksen nimi vaaditaan']);
|
||||
break;
|
||||
}
|
||||
$customers[] = $customer;
|
||||
saveCustomers($customers);
|
||||
echo json_encode($customer);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'customer_update':
|
||||
requireAuth();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
$customers = loadCustomers();
|
||||
$found = false;
|
||||
foreach ($customers as &$c) {
|
||||
if ($c['id'] === $id) {
|
||||
$c['yritys'] = trim($input['yritys'] ?? $c['yritys']);
|
||||
$c['asennusosoite'] = trim($input['asennusosoite'] ?? $c['asennusosoite']);
|
||||
$c['liittymanopeus'] = trim($input['liittymanopeus'] ?? $c['liittymanopeus']);
|
||||
$c['hinta'] = floatval($input['hinta'] ?? $c['hinta']);
|
||||
$c['yhteyshenkilö'] = trim($input['yhteyshenkilö'] ?? $c['yhteyshenkilö']);
|
||||
$c['puhelin'] = trim($input['puhelin'] ?? $c['puhelin']);
|
||||
$c['sahkoposti'] = trim($input['sahkoposti'] ?? $c['sahkoposti']);
|
||||
$c['laskutusosoite'] = trim($input['laskutusosoite'] ?? $c['laskutusosoite']);
|
||||
$c['laskutussahkoposti'] = trim($input['laskutussahkoposti'] ?? $c['laskutussahkoposti']);
|
||||
$c['ytunnus'] = trim($input['ytunnus'] ?? $c['ytunnus']);
|
||||
$c['lisatiedot'] = trim($input['lisatiedot'] ?? $c['lisatiedot']);
|
||||
$c['muokattu'] = date('Y-m-d H:i:s');
|
||||
$found = true;
|
||||
echo json_encode($c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($c);
|
||||
if (!$found) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Asiakasta ei löydy']);
|
||||
break;
|
||||
}
|
||||
saveCustomers($customers);
|
||||
break;
|
||||
|
||||
case 'customer_delete':
|
||||
requireAuth();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
$customers = loadCustomers();
|
||||
$customers = array_values(array_filter($customers, fn($c) => $c['id'] !== $id));
|
||||
saveCustomers($customers);
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
|
||||
default:
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Tuntematon toiminto']);
|
||||
break;
|
||||
}
|
||||
Reference in New Issue
Block a user