Remove temp diag endpoints, improve auto-recovery for companies.json

- Remove temporary data_diag/data_read/data_write endpoints
- Auto-recovery now scans existing company directories if companies.json
  is empty or missing (handles git deploy wiping the file)
- Auto-creates config.json for any company dir missing it

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 11:33:21 +02:00
parent 443e8fcfc3
commit 9973485cb4

85
api.php
View File

@@ -138,24 +138,35 @@ function saveCompanyConfig(array $config): void {
function runMigration(): void { function runMigration(): void {
$companiesDir = DATA_DIR . '/companies'; $companiesDir = DATA_DIR . '/companies';
// Varmista companies.json olemassaolo (voi kadota git deploy:ssa) // Varmista companies.json olemassaolo ja sisältö (voi kadota/tyhjentyä git deploy:ssa)
if (!file_exists(COMPANIES_FILE)) { $companiesData = file_exists(COMPANIES_FILE) ? (json_decode(file_get_contents(COMPANIES_FILE), true) ?: []) : [];
// Jos cuitunet-hakemisto on olemassa, se on jo migrattu aiemmin → luo pelkkä companies.json if (empty($companiesData)) {
if (is_dir($companiesDir . '/cuitunet')) { // Skannaa olemassaolevat yritys-hakemistot ja luo companies.json
$companies = [[ $companies = [];
'id' => 'cuitunet', if (is_dir($companiesDir)) {
'nimi' => 'CuituNet', foreach (glob($companiesDir . '/*', GLOB_ONLYDIR) as $dir) {
'luotu' => date('Y-m-d H:i:s'), $id = basename($dir);
'aktiivinen' => true, $companies[] = [
]]; 'id' => $id,
'nimi' => $id === 'cuitunet' ? 'CuituNet' : ucfirst($id),
'luotu' => date('Y-m-d H:i:s'),
'aktiivinen' => true,
];
}
}
if (!empty($companies)) {
file_put_contents(COMPANIES_FILE, json_encode($companies, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); file_put_contents(COMPANIES_FILE, json_encode($companies, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
} }
} }
// Varmista yrityksen config.json (voi kadota git deploy:ssa) // Varmista jokaisen yrityksen config.json
$cuitunetConfig = $companiesDir . '/cuitunet/config.json'; if (is_dir($companiesDir)) {
if (is_dir($companiesDir . '/cuitunet') && !file_exists($cuitunetConfig)) { foreach (glob($companiesDir . '/*', GLOB_ONLYDIR) as $dir) {
file_put_contents($cuitunetConfig, json_encode(['mailboxes' => [], 'ticket_rules' => []], JSON_PRETTY_PRINT)); $configFile = $dir . '/config.json';
if (!file_exists($configFile)) {
file_put_contents($configFile, json_encode(['mailboxes' => [], 'ticket_rules' => []], JSON_PRETTY_PRINT));
}
}
} }
// Tarkista onko vanha data olemassa juuressa (pre-multitenant) // Tarkista onko vanha data olemassa juuressa (pre-multitenant)
@@ -970,52 +981,6 @@ switch ($action) {
echo json_encode($config); echo json_encode($config);
break; break;
// ---------- TEMP: DATA DIAGNOSTICS (poista myöhemmin) ----------
case 'data_diag':
$key = $_GET['key'] ?? '';
if ($key !== 'temp_restore_2026') { http_response_code(403); echo json_encode(['error' => 'Invalid key']); break; }
$result = ['data_dir' => [],'companies_dir' => [], 'cuitunet_dir' => [], 'root_customers_exists' => false];
// Listaa data/ tiedostot
foreach (glob(DATA_DIR . '/*') as $f) $result['data_dir'][] = basename($f) . (is_dir($f) ? '/' : ' (' . filesize($f) . 'b)');
// Listaa data/companies/
foreach (glob(DATA_DIR . '/companies/*') as $f) $result['companies_dir'][] = basename($f) . (is_dir($f) ? '/' : ' (' . filesize($f) . 'b)');
// Listaa data/companies/cuitunet/
foreach (glob(DATA_DIR . '/companies/cuitunet/*') as $f) $result['cuitunet_dir'][] = basename($f) . (is_dir($f) ? '/' : ' (' . filesize($f) . 'b)');
$result['root_customers_exists'] = file_exists(DATA_DIR . '/customers.json');
$result['companies_json_exists'] = file_exists(COMPANIES_FILE);
// Myös backups
foreach (glob(DATA_DIR . '/companies/cuitunet/backups/*') as $f) $result['backups'][] = basename($f) . ' (' . filesize($f) . 'b)';
foreach (glob(DATA_DIR . '/backups/*') as $f) $result['root_backups'][] = basename($f) . ' (' . filesize($f) . 'b)';
echo json_encode($result, JSON_PRETTY_PRINT);
break;
case 'data_read':
$key = $_GET['key'] ?? '';
if ($key !== 'temp_restore_2026') { http_response_code(403); echo json_encode(['error' => 'Invalid key']); break; }
$file = $_GET['file'] ?? '';
// Salli vain data/ alla olevat tiedostot
$path = DATA_DIR . '/' . str_replace('..', '', $file);
if (file_exists($path) && !is_dir($path)) {
echo file_get_contents($path);
} else {
http_response_code(404);
echo json_encode(['error' => 'Not found', 'path' => $path]);
}
break;
case 'data_write':
$key = $_GET['key'] ?? '';
if ($key !== 'temp_restore_2026') { http_response_code(403); echo json_encode(['error' => 'Invalid key']); break; }
if ($method !== 'POST') break;
$file = $_GET['file'] ?? '';
$path = DATA_DIR . '/' . str_replace('..', '', $file);
$dir = dirname($path);
if (!is_dir($dir)) mkdir($dir, 0755, true);
$body = file_get_contents('php://input');
file_put_contents($path, $body);
echo json_encode(['success' => true, 'path' => $path, 'bytes' => strlen($body)]);
break;
// ---------- CAPTCHA ---------- // ---------- CAPTCHA ----------
case 'captcha': case 'captcha':
$a = rand(1, 20); $a = rand(1, 20);