database.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. class Database {
  3. private $host;
  4. private $db_name;
  5. private $username;
  6. private $password;
  7. public $conn;
  8. public function __construct() {
  9. // In Docker, environment variables are passed directly
  10. // For local development, try to load .env.local file
  11. $envFile = __DIR__ . '/../.env.local';
  12. if (file_exists($envFile)) {
  13. $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  14. foreach ($lines as $line) {
  15. if (strpos($line, '=') !== false) {
  16. list($key, $value) = explode('=', $line, 2);
  17. putenv("$key=$value");
  18. }
  19. }
  20. }
  21. // Get database configuration from environment variables
  22. $this->host = getenv('DB_HOST');
  23. $this->db_name = getenv('DB_NAME');
  24. $this->username = getenv('DB_USER');
  25. $this->password = getenv('DB_PASS');
  26. // Debug: Log environment variables (remove in production)
  27. error_log("Database config - Host: " . ($this->host ?: 'NOT SET') .
  28. ", DB: " . ($this->db_name ?: 'NOT SET') .
  29. ", User: " . ($this->username ?: 'NOT SET'));
  30. }
  31. public function getConnection() {
  32. $this->conn = null;
  33. // Check for local MySQL socket first
  34. $localSocketPath = '/var/run/mysqld/mysqld.sock';
  35. $localSocketExists = file_exists($localSocketPath);
  36. // Try local database connection first if socket exists
  37. if ($localSocketExists) {
  38. try {
  39. $dsn = "mysql:unix_socket=" . $localSocketPath . ";dbname=" . $this->db_name . ";charset=utf8mb4";
  40. error_log("Attempting local database connection via socket: $dsn with user " . $this->username);
  41. $this->conn = new PDO($dsn, $this->username, $this->password);
  42. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  43. $this->conn->exec("set names utf8");
  44. error_log("Local database connection successful via socket");
  45. return $this->conn;
  46. } catch(PDOException $exception) {
  47. error_log("Local database connection failed: " . $exception->getMessage());
  48. // Fall through to external connection
  49. }
  50. }
  51. // Try external database connection
  52. if (empty($this->host) || empty($this->db_name) || empty($this->username)) {
  53. error_log("Database configuration missing: Host=" . ($this->host ?: 'MISSING') .
  54. ", DB=" . ($this->db_name ?: 'MISSING') .
  55. ", User=" . ($this->username ?: 'MISSING'));
  56. throw new Exception("Database configuration is incomplete. Please check environment variables.");
  57. }
  58. try {
  59. $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";
  60. $connectionType = $localSocketExists ? "external (local socket failed)" : "external";
  61. error_log("Attempting $connectionType database connection: $dsn with user " . $this->username);
  62. $this->conn = new PDO($dsn, $this->username, $this->password);
  63. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  64. $this->conn->exec("set names utf8");
  65. error_log("$connectionType database connection successful");
  66. } catch(PDOException $exception) {
  67. error_log("External database connection error: " . $exception->getMessage());
  68. error_log("Connection details - Host: " . $this->host . ", DB: " . $this->db_name . ", User: " . $this->username);
  69. throw new Exception("Database connection failed: " . $exception->getMessage());
  70. }
  71. return $this->conn;
  72. }
  73. }
  74. ?>