Advertisement
Talar97

Kontorler rejestracji

Jun 13th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.45 KB | None | 0 0
  1. <?php
  2. namespace app\controllers;
  3. use app\forms\RegisterForm;
  4. use core\Logs;
  5. use core\SessionUtils;
  6. use core\ParamUtils;
  7. use core\App;
  8. use core\Utils;
  9. use core\Validator;
  10. /**
  11.  * Class RegisterControl
  12.  * @package app\controllers
  13.  */
  14. class RegisterControl
  15. {
  16.     /**
  17.      * @var RegisterForm
  18.      */
  19.     public $form;
  20.     /**
  21.      * RegisterControl constructor.
  22.      */
  23.     public function __construct(){
  24.         $this->form = new RegisterForm();
  25.     }
  26.     /**
  27.      *
  28.      */
  29.     public function getFormParams(){
  30.         $this->form->email = ParamUtils::getFromRequest("email");
  31.         $this->form->login = ParamUtils::getFromRequest("login");
  32.         $this->form->password = ParamUtils::getFromRequest("password");
  33.         $this->form->password_repeat = ParamUtils::getFromRequest("password_repeat");
  34.         $this->form->security_question = ParamUtils::getFromRequest("security_question");
  35.         $this->form->security_answer = ParamUtils::getFromRequest("security_answer");
  36.     }
  37.     /**
  38.      * @return bool
  39.      */
  40.     public function validateForm(){
  41.         if(!empty(SessionUtils::load("id", true))) return true;
  42.         if(!$this->form->checkIsNull()) return false;
  43.         $v = new Validator();
  44.         $v->validate($this->form->email,[
  45.             'email' => true,
  46.             'trim' => true,
  47.             'required' => true,
  48.             'min_length' => 4,
  49.             'max_length' => 128,
  50.             'required_message' => 'Adres email jest wymagany',
  51.             'validator_message' => "Nieprawidłowy adres email"
  52.         ]);
  53.         $v->validate($this->form->login,[
  54.             'trim' => true,
  55.             'required' => true,
  56.             'required_message' => 'Login jest wymagany',
  57.             'min_length' => 3,
  58.             'max_length' => 32,
  59.             'validator_message' => 'Login powinien zawierać od 3 do 32 znaków'
  60.         ]);
  61.         $v->validate($this->form->password,[
  62.             'required' => true,
  63.             'required_message' => 'Hasło jest wymagane',
  64.             'regexp' => "/(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{5,}/",
  65.             'validator_message' => 'Hasło powinno mieć conajmniej 5 znaków, jedną literę i jedną liczbę'
  66.         ]);
  67.         $v->validate($this->form->password_repeat,[
  68.             'required' => true,
  69.             'required_message' => 'Hasło jest wymagane'
  70.         ]);
  71.         if($this->form->password_repeat != $this->form->password){
  72.             Utils::addErrorMessage("Hasła nie są identyczne!");
  73.         }
  74.         $v->validate($this->form->security_question,[
  75.             'required' => true,
  76.             'min_length' => 1,
  77.             'max_length' => 1024,
  78.             'required_message' => 'Pytanie bezpieczeństwa jest wymagane!'
  79.         ]);
  80.         $v->validate($this->form->security_answer,[
  81.             'required' => true,
  82.             'min_length' => 1,
  83.             'max_length' => 256,
  84.             'required_message' => 'Odpowiedź na pytanie bezpieczeństwa jest wymagane!'
  85.         ]);
  86.         $this->checkForDuplicate();
  87.         if(!App::getMessages()->isError()) return true;
  88.         else return false;
  89.     }
  90.     /**
  91.      *
  92.      */
  93.     public function checkForDuplicate(){
  94.         try{
  95.             $loginCount = App::getDB()->has("user", [
  96.                 'login' => $this->form->login
  97.             ]);
  98.             $emailCount = App::getDB()->has("user",[
  99.                 'email' => $this->form->email
  100.             ]);
  101.             if($loginCount){
  102.                 Utils::addErrorMessage("Podany login jest już zajęty");
  103.             }
  104.             if($emailCount){
  105.                 Utils::addErrorMessage("Podany email jest już zajęty");
  106.             }
  107.         }catch(\PDOException $e){
  108.             Utils::addErrorMessage("Błąd połączenia z bazą danych");
  109.         }
  110.     }
  111.     /**
  112.      *
  113.      */
  114.     public function insertToDb(){
  115.         try{
  116.             $userRole_id = App::getDB()->get("role", "id_role",[
  117.                 'name' => 'user'
  118.             ]);
  119.             App::getDB()->insert("user_details",[
  120.                 'register_date' => (new \DateTime())->format('Y-m-d H:i:s')
  121.             ]);
  122.             $userId = App::getDB()->id();
  123.             App::getDB()->insert("user",[
  124.                 'id' => $userId,
  125.                 'login' => $this->form->login,
  126.                 'password' => md5($this->form->password),
  127.                 'email' => $this->form->email,
  128.                 'security_question' => $this->form->security_question,
  129.                 'security_answer' => md5($this->form->security_answer),
  130.                 'id_role' => $userRole_id
  131.             ]);
  132.             Utils::addInfoMessage("Konto zostało utworzone");
  133.             Logs::addLog("Utworzenie nowego konta: ".$this->form->login);
  134.         }catch(\PDOException $e){
  135.             Utils::addErrorMessage("Błąd połączenia z bazą danych!");
  136.         }
  137.     }
  138.     /**
  139.      * @throws \SmartyException
  140.      */
  141.     public function generateView(){
  142.         if($this->validateForm()){
  143.             $this->insertToDb();
  144.             header("Location: ".App::getConf()->app_url."/login");
  145.         }
  146.         else{
  147.             App::getSmarty()->assign('page_title','Rejestracja');
  148.             App::getSmarty()->assign('page_description','Rejestracja');
  149.             App::getSmarty()->display('RegisterView.tpl');
  150.         }
  151.     }
  152.     /**
  153.      * @throws \SmartyException
  154.      */
  155.     public function action_register(){
  156.         $this->getFormParams();
  157.         $this->generateView();
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement