-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.php
More file actions
72 lines (62 loc) · 2.1 KB
/
config.php
File metadata and controls
72 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Ufazien Configuration
* Loads environment variables from .env file
*/
// Load environment variables from .env file
function loadEnv($path) {
if (!file_exists($path)) {
// .env file not found, use defaults or environment variables
return;
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}
if (strpos($line, '=') === false) {
continue;
}
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value);
if (!array_key_exists($name, $_ENV)) {
putenv("$name=$value");
$_ENV[$name] = $value;
}
}
}
// Load .env file - try multiple possible locations
$envPaths = [
__DIR__ . '/.env', // Same directory as config.php (root)
dirname(__DIR__) . '/.env', // Parent directory (if config.php is in subdirectory)
getcwd() . '/.env', // Current working directory
];
$envLoaded = false;
foreach ($envPaths as $envPath) {
if (file_exists($envPath)) {
loadEnv($envPath);
$envLoaded = true;
break;
}
}
define('DB_HOST', getenv('DB_HOST') ?: 'localhost');
define('DB_USER', getenv('DB_USER') ?: 'root');
define('DB_PASSWORD', getenv('DB_PASSWORD') ?: '');
define('DB_NAME', getenv('DB_NAME') ?: 'martian_backend_phgwir_db');
define('DB_PORT', getenv('DB_PORT') ?: '3306');
function getDBConnection() {
try {
$dsn = "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME . ";charset=utf8mb4";
$conn = new PDO($dsn, DB_USER, DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $conn;
} catch (PDOException $e) {
die("Database connection error: " . $e->getMessage());
}
}
function get_db_connection() {
return getDBConnection();
}