Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. ////////////////////////////////////////////// register.php
  2.  
  3. $username = filter_var( $_POST[ 'username' ], FILTER_SANITIZE_STRING );
  4. $email = filter_var( $_POST[ 'email' ], FILTER_SANITIZE_STRING );
  5. $password = filter_var( $_POST[ 'password' ], FILTER_SANITIZE_STRING );
  6. $password2 = filter_var( $_POST[ 'password2' ], FILTER_SANITIZE_STRING );
  7. $tos = filter_var( $_POST[ 'tos' ], FILTER_SANITIZE_STRING );
  8. $session_key = randomstring( 64 );
  9. $ip = getIP();
  10. $registerUser = new register( $username, $email, $password, $password2, $tos, $ip, $session_key, $con );
  11. $registerUser -> test();
  12. if ( $registerUser -> isFailed() ) {
  13. echo $registerUser -> getErrors();
  14. $registerUser = null;
  15. } else {
  16. $registerUser -> register();
  17. echo 'true';
  18. }
  19.  
  20. //////////////////////////// register_class.php
  21.  
  22. <?php
  23. class register{
  24.  
  25. private $username;
  26. private $email;
  27. private $password;
  28. private $password2;
  29. private $tos;
  30. private $ip;
  31. private $session_key;
  32. private $errorMSGS = array( 'error' => array() );
  33. private $registrationFailed;
  34. private $con;
  35.  
  36. function __construct( $username, $email, $password, $password2, $tos, $ip, $session_key, $con ){
  37. $this -> username = $username;
  38. $this -> email = $email;
  39. $this -> password = $password;
  40. $this -> password = $password2;
  41. $this -> tos = $tos;
  42. $this -> ip = $ip;
  43. $this -> session_key = $session_key;
  44. $this -> con = $con;
  45. }
  46.  
  47. public function isFailed() {
  48. if ( $this -> registrationFailed ) {
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54.  
  55. public function getErrors() {
  56. return json_encode( $this -> errorMSGS );
  57. }
  58.  
  59. public function test() {
  60. $sql = $this -> con -> prepare( "Select username from forum_users where username = ?" );
  61. $sql -> bind_param( 's', $this -> username );
  62. $sql -> execute();
  63. $sql -> bind_result( $checkusername );
  64. $sql -> fetch();
  65. $sql -> close();
  66.  
  67. $sql = $this -> con -> prepare( "Select email from forum_users where email = ?" );
  68. $sql -> bind_param( 's', $this -> email );
  69. $sql -> execute();
  70. $sql -> bind_result( $checkemail );
  71. $sql -> fetch();
  72. $sql -> close();
  73.  
  74. if ( $checkusername != null ) {
  75. $this -> registrationFailed = true;
  76. array_push( $this -> errorMSGS[ 'error' ], "This username is already taken!");
  77. }
  78. if ( $checkemail != null ) {
  79. $this -> registrationFailed = true;
  80. array_push( $this -> errorMSGS[ 'error' ], "This email is already taken!" );
  81. }
  82. if ( !filter_var( $this -> email, FILTER_VALIDATE_EMAIL ) ) {
  83. $this -> registrationFailed = true;
  84. array_push( $this -> errorMSGS[ 'error' ], "Email is invalid!" );
  85. }
  86. if ( $this -> tos == 'false' ) {
  87. $this -> registrationFailed = true;
  88. array_push( $this -> errorMSGS[ 'error' ], "You have to agree to Terms of Service!" );
  89. }
  90. if ( strlen( $this -> username ) < 3 ) {
  91. $this -> registrationFailed = true;
  92. array_push( $this -> errorMSGS[ 'error' ], "Username must be at least 3 characters long!" );
  93. }
  94. if ( strlen( $this -> password ) < 8 ) {
  95. $this -> registrationFailed = true;
  96. array_push( $this -> errorMSGS[ 'error' ], "Password must be at least 8 characters long!" );
  97. }
  98. if ( $this -> password == $this -> password2) {
  99. $this -> registrationFailed = true;
  100. array_push( $this -> errorMSGS[ 'error' ], "Passwords do no match!" );
  101. }
  102. }
  103.  
  104. public function register() {
  105. $uuid = randomstring( 32 );
  106. $salt = sprintf( "$2a$%02d$", 10 ) . randomstring( 55 );
  107. $encrypedpass = crypt( $this -> password, $salt );
  108. $rank = 'member';
  109.  
  110. $sql = $this -> con -> prepare( "insert into forum_users ( uuid, username, email, password_hash, salt, display_name, rank ) values ( ?, ?, ?, ?, ?, ?, ? )" );
  111. $sql -> bind_param( 'sssssss', $uuid, $this -> username, $this -> email, $encrypedpass, $salt, $this -> username, $rank );
  112. $sql -> execute();
  113. $sql -> close();
  114.  
  115. $sql = $this -> con -> prepare( "insert into forum_sessions ( session_key, ip, userUUID ) values ( ?, ?, ? )" );
  116. $sql -> bind_param( 'sss', $this -> session_key, $this -> ip, $uuid );
  117. $sql -> execute();
  118. $sql -> close();
  119.  
  120. session_start(['cookie_lifetime' => 259200]);
  121. setcookie("forum", $this -> session_key, time() + ( 259200 ), '/');
  122. }
  123.  
  124. private function randomstring( $size ){
  125. $list = array( 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
  126. $string = '';
  127. for( $x = 0; $x < $size; $x++ ){
  128. $string = $string . $list[ rand( 0, count( $list ) - 1 ) ];
  129. }
  130. return $string;
  131. }
  132. }
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement