<?php
    if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/.env')){
        $lines = file($_SERVER['DOCUMENT_ROOT'] . '/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

        if(isset($lines) && !empty($lines)){
            foreach($lines as $line){
                if(strpos(trim($line), '#') === 0){
                    continue;
                }

                list($name, $value) = explode('=', $line, 2);
                $name = trim($name);
                $value = trim($value);

                $value = trim($value, '"\'');

                putenv("$name=$value");
            }
        }
    }
    
    try{
        $db_user    = getenv("DB_USER");
        $db_pass    = getenv("DB_PASS");
        $db_server  = getenv("DB_SERVER");
        $db_name    = getenv("DB_NAME");

        $conn = new PDO("mysql:host=$db_server;dbname=$db_name;charset=utf8mb4", $db_user, $db_pass);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }catch(PDOException $e){
        var_dump($e);
        echo json_encode('Connection exception');
        exit();
    }

?>