database.php 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (getenv('DB_HOST') === false) {
  12. $envFile = __DIR__ . '/../.env.local';
  13. if (file_exists($envFile)) {
  14. $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  15. foreach ($lines as $line) {
  16. if (strpos($line, '=') !== false) {
  17. list($key, $value) = explode('=', $line, 2);
  18. putenv("$key=$value");
  19. }
  20. }
  21. }
  22. // Get database configuration from environment variables
  23. $this->host = getenv('DB_HOST');
  24. $this->db_name = getenv('DB_NAME');
  25. $this->username = getenv('DB_USER');
  26. $this->password = getenv('DB_PASS');
  27. }
  28. // Debug: Log environment variables (remove in production)
  29. error_log("Database config - Host: " . ($this->host ?: 'NOT SET') .
  30. ", DB: " . ($this->db_name ?: 'NOT SET') .
  31. ", User: " . ($this->username ?: 'NOT SET'));
  32. }
  33. public function getConnection() {
  34. $this->conn = null;
  35. // Check if we're in Docker environment or if host is a container name
  36. $isDockerEnvironment = ($this->host === 'mariadb' || $this->host === 'mysql' ||
  37. strpos($this->host, 'inventory-') === 0 ||
  38. !file_exists('/var/run/mysqld/mysqld.sock'));
  39. // In Docker environment, always use TCP connection
  40. if ($isDockerEnvironment) {
  41. error_log("Docker environment detected, using TCP connection to host: " . $this->host);
  42. } else {
  43. // Check for local MySQL socket first in non-Docker environments
  44. $localSocketPath = '/var/run/mysqld/mysqld.sock';
  45. $localSocketExists = file_exists($localSocketPath);
  46. // Try local database connection first if socket exists
  47. if ($localSocketExists) {
  48. try {
  49. $dsn = "mysql:unix_socket=" . $localSocketPath . ";dbname=" . $this->db_name . ";charset=utf8mb4";
  50. error_log("Attempting local database connection via socket: $dsn with user " . $this->username);
  51. $this->conn = new PDO($dsn, $this->username, $this->password);
  52. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  53. $this->conn->exec("set names utf8");
  54. error_log("Local database connection successful via socket");
  55. return $this->conn;
  56. } catch(PDOException $exception) {
  57. error_log("Local database connection failed: " . $exception->getMessage());
  58. // Fall through to external connection
  59. }
  60. }
  61. }
  62. // Try external database connection
  63. if (empty($this->host) || empty($this->db_name) || empty($this->username)) {
  64. error_log("Database configuration missing: Host=" . ($this->host ?: 'MISSING') .
  65. ", DB=" . ($this->db_name ?: 'MISSING') .
  66. ", User=" . ($this->username ?: 'MISSING'));
  67. throw new Exception("Database configuration is incomplete. Please check environment variables.");
  68. }
  69. try {
  70. $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";
  71. $connectionType = $isDockerEnvironment ? "external (Docker environment)" : "external";
  72. error_log("Attempting $connectionType database connection: $dsn with user " . $this->username);
  73. $this->conn = new PDO($dsn, $this->username, $this->password);
  74. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  75. $this->conn->exec("set names utf8");
  76. error_log("$connectionType database connection successful");
  77. } catch(PDOException $exception) {
  78. error_log("External database connection error: " . $exception->getMessage());
  79. error_log("Connection details - Host: " . $this->host . ", DB: " . $this->db_name . ", User: " . $this->username);
  80. throw new Exception("Database connection failed: " . $exception->getMessage());
  81. }
  82. return $this->conn;
  83. }
  84. }
  85. ?>