Guest User

Untitled

a guest
Oct 21st, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.45 KB | None | 0 0
  1. <?php
  2.  
  3.     class Register
  4.     {
  5.         /*
  6.          *
  7.          * @To-DO: Add age restrictions.
  8.          *
  9.          */
  10.         private $dob = null,
  11.                 $day = null,
  12.                 $month = null,
  13.                 $year = null,
  14.                 $gender = 'male',
  15.                 $username = 'Guest',
  16.                 $password = null,
  17.                 $email = null,
  18.                 $retypedEmail = null,
  19.                 $secCode = null,
  20.                 $termsOfServiceSelection = null,
  21.                 $marketing = false,
  22.                 $minAge = 11,
  23.                 $maxAge = 100;
  24.        
  25.         private $step = 1,
  26.                 $errorMessage = null,
  27.                 $currentPage = null,
  28.                 $bgClass = null;
  29.                
  30.         public function __construct()
  31.         {
  32.            
  33.             global $Smarty;
  34.            
  35.             $this->currentPage = $_GET['page'];
  36.                    
  37.             $this->_setData();
  38.            
  39.             $this->_findValidPage();
  40.            
  41.             if( $_POST )
  42.             {
  43.                
  44.                 $this->_processData();
  45.                
  46.             }
  47.            
  48.             $Smarty->assign('step', $this->grabData('step') );
  49.             $Smarty->assign('bodyClass', $this->grabData('bgClass') );
  50.             $Smarty->assign('error', $this->grabData('errorMessage') );
  51.            
  52.         }
  53.        
  54.         public function grabSession( $key )
  55.         {
  56.            
  57.             return ($_SESSION[$key] ? $_SESSION[$key] : false);
  58.            
  59.         }
  60.        
  61.         public function setSession( $key, $value = '' )
  62.         {
  63.        
  64.             $_SESSION[$key] = $value;
  65.                        
  66.         }
  67.        
  68.         public function grabData( $key, $value = '' )
  69.         {
  70.            
  71.             if( $value )
  72.             {
  73.                
  74.                 $this->$key = $value;
  75.                
  76.                 return $value;
  77.                
  78.             }
  79.             else
  80.             {
  81.                
  82.                 return $this->$key ? $this->$key : false;
  83.                
  84.             }
  85.            
  86.         }
  87.        
  88.         private function _setData()
  89.         {
  90.            
  91.             if( $_SESSION )
  92.             {
  93.            
  94.                 foreach( $_SESSION as $key => $value )
  95.                 {
  96.                                        
  97.                     $this->$key = $value;
  98.                    
  99.                 }
  100.                
  101.             }
  102.            
  103.             if( $_POST )
  104.             {
  105.                
  106.                 foreach( $_POST as $key => $value )
  107.                 {
  108.                    
  109.                     $key = str_replace('bean_', '', $key);
  110.                    
  111.                     $this->$key = $value;
  112.                    
  113.                 }
  114.                
  115.             }
  116.            
  117.         }
  118.        
  119.         private function _findValidPage( )
  120.         {
  121.                        
  122.             switch( $this->grabSession('step') )
  123.             {
  124.                
  125.                 default:
  126.                    
  127.                     $this->setSession('step', 1);
  128.                    
  129.                     $this->redirect();
  130.                    
  131.                    
  132.                    
  133.                 break;
  134.                
  135.                 case 1:
  136.                    
  137.                     if( $this->currentPage != 'start' )
  138.                     {
  139.                        
  140.                         if( !$this->grabData('dob') || $this->grabData('bean_gender') )
  141.                         {
  142.                            
  143.                             $this->redirect();
  144.                            
  145.                         }
  146.                     }
  147.                    
  148.                     $this->grabData('bgClass', 'background-agegate');
  149.                     $this->grabData('template', 'start');
  150.                    
  151.                 break;
  152.                
  153.                 case 2:
  154.                    
  155.                     if( $this->currentPage != 'email_password' )
  156.                     {
  157.                        
  158.                         if( !$this->grabData('email') || !$this->grabData('password') )
  159.                         {
  160.                            
  161.                             $this->redirect('email_password');
  162.                            
  163.                         }
  164.                        
  165.                     }
  166.                    
  167.                     $this->grabData('bgClass', 'background-accountdetails-'.$this->grabData('gender') );
  168.                     $this->grabData('template', 'email_password');     
  169.                                
  170.                 break;
  171.                
  172.                 case 3:
  173.                                        
  174.                     if( $this->currentPage != 'captcha' )
  175.                     {
  176.                        
  177.                         if( $this->grabData('email') && $this->grabData('password') )
  178.                         {
  179.                            
  180.                             $this->redirect('captcha');
  181.                            
  182.                         }
  183.                        
  184.                     }
  185.                    
  186.                     $this->grabData('bgClass', 'background-captcha' );
  187.                     $this->grabData('template', 'captcha');
  188.                    
  189.                    
  190.                 break;
  191.                
  192.             }
  193.            
  194.         }
  195.        
  196.         private function redirect( $pageName = 'start' )
  197.         {
  198.            
  199.             header('Location: /quickregister/' . $pageName );
  200.            
  201.         }
  202.        
  203.         private function _processData()
  204.         {
  205.            
  206.             switch( $this->grabData('step') )
  207.             {
  208.                
  209.                 case 1:
  210.                    
  211.                     if( $this->gender != 'male' )
  212.                     {
  213.                    
  214.                         $this->gender = 'female';
  215.                        
  216.                     }
  217.                     else
  218.                     {
  219.                        
  220.                         $this->gender = 'male';
  221.                        
  222.                     }
  223.                    
  224.                     $this->setSession('gender', $this->gender);
  225.                    
  226.                     foreach( array ( 'day', 'month', 'year' ) as $check )
  227.                     {
  228.                        
  229.                         if( $this->grabData( $check ) )
  230.                         {
  231.                            
  232.                             $this->dob .= $this->$check . ( $check == 'year' ?: '-' );
  233.                            
  234.                             $this->setSession('dob', $this->dob);
  235.                            
  236.                         }
  237.                         else
  238.                         {
  239.                            
  240.                             $this->errorMessage = 'Please supply a valid birthdate.';
  241.                             break;
  242.                                                        
  243.                         }
  244.                        
  245.                     }
  246.                    
  247.                     if( !$this->errorMessage )
  248.                     {
  249.                        
  250.                         $this->setSession('step', 2);
  251.                        
  252.                         $this->redirect('password_email');
  253.  
  254.                     }
  255.                    
  256.                 break;
  257.                
  258.                 case 2:
  259.                    
  260.                     if( !$this->email || !filter_var( $this->email, FILTER_VALIDATE_EMAIL ) )
  261.                     {
  262.                        
  263.                         $this->errorMessage['email'] = 'Please enter a valid email address';
  264.                        
  265.                     }
  266.                    
  267.                     if( !$this->retypedEmail )
  268.                     {
  269.                        
  270.                         $this->errorMessage['retypedEmail'] = 'Please type your email again';                  
  271.                        
  272.                     }
  273.                    
  274.                     if( $this->email != $this->retypedEmail )
  275.                     {
  276.                        
  277.                         $this->errorMessage['email'] = 'Emails don\'t match';
  278.                        
  279.                     }
  280.                    
  281.                     if( MySQL::newQuery()->Query('SELECT null FROM users WHERE email = ? LIMIT 1;')->bind('s', $this->email)->count() > 0 )
  282.                     {
  283.                        
  284.                         $this->errorMessage['email'] = 'This email is already in use';
  285.                        
  286.                     }
  287.                    
  288.                     if( strlen( $this->password ) < 5 || strlen( $this->password ) > 20 )
  289.                     {
  290.                        
  291.                         $this->errorMessage['password'] = 'Please enter a valid password ';
  292.                        
  293.                     }
  294.                     elseif( !preg_match( '/([A-Za-z]+)/', $this->password ) || !preg_match('/([0-9]+)/', $this->password) )
  295.                     {
  296.                        
  297.                         $this->errorMessage['password'] = 'Your password must also include numbers';
  298.                        
  299.                     }
  300.                    
  301.                     if( !$this->termsOfServiceSelection )
  302.                     {
  303.                        
  304.                         $this->errorMessage['tos'] = 'Please accept the terms of service';
  305.                        
  306.                     }
  307.                    
  308.                     if( !$this->errorMessage )
  309.                     {
  310.                        
  311.                         $this->setSession('email', $this->email);
  312.                         $this->setSession('password', $this->password );
  313.                         $this->setSession('newsletter', $this->marketing ? true : false);
  314.                        
  315.                         $this->setSession('step', 3);
  316.                         $this->redirect('captcha');
  317.                        
  318.                     }
  319.                    
  320.                 break;
  321.                
  322.                 case 3:
  323.                    
  324.                     global $config;
  325.                                    
  326.                     require_once BASE . 'libs/class.recaptcha.php';
  327.                    
  328.                     $inst = recaptcha_check_answer
  329.                             (
  330.                                 $config['recaptcha']['privateKey'],
  331.                                 $_SERVER['REMOTE_ADDR'],
  332.                                 $this->recaptcha_challenge_field,
  333.                                 $this->captchaResponse
  334.                                
  335.                             );
  336.                    
  337.                     if( $inst->is_valid )
  338.                     {
  339.                        
  340.                         global $user;
  341.                        
  342.                         $userNameClunks = explode('@', $this->email);
  343.                        
  344.                         $this->username = $userNameClunks[0] . rand( 0, 4000 );
  345.                        
  346.                         MySQL::newQuery()
  347.                         ->Query('INSERT INTO users (email, password)VALUES(?, ?);')
  348.                         ->bind('ss', $this->email, User::encrypt( $this->password ) )
  349.                         ->execute();
  350.                        
  351.                         $uId = MySQL::$link->insert_id;
  352.                        
  353.                         MySQL::newQuery()
  354.                         ->Query('INSERT INTO characters
  355.                        
  356.                                     (account_uid, username, motto, gender, last_ip, timestamp_created)
  357.                                    
  358.                                 VALUES
  359.                                
  360.                                     (?, ?, ?, ?, ?, UNIX_TIMESTAMP() );
  361.                                 ')
  362.                         ->bind('issss', $uId, $this->username, 'James rocks!', ($this->gender == 'male' ? 'M' : 'F'), $_SERVER['REMOTE_ADDR'] )
  363.                         ->execute();
  364.                        
  365.                         foreach( $_SESSION as $key => $value ) { $_SESSION[$key] = ''; }
  366.                        
  367.                         $user->login( $this->email, $this->password );
  368.                        
  369.                         Header('Location: /me');
  370.                        
  371.                     }
  372.                     else
  373.                     {
  374.                        
  375.                         $this->errorMessage[] = 'The security code was invalid, please try again.';
  376.                        
  377.                     }
  378.                    
  379.                    
  380.                 break;
  381.                
  382.             }
  383.            
  384.            
  385.         }
  386.                
  387.        
  388.     }
Add Comment
Please, Sign In to add comment