mirror of
https://github.com/xodivorce/isdowndetectordown.git
synced 2025-12-20 01:09:34 +05:30
57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
// core/connection.php
|
|
declare(strict_types=1);
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
use Dotenv\Dotenv;
|
|
|
|
// Load .env from project root
|
|
$dotenv = Dotenv::createImmutable(dirname(__DIR__));
|
|
$dotenv->load();
|
|
|
|
date_default_timezone_set('Asia/Kolkata');
|
|
|
|
function env(string $key, ?string $default = null): ?string
|
|
{
|
|
return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
|
|
}
|
|
|
|
$debug = filter_var(env('APP_DEBUG', 'false'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
ini_set('display_errors', $debug ? '1' : '0');
|
|
ini_set('display_startup_errors', $debug ? '1' : '0');
|
|
error_reporting(
|
|
$debug ? E_ALL : (E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT)
|
|
);
|
|
|
|
try {
|
|
$pdo = new PDO(
|
|
sprintf(
|
|
'mysql:host=%s;dbname=%s;charset=utf8mb4',
|
|
env('DB_HOST', '127.0.0.1'),
|
|
env('DB_NAME', 'isdowndetectordown')
|
|
),
|
|
env('DB_USER', 'root'),
|
|
env('DB_PASS', ''),
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
]
|
|
);
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo 'DB connection failed';
|
|
if ($debug) {
|
|
echo ': ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
|
|
}
|
|
exit;
|
|
}
|
|
|
|
function db(): PDO
|
|
{
|
|
global $pdo;
|
|
return $pdo;
|
|
}
|