database.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 if we're in Docker environment or if host is a container name
  34. $isDockerEnvironment = ($this->host === 'mariadb' || $this->host === 'mysql' ||
  35. strpos($this->host, 'inventory-') === 0 ||
  36. !file_exists('/var/run/mysqld/mysqld.sock'));
  37. // In Docker environment, always use TCP connection
  38. if ($isDockerEnvironment) {
  39. error_log("Docker environment detected, using TCP connection to host: " . $this->host);
  40. } else {
  41. // Check for local MySQL socket first in non-Docker environments
  42. $localSocketPath = '/var/run/mysqld/mysqld.sock';
  43. $localSocketExists = file_exists($localSocketPath);
  44. // Try local database connection first if socket exists
  45. if ($localSocketExists) {
  46. try {
  47. $dsn = "mysql:unix_socket=" . $localSocketPath . ";dbname=" . $this->db_name . ";charset=utf8mb4";
  48. error_log("Attempting local database connection via socket: $dsn with user " . $this->username);
  49. $this->conn = new PDO($dsn, $this->username, $this->password);
  50. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  51. $this->conn->exec("set names utf8");
  52. error_log("Local database connection successful via socket");
  53. return $this->conn;
  54. } catch(PDOException $exception) {
  55. error_log("Local database connection failed: " . $exception->getMessage());
  56. // Fall through to external connection
  57. }
  58. }
  59. }
  60. // Try external database connection
  61. if (empty($this->host) || empty($this->db_name) || empty($this->username)) {
  62. error_log("Database configuration missing: Host=" . ($this->host ?: 'MISSING') .
  63. ", DB=" . ($this->db_name ?: 'MISSING') .
  64. ", User=" . ($this->username ?: 'MISSING'));
  65. throw new Exception("Database configuration is incomplete. Please check environment variables.");
  66. }
  67. try {
  68. $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";
  69. $connectionType = $localSocketExists ? "external (local socket failed)" : "external";
  70. error_log("Attempting $connectionType database connection: $dsn with user " . $this->username);
  71. $this->conn = new PDO($dsn, $this->username, $this->password);
  72. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  73. $this->conn->exec("set names utf8");
  74. error_log("$connectionType database connection successful");
  75. } catch(PDOException $exception) {
  76. error_log("External database connection error: " . $exception->getMessage());
  77. error_log("Connection details - Host: " . $this->host . ", DB: " . $this->db_name . ", User: " . $this->username);
  78. throw new Exception("Database connection failed: " . $exception->getMessage());
  79. }
  80. return $this->conn;
  81. }
  82. }
  83. ?>