sign-up.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /* Regists a new user unless it already exists in the database */
  3. $params = array();
  4. parse_str($_POST['sign_up_data'], $params);
  5. if(!array_key_exists('firstname-signup', $params) || !array_key_exists('email-signup', $params) || !array_key_exists('password-signup', $params)) {
  6. echo 400; // Missing parameters
  7. }
  8. else {
  9. // Generate user credentials
  10. $name = $params['firstname-signup'];
  11. $email = $params['email-signup'];
  12. $password = $params['password-signup'];
  13. $algorithm = '$2a'; // Blowfish
  14. $cost = '$10'; // for hashing
  15. $salt = $algorithm . $cost . '$' . substr(sha1(mt_rand()),0,22);
  16. $hash = crypt($password, $salt);
  17. // Activation email
  18. $activationLink = "https://ethicscanvas.org/activation.php?salt=$salt";
  19. $activationEmail = "Hi $name,<br><br>Thanks for signing up at EthicsCanvas.org!<br><br>Please click on the link below in order to verify your email address:<br>$activationLink<br><br>Thanks,<br><br>Ethics Canvas Team";
  20. require_once('db_utils.php');
  21. include('mailer.php');
  22. $conn = db_connect(); // Connect to the database
  23. // Check if the username already exists
  24. if(!($result = mysqli_query($conn, "SELECT * FROM user WHERE username = '$email'"))) {
  25. echo 400; // Wrong query
  26. }
  27. else {
  28. if(mysqli_num_rows($result) > 0) { // User already registered - not allow
  29. echo 401;
  30. }
  31. else {
  32. // Register the new user
  33. if(!mysqli_query($conn, "INSERT INTO user (username, password, name, salt) VALUES ('$email', '$hash', '$name', '$salt')")) {
  34. echo 400; // Wrong query
  35. }
  36. else {
  37. smtpmailer($email, 'Welcome to ethicscanvas.org', $activationEmail, null);
  38. echo 201;
  39. }
  40. }
  41. }
  42. mysqli_free_result($result);
  43. db_close($conn); // Close the database
  44. }
  45. ?>