diff --git a/.env b/.env index 831e36579fee329d96662ae6ae2971ee1bd40d14..4f70e3d709b67de83b9b0e8839b00c9e34ba876c 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ -POSTGRES_USER="postgres" -POSTGRES_PASSWORD="postgres" -POSTGRES_DB="labpro" \ No newline at end of file +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +POSTGRES_DB=labpro +POSTGRES_HOST=db \ No newline at end of file diff --git a/api/auth/login.php b/api/auth/login.php index 6e50ebd26fa141656c92f5699a996fbcd62e63fa..3e84db03e511a39964050a725b9e25d68f0b43ad 100644 --- a/api/auth/login.php +++ b/api/auth/login.php @@ -4,7 +4,8 @@ require_once("../../app/models/User.php"); require_once("../../app/core/Table.php"); require_once("../../config/config.php"); - + // $dotenv = DotEnv::getInstance(__DIR__. "/../../.env"); + // $dotenv->load(); $xml = file_get_contents('php://input'); $data = json_decode($xml, true); if(session_status() === PHP_SESSION_NONE){ diff --git a/api/auth/register.php b/api/auth/register.php index c67f48d6dd62c53fd6570fd9a4c4e7dd7fbc5ec3..f28d8b897019f6cfcd498b199ec03c4e022eaeca 100644 --- a/api/auth/register.php +++ b/api/auth/register.php @@ -4,6 +4,9 @@ require_once("../../app/core/Database.php"); require_once("../../app/models/User.php"); require_once("../../app/core/Table.php"); require_once("../../config/config.php"); + +// $dotenv = DotEnv::getInstance(__DIR__. "/../../.env"); +// $dotenv->load(); if(session_status() === PHP_SESSION_NONE){ session_start(); } diff --git a/app/core/App.php b/app/core/App.php index 0d18797dd331071b47ea6bb8b36430d7891b212d..b795779b65bdd778e778e06d50c70eb12ba28a2d 100644 --- a/app/core/App.php +++ b/app/core/App.php @@ -1,4 +1,4 @@ -<?php +<?php class App{ private $controllers; private $methods = 'index'; diff --git a/app/core/Database.php b/app/core/Database.php index 335b13fee38212b9d6732830b392add9bc0cb1c2..6da163c0dcf56d4e2af259aa4770fd0efb957e06 100644 --- a/app/core/Database.php +++ b/app/core/Database.php @@ -1,14 +1,18 @@ <?php class Database{ public static $instance; - private $host = "db"; - private $username = "postgres"; - private $password = "postgres"; - private $db = "labpro"; + private $host; + private $username; + private $password; + private $db; private $connection; private $port = 5432; private $stmt; public function __construct(){ + $this->host = getenv("POSTGRES_HOST"); + $this->username = getenv("POSTGRES_USER"); + $this->password = getenv("POSTGRES_PASSWORD"); + $this->db = getenv("POSTGRES_DB"); $dsn = "pgsql:host=" . $this->host . ";dbname=" . $this->db . ";user=" . $this->username . ";password=" . $this->password; try { $this->connection = new PDO($dsn); diff --git a/app/models/Course.php b/app/models/Course.php index 74b28f51d2704074b16281e3030143ead4a59505..d44f0bfcee9cfc13fc2d2f45a0305cb61a1e2845 100644 --- a/app/models/Course.php +++ b/app/models/Course.php @@ -56,7 +56,6 @@ require_once(__DIR__."/Model.php"); public function searchCourses($data){ $query = "SELECT * FROM courses "; $search = false; - if(isset($data["title"])){ $query.= "WHERE (LOWER(title) LIKE :title OR LOWER(description) LIKE :title)"; $search = true; diff --git a/app/views/register/index.php b/app/views/register/index.php index 001a1695b8b17ed9814c805fad3128624a5364e3..40801598397ec234e88c788126974966ee90cac2 100644 --- a/app/views/register/index.php +++ b/app/views/register/index.php @@ -36,7 +36,7 @@ id="username-input" name="username" class="login-input" - onkeyup="check_username_debounce()" + onkeyup="check_username()" required /> </div> diff --git a/config/config.php b/config/config.php index f50d10b3bb133765431a04ca8cd4cd07abd05ec9..4cf8fc1bf7d6b0885c4cf70eb968dd313f03e16d 100644 --- a/config/config.php +++ b/config/config.php @@ -1,3 +1,6 @@ <?php - define("BASE_URL",__DIR__); + require_once("dotenv.php"); + $dir = __DIR__."/../.env"; + $dotenv = DotEnv::getInstance($dir); + $dotenv->load(); ?> \ No newline at end of file diff --git a/config/dotenv.php b/config/dotenv.php new file mode 100644 index 0000000000000000000000000000000000000000..f489b9bcebe27c1b281b01e8ed8f49f8785a0641 --- /dev/null +++ b/config/dotenv.php @@ -0,0 +1,49 @@ +<?php + class DotEnv + { + private static $instance; + protected $path; + + private function __construct(string $path) + { + if (!file_exists($path)) { + throw new InvalidArgumentException(sprintf("File doesn't exists")); + } + $this->path = $path; + } + + public static function getInstance(string $path) + { + if (!self::$instance) { + self::$instance = new self($path); + } + return self::$instance; + } + public function load(){ + if(!is_readable($this->path)){ + throw new RuntimeException(sprintf("File is not readable!")); + } + $lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + foreach ($lines as $line) { + + if (strpos(trim($line), '#') === 0) { + continue; + } + + list($name, $value) = explode('=', $line, 2); + $name = trim($name); + $value = trim($value); + + if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) { + putenv(sprintf('%s=%s', $name, $value)); + $_ENV[$name] = $value; + $_SERVER[$name] = $value; + } + } + define('POSTGRES_USER', getenv('POSTGRES_USER')); + define('POSTGRES_PASSWORD',getenv("POSTGRES_PASSWORD")); + define('POSTGRES_DB',getenv("POSTGRES_DB")); + define('POSTGRES_HOST',getenv("POSTGRES_HOST")); + } + }; +?> \ No newline at end of file diff --git a/index.php b/index.php index 2d4017d1e23562e8580e8f415bd0cd437620724b..dd494dfe204782a6994ea087494ad4471dd2f4af 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ <?php - session_start(); - require_once("app/init.php"); + define("BASE_URL",__DIR__); + session_start(); require_once("config/config.php"); + require_once("app/init.php"); ?> \ No newline at end of file diff --git a/public/js/login.js b/public/js/login.js index 3e008d8cff4ac8d8bbb5f429ebabb3ba5bd33dd5..a3d54d563d9cb277e7f8359dcb9ced3e221cf60c 100644 --- a/public/js/login.js +++ b/public/js/login.js @@ -16,6 +16,7 @@ const check_username = () => { const xml = new XMLHttpRequest(); xml.open("POST", "/api/auth/login.php"); xml.onload = function () { + console.log(this); if (this.status == 200) { let response = JSON.parse(this.responseText); if (response.status === "success") {