Initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
// api/setup.php — Wizard d'installation v3
|
||||
// ⚠️ Supprimer après usage : rm /var/www/menage/setup.html /var/www/menage/api/setup.php
|
||||
require_once __DIR__ . '/config.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
|
||||
|
||||
$action = $_GET['action'] ?? '';
|
||||
$body = json_decode(file_get_contents('php://input'), true) ?? [];
|
||||
|
||||
switch ($action) {
|
||||
|
||||
case 'check':
|
||||
$result = ['db_ok'=>false,'db_name'=>DB_NAME,'db_error'=>'','tables_ok'=>false,'table_count'=>0,'no_admin'=>true,'php_ok'=>false,'php_missing'=>[]];
|
||||
$required = ['pdo_mysql','mbstring'];
|
||||
$missing = array_values(array_filter($required, fn($e)=>!extension_loaded($e)));
|
||||
$result['php_ok'] = empty($missing);
|
||||
$result['php_missing'] = $missing;
|
||||
try {
|
||||
$db = getDB();
|
||||
$result['db_ok'] = true;
|
||||
$needed = ['accounts','profiles','categories','tasks','completions','profile_category_assignments','profile_task_assignments'];
|
||||
$existing = $db->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
|
||||
$found = array_intersect($needed, $existing);
|
||||
$result['table_count'] = count($found);
|
||||
$result['tables_ok'] = count($found) === count($needed);
|
||||
$result['tables_missing'] = array_values(array_diff($needed, $existing));
|
||||
if ($result['tables_ok']) {
|
||||
$result['no_admin'] = ((int)$db->query("SELECT COUNT(*) FROM accounts WHERE role='admin'")->fetchColumn() === 0);
|
||||
}
|
||||
} catch (PDOException $e) { $result['db_error'] = $e->getMessage(); }
|
||||
echo json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
break;
|
||||
|
||||
case 'create_admin':
|
||||
$username = trim($body['username'] ?? '');
|
||||
$password = $body['password'] ?? '';
|
||||
$dn = trim($body['display_name'] ?? '');
|
||||
$color = preg_match('/^#[0-9a-fA-F]{6}$/', $body['color']??'') ? $body['color'] : '#3b82f6';
|
||||
$avatar = strtoupper(mb_substr(trim($body['avatar'] ?? mb_substr($dn,0,1)), 0, 2));
|
||||
$email = filter_var($body['email']??'', FILTER_VALIDATE_EMAIL) ?: '';
|
||||
|
||||
if (!$username || !$dn) { http_response_code(400); echo json_encode(['error'=>'Champs requis']); break; }
|
||||
if (!preg_match('/^[a-z0-9_-]{2,30}$/', $username)) { http_response_code(400); echo json_encode(['error'=>'Identifiant invalide']); break; }
|
||||
if (strlen($password) < 8) { http_response_code(400); echo json_encode(['error'=>'Mot de passe trop court (8 car. min)']); break; }
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
$exists = $db->prepare("SELECT COUNT(*) FROM accounts WHERE BINARY username=:u");
|
||||
$exists->execute([':u'=>$username]);
|
||||
if ((int)$exists->fetchColumn() > 0) { http_response_code(409); echo json_encode(['error'=>"Identifiant déjà utilisé."]); break; }
|
||||
|
||||
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost'=>12]);
|
||||
$db->prepare("INSERT INTO accounts (username,password,role,display_name,color,avatar,email) VALUES(:u,:p,'admin',:dn,:c,:a,:e)")
|
||||
->execute([':u'=>$username,':p'=>$hash,':dn'=>$dn,':c'=>$color,':a'=>$avatar,':e'=>$email]);
|
||||
|
||||
echo json_encode(['success'=>true,'username'=>$username], JSON_UNESCAPED_UNICODE);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500); echo json_encode(['error'=>'DB: '.$e->getMessage()]);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
http_response_code(404); echo json_encode(['error'=>'Action inconnue']);
|
||||
}
|
||||
Reference in New Issue
Block a user