Advertisement
Whistik

Untitled

Sep 24th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.68 KB | None | 0 0
  1. /**
  2.     * Create a new account
  3.     *
  4.     * @param mixed $data Posted form data
  5.     * @param int $captcha Randomly generated code
  6.     */
  7.     public function create($request)
  8.     {
  9.  
  10.         $this->request = (object) $request;
  11.         $allowed_states = array('US', 'CA', 'AF', 'AL', 'DZ', 'DS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CG', 'CK', 'CR', 'HR', 'CU', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'TP', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'FX', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GN', 'GW', 'GY', 'HT', 'HM', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IL', 'IT', 'CI', 'JM', 'JP', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'XK', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'TY', 'MX', 'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'AN', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'KN', 'LC', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'ES', 'LK', 'SH', 'PM', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'UM', 'UY', 'UZ', 'VU', 'VA', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'YU', 'ZR', 'ZM', 'ZW');
  12.         $_SESSION['request_form'] = $request;
  13.         $clients = DB::table('clients')->get();
  14.  
  15.         $clients->each(function($client){
  16.  
  17.             $data = json_decode($client->data, true);
  18.  
  19.             if($data['client_info']['email'] == $this->request->client_email){
  20.                echo throw_alert('Nastala chyba!', "Tato emailová adresa ({$this->request->client_email}) je již využita jiným uživatelem.", 'danger');
  21.                $this->isError = true;
  22.             }
  23.         });
  24.  
  25.         $unfilled = [];
  26.  
  27.         if(!in_array($this->request->client_state, $allowed_states)){
  28.             echo throw_alert('Nastala chyba!', "Vybraný stát není možno akceptovat.", 'danger');
  29.             $this->isError = true;
  30.         }
  31.  
  32.         if(!isset($this->request->client_firstname)){
  33.             $unfilled[] = 'křestní jméno';
  34.         }
  35.  
  36.         if(!isset($this->request->client_lastname)){
  37.             $unfilled[] = 'příjmení';
  38.         }
  39.  
  40.         if(!isset($this->request->client_email)){
  41.             $unfilled[] = 'emailová adresa';
  42.         }
  43.  
  44.         if(!isset($this->request->client_password)){
  45.             $unfilled[] = 'heslo';
  46.         }
  47.  
  48.         if(!isset($this->request->client_password_check)){
  49.             $unfilled[] = 'heslo (kontrola)';
  50.         }
  51.  
  52.         if(!isset($this->request->client_city)){
  53.             $unfilled[] = 'město/obec, ulice a číslo popisné';
  54.         }
  55.  
  56.         if(!isset($this->request->client_zip)){
  57.             $unfilled[] = 'psč';
  58.         }
  59.  
  60.         if(!isset($this->request->vop_accept)){
  61.             echo throw_alert('Nastala chyba!', "Touto registrací musíte souhlasit s VOP.", 'danger');
  62.             $this->isError = true;
  63.         }
  64.  
  65.         if(!isset($this->request->client_captcha)){
  66.             $unfilled[] = 'captcha';
  67.         }
  68.  
  69.         if($this->request->client_captcha != $_SESSION['server_captcha']){
  70.             echo throw_alert('Nastala chyba!', "Zadaný captcha kód je neplatný.", 'danger');
  71.             $this->isError = true;
  72.         }
  73.  
  74.         if(str_contains_helper($this->request->client_zip, ' ')){
  75.             $zip_parse = explode(' ', $this->request->client_zip);
  76.  
  77.             if(!is_numeric($zip_parse[0].$zip_parse[1])){
  78.                 echo throw_alert('Nastala chyba!', "Zadané PSČ je neplatné.", 'danger');
  79.                 $this->isError = true;
  80.             }
  81.         }else{
  82.             if(!is_numeric($this->request->client_zip)){
  83.                 echo throw_alert('Nastala chyba!', "Zadané PSČ je neplatné.", 'danger');
  84.                 $this->isError = true;
  85.             }
  86.         }
  87.  
  88.         if($this->request->client_password != $this->request->client_password_check){
  89.             echo throw_alert('Nastala chyba!', "Zadaná hesla, která jste zadal, nejsou shodná.", 'danger');
  90.             $this->isError = true;
  91.         }
  92.  
  93.         if(!filter_var($this->request->client_email, FILTER_VALIDATE_EMAIL)){
  94.             echo throw_alert('Nastala chyba!', "Tato emailová adresa ({$this->request->client_email}) není platná.", 'danger');
  95.             $this->isError = true;
  96.         }
  97.  
  98.         if(count($unfilled) > 0){
  99.             $error_text = "{0} [{1}] {2}";
  100.             for($i=0;$i<count($unfilled);$i++){
  101.                 if(count($unfilled) > 1){
  102.                     $error_text = str_replace('{0}', 'Tyto políčka', $error_text);
  103.                     $error_text = str_replace('{2}', 'nejsou vyplněná.', $error_text);
  104.                 }else{
  105.                     $error_text = str_replace('{0}', 'Toto políčko', $error_text);
  106.                     $error_text = str_replace('{2}', 'není vyplněné.', $error_text);
  107.                 }
  108.                 $error_text = str_replace('{1}', implode(',', $unfilled), $error_text);
  109.             }
  110.             echo throw_alert('Nastala chyba!', $error_text, 'danger');
  111.             $this->isError = true;
  112.         }
  113.  
  114.         $structure = json_encode([
  115.             'client_info' => [
  116.                 'email' => $this->request->client_email,
  117.                 'password' => hash('sha256', $this->request->client_password)
  118.             ],
  119.             'client_data' => [
  120.                 'balancne' => 0,
  121.                 'permissions' => 0
  122.             ],
  123.             'billing_info' => [
  124.                 'first_name' => $this->request->client_firstname,
  125.                 'last_name' => $this->request->client_lastname,
  126.                 'state' => $this->request->client_state,
  127.                 'city' => $this->request->client_city,
  128.                 'zip' => $this->request->client_zip,
  129.                 'phone' => $this->request->client_phone
  130.             ]
  131.         ]);
  132.  
  133.         if($this->isError == false){
  134.             \App\Client::insert(['data' => $structure]);
  135.             echo throw_alert('Úspěch!', 'Váš uživatelský účet byl zaregistrován. Přihlásit se je možno <a href="/client/login">zde</a>.', 'success');
  136.             unset($_SESSION['request_form']);
  137.             unset($_SESSION['server_captcha']);
  138.             redirect_to('/client/login', 3);
  139.         }
  140.  
  141.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement