From f40eb92e686b4e7ff7a682eb1002c7abde6927db Mon Sep 17 00:00:00 2001 From: Jordy van Zeeland Date: Fri, 16 Aug 2024 16:31:58 +0200 Subject: [PATCH] Moved core to composer package --- app/controllers/welcome.php | 2 +- composer.json | 3 +- framework/auth.php | 80 -------------------- framework/database.php | 146 ------------------------------------ framework/router.php | 67 ----------------- framework/template.php | 27 ------- routes/routes.php | 2 +- 7 files changed, 4 insertions(+), 323 deletions(-) delete mode 100644 framework/auth.php delete mode 100644 framework/database.php delete mode 100644 framework/router.php delete mode 100644 framework/template.php diff --git a/app/controllers/welcome.php b/app/controllers/welcome.php index 8de7698..837eca1 100644 --- a/app/controllers/welcome.php +++ b/app/controllers/welcome.php @@ -1,6 +1,6 @@ =8.0" + "php": ">=8.0", + "sealandia/core": "*" } } \ No newline at end of file diff --git a/framework/auth.php b/framework/auth.php deleted file mode 100644 index 5be35fd..0000000 --- a/framework/auth.php +++ /dev/null @@ -1,80 +0,0 @@ -pdo = new PDO($dsn, $username. $password, [ - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - ]); - - session_start(); - } - - /** - * Method to autenticate the user based on the provided routes. - */ - - public function login($username, $password){ - $sql = "SELECT * FROM users WHERE username = :username or email = :email LIMIT 1"; - - /** - * Prepare the SQL query for execution. - * Then txecute the query with the provided username and password parameters and - * fetch the user record from the database. - */ - - $query = $this->pdo->prepare($query); - $query->execute([ - ":username" => $username, - ":password" => $password - ]); - - $user = $query->fetch(); - - /** - * Check if the provided password is verified against the hashed password in the database. - * If correct, store user information in a session. - * - * Return true indicating a successful login - */ - - if($user && password_verify($password, $user['password'])){ - $_SESSION['user_id'] = $user['id']; - $_SESSION['username'] = $user['username']; - $_SESSION['token'] = bin2hex(random_bytes(32)); - - return true; - } - - return false; - } - -} - -/** - * Example CSRF token in a form: - * "> - * - * Possible check for CSRF in a request: - * if (!empty($_REQUEST["csrf"]) && hash_equals($_REQUEST["csrf_token"], $_SESSION["csrf_token"])) { - */ - -?> \ No newline at end of file diff --git a/framework/database.php b/framework/database.php deleted file mode 100644 index 86d6757..0000000 --- a/framework/database.php +++ /dev/null @@ -1,146 +0,0 @@ -pdo = new PDO('mysql:host=' . $db_config['host'] . ';dbname=' . $db_config['database'] . ';charset=utf8mb4', $db_config['username'], $db_config['password']); - } - - /** - * Method to start a SELECT query and specify columns - */ - - public function select($columns = "*"){ - $this->type = "SELECT"; - $this->columns = $columns; - } - - /** - * Method to specify the table to query - */ - - public function from($table){ - $this->table = $table; - } - - /** - * Method to add a WHERE clause to the query - */ - - public function where($key, $value){ - $this->where = array($key, $value); - } - - /** - * Method to add additional conditions to the WHERE clause - */ - - public function andWhere($key, $value){ - if(!$this->where) $this->where = array(); - $this->where[$key] = $value; - } - - /** - * Method to add a LIMIT clause to the query - */ - - public function limit($number){ - $this->limit = $number; - } - - /** - * Method to execute the query and return all results - */ - - public function all(){ - $this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table; - - /** - * Check if a WHERE clause is set, if so, then append it to the query. - * Then construct WHERE conditions using placeholders for prepared statements and - * join the WHERE conditions with AND and append them to the query - */ - - if(!empty($this->where)){ - $whereClauses = []; - foreach($this->where as $condition){ - $whereClauses[] = $condition[0] . ' = :"' . $condition[0]; - } - $this->query .= ' WHERE ' . implode(' AND ', $whereClauses); - } - - if(!empty($this->limit)){ - $this->query .= ' LIMIT ' . $this->limit; - } - - $results = $this->pdo->prepare($Query); - - /** - * Bind the values from the WHERE clause to the prepared statement - */ - - foreach($this->where as $condition){ - $results->bindValue(':' . $condition[0], $condition[1]); - } - - $results->execute(); - return $results->fetchAll(); - } - - /** - * Method to execute the query and return a single result - */ - - public function one(){ - $this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table; - - /** - * Check if a WHERE clause is set, if so, then append it to the query. - * Then construct WHERE conditions using placeholders for prepared statements and - * join the WHERE conditions with AND and append them to the query - */ - - if(!empty($this->where)){ - $whereClauses = []; - foreach($this->where as $condition){ - $whereClauses[] = $condition[0] . ' = :"' . $condition[0]; - } - $this->query .= ' WHERE ' . implode(' AND ', $whereClauses); - } - - $this->query .= " LIMIT 1"; - $results = $this->pdo->prepare($Query); - - /** - * Bind the values from the WHERE clause to the prepared statement - */ - - foreach($this->where as $condition){ - $results->bindValue(':' . $condition[0], $condition[1]); - } - - $results->execute(); - return $results->fetch(); - } -} - -// Database::select(id, name)->from('tablename')->all() -// Database::select(id, name)->from('tablename')->limit(100)->all() -// Database::select()->from('tablename')->where('id', 3)->one() - -?> \ No newline at end of file diff --git a/framework/router.php b/framework/router.php deleted file mode 100644 index 2d1768d..0000000 --- a/framework/router.php +++ /dev/null @@ -1,67 +0,0 @@ -uri = parse_url($_SERVER['REQUEST_URI'])['path']; - } - - /** - * Method to route the request to the appropriate controller - * based on the provided routes - */ - - public function routeToController($routes){ - /** - * Check if the requested URI exists in the routes array. - * If it exists, require (include and execute) the file corresponding to the route. - * If the route doesn't exist, call the abort method to handle the error - */ - - if(array_key_exists($this->uri, $routes)){ - list($controller, $method) = explode('@', $routes[$this->uri]); - $controllerPath = __DIR__ . '/../app/controllers/' . $controller . '.php'; - - if(file_exists($controllerPath)){ - require $controllerPath; - $controller = new $controller(); - - if(method_exists($controller, $method)){ - $controller->$method(); - }else{ - $this->abort(); - } - }else{ - $this->abort(); - } - }else{ - $this->abort(); - } - } - - /** - * Method to handle HTTP errors - * Default is 404 - Not Found - */ - - public function abort($code = 404){ - /** - * Set the HTTP response code to the specified value. - * Then include and execute the corresponding error view (e.g., "views/404.php") and Terminate the script execution - */ - - http_response_code($code); - require "views/{$code}.php"; - die(); - } -} -?> \ No newline at end of file diff --git a/framework/template.php b/framework/template.php deleted file mode 100644 index 9b2e204..0000000 --- a/framework/template.php +++ /dev/null @@ -1,27 +0,0 @@ - 'About']'); - -?> \ No newline at end of file diff --git a/routes/routes.php b/routes/routes.php index 624f3cf..9332223 100644 --- a/routes/routes.php +++ b/routes/routes.php @@ -1,6 +1,6 @@ 'Welcome@index',