init_database.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. header("Content-Type: application/json; charset=UTF-8");
  3. header("Access-Control-Allow-Origin: *");
  4. header("Access-Control-Allow-Methods: GET, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  6. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  7. exit(0);
  8. }
  9. require_once '../config/database.php';
  10. require_once '../models/User.php';
  11. $database = new Database();
  12. $conn = null;
  13. try {
  14. $conn = $database->getConnection();
  15. // Check if database is already initialized
  16. $checkTables = $conn->query("SHOW TABLES LIKE 'users'");
  17. if ($checkTables->rowCount() > 0) {
  18. // Check if admin user exists
  19. $checkAdmin = $conn->prepare("SELECT COUNT(*) as count FROM users WHERE username = 'admin'");
  20. $checkAdmin->execute();
  21. $adminCount = $checkAdmin->fetch(PDO::FETCH_ASSOC)['count'];
  22. if ($adminCount > 0) {
  23. echo json_encode([
  24. "success" => true,
  25. "message" => "Database already initialized",
  26. "status" => "already_initialized"
  27. ]);
  28. exit;
  29. }
  30. }
  31. // Read and execute setup script
  32. $setupScript = file_get_contents('../setup_database.sql');
  33. // Split the script into individual statements
  34. $statements = preg_split('/;\s*\n/', $setupScript);
  35. $executedStatements = 0;
  36. $errors = [];
  37. foreach ($statements as $statement) {
  38. $statement = trim($statement);
  39. if (empty($statement) || strpos($statement, '--') === 0) {
  40. continue; // Skip empty lines and comments
  41. }
  42. try {
  43. $conn->exec($statement);
  44. $executedStatements++;
  45. } catch (PDOException $e) {
  46. // Log error but continue execution
  47. $errors[] = [
  48. 'statement' => substr($statement, 0, 100) . '...',
  49. 'error' => $e->getMessage()
  50. ];
  51. }
  52. }
  53. // Verify initialization
  54. $finalCheck = $conn->query("SELECT COUNT(*) as table_count FROM information_schema.tables WHERE table_schema = DATABASE()");
  55. $tableCount = $finalCheck->fetch(PDO::FETCH_ASSOC)['table_count'];
  56. // Check for admin user
  57. $adminCheck = $conn->prepare("SELECT COUNT(*) as admin_count FROM users WHERE username = 'admin'");
  58. $adminCheck->execute();
  59. $adminExists = $adminCheck->fetch(PDO::FETCH_ASSOC)['admin_count'] > 0;
  60. echo json_encode([
  61. "success" => true,
  62. "message" => "Database initialization completed",
  63. "status" => "initialized",
  64. "executed_statements" => $executedStatements,
  65. "table_count" => $tableCount,
  66. "admin_user_created" => $adminExists,
  67. "errors" => count($errors) > 0 ? $errors : null
  68. ]);
  69. } catch (Exception $e) {
  70. http_response_code(500);
  71. echo json_encode([
  72. "success" => false,
  73. "message" => "Database initialization failed",
  74. "error" => $e->getMessage()
  75. ]);
  76. }
  77. ?>