%s
', $this->error); + } + + return $field.$errorField; + } + + private function getInputType(string $type): string{ + return match($type){ + "username", "email" => "text", + "password" => "password" + }; + } } \ No newline at end of file diff --git a/knowyourfood/core/form/Region.php b/knowyourfood/core/form/Region.php new file mode 100755 index 0000000..a2f35f2 --- /dev/null +++ b/knowyourfood/core/form/Region.php @@ -0,0 +1,40 @@ +type = self::TYPE_TEXT; + parent::__construct($model, $attribute); + } + + public function startSelect(): string + { + return sprintf(''; + } + + public function renderInput(): string + { + return $this->startSelect().$this->content().$this->endSelect(); + } +} \ No newline at end of file diff --git a/knowyourfood/core/form/TextareaField.php b/knowyourfood/core/form/TextareaField.php old mode 100644 new mode 100755 index 43baa3d..888e678 --- a/knowyourfood/core/form/TextareaField.php +++ b/knowyourfood/core/form/TextareaField.php @@ -1,13 +1,13 @@ -%s', - $this->attribute, - $this->model->hasError($this->attribute) ? ' is invalid' : '', - $this->model->{$this->attribute} - ); - } +%s', + $this->attribute, + $this->model->hasError($this->attribute) ? ' is invalid' : '', + $this->model->{$this->attribute} + ); + } } \ No newline at end of file diff --git a/knowyourfood/core/middlewares/AuthMiddleware.php b/knowyourfood/core/middlewares/AuthMiddleware.php deleted file mode 100644 index aa6a702..0000000 --- a/knowyourfood/core/middlewares/AuthMiddleware.php +++ /dev/null @@ -1,21 +0,0 @@ -actions = $actions; - } - - public function execute(){ - if(Application::isLoggedIn()){ - if(empty($this->actions) || in_array(Application::$app->controller->action, $this->actions)){ - throw new ForbiddenException; - } - } - } -} \ No newline at end of file diff --git a/knowyourfood/core/middlewares/BaseMiddleware.php b/knowyourfood/core/middlewares/BaseMiddleware.php deleted file mode 100644 index 4bb4ec1..0000000 --- a/knowyourfood/core/middlewares/BaseMiddleware.php +++ /dev/null @@ -1,7 +0,0 @@ -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)); + } +} \ No newline at end of file diff --git a/knowyourfood/external_requests/CurlRequestBuilder.php b/knowyourfood/external_requests/CurlRequestBuilder.php new file mode 100755 index 0000000..333d462 --- /dev/null +++ b/knowyourfood/external_requests/CurlRequestBuilder.php @@ -0,0 +1,37 @@ +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); + } +} \ No newline at end of file diff --git a/knowyourfood/index.php b/knowyourfood/index.php old mode 100644 new mode 100755 index 2533f16..8858009 --- a/knowyourfood/index.php +++ b/knowyourfood/index.php @@ -1,41 +1,25 @@ -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(); +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(); \ No newline at end of file diff --git a/knowyourfood/migrations.php b/knowyourfood/migrations.php deleted file mode 100644 index 8924ef1..0000000 --- a/knowyourfood/migrations.php +++ /dev/null @@ -1,10 +0,0 @@ -db->applyMigrations(); diff --git a/knowyourfood/migrations/m01_initial.php b/knowyourfood/migrations/m01_initial.php deleted file mode 100644 index 3e5fef3..0000000 --- a/knowyourfood/migrations/m01_initial.php +++ /dev/null @@ -1,25 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/knowyourfood/migrations/m02_add_password_column.php b/knowyourfood/migrations/m02_add_password_column.php deleted file mode 100644 index 5061466..0000000 --- a/knowyourfood/migrations/m02_add_password_column.php +++ /dev/null @@ -1,17 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/knowyourfood/models/ContactForm.php b/knowyourfood/models/ContactForm.php old mode 100644 new mode 100755 index 083c247..a10229a --- a/knowyourfood/models/ContactForm.php +++ b/knowyourfood/models/ContactForm.php @@ -1,28 +1,28 @@ - [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; - } + [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; + } } \ No newline at end of file diff --git a/knowyourfood/models/LoginForm.php b/knowyourfood/models/LoginForm.php deleted file mode 100644 index b9e1894..0000000 --- a/knowyourfood/models/LoginForm.php +++ /dev/null @@ -1,40 +0,0 @@ - [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); - } -} \ No newline at end of file diff --git a/knowyourfood/models/LoginModel.php b/knowyourfood/models/LoginModel.php new file mode 100755 index 0000000..41a886e --- /dev/null +++ b/knowyourfood/models/LoginModel.php @@ -0,0 +1,58 @@ + [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; + } +} \ No newline at end of file diff --git a/knowyourfood/models/RegisterModel.php b/knowyourfood/models/RegisterModel.php new file mode 100644 index 0000000..c2cd4ae --- /dev/null +++ b/knowyourfood/models/RegisterModel.php @@ -0,0 +1,59 @@ + [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; + } +} \ No newline at end of file diff --git a/knowyourfood/models/User.php b/knowyourfood/models/User.php deleted file mode 100644 index 281b244..0000000 --- a/knowyourfood/models/User.php +++ /dev/null @@ -1,53 +0,0 @@ -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']; - } -} \ No newline at end of file diff --git a/knowyourfood/runtime/.gitignore b/knowyourfood/runtime/.gitignore old mode 100644 new mode 100755 diff --git a/knowyourfood/views/Login.php b/knowyourfood/views/Login.php new file mode 100644 index 0000000..2b4d5b0 --- /dev/null +++ b/knowyourfood/views/Login.php @@ -0,0 +1,28 @@ +view->getModel(); +$loginError = Application::$app->view->getActionError(); + +if(!$loginError == ''){ + echo "$loginError
"; +} + +echo '$loginError
"; +} + +echo '