39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
<?php
|
|
$configFile = __DIR__ . '/config.php';
|
|
|
|
// Calculate project root URL path based on db.php location
|
|
$projectRootFs = str_replace('\\', '/', dirname(__DIR__));
|
|
$documentRootFs = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
|
|
$projectRootUrl = str_replace($documentRootFs, '', $projectRootFs);
|
|
|
|
// Ensure it starts with / and doesn't end with /
|
|
$projectRootUrl = '/' . ltrim($projectRootUrl, '/');
|
|
$projectRootUrl = rtrim($projectRootUrl, '/');
|
|
define('PROJECT_ROOT_URL', $projectRootUrl);
|
|
|
|
if (!file_exists($configFile)) {
|
|
// If not in setup, redirect to setup
|
|
if (basename($_SERVER['PHP_SELF']) !== 'setup.php') {
|
|
header("Location: " . PROJECT_ROOT_URL . "/setup.php");
|
|
exit;
|
|
}
|
|
} else {
|
|
require_once $configFile;
|
|
|
|
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
];
|
|
|
|
try {
|
|
$pdo = new PDO($dsn, $user, $pass, $options);
|
|
} catch (\PDOException $e) {
|
|
// If connection fails, maybe the config is wrong?
|
|
// For now just die with a friendly message
|
|
die("Database connection failed. Please check your config.php or run setup again.");
|
|
}
|
|
}
|
|
?>
|