microserviciile de register, login finalizate
@@ -1,3 +0,0 @@
|
||||
DB_DSN = mysql:host=localhost;port=3306;dbname=Web
|
||||
DB_USER = root
|
||||
DB_PASSWORD = root
|
||||
@@ -1,4 +1,4 @@
|
||||
vendor
|
||||
.idea
|
||||
dbconfig
|
||||
vendor
|
||||
.idea
|
||||
dbconfig
|
||||
!.gitignore
|
||||
@@ -1,14 +1,4 @@
|
||||
ErrorDocument 403 /404.php
|
||||
ErrorDocument 404 /404.php
|
||||
DirectoryINdex index.php
|
||||
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine on
|
||||
RewriteEngine On
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
|
||||
|
||||
RewriteCond %{REQUEST_URI} !^/$
|
||||
RewriteCond %{REQUEST_URI} !^/admin
|
||||
RewriteRule .*/ [L,R=301]
|
||||
</IfModule>
|
||||
RewriteRule ^(.*)$ index.php [L]
|
||||
@@ -1,17 +1,11 @@
|
||||
{
|
||||
"name": "andrei/knowyourfood",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Cerbu Andrei Mihnea",
|
||||
"email": "andreimihneacerbu@gmail.com"
|
||||
}
|
||||
],
|
||||
"autoload" : {
|
||||
"psr-4" : {
|
||||
"name": "andrei_cerbu/knowyourfood",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"app\\": "./"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"vlucas/phpdotenv": "^5.5"
|
||||
"ext-curl": "*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,485 +4,17 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "c85ce2bf1fa0099ca65ec02d2a26abb1",
|
||||
"packages": [
|
||||
{
|
||||
"name": "graham-campbell/result-type",
|
||||
"version": "v1.1.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/GrahamCampbell/Result-Type.git",
|
||||
"reference": "672eff8cf1d6fe1ef09ca0f89c4b287d6a3eb831"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/672eff8cf1d6fe1ef09ca0f89c4b287d6a3eb831",
|
||||
"reference": "672eff8cf1d6fe1ef09ca0f89c4b287d6a3eb831",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.2.5 || ^8.0",
|
||||
"phpoption/phpoption": "^1.9.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.5.32 || ^9.6.3 || ^10.0.12"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GrahamCampbell\\ResultType\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Graham Campbell",
|
||||
"email": "hello@gjcampbell.co.uk",
|
||||
"homepage": "https://github.com/GrahamCampbell"
|
||||
}
|
||||
],
|
||||
"description": "An Implementation Of The Result Type",
|
||||
"keywords": [
|
||||
"Graham Campbell",
|
||||
"GrahamCampbell",
|
||||
"Result Type",
|
||||
"Result-Type",
|
||||
"result"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/GrahamCampbell/Result-Type/issues",
|
||||
"source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/GrahamCampbell",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/graham-campbell/result-type",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-02-25T20:23:15+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpoption/phpoption",
|
||||
"version": "1.9.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/schmittjoh/php-option.git",
|
||||
"reference": "dd3a383e599f49777d8b628dadbb90cae435b87e"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/schmittjoh/php-option/zipball/dd3a383e599f49777d8b628dadbb90cae435b87e",
|
||||
"reference": "dd3a383e599f49777d8b628dadbb90cae435b87e",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.2.5 || ^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"bamarni/composer-bin-plugin": "^1.8.2",
|
||||
"phpunit/phpunit": "^8.5.32 || ^9.6.3 || ^10.0.12"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"bamarni-bin": {
|
||||
"bin-links": true,
|
||||
"forward-command": true
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-master": "1.9-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhpOption\\": "src/PhpOption/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache-2.0"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Johannes M. Schmitt",
|
||||
"email": "schmittjoh@gmail.com",
|
||||
"homepage": "https://github.com/schmittjoh"
|
||||
},
|
||||
{
|
||||
"name": "Graham Campbell",
|
||||
"email": "hello@gjcampbell.co.uk",
|
||||
"homepage": "https://github.com/GrahamCampbell"
|
||||
}
|
||||
],
|
||||
"description": "Option Type for PHP",
|
||||
"keywords": [
|
||||
"language",
|
||||
"option",
|
||||
"php",
|
||||
"type"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/schmittjoh/php-option/issues",
|
||||
"source": "https://github.com/schmittjoh/php-option/tree/1.9.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/GrahamCampbell",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/phpoption/phpoption",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-02-25T19:38:58+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/polyfill-ctype",
|
||||
"version": "v1.27.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/polyfill-ctype.git",
|
||||
"reference": "5bbc823adecdae860bb64756d639ecfec17b050a"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/5bbc823adecdae860bb64756d639ecfec17b050a",
|
||||
"reference": "5bbc823adecdae860bb64756d639ecfec17b050a",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"provide": {
|
||||
"ext-ctype": "*"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-ctype": "For best performance"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "1.27-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/polyfill",
|
||||
"url": "https://github.com/symfony/polyfill"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"bootstrap.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Symfony\\Polyfill\\Ctype\\": ""
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Gert de Pagter",
|
||||
"email": "BackEndTea@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Symfony polyfill for ctype functions",
|
||||
"homepage": "https://symfony.com",
|
||||
"keywords": [
|
||||
"compatibility",
|
||||
"ctype",
|
||||
"polyfill",
|
||||
"portable"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/polyfill-ctype/tree/v1.27.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2022-11-03T14:55:06+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/polyfill-mbstring",
|
||||
"version": "v1.27.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/polyfill-mbstring.git",
|
||||
"reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534",
|
||||
"reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"provide": {
|
||||
"ext-mbstring": "*"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-mbstring": "For best performance"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "1.27-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/polyfill",
|
||||
"url": "https://github.com/symfony/polyfill"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"bootstrap.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Symfony\\Polyfill\\Mbstring\\": ""
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Symfony polyfill for the Mbstring extension",
|
||||
"homepage": "https://symfony.com",
|
||||
"keywords": [
|
||||
"compatibility",
|
||||
"mbstring",
|
||||
"polyfill",
|
||||
"portable",
|
||||
"shim"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2022-11-03T14:55:06+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/polyfill-php80",
|
||||
"version": "v1.27.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/polyfill-php80.git",
|
||||
"reference": "7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936",
|
||||
"reference": "7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "1.27-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/polyfill",
|
||||
"url": "https://github.com/symfony/polyfill"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"bootstrap.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Symfony\\Polyfill\\Php80\\": ""
|
||||
},
|
||||
"classmap": [
|
||||
"Resources/stubs"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Ion Bazan",
|
||||
"email": "ion.bazan@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions",
|
||||
"homepage": "https://symfony.com",
|
||||
"keywords": [
|
||||
"compatibility",
|
||||
"polyfill",
|
||||
"portable",
|
||||
"shim"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/polyfill-php80/tree/v1.27.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2022-11-03T14:55:06+00:00"
|
||||
},
|
||||
{
|
||||
"name": "vlucas/phpdotenv",
|
||||
"version": "v5.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/vlucas/phpdotenv.git",
|
||||
"reference": "1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7",
|
||||
"reference": "1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-pcre": "*",
|
||||
"graham-campbell/result-type": "^1.0.2",
|
||||
"php": "^7.1.3 || ^8.0",
|
||||
"phpoption/phpoption": "^1.8",
|
||||
"symfony/polyfill-ctype": "^1.23",
|
||||
"symfony/polyfill-mbstring": "^1.23.1",
|
||||
"symfony/polyfill-php80": "^1.23.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"bamarni/composer-bin-plugin": "^1.4.1",
|
||||
"ext-filter": "*",
|
||||
"phpunit/phpunit": "^7.5.20 || ^8.5.30 || ^9.5.25"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-filter": "Required to use the boolean validator."
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"bamarni-bin": {
|
||||
"bin-links": true,
|
||||
"forward-command": true
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-master": "5.5-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Dotenv\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Graham Campbell",
|
||||
"email": "hello@gjcampbell.co.uk",
|
||||
"homepage": "https://github.com/GrahamCampbell"
|
||||
},
|
||||
{
|
||||
"name": "Vance Lucas",
|
||||
"email": "vance@vancelucas.com",
|
||||
"homepage": "https://github.com/vlucas"
|
||||
}
|
||||
],
|
||||
"description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.",
|
||||
"keywords": [
|
||||
"dotenv",
|
||||
"env",
|
||||
"environment"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/vlucas/phpdotenv/issues",
|
||||
"source": "https://github.com/vlucas/phpdotenv/tree/v5.5.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/GrahamCampbell",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/vlucas/phpdotenv",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2022-10-16T01:01:54+00:00"
|
||||
}
|
||||
],
|
||||
"content-hash": "d99f7bada71212307c9485da334476d5",
|
||||
"packages": [],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform": {
|
||||
"ext-curl": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
|
||||
@@ -1,70 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
use app\core\middlewares\AuthMiddleware;
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\LoginForm;
|
||||
use app\models\User;
|
||||
|
||||
class AuthController extends Controller{
|
||||
public function __construct(){
|
||||
$this->registerMiddleware(new AuthMiddleware(['profile']));
|
||||
}
|
||||
|
||||
public function login(Request $request, Response $response){
|
||||
$loginForm = new LoginForm();
|
||||
|
||||
if($request->isPost()){
|
||||
$loginForm->loadData($request->getBody());
|
||||
if($loginForm->validate() && $loginForm->login()){
|
||||
$response->redirect('home');
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('login', [
|
||||
'model' => $loginForm
|
||||
]);
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('login', [
|
||||
'model' => $loginForm
|
||||
]);
|
||||
}
|
||||
|
||||
public function register(Request $request, $response){
|
||||
$user = new User();
|
||||
|
||||
if($request->isPost()){
|
||||
$user->loadData($request->getBody());
|
||||
|
||||
if($user->validate() && $user->save()){
|
||||
$response->redirect('home');
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('register', [
|
||||
'model'=>$user
|
||||
]);
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('register', [
|
||||
'model'=>$user
|
||||
]);
|
||||
}
|
||||
|
||||
public function logout($request, $response){
|
||||
Application::$app->logout();
|
||||
$response->redirect('/');
|
||||
}
|
||||
|
||||
public function profile(){
|
||||
return $this->render('profile');
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\models\LoginModel;
|
||||
use app\core\Model;
|
||||
use app\models\RegisterModel;
|
||||
|
||||
class AuthController extends Controller{
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public static function loginPage(Request $request): string
|
||||
{
|
||||
$model = new LoginModel();
|
||||
|
||||
if($request->isPost()){
|
||||
$model->loadData($request->getPOSTBody());
|
||||
$model->checkForErrors();
|
||||
|
||||
if($model->hasErrors()){
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
$model->removeErrors();
|
||||
|
||||
$body = $model->createBody();
|
||||
$headers = $model->createHeaders(json_encode($body));
|
||||
|
||||
$model->setParametersForRequest("GET", $headers, $body);
|
||||
|
||||
$model->execute();
|
||||
if($model->isRequestFulfilled()){
|
||||
//Application::$app->response->redirect("homepage");
|
||||
}
|
||||
|
||||
$model->setRequestError();
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
public static function registerPage(Request $request): string
|
||||
{
|
||||
$model = new RegisterModel();
|
||||
|
||||
if($request->isPost()){
|
||||
$model->loadData($request->getPOSTBody());
|
||||
$model->checkForErrors();
|
||||
|
||||
if($model->hasErrors()){
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
$model->removeErrors();
|
||||
|
||||
$body = $model->createBody();
|
||||
$headers = $model->createHeaders(json_encode($body));
|
||||
|
||||
$model->setParametersForRequest("PUT", $headers, $body);
|
||||
|
||||
$model->execute();
|
||||
if($model->isRequestFulfilled()){
|
||||
Application::$app->response->redirect("login");
|
||||
}
|
||||
|
||||
$model->setRequestError();
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
public static function setControllerParams(string $title, string $view, string $layout, ?Model $model): void
|
||||
{
|
||||
Application::$app->controller->setLayout($layout);
|
||||
Application::$app->controller->setModel($model);
|
||||
Application::$app->controller->setTitle($title);
|
||||
Application::$app->controller->setView($view);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Model;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\ContactForm;
|
||||
|
||||
class SiteController extends Controller
|
||||
{
|
||||
|
||||
|
||||
public function Homepage(): string
|
||||
{
|
||||
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
public function profile(): string
|
||||
{
|
||||
return $this->render('profile');
|
||||
}
|
||||
|
||||
public static function renderMainPage(): string
|
||||
{
|
||||
Application::$app->view->setLayout('Main');
|
||||
return Application::$app->view->renderLayout();
|
||||
}
|
||||
|
||||
public function _404(): string
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
public function _403(): string
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
protected static function setControllerParams(string $title, string $view, string $layout, ?Model $model)
|
||||
{
|
||||
// TODO: Implement setControllerParams() method.
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\ContactForm;
|
||||
|
||||
class SiteController extends Controller
|
||||
{
|
||||
public function contact(Request $request, Response $response)
|
||||
{
|
||||
$contact = new ContactForm();
|
||||
|
||||
if ($request->isPost()) {
|
||||
$contact->loadData($request->getBody());
|
||||
|
||||
if ($contact->validate() && $contact->send()) {
|
||||
Application::$app->session->setFlash('success', 'The form has been succesfully sent!');
|
||||
var_dump($_SESSION['flash_messages']);
|
||||
$response->redirect('contact');
|
||||
exit;
|
||||
}
|
||||
|
||||
return $this->render('contact', [
|
||||
'model' => $contact
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->render('contact', [
|
||||
'model' => $contact
|
||||
]);
|
||||
}
|
||||
|
||||
public function home()
|
||||
{
|
||||
$params = [
|
||||
'name' => "TheCodeholic"
|
||||
];
|
||||
|
||||
return $this->render('home', $params);
|
||||
}
|
||||
|
||||
public function profile()
|
||||
{
|
||||
return $this->render('profile');
|
||||
}
|
||||
|
||||
public function _404()
|
||||
{
|
||||
return $this->render('_404');
|
||||
}
|
||||
}
|
||||
@@ -1,93 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
use app\core\exceptions\ForbiddenException;
|
||||
use app\core\exceptions\NotFoundException;
|
||||
|
||||
class Application
|
||||
{
|
||||
public static string $ROOT_DIR;
|
||||
public string $userClass;
|
||||
public string $layout = 'main';
|
||||
|
||||
public Router $router;
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
public static Application $app;
|
||||
public ?Controller $controller = null;
|
||||
public Database $db;
|
||||
public Session $session;
|
||||
public ?DbModel $user;
|
||||
public View $view;
|
||||
|
||||
public function __construct($rootPath, array $config)
|
||||
{
|
||||
self::$ROOT_DIR = $rootPath;
|
||||
$this->userClass = $config['userClass'];
|
||||
self::$app = $this;
|
||||
|
||||
$this->request = new Request();
|
||||
$this->response = new Response();
|
||||
$this->router = new Router($this->request, $this->response);
|
||||
$this->view = new View();
|
||||
|
||||
$this->db = new Database($config['db']);
|
||||
$this->session = new Session();
|
||||
|
||||
$primaryValue = $this->session->get('user');
|
||||
if ($primaryValue) {
|
||||
$userObj = new $this->userClass();
|
||||
$primaryKey = $userObj->primaryKey();
|
||||
$this->user = $userObj->findOne([$primaryKey => $primaryValue]);
|
||||
} else {
|
||||
$this->user = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function setController($controller)
|
||||
{
|
||||
$this->controller = $controller;
|
||||
}
|
||||
|
||||
public function getController()
|
||||
{
|
||||
return $this->controller;
|
||||
}
|
||||
|
||||
public function login(DbModel $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$primaryKey = $user->primaryKey();
|
||||
$primaryValue = $user->{$primaryKey};
|
||||
$this->session->set('user', $primaryValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
$this->user = null;
|
||||
$this->session->remove('user');
|
||||
}
|
||||
|
||||
public static function isLoggedIn()
|
||||
{
|
||||
return !self::$app->user;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
try{
|
||||
echo $this->router->resolve();
|
||||
}catch(ForbiddenException $e){
|
||||
$this->response->setStatusCode(403);
|
||||
echo $this->view->renderView('_error', [
|
||||
'exception' => $e
|
||||
]);
|
||||
}catch(NotFoundException $e){
|
||||
$this->response->setStatusCode(404);
|
||||
echo $this->view->renderView('_error', [
|
||||
'exception' => $e
|
||||
]);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
use AllowDynamicProperties;
|
||||
use app\exceptions\ForbiddenAccessException;
|
||||
use app\exceptions\PageNotFoundException;
|
||||
|
||||
#[AllowDynamicProperties] class Application
|
||||
{
|
||||
public static string $ROOT_DIR;
|
||||
|
||||
public static Application $app;
|
||||
public Router $router;
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
public ?Controller $controller = null;
|
||||
public Session $session;
|
||||
public View $view;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
self::$ROOT_DIR = dirname(__DIR__);
|
||||
self::$app = $this;
|
||||
$this->request = new Request();
|
||||
$this->response = new Response();
|
||||
$this->router = new Router($this->request);
|
||||
$this->view = new View();
|
||||
$this->session = new Session();
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
try{
|
||||
echo $this->router->resolve();
|
||||
}catch(ForbiddenAccessException $e){
|
||||
|
||||
}catch(PageNotFoundException $e){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
use app\core\middlewares\BaseMiddleware;
|
||||
|
||||
class Controller{
|
||||
public string $layout = 'main';
|
||||
public string $action = '';
|
||||
protected array $middlewares = [];
|
||||
|
||||
public function render($view, $params = []){
|
||||
return Application::$app->view->renderView($view, $params);
|
||||
}
|
||||
|
||||
public function setLayout($layout){
|
||||
$this->layout = $layout;
|
||||
}
|
||||
|
||||
public function registerMiddleware(BaseMiddleware $middleware){
|
||||
$this->middlewares[] = $middleware;
|
||||
}
|
||||
|
||||
public function getMiddlewares(){
|
||||
return $this->middlewares;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
abstract class Controller{
|
||||
protected string $view = "";
|
||||
protected string $title = "";
|
||||
protected string $layout = "";
|
||||
protected ?Model $model = NULL;
|
||||
public function render(): string
|
||||
{
|
||||
Application::$app->view->setLayout($this->layout);
|
||||
Application::$app->view->setTitle($this->title);
|
||||
Application::$app->view->setView($this->view);
|
||||
Application::$app->view->setModel($this->model);
|
||||
|
||||
return Application::$app->view->render();
|
||||
}
|
||||
|
||||
protected static abstract function setControllerParams(string $title, string $view, string $layout, ?Model $model);
|
||||
|
||||
public function setView(string $view): void {$this->view = $view;}
|
||||
public function setTitle(string $title): void {$this->title = $title;}
|
||||
public function setLayout(string $layout): void {$this->layout = $layout;}
|
||||
public function setModel(?Model $model): void {$this->model = $model;}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
Class DVC
|
||||
{
|
||||
public static string $nameOfResponsePackage = "response_package";
|
||||
|
||||
public static string $successName = "Success";
|
||||
public static string $genericExceptionName = "GenericErrorException";
|
||||
public static string $invalidTokenName = "InvalidTokenException";
|
||||
public static string $invalidRequestName = "InvalidRequestException";
|
||||
public static string $invalidCurlStructureName = "InvalidCurlStructureException";
|
||||
public static string $invalidBodyName = "InvalidBodyException";
|
||||
public static string $alreadyExistsName = "AlreadyExistsException";
|
||||
public static string $userNotExistingName = "userNotExistingException";
|
||||
public static string $fieldNotExistingName = "FieldNotExistingException";
|
||||
public static string $pageNotFoundName = "pageNotfoundException";
|
||||
public static string $forbiddenAccessName = "forbiddenAccessException";
|
||||
public static string $serviceNotFoundName = "FieldNotFoundException";
|
||||
|
||||
public static string $returnCode = "returnCode";
|
||||
public static string $returnMessage = "returnMessage";
|
||||
|
||||
public static int $successReturnCode = 460;
|
||||
|
||||
public static function getArrayResponse(string $name): array
|
||||
{
|
||||
return match ($name){
|
||||
DVC::$successName => [
|
||||
DVC::$returnCode => 460,
|
||||
DVC::$returnMessage => "Operation done successfully"
|
||||
],
|
||||
DVC::$genericExceptionName => [
|
||||
DVC::$returnCode => 461,
|
||||
DVC::$returnMessage => "Internal Server Error"
|
||||
],
|
||||
DVC::$invalidTokenName => [
|
||||
DVC::$returnCode => 462,
|
||||
DVC::$returnMessage => "Unauthorized access"
|
||||
],
|
||||
DVC::$invalidRequestName => [
|
||||
DVC::$returnCode => 463,
|
||||
DVC::$returnMessage => "Service doesn't provide this type of request"
|
||||
],
|
||||
DVC::$invalidBodyName => [
|
||||
DVC::$returnCode => 464,
|
||||
DVC::$returnMessage => "Not valid parameters sent"
|
||||
],
|
||||
DVC::$alreadyExistsName => [
|
||||
|
||||
DVC::$returnCode => 465,
|
||||
DVC::$returnMessage => "Field already existing"
|
||||
],
|
||||
DVC::$fieldNotExistingName => [
|
||||
DVC::$returnCode => 466,
|
||||
DVC::$returnMessage => "Field not existing anymore"
|
||||
],
|
||||
DVC::$invalidCurlStructureName => [
|
||||
DVC::$returnCode => 467,
|
||||
DVC::$returnMessage => "cURL class needs URL, request method and headers to run"
|
||||
],
|
||||
DVC::$userNotExistingName => [
|
||||
DVC::$returnCode => 468,
|
||||
DVC::$returnMessage => "User account doesn't exists"
|
||||
],
|
||||
DVC::$pageNotFoundName => [
|
||||
DVC::$returnCode => 469,
|
||||
DVC::$returnMessage => "Page not found"
|
||||
],
|
||||
DVC::$forbiddenAccessName => [
|
||||
DVC::$returnCode => 470,
|
||||
DVC::$returnMessage => "You are not authorised to access this page"
|
||||
],
|
||||
DVC::$serviceNotFoundName => [
|
||||
DVC::$returnCode => 471,
|
||||
DVC::$returnCode => "Service not existing"
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
public static function getHttpCodeWithCode(int $code): int
|
||||
{
|
||||
return match ($code){
|
||||
460 => 200,
|
||||
461 => 500,
|
||||
462 => 401,
|
||||
463 => 405,
|
||||
464 => 400,
|
||||
465, 466 => 204,
|
||||
467 => 409,
|
||||
468, 469, 471 => 404,
|
||||
470 => 403,
|
||||
default => 100
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,78 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Database{
|
||||
public \PDO $pdo;
|
||||
|
||||
public function __construct(array $config){
|
||||
$dsn = $config['dsn'] ?? '';
|
||||
$user = $config['user'] ?? '';
|
||||
$password = $config['password'] ?? '';
|
||||
$this->pdo = new \PDO($dsn, $user, $password);
|
||||
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
public function applyMigrations(){
|
||||
$this->createMigrationsTable();
|
||||
$appliedMigrations = $this->getAppliedMigrations();
|
||||
|
||||
$newMigrations = [];
|
||||
$files = scandir(Application::$ROOT_DIR.'/migrations');
|
||||
$toApplyMigrations = array_diff($files, $appliedMigrations);
|
||||
|
||||
foreach($toApplyMigrations as $migration){
|
||||
if($migration === '.' || $migration === '..'){
|
||||
continue;
|
||||
}
|
||||
|
||||
require_once Application::$ROOT_DIR.'/migrations/'.$migration;
|
||||
$className = pathinfo($migration, PATHINFO_FILENAME);
|
||||
|
||||
$instance = new $className();
|
||||
$this->log("Applying migration $migration" . PHP_EOL);
|
||||
$instance->up();
|
||||
$this->log("Applied migration $migration" . PHP_EOL);
|
||||
|
||||
$newMigrations[] = $migration;
|
||||
}
|
||||
|
||||
if(!empty($newMigrations)){
|
||||
$this->saveMigrations($newMigrations);
|
||||
}else{
|
||||
$this->log('All migrations are applied');
|
||||
}
|
||||
}
|
||||
|
||||
public function createMigrationsTable(){
|
||||
$this->pdo->exec("
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
id INT AUTO_INCREMENT,
|
||||
migration varchar(255),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
ENGINE=INNODB;");
|
||||
}
|
||||
|
||||
public function getAppliedMigrations(){
|
||||
$statement = $this->pdo->prepare("SELECT migration FROM migrations");
|
||||
$statement->execute();
|
||||
|
||||
return $statement->fetchAll(\PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
public function saveMigrations(array $migrations){
|
||||
$str = implode(",", array_map(fn($m) => "('$m')", $migrations));
|
||||
$statement = $this->pdo->prepare("INSERT INTO migrations (migration) VALUES $str");
|
||||
$statement->execute();
|
||||
}
|
||||
|
||||
public function prepare($sql){
|
||||
return $this->pdo->prepare($sql);
|
||||
}
|
||||
|
||||
protected function log($message){
|
||||
echo '['.date('Y-m-d H:i:s').'] - '.$message.PHP_EOL;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Database{
|
||||
public \PDO $pdo;
|
||||
|
||||
public function __construct(array $config){
|
||||
$dsn = $config['dsn'] ?? '';
|
||||
$user = $config['user'] ?? '';
|
||||
$password = $config['password'] ?? '';
|
||||
$this->pdo = new \PDO($dsn, $user, $password);
|
||||
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
public function applyMigrations(){
|
||||
$this->createMigrationsTable();
|
||||
$appliedMigrations = $this->getAppliedMigrations();
|
||||
|
||||
$newMigrations = [];
|
||||
$files = scandir(Application::$ROOT_DIR.'/migrations');
|
||||
$toApplyMigrations = array_diff($files, $appliedMigrations);
|
||||
|
||||
foreach($toApplyMigrations as $migration){
|
||||
if($migration === '.' || $migration === '..'){
|
||||
continue;
|
||||
}
|
||||
|
||||
require_once Application::$ROOT_DIR.'/migrations/'.$migration;
|
||||
$className = pathinfo($migration, PATHINFO_FILENAME);
|
||||
|
||||
$instance = new $className();
|
||||
$this->log("Applying migration $migration" . PHP_EOL);
|
||||
$instance->up();
|
||||
$this->log("Applied migration $migration" . PHP_EOL);
|
||||
|
||||
$newMigrations[] = $migration;
|
||||
}
|
||||
|
||||
if(!empty($newMigrations)){
|
||||
$this->saveMigrations($newMigrations);
|
||||
}else{
|
||||
$this->log('All migrations are applied');
|
||||
}
|
||||
}
|
||||
|
||||
public function createMigrationsTable(){
|
||||
$this->pdo->exec("
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
id INT AUTO_INCREMENT,
|
||||
migration varchar(255),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
ENGINE=INNODB;");
|
||||
}
|
||||
|
||||
public function getAppliedMigrations(){
|
||||
$statement = $this->pdo->prepare("SELECT migration FROM migrations");
|
||||
$statement->execute();
|
||||
|
||||
return $statement->fetchAll(\PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
public function saveMigrations(array $migrations){
|
||||
$str = implode(",", array_map(fn($m) => "('$m')", $migrations));
|
||||
$statement = $this->pdo->prepare("INSERT INTO migrations (migration) VALUES $str");
|
||||
$statement->execute();
|
||||
}
|
||||
|
||||
public function prepare($sql){
|
||||
return $this->pdo->prepare($sql);
|
||||
}
|
||||
|
||||
protected function log($message){
|
||||
echo '['.date('Y-m-d H:i:s').'] - '.$message.PHP_EOL;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
abstract class DbModel extends Model{
|
||||
abstract public function tableName(): string;
|
||||
|
||||
abstract public function attributes(): array;
|
||||
|
||||
abstract public function primaryKey(): string;
|
||||
|
||||
public function save(){
|
||||
$tableName = $this->tableName();
|
||||
$attributes = $this->attributes();
|
||||
$params = array_map(fn($attr) => ":$attr", $attributes);
|
||||
|
||||
$statement = self::prepare("INSERT INTO $tableName ("
|
||||
.implode(',', $attributes)
|
||||
.") VALUES ("
|
||||
.implode(',', $params)
|
||||
.")"
|
||||
);
|
||||
|
||||
foreach($attributes as $attribute){
|
||||
$statement->bindValue(":$attribute", $this->{$attribute});
|
||||
}
|
||||
|
||||
$statement->execute();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function prepare($sql){
|
||||
return Application::$app->db->pdo->prepare($sql);
|
||||
}
|
||||
|
||||
public function findOne($where){
|
||||
$tableName = static::tableName();
|
||||
$attributes = array_keys($where);
|
||||
|
||||
$sql = implode("AND ", array_map(fn($attr) => "$attr = :$attr", $attributes));
|
||||
$statement = self::prepare("SELECT * from $tableName WHERE $sql");
|
||||
|
||||
foreach($where as $key => $item){
|
||||
$statement->bindValue(":$key", $item);
|
||||
}
|
||||
|
||||
$statement->execute();
|
||||
|
||||
return $statement->fetchObject(static::class);
|
||||
}
|
||||
}
|
||||
@@ -1,104 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
abstract class Model{
|
||||
public const RULE_REQUIRED = 'required';
|
||||
public const RULE_EMAIL = 'email';
|
||||
public const RULE_MIN = 'min';
|
||||
public const RULE_MAX = 'max';
|
||||
public const RULE_UNIQUE = 'unique';
|
||||
|
||||
public function loadData($data){
|
||||
foreach ($data as $key => $value){
|
||||
if(property_exists($this, $key)){
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function rules():array;
|
||||
|
||||
public function labels(): array{
|
||||
return [];
|
||||
}
|
||||
|
||||
public array $errors = [];
|
||||
|
||||
private function addErrorForRule(string $attribute, string $rule, $params =[]){
|
||||
$message = $this->errorMessages()[$rule] ?? '';
|
||||
foreach($params as $key => $value){
|
||||
$message = str_replace("{{$key}}", $value, $message);
|
||||
}
|
||||
|
||||
$this->errors[$attribute][] = $message;
|
||||
}
|
||||
|
||||
public function addError(string $attribute, string $message){
|
||||
$this->errors[$attribute][] = $message;
|
||||
}
|
||||
|
||||
public function errorMessages(){
|
||||
return [
|
||||
self::RULE_REQUIRED => 'This field is required',
|
||||
self::RULE_EMAIL => 'This field must be valid email address',
|
||||
self::RULE_MIN => 'Min length of this field must be {min}',
|
||||
self::RULE_MAX => 'Max length of this field must be {max}',
|
||||
self::RULE_UNIQUE => 'Email already exists'
|
||||
];
|
||||
}
|
||||
|
||||
public function validate(){
|
||||
foreach($this->rules() as $attribute => $rules){
|
||||
$value = $this->{$attribute};
|
||||
foreach($rules as $rule){
|
||||
$ruleName = $rule;
|
||||
|
||||
if(!is_string($ruleName)){
|
||||
$ruleName = $rule[0];
|
||||
}
|
||||
|
||||
if($ruleName === self::RULE_REQUIRED && !$value){
|
||||
$this->addErrorForRule($attribute, self::RULE_REQUIRED);
|
||||
}
|
||||
|
||||
if($ruleName === self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)){
|
||||
$this->addErrorForRule($attribute, self::RULE_EMAIL);
|
||||
}
|
||||
|
||||
if($ruleName === self::RULE_MIN && strlen($value) < $rule['min']){
|
||||
$this->addErrorForRule($attribute, self::RULE_MIN, $rule);
|
||||
}
|
||||
|
||||
if($ruleName === self::RULE_MAX && strlen($value) > $rule['max']){
|
||||
$this->addErrorForRule($attribute, self::RULE_MAX, $rule);
|
||||
}
|
||||
|
||||
if($ruleName === self::RULE_UNIQUE){
|
||||
$className = $rule['class'];
|
||||
$uniqueAttr = $rule['attirubute'] ?? $attribute;
|
||||
$tableName = $className::tableName();
|
||||
|
||||
$statement = Application::$app->db->prepare(("SELECT * FROM $tableName WHERE $uniqueAttr = :attr"));
|
||||
$statement->bindValue(":attr", $value);
|
||||
$statement->execute();
|
||||
$record = $statement->fetchObject();
|
||||
|
||||
if($record){
|
||||
$this->addErrorForRule($attribute, SELF::RULE_UNIQUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return empty($this->errors);
|
||||
}
|
||||
|
||||
public function hasError($attribute){
|
||||
return $this->errors[$attribute] ?? false;
|
||||
}
|
||||
|
||||
public function getFirstError($attribute){
|
||||
return $this->errors[$attribute][0] ?? '';
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
use app\external_requests\CurlRequestBuilder;
|
||||
|
||||
abstract class Model{
|
||||
public const RULE_REQUIRED = 'required';
|
||||
public const RULE_EMAIL = 'email';
|
||||
public array $errors = array();
|
||||
|
||||
|
||||
private ?string $request = NULL;
|
||||
private string $url = "192.168.0.13:701/authentication";
|
||||
private ?array $headers = [];
|
||||
private ?array $body = [];
|
||||
|
||||
|
||||
private ?int $code = NULL;
|
||||
private ?string $message = NULL;
|
||||
|
||||
public function loadData($data): void
|
||||
{
|
||||
foreach ($data as $key => $value){
|
||||
if(property_exists($this, $key)){
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
abstract public function rules(): array;
|
||||
public abstract function labels(): array;
|
||||
public function errorMessages(): array{
|
||||
return [
|
||||
self::RULE_REQUIRED => 'This field is required',
|
||||
self::RULE_EMAIL => 'This field must be a valid email address',
|
||||
];
|
||||
}
|
||||
|
||||
public function checkForErrors(): void{
|
||||
foreach($this->rules() as $field => $rules){
|
||||
$value = $this->{$field};
|
||||
foreach ($rules as $rule){
|
||||
if($rule == self::RULE_REQUIRED && !strlen($value)) {
|
||||
$this->addError($field, $this->errorMessages()[$rule]);
|
||||
break;
|
||||
}
|
||||
|
||||
if($rule == self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->addError($field, $this->errorMessages()[$rule]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract function createBody(): array;
|
||||
protected abstract function createHeaders(string $body): array;
|
||||
protected abstract function formatHeaderArray(array $headers): array;
|
||||
public function setParametersForRequest(string $request, array $headers, array $body): void
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->headers = $headers;
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
public function execute(): void
|
||||
{
|
||||
$requestBuilder = new CurlRequestBuilder();
|
||||
|
||||
$requestBuilder->setRequest($this->request);
|
||||
$requestBuilder->setUrl($this->url);
|
||||
$requestBuilder->setHeaders($this->headers);
|
||||
$requestBuilder->setBody($this->body);
|
||||
$requestBuilder->setIsReturnable(true);
|
||||
|
||||
$curl = $requestBuilder->getCurlRequestObject();
|
||||
$response = json_decode($curl->makeRequest(), true);
|
||||
|
||||
$this->code = $response["code"];
|
||||
$this->message = $response["message"];
|
||||
}
|
||||
|
||||
public function isRequestFulfilled(): bool
|
||||
{
|
||||
return $this->code == DVC::$successReturnCode;
|
||||
}
|
||||
|
||||
public function setRequestError(): void
|
||||
{
|
||||
Application::$app->view->setActionError($this->message);
|
||||
}
|
||||
|
||||
|
||||
public function addError($attribute, $message): void
|
||||
{
|
||||
$this->errors[$attribute] = $message;
|
||||
}
|
||||
|
||||
public function removeErrors(): void
|
||||
{
|
||||
unset($this->errors);
|
||||
$this->errors = array();
|
||||
}
|
||||
|
||||
public function hasErrors(): bool
|
||||
{
|
||||
return count($this->errors);
|
||||
}
|
||||
|
||||
public function getError($field): ?string
|
||||
{
|
||||
return array_key_exists($field, $this->errors) ? $this->errors[$field] : NULL;
|
||||
}
|
||||
|
||||
public function getValueForField($field):string
|
||||
{
|
||||
return $this->{$field};
|
||||
}
|
||||
}
|
||||
@@ -1,54 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Request{
|
||||
private int $lengthOfRoot;
|
||||
|
||||
public function __construct(){
|
||||
$this->lengthOfRoot = strlen('knowyourfood') + 1;
|
||||
}
|
||||
|
||||
public function getPath(){//identificam din URL pe pagina trebuie accesata
|
||||
$path = $_SERVER['REQUEST_URI'];
|
||||
$path = substr($path, $this->lengthOfRoot);
|
||||
|
||||
$position = strpos($path, '?');
|
||||
|
||||
if(!$position){
|
||||
return $path;
|
||||
}
|
||||
|
||||
return substr($path, 0, $position);
|
||||
}
|
||||
|
||||
public function method(){
|
||||
return strtolower($_SERVER['REQUEST_METHOD']);
|
||||
}
|
||||
|
||||
public function isGet(){
|
||||
return $this->method() === 'get';
|
||||
}
|
||||
|
||||
public function isPost(){
|
||||
return $this->method() === 'post';
|
||||
}
|
||||
|
||||
//bazat pe tipul de request, accesam tuplele key/value pentru a identifica body ul requestului
|
||||
public function getBody(){
|
||||
$body = [];
|
||||
if($this->method() === 'get'){
|
||||
foreach($_GET as $key => $value){
|
||||
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
}
|
||||
}
|
||||
|
||||
if($this->method() === 'post'){
|
||||
foreach($_POST as $key => $value){
|
||||
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
}
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Request{
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public function getPath(): string
|
||||
{
|
||||
return $_SERVER['REQUEST_URI'];
|
||||
}
|
||||
|
||||
public function method():string
|
||||
{
|
||||
return $_SERVER['REQUEST_METHOD'];
|
||||
}
|
||||
|
||||
public function isGet(): bool
|
||||
{
|
||||
return $_SERVER['REQUEST_METHOD'] === "GET";
|
||||
}
|
||||
|
||||
public function isPost(): bool
|
||||
{
|
||||
return $_SERVER['REQUEST_METHOD'] === "POST";
|
||||
}
|
||||
|
||||
public function getGETBody(): ?array
|
||||
{
|
||||
$body = [];
|
||||
|
||||
if($this->method() === 'GET'){
|
||||
foreach($_GET as $key => $value){
|
||||
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
}
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function getPOSTBody(): ?array
|
||||
{
|
||||
$body = [];
|
||||
|
||||
if($this->method() === 'POST'){
|
||||
foreach($_POST as $key => $value){
|
||||
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
}
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Response{
|
||||
public function setStatusCode(int $code){
|
||||
http_response_code($code);
|
||||
}
|
||||
|
||||
public function redirect(string $url){
|
||||
header("Location: ".$url);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Response{
|
||||
public function setStatusCode(int $code){
|
||||
http_response_code($code);
|
||||
}
|
||||
|
||||
public function redirect(string $url){
|
||||
header("Location: ".$url);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
use app\core\exceptions\NotFoundException;
|
||||
|
||||
class Router
|
||||
{
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
protected array $routes = [];
|
||||
|
||||
public function __construct(Request $request, Response $response)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function get($path, $callback)
|
||||
{
|
||||
$this->routes['get'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function post($path, $callback)
|
||||
{
|
||||
$this->routes['post'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function resolve()
|
||||
{
|
||||
$path = $this->request->getPath();
|
||||
$method = $this->request->method();
|
||||
$callback = $this->routes[$method][$path] ?? false;
|
||||
|
||||
if (!$callback) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
$controller = new $callback[0]();
|
||||
Application::$app->controller = $controller;
|
||||
$controller->action = $callback[1];
|
||||
$callback[0] = $controller;
|
||||
|
||||
foreach ($controller->getMiddlewares() as $middleware) {
|
||||
$middleware->execute();
|
||||
}
|
||||
|
||||
return call_user_func($callback, $this->request, $this->response);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
use app\exceptions\PageNotFoundException;
|
||||
|
||||
class Router
|
||||
{
|
||||
public Request $request;
|
||||
protected array $routes = [];
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function get($path, $callback): void
|
||||
{
|
||||
$this->routes['GET'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function post($path, $callback): void
|
||||
{
|
||||
$this->routes['POST'][$path] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws PageNotFoundException
|
||||
*/
|
||||
public function resolve()
|
||||
{
|
||||
$path = $this->request->getPath();
|
||||
$method = $this->request->method();
|
||||
$callback = $this->routes[$method][$path] ?? false;
|
||||
|
||||
if (!$callback) {
|
||||
throw new PageNotFoundException();
|
||||
}
|
||||
|
||||
Application::$app->controller = new $callback[0]();
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* de adaugat aici middleware urile asociate unui controller
|
||||
*/
|
||||
|
||||
return call_user_func($callback, $this->request);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Session{
|
||||
protected const FLASH_KEY = 'flash_messages';
|
||||
|
||||
public function __construct(){
|
||||
session_start();
|
||||
foreach($_SESSION[self::FLASH_KEY] as $key => &$flashMessage){
|
||||
$flashMessage['remove'] = 'true';
|
||||
}
|
||||
}
|
||||
|
||||
public function setFlash($key, $message){
|
||||
$_SESSION[self::FLASH_KEY][$key] = [
|
||||
'value' => $message,
|
||||
'remove' => 'false'
|
||||
];
|
||||
}
|
||||
|
||||
public function getFlash($key){
|
||||
return $_SESSION[self::FLASH_KEY][$key]['value'] ?? false;
|
||||
}
|
||||
|
||||
public function set($key, $value){
|
||||
$_SESSION[$key] = $value;
|
||||
}
|
||||
|
||||
public function get($key){
|
||||
return $_SESSION[$key];
|
||||
}
|
||||
|
||||
public function remove($key){
|
||||
unset($_SESSION[$key]);
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
$flashMessages = $_SESSION[self::FLASH_KEY] ?? [];
|
||||
foreach($flashMessages as $key => &$flashMessage){
|
||||
if($flashMessage['remove'] == 'true'){
|
||||
unset($flashMessages[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$_SESSION[self::FLASH_KEY] = $flashMessages;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class Session{
|
||||
|
||||
}
|
||||
@@ -1,45 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class View{
|
||||
public string $title = '';
|
||||
|
||||
protected function layoutContent()
|
||||
{
|
||||
$layout = Application::$app->layout;
|
||||
if (Application::$app->controller) {
|
||||
$layout = Application::$app->controller->layout;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
include_once Application::$ROOT_DIR . "/views/layouts/$layout.php";
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
|
||||
public function renderView($view, $params = [])
|
||||
{
|
||||
$viewContent = $this->renderOnlyView($view, $params);
|
||||
$layoutContent = $this->layoutContent();
|
||||
|
||||
return str_replace('{{content}}', $viewContent, $layoutContent);
|
||||
}
|
||||
|
||||
public function renderContent($viewContent)
|
||||
{
|
||||
$layoutContent = $this->layoutContent();
|
||||
return str_replace('{{content}}', $viewContent, $layoutContent);
|
||||
}
|
||||
|
||||
protected function renderOnlyView($view, $params)
|
||||
{
|
||||
foreach ($params as $key => $value) {
|
||||
$$key = $value;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
include_once Application::$ROOT_DIR."/views/$view.php";
|
||||
return ob_get_clean();
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
class View{
|
||||
private string $title = '';
|
||||
private string $view = '';
|
||||
private string $layout = 'main';
|
||||
private ?Model $model = NULL;
|
||||
private string $actionError = '';
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$viewContent = $this->renderView();
|
||||
$layoutContent = $this->renderLayout();
|
||||
|
||||
return str_replace('{{content}}', $viewContent, $layoutContent);
|
||||
}
|
||||
|
||||
public function renderView(): string
|
||||
{
|
||||
ob_start();
|
||||
include_once Application::$ROOT_DIR . "/views/$this->view.php";
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
public function renderLayout(): string
|
||||
{
|
||||
ob_start();
|
||||
include_once Application::$ROOT_DIR . "/views/layouts/HTML/$this->layout.php";
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
public function setLayout($layout): void {$this->layout = $layout;}
|
||||
public function setTitle($title): void {$this->title = $title;}
|
||||
public function setView($view): void {$this->view = $view;}
|
||||
public function setModel(?Model $model): void {$this->model = $model;}
|
||||
public function setActionError(string $actionError): void {$this->actionError = $actionError;}
|
||||
|
||||
public function getTitle(): string {return $this->title;}
|
||||
public function getView(): string {return $this->view;}
|
||||
public function getLayout(): string {return $this->layout;}
|
||||
public function getModel(): ?Model {return $this->model;}
|
||||
public function getActionError(): string {return $this->actionError;}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\exceptions;
|
||||
|
||||
class ForbiddenException extends \Exception{
|
||||
protected $code = 403;
|
||||
protected $message = 'You do not have permission to access this page';
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\exceptions;
|
||||
|
||||
class NotFoundException extends \Exception{
|
||||
protected $code = 404;
|
||||
protected $message = 'Page not found';
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
abstract class BaseField
|
||||
{
|
||||
public Model $model;
|
||||
public string $attribute;
|
||||
|
||||
public function __construct(Model $model, string $attribute)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->attribute = $attribute;
|
||||
}
|
||||
|
||||
abstract public function renderInput(): string;
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return sprintf('
|
||||
<div class="mb-3">
|
||||
<label>%s</label>
|
||||
%s
|
||||
<div class="invalid-feedback">
|
||||
%s
|
||||
</div>
|
||||
</div>
|
||||
',
|
||||
$this->model->labels()[$this->attribute] ?? $this->attribute,
|
||||
$this->renderInput(),
|
||||
$this->model->getFirstError($this->attribute)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class Button{
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<button type="submit" class="button">Submit</button>
|
||||
',
|
||||
);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class Button{
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<button type="submit" class="button">Submit</button>
|
||||
',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
class Field{
|
||||
public const TYPE_TEXT = 'text';
|
||||
public const TYPE_PASSWORD = 'password';
|
||||
|
||||
public string $type;
|
||||
public Model $model;
|
||||
public string $attribute;
|
||||
|
||||
public function __construct(Model $model, string $attribute){
|
||||
$this->type = self::TYPE_TEXT;
|
||||
$this->model = $model;
|
||||
$this->attribute = $attribute;
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return sprintf('
|
||||
<div class="mb-3">
|
||||
<label>%s</label>
|
||||
<input type="%s" name="%s" value="%s" class="form-control%s">
|
||||
<div class="invalid-feedback">
|
||||
%s
|
||||
</div>
|
||||
</div>
|
||||
',
|
||||
$this->model->labels()[$this->attribute] ?? $this->attribute,
|
||||
$this->type,
|
||||
$this->attribute,
|
||||
$this->model->{$this->attribute},
|
||||
$this->model->hasError($this->attribute) ? ' is invalid' : '',
|
||||
$this->model->getFirstError($this->attribute)
|
||||
);
|
||||
}
|
||||
|
||||
public function passwordField(){
|
||||
$this->type = self::TYPE_PASSWORD;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
class Form{
|
||||
public static function begin($action, $method){
|
||||
echo sprintf('<form action ="%s" method ="%s">', $action, $method);
|
||||
return new Form();
|
||||
}
|
||||
|
||||
public static function end(){
|
||||
echo '</form>';
|
||||
}
|
||||
|
||||
public function inputField(Model $model, $attribute){
|
||||
return new InputField($model, $attribute);
|
||||
}
|
||||
|
||||
public function button(){
|
||||
return new Button();
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
use app\core\Model;
|
||||
|
||||
class Form{
|
||||
private string $formDestinationPage = "";
|
||||
|
||||
public function __construct(string $formDestinationPage){
|
||||
$this->formDestinationPage = $formDestinationPage;
|
||||
}
|
||||
public function beginForm(): void
|
||||
{
|
||||
echo "<form action='$this->formDestinationPage' method ='POST'>";
|
||||
}
|
||||
|
||||
public function endForm(): void
|
||||
{
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
public function renderInputField(string $type, string $name, string $placeholder, string $value, ?string $error): void
|
||||
{
|
||||
echo new InputField($type, $name, $placeholder, $value, $error);
|
||||
}
|
||||
|
||||
public function renderButton(): void
|
||||
{
|
||||
echo new Button();
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
use app\core\Model;
|
||||
|
||||
class InputField extends BaseField
|
||||
{
|
||||
public const TYPE_TEXT = 'text';
|
||||
public const TYPE_PASSWORD = 'password';
|
||||
|
||||
public string $type;
|
||||
|
||||
public function __construct(Model $model, string $attribute)
|
||||
{
|
||||
$this->type = self::TYPE_TEXT;
|
||||
parent::__construct($model, $attribute);
|
||||
}
|
||||
|
||||
public function passwordField()
|
||||
{
|
||||
$this->type = self::TYPE_PASSWORD;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function renderInput(): string
|
||||
{
|
||||
return sprintf(
|
||||
'<input type="%s" name="%s" value="%s" class="form-control%s">',
|
||||
$this->type,
|
||||
$this->attribute,
|
||||
$this->model->{$this->attribute},
|
||||
$this->model->hasError($this->attribute) ? ' is invalid' : ''
|
||||
);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class InputField
|
||||
{
|
||||
private string $type;
|
||||
private string $name;
|
||||
private string $placeholder;
|
||||
private string $value;
|
||||
private ?string $error;
|
||||
|
||||
public function __construct(string $type, string $name, string $placeholder, string $value, ?string $error)
|
||||
{
|
||||
$this->type = $this->getInputType($type);
|
||||
$this->name = $name;
|
||||
$this->placeholder = $placeholder;
|
||||
$this->value = $value;
|
||||
$this->error = $error;
|
||||
}
|
||||
public function __toString():string
|
||||
{
|
||||
$field = sprintf(
|
||||
'<input type="%s" name="%s" placeholder="%s" value="%s">',
|
||||
$this->type, $this->name, $this->placeholder, $this->value
|
||||
);
|
||||
|
||||
$errorField = NULL;
|
||||
if(!$this->error == NULL){
|
||||
$errorField = sprintf('<p class="errorStyle">%s</p>', $this->error);
|
||||
}
|
||||
|
||||
return $field.$errorField;
|
||||
}
|
||||
|
||||
private function getInputType(string $type): string{
|
||||
return match($type){
|
||||
"username", "email" => "text",
|
||||
"password" => "password"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
use app\core\Model;
|
||||
|
||||
class Region extends BaseField
|
||||
{
|
||||
public const TYPE_TEXT = 'region';
|
||||
public string $type;
|
||||
|
||||
public function __construct(Model $model, string $attribute)
|
||||
{
|
||||
$this->type = self::TYPE_TEXT;
|
||||
parent::__construct($model, $attribute);
|
||||
}
|
||||
|
||||
public function startSelect(): string
|
||||
{
|
||||
return sprintf('<select class="%s" name="%s">',
|
||||
$this->type,
|
||||
$this->type,
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function endSelect(): string
|
||||
{
|
||||
return '</select>';
|
||||
}
|
||||
|
||||
public function renderInput(): string
|
||||
{
|
||||
return $this->startSelect().$this->content().$this->endSelect();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class TextareaField extends BaseField{
|
||||
public function renderInput(): string{
|
||||
return sprintf('<textarea name=%s class=form-control%s>%s</textarea>',
|
||||
$this->attribute,
|
||||
$this->model->hasError($this->attribute) ? ' is invalid' : '',
|
||||
$this->model->{$this->attribute}
|
||||
);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core\form;
|
||||
|
||||
class TextareaField extends BaseField{
|
||||
public function renderInput(): string{
|
||||
return sprintf('<textarea name=%s class=form-control%s>%s</textarea>',
|
||||
$this->attribute,
|
||||
$this->model->hasError($this->attribute) ? ' is invalid' : '',
|
||||
$this->model->{$this->attribute}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\middlewares;
|
||||
use app\core\Application;
|
||||
use app\core\exceptions\ForbiddenException;
|
||||
|
||||
class AuthMiddleware extends BaseMiddleware{
|
||||
public array $actions = [];
|
||||
|
||||
public function __construct(array $actions = []){
|
||||
$this->actions = $actions;
|
||||
}
|
||||
|
||||
public function execute(){
|
||||
if(Application::isLoggedIn()){
|
||||
if(empty($this->actions) || in_array(Application::$app->controller->action, $this->actions)){
|
||||
throw new ForbiddenException;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\core\middlewares;
|
||||
|
||||
abstract class BaseMiddleware{
|
||||
abstract public function execute();
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
$config = [
|
||||
'userClass' => \app\models\User::class,
|
||||
'db' => [
|
||||
'dsn' => 'mysql:host=localhost;port=3306;dbname=Web',
|
||||
'user' => 'root',
|
||||
'password' => '',
|
||||
]
|
||||
];
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace app\exceptions;
|
||||
use app\core\DVC;
|
||||
use Exception;
|
||||
|
||||
class ForbiddenAccessException extends Exception
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$array = DVC::getArrayResponse(DVC::$forbiddenAccessName);
|
||||
parent::__construct($array[DVC::$returnMessage], $array[DVC::$returnCode]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace app\exceptions;
|
||||
use app\core\DVC;
|
||||
use Exception;
|
||||
|
||||
class GenericErrorException extends Exception
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$array = DVC::getArrayResponse(DVC::$genericExceptionName);
|
||||
parent::__construct($array[DVC::$returnMessage], $array[DVC::$returnCode]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\exceptions;
|
||||
use app\core\DVC;
|
||||
use Exception;
|
||||
|
||||
class InvalidCurlStructureException extends Exception
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$array = DVC::getArrayResponse(DVC::$invalidCurlStructureName);
|
||||
parent::__construct($array[DVC::$returnMessage], $array[DVC::$returnCode]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace app\exceptions;
|
||||
use app\core\DVC;
|
||||
use Exception;
|
||||
|
||||
class PageNotFoundException extends Exception
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$array = DVC::getArrayResponse(DVC::$pageNotFoundName);
|
||||
parent::__construct($array[DVC::$returnMessage], $array[DVC::$returnCode]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace app\external_requests;
|
||||
|
||||
use app\exceptions\GenericErrorException;
|
||||
|
||||
class CurlRequest
|
||||
{
|
||||
private string $url;
|
||||
private string $request;
|
||||
private array $headers;
|
||||
private ?array $body;
|
||||
private bool $isReturnable;
|
||||
|
||||
protected function __construct(string $url, string $request, array $headers, ?array $body, bool $isReturnable)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->request = $request;
|
||||
$this->headers = $headers;
|
||||
$this->body = $body;
|
||||
$this->isReturnable = $isReturnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GenericErrorException
|
||||
*/
|
||||
public function makeRequest(): string
|
||||
{
|
||||
$curl = curl_init();
|
||||
$this->setCurlBody($curl);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
if(!$response){
|
||||
throw new GenericErrorException();
|
||||
}
|
||||
curl_close($curl);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function setCurlBody(\CurlHandle &$curl): void
|
||||
{
|
||||
curl_setopt($curl, CURLOPT_URL, $this->url);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
|
||||
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->request);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, $this->isReturnable);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
|
||||
curl_setopt($curl,CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($this->body));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace app\external_requests;
|
||||
|
||||
use app\exceptions\InvalidCurlStructureException;
|
||||
|
||||
class CurlRequestBuilder extends CurlRequest
|
||||
{
|
||||
private ?string $url = NULL;
|
||||
private ?string $request = NULL;
|
||||
private ?array $headers = NULL;
|
||||
private ?array $body = NULL;
|
||||
private bool $isReturnable = false;
|
||||
|
||||
public function __construct(){}
|
||||
|
||||
public function setUrl(?string $url): void{$this->url = $url;}
|
||||
public function setHeaders(?array $headers): void{$this->headers = $headers;}
|
||||
public function setRequest(?string $request): void{$this->request = $request;}
|
||||
public function setBody(?array $body): void{$this->body = $body;}
|
||||
public function setIsReturnable(?bool $isReturnable): void{$this->isReturnable = $isReturnable;}
|
||||
|
||||
private function areMainParametersNotSet(): bool{
|
||||
return $this->url == NULL || $this->request == NULL || $this->headers == NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidCurlStructureException
|
||||
*/
|
||||
public function getCurlRequestObject(): CurlRequest{
|
||||
if($this->areMainParametersNotSet()){
|
||||
throw new InvalidCurlStructureException("Invalid structure for making a request");
|
||||
}
|
||||
|
||||
return new CurlRequest($this->url, $this->request, $this->headers, $this->body, $this->isReturnable);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,25 @@
|
||||
<?php
|
||||
|
||||
use app\controllers\AuthController;
|
||||
use app\core\Application;
|
||||
use app\controllers\SiteController;
|
||||
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
require_once __DIR__.'/dbconfig.php';
|
||||
|
||||
/*
|
||||
Clasa Application se va ocupa de stocarea asocierilor dintre o pagina si controller-ul corespunzator. Deoarece prin .htaccess am fortat
|
||||
ca user ul sa ajunga mereu in acest 'index.php' la orice URL specificat, putem identifica din URL ce pagina vrea sa acceseze pentru a apela
|
||||
un GET cu controller-ul, view ul si model ul corespunzator, ori printr-un submit sa se ajunga la metoda corespunzatoare de POST care sa intoarca
|
||||
un raspuns valid.
|
||||
*/
|
||||
|
||||
$app = new Application('.', $config);
|
||||
|
||||
$app->router->get('/', [SiteController::class, '/']);
|
||||
|
||||
$app->router->get('/home', [SiteController::class, 'home']);
|
||||
|
||||
$app->router->get('/_404', [SiteController::class, '_404']);
|
||||
|
||||
$app->router->get('/contact', [SiteController::class, 'contact']);
|
||||
$app->router->post('/contact', [SiteController::class, 'contact']);
|
||||
|
||||
$app->router->get('/login', [AuthController::class, 'login']);
|
||||
$app->router->post('/login', [AuthController::class, 'login']);
|
||||
|
||||
$app->router->get('/register', [AuthController::class, 'register']);
|
||||
$app->router->post('/register', [AuthController::class, 'register']);
|
||||
|
||||
$app->router->get('/profile', [AuthController::class, 'profile']);
|
||||
$app->router->post('/profile', [AuthController::class, 'profile']);
|
||||
|
||||
$app->router->get('/logout', [AuthController::class, 'logout']);
|
||||
|
||||
$app->router->get('/login/{id}', [SiteController::class, 'login']);
|
||||
|
||||
$app->run();
|
||||
<?php
|
||||
|
||||
use app\controllers\AuthController;
|
||||
use app\core\Application;
|
||||
use app\controllers\SiteController;
|
||||
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
|
||||
$app = new Application();
|
||||
|
||||
$app->router->get('/', [SiteController::class, 'renderMainPage']);
|
||||
$app->router->get('/homepage', [SiteController::class, 'homepage']);
|
||||
|
||||
$app->router->get('/login', [AuthController::class, 'loginPage']);
|
||||
$app->router->post('/login', [AuthController::class, 'loginPage']);
|
||||
|
||||
$app->router->get('/register', [AuthController::class, 'registerPage']);
|
||||
$app->router->post('/register', [AuthController::class, 'registerPage']);
|
||||
|
||||
$app->router->get('/profile', [AuthController::class, 'profile']);
|
||||
$app->router->post('/profile', [AuthController::class, 'profile']);
|
||||
|
||||
$app->router->get('/logout', [AuthController::class, 'logout']);
|
||||
|
||||
$app->run();
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
use app\core\Application;
|
||||
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
require_once __DIR__.'/dbconfig.php';
|
||||
|
||||
$app = new Application('.', $config);
|
||||
|
||||
$app->db->applyMigrations();
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
use app\core\Application;
|
||||
|
||||
class m01_initial{
|
||||
public function up(){
|
||||
$db = Application::$app->db;
|
||||
$SQL = "CREATE TABLE users(
|
||||
id INT AUTO_INCREMENT,
|
||||
email VARCHAR(255) NOT NULL,
|
||||
firstname VARCHAR(255) NOT NULL,
|
||||
lastname VARCHAR(255) NOT NULL,
|
||||
status TINYINT NOT NULL,
|
||||
create_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
) Engine=INNODB;
|
||||
";
|
||||
$db->pdo->exec($SQL);
|
||||
}
|
||||
|
||||
public function down(){
|
||||
$db = Application::$app->db;
|
||||
$SQL = "DROP TABLE users";
|
||||
$db->pdo->exec($SQL);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
use app\core\Application;
|
||||
|
||||
class m02_add_password_column{
|
||||
public function up(){
|
||||
$db = Application::$app->db;
|
||||
$SQL = "ALTER TABLE users ADD COLUMN password VARCHAR(512) NOT NULL";
|
||||
$db->pdo->exec($SQL);
|
||||
}
|
||||
|
||||
public function down(){
|
||||
$db = Application::$app->db;
|
||||
$SQL = "ALTER TABLE users DROP COLUMN password";
|
||||
$db->pdo->exec($SQL);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class ContactForm extends Model{
|
||||
public string $subject = '';
|
||||
public string $body = '';
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'subject' => [self::RULE_REQUIRED],
|
||||
'body' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels(): array{
|
||||
return [
|
||||
'subject' => 'Enter your subject',
|
||||
'body' => 'Enter the body'
|
||||
];
|
||||
}
|
||||
|
||||
public function send(){
|
||||
return true;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class ContactForm extends Model{
|
||||
public string $subject = '';
|
||||
public string $body = '';
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'subject' => [self::RULE_REQUIRED],
|
||||
'body' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels(): array{
|
||||
return [
|
||||
'subject' => 'Enter your subject',
|
||||
'body' => 'Enter the body'
|
||||
];
|
||||
}
|
||||
|
||||
public function send(){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Application;
|
||||
use app\core\Model;
|
||||
|
||||
class LoginForm extends Model{
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function rules(): array{
|
||||
return [
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
|
||||
'password' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels():array {
|
||||
return [
|
||||
'email' => 'Email',
|
||||
'password' => 'Password'
|
||||
];
|
||||
}
|
||||
|
||||
public function login(){
|
||||
$user = new User();
|
||||
$user = $user->findOne(['email' => $this->email]);
|
||||
if(!$user){
|
||||
$this->addError('email', 'User does not exist with this email');
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!password_verify($this->password, $user->password)){
|
||||
$this->addError('password', 'Incorrect password');
|
||||
return false;
|
||||
}
|
||||
|
||||
return Application::$app->login($user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class LoginModel extends Model{
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
|
||||
'password' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels():array
|
||||
{
|
||||
return [
|
||||
'email' => 'Email',
|
||||
'password' => 'Password'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function createBody(): array
|
||||
{
|
||||
$body = array();
|
||||
$body["values"] = [
|
||||
"email" => $this->email,
|
||||
"password" => $this->password
|
||||
];
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function createHeaders(string $body): array
|
||||
{
|
||||
$headers = array();
|
||||
$headers["Content-type"] = "application/json";
|
||||
$headers["Content-Length"] = strlen($body);
|
||||
$headers["Authorization"] = hash('md5', 'api.knowyourfood');
|
||||
$headers["Auth-Request"] = "login";
|
||||
|
||||
return $this->formatHeaderArray($headers);
|
||||
}
|
||||
|
||||
protected function formatHeaderArray(array $headers): array{
|
||||
$array = array();
|
||||
|
||||
foreach ($headers as $key => $value){
|
||||
$array[] = $key . ": " . $value;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class RegisterModel extends Model{
|
||||
public string $username = '';
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function rules(): array{
|
||||
return [
|
||||
'username' => [self::RULE_REQUIRED],
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
|
||||
'password' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels():array {
|
||||
return [
|
||||
'username' => 'Username',
|
||||
'email' => 'Email',
|
||||
'password' => 'Password',
|
||||
];
|
||||
}
|
||||
|
||||
public function createBody(): array
|
||||
{
|
||||
$body = array();
|
||||
$body["values"] = [
|
||||
"username" => $this->username,
|
||||
"email" => $this->email,
|
||||
"password" => $this->password
|
||||
];
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function createHeaders(string $body): array
|
||||
{
|
||||
$headers = array();
|
||||
$headers["Content-type"] = "application/json";
|
||||
$headers["Content-Length"] = strlen($body);
|
||||
$headers["Authorization"] = hash('md5', 'api.knowyourfood');
|
||||
$headers["Auth-Request"] = "register";
|
||||
|
||||
return $this->formatHeaderArray($headers);
|
||||
}
|
||||
|
||||
protected function formatHeaderArray(array $headers): array{
|
||||
$array = array();
|
||||
|
||||
foreach ($headers as $key => $value){
|
||||
$array[] = $key . ": " . $value;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\DbModel;
|
||||
|
||||
|
||||
class User extends DbModel{
|
||||
const STATUS_INACTIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
CONST STATUS_DELETED = 2;
|
||||
|
||||
public string $firstname = '';
|
||||
public string $lastname = '';
|
||||
public int $status = self::STATUS_INACTIVE;
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function save(){
|
||||
$this->status = self::STATUS_INACTIVE;
|
||||
$this->password = password_hash($this->password, PASSWORD_DEFAULT);
|
||||
return parent::save();
|
||||
}
|
||||
|
||||
public function tableName(): string{
|
||||
return 'users';
|
||||
}
|
||||
|
||||
public function rules(): array{
|
||||
return [
|
||||
'firstname' => [self::RULE_REQUIRED],
|
||||
'lastname' => [self::RULE_REQUIRED],
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [self::RULE_UNIQUE, 'class' => self::class]],
|
||||
'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8], [self::RULE_MAX, 'max' => 15]]
|
||||
];
|
||||
}
|
||||
|
||||
public function primaryKey(): string{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
public function labels():array {
|
||||
return [
|
||||
'firstname' => 'First name',
|
||||
'lastname' => 'Last name',
|
||||
'email' => 'Email',
|
||||
'password' => 'Password'
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes(): array{
|
||||
return ['firstname', 'lastname', 'email', 'password', 'status'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use app\core\Application;
|
||||
use app\core\form\Form;
|
||||
|
||||
$form = new Form('login');
|
||||
$model = Application::$app->view->getModel();
|
||||
$loginError = Application::$app->view->getActionError();
|
||||
|
||||
if(!$loginError == ''){
|
||||
echo "<p class='errorStyle'>$loginError</p>";
|
||||
}
|
||||
|
||||
echo '<hr class="hr1">';
|
||||
$form->beginForm();
|
||||
|
||||
foreach($model->labels() as $key=>$value){
|
||||
$form->renderInputField(
|
||||
$key, $key, $value, $model->getValueForField($key), $model->getError($key)
|
||||
);
|
||||
}
|
||||
|
||||
echo '<hr class="hr1">';
|
||||
|
||||
$form->renderButton();
|
||||
|
||||
$form->endForm();
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use app\core\Application;
|
||||
use app\core\form\Form;
|
||||
|
||||
$form = new Form('register');
|
||||
$model = Application::$app->view->getModel();
|
||||
$loginError = Application::$app->view->getActionError();
|
||||
|
||||
if(!$loginError == ''){
|
||||
echo "<p class='errorStyle'>$loginError</p>";
|
||||
}
|
||||
|
||||
echo '<hr class="hr1">';
|
||||
$form->beginForm();
|
||||
|
||||
foreach($model->labels() as $key=>$value){
|
||||
$form->renderInputField(
|
||||
$key, $key, $value, $model->getValueForField($key), $model->getError($key)
|
||||
);
|
||||
}
|
||||
|
||||
echo '<hr class="hr1">';
|
||||
|
||||
$form->renderButton();
|
||||
|
||||
$form->endForm();
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
<h1><?php echo $exception->getCode() ?> - <?php echo $exception->getMessage();?> </h1>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
use app\core\form\Form;
|
||||
use app\core\form\TextareaField;
|
||||
$this->title = 'Contact';
|
||||
|
||||
$form = Form::begin('', 'post');
|
||||
echo $form->inputField($model, 'subject');
|
||||
echo new TextareaField($model, 'body');
|
||||
echo $form->button();
|
||||
|
||||
Form::end();
|
||||
?>
|
||||
@@ -1,6 +0,0 @@
|
||||
<?php
|
||||
$this->title = 'Home';
|
||||
?>
|
||||
|
||||
<h1>Home</h1>
|
||||
<h2> Welcome <?php echo $name;?> to our site!</h2>
|
||||
@@ -0,0 +1,148 @@
|
||||
*{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
font-family: 'Josefin Sans', sans-serif;
|
||||
}
|
||||
|
||||
html,
|
||||
body{
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body{
|
||||
background-image: url("../Images/background/background.jpg");
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
background-attachment: fixed;
|
||||
}
|
||||
|
||||
body{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-evenly;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
h1{
|
||||
color: white;
|
||||
text-shadow:
|
||||
-1px -1px 0 #000,
|
||||
0 -1px 0 #000,
|
||||
1px -1px 0 #000,
|
||||
1px 0 0 #000,
|
||||
1px 1px 0 #000,
|
||||
0 1px 0 #000,
|
||||
-1px 1px 0 #000,
|
||||
-1px 0 0 #000;
|
||||
}
|
||||
|
||||
.hr1{
|
||||
width: 70%;
|
||||
}
|
||||
|
||||
.Title{
|
||||
margin: 3vh;
|
||||
margin-top: 0;
|
||||
font-size: 3rem;
|
||||
}
|
||||
|
||||
input, button{
|
||||
margin: 1rem;
|
||||
margin-left: 0;
|
||||
margin-right: 0;
|
||||
background-color: #60FF8C;
|
||||
border: 0.1em solid black;
|
||||
border-radius: 20px;
|
||||
padding: 1vh;
|
||||
padding-left: 5vh;
|
||||
padding-right: 5vh;
|
||||
min-width: 100%;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
button{
|
||||
background-color: #FF5555
|
||||
}
|
||||
|
||||
button:hover{
|
||||
background-color: #FF2B2B;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.Inputs{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.Inputs > *, button{
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
input[type="radio"]{
|
||||
margin:0;
|
||||
}
|
||||
|
||||
input:focus{
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.LoginButtons{
|
||||
width: 70%;
|
||||
}
|
||||
|
||||
.LoginForm{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.errorStyle{
|
||||
text-align: center;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.LoginButtons hr{
|
||||
width:100%;
|
||||
}
|
||||
|
||||
.BigContainer{
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
border-radius: 20px;
|
||||
position: fixed;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 2rem;
|
||||
transform: scale(1);
|
||||
flex: 100%;
|
||||
width:50%;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 800px){
|
||||
*{
|
||||
overflow: hidden;
|
||||
}
|
||||
.BigContainer{
|
||||
margin-top: 5vh;
|
||||
margin-bottom: 5vh;
|
||||
min-width: 70%;
|
||||
transform: scale(1);
|
||||
}
|
||||
.Inputs > *{
|
||||
width: 100%;
|
||||
}
|
||||
h1{
|
||||
font-size: 1rem;
|
||||
text-align: center;
|
||||
}
|
||||
.LoginButtons{
|
||||
width:100%;
|
||||
}
|
||||
.hr1{
|
||||
width:100%;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
font-family: 'Josefin Sans', sans-serif;
|
||||
}
|
||||
|
||||
html,
|
||||
body {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
background-image: url("../Images/background/background.jpg");
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
background-attachment: fixed;
|
||||
}
|
||||
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
h1, h3 {
|
||||
color: white;
|
||||
text-shadow: -1px -1px 0 #000, 0 -1px 0 #000, 1px -1px 0 #000, 1px 0 0 #000, 1px 1px 0 #000, 0 1px 0 #000, -1px 1px 0 #000, -1px 0 0 #000;
|
||||
}
|
||||
|
||||
.Title {
|
||||
margin: 5vh;
|
||||
font-size: 3rem;
|
||||
}
|
||||
|
||||
button {
|
||||
margin: 1rem;
|
||||
background-color: #60FF8C;
|
||||
border: 0.1em solid black;
|
||||
border-radius: 20px;
|
||||
padding: 1vh;
|
||||
padding-left: 5vh;
|
||||
padding-right: 5vh;
|
||||
max-width: 100%;
|
||||
cursor: default;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
margin: 1rem;
|
||||
background-color: #FF5555;
|
||||
border: 0.1em solid black;
|
||||
border-radius: 20px;
|
||||
padding: 1vh;
|
||||
padding-left: 5vh;
|
||||
padding-right: 5vh;
|
||||
max-width: 100%;
|
||||
cursor: pointer;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.RegisterButton {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.RegisterButton:hover {
|
||||
margin-bottom: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.LoginButtons {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
margin-bottom: 0;
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.BigContainer {
|
||||
position: fixed;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
transform: scale(1.5);
|
||||
flex: 100%;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 888px) {
|
||||
.BigContainer {
|
||||
width: 30%;
|
||||
}
|
||||
|
||||
h1, h3 {
|
||||
font-size: 1rem;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
use app\core\Application;
|
||||
$pathToCSS = "/views/layouts/CSS/styleAuth.css";
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" type="text/css" href=<?php echo $pathToCSS ?> />
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans&display=swap" rel="stylesheet">
|
||||
<title><?php echo Application::$app->view->getTitle()?></title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="BigContainer">
|
||||
<h1 class="Title"><?php echo Application::$app->view->getTitle()?></h1>
|
||||
<div class="LoginForm">
|
||||
{{content}}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php $pathToCSS = "/views/layouts/CSS/styleMain.css";?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">
|
||||
<link rel="stylesheet" type="text/css" href=<?php echo $pathToCSS ?>>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans&display=swap" rel="stylesheet">
|
||||
<title>Know Your Food.</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="BigContainer">
|
||||
<h1 class="Title">Know Your Food.</h1>
|
||||
<div class="LoginButtons">
|
||||
<h3>If you have an account already</h3>
|
||||
<button type="button" class="LoginButton" onclick="location.href='login'">Login</button>
|
||||
<h3>or</h3>
|
||||
<button type="button" class="RegisterButton" onclick="location.href='register'">Register</button>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
After Width: | Height: | Size: 71 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 417 KiB |
|
After Width: | Height: | Size: 312 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 2.1 MiB |
|
After Width: | Height: | Size: 5.5 KiB |
@@ -0,0 +1,5 @@
|
||||
recipe1 - chilli pork
|
||||
recipe2 - garlic & paprika chicken
|
||||
recipe3 - garlic butter chicken
|
||||
recipe4 - Marry Me Chicken
|
||||
recipe5 - Teriyaki Chicken Noodle
|
||||
|
After Width: | Height: | Size: 442 KiB |
|
After Width: | Height: | Size: 188 KiB |
|
After Width: | Height: | Size: 268 KiB |
|
After Width: | Height: | Size: 227 KiB |
|
After Width: | Height: | Size: 426 KiB |
|
After Width: | Height: | Size: 197 KiB |
|
After Width: | Height: | Size: 130 KiB |
|
After Width: | Height: | Size: 207 KiB |
|
After Width: | Height: | Size: 452 KiB |
|
After Width: | Height: | Size: 164 KiB |
|
After Width: | Height: | Size: 1011 KiB |
|
After Width: | Height: | Size: 139 KiB |
|
After Width: | Height: | Size: 163 KiB |
@@ -1,27 +0,0 @@
|
||||
<?php use app\core\Application ?>
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Bootstrap demo</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<?php if(Application::$app->session->getFlash('success')): ?>
|
||||
<div class="alert alert-success">
|
||||
<?php
|
||||
echo Application::$app->session->getFlash('success');
|
||||
?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
{{content}}
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
use app\core\Application;
|
||||
?>
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title><?php echo $this->title; ?></title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<nav class="navbar navbar-expand-lg bg-body-tertiary">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">Navbar</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
|
||||
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
|
||||
aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" href="/knowyourfood/home">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/knowyourfood/contact">contact</a>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" href="/knowyourfood/login">Login</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/knowyourfood/register">Register</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/knowyourfood/profile">Profile</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container">
|
||||
<?php
|
||||
$flashMessage = Application::$app->session->getFlash('success');
|
||||
if($flashMessage){
|
||||
echo $flashMessage;
|
||||
}
|
||||
?>
|
||||
{{content}}
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
use app\core\form\Form;
|
||||
$this->title='Login';
|
||||
|
||||
$form = Form::begin('', 'post');
|
||||
|
||||
echo $form->InputField($model, 'email');
|
||||
echo $form->inputField($model, 'password')->passwordField();
|
||||
|
||||
echo $form->button();
|
||||
|
||||
Form::end()
|
||||
?>
|
||||
|
||||
<h1>Login</h1>
|
||||
@@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
$this->title = 'Profile';
|
||||
|
||||
?>
|
||||
|
||||
<h1>Profile</h1>
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
use app\core\form\Form;
|
||||
$this->title = 'Register';
|
||||
|
||||
$form = Form::begin('', 'post');
|
||||
|
||||
echo $form->inputField($model, 'firstname');
|
||||
echo $form->inputField($model, 'lastname');
|
||||
echo $form->inputField($model, 'email');
|
||||
echo $form->inputField($model, 'password')->passwordField();
|
||||
|
||||
echo $form->button();
|
||||
|
||||
Form::end()
|
||||
?>
|
||||
|
||||
<h1>Create An Account</h1>
|
||||