No Description

signup.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. header('Content-type: application/json');
  3. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  4. # Gets json from POST
  5. $json = json_decode(file_get_contents('php://input'), true);
  6. # Input validation for username y password
  7. $correo = $json["correo"];
  8. $password = $json["password"];
  9. $nombre = $json["nombre"];
  10. $organizacion = $json["organizacion"];
  11. $puesto = $json["puesto"];
  12. $urbanizacion = $json["urbanizacion"];
  13. $calle = $json["calle"];
  14. $pueblo = $json["pueblo"];
  15. $cpostal = $json["cpostal"];
  16. $telefono = $json["telefono"];
  17. # Ignores not properly formatted emails
  18. if (!filter_var($correo, FILTER_VALIDATE_EMAIL)) {
  19. http_response_code(400);
  20. echo json_encode(array("error" => "Email no valido."));
  21. exit();
  22. }
  23. if(strlen($correo) == 0 or strlen($password) == 0 or strlen($nombre) == 0 or strlen($urbanizacion) == 0
  24. or strlen($calle) == 0 or strlen($pueblo) == 0 or strlen($cpostal) == 0 or strlen($telefono) == 0){
  25. http_response_code(400);
  26. echo json_encode(array("error" => "Campos requeridos vacio."));
  27. exit();
  28. }
  29. # Hashed password
  30. $hash = password_hash($password, PASSWORD_BCRYPT);
  31. include 'db.php';
  32. # Starts a transaction
  33. $conn->autocommit(FALSE);
  34. $conn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  35. # Inserts into login
  36. $sql = "INSERT INTO Login (correo, hash) VALUES ('".$correo."', '".$hash."')";
  37. if($conn->query($sql) === FALSE){
  38. http_response_code(500);
  39. echo json_encode(array("error" => "Correo en uso."));
  40. $conn->rollback();
  41. $conn->close();
  42. exit();
  43. }
  44. $userid = $conn->insert_id;
  45. # Inserts into miembros
  46. $sql = "INSERT INTO Miembro (userid, nombre, organizacion, puesto, urbanizacion, calle, pueblo, cpostal, telefono) VALUES "
  47. ."('".$userid."', '".$nombre."', '".$organizacion."', '".$puesto."', '".$urbanizacion
  48. ."', '".$calle."', '".$pueblo."', '".$cpostal."', '".$telefono."')";
  49. if($conn->query($sql) === FALSE){
  50. http_response_code(500);
  51. echo json_encode(array("error" => "Error de base de datos."));
  52. $conn->rollback();
  53. $conn->close();
  54. exit();
  55. }
  56. # Commits changes
  57. $conn->commit();
  58. $conn->close();
  59. # Returns 200
  60. header("200 OK", true, 200);
  61. exit();
  62. } else {
  63. header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);
  64. exit();
  65. }
  66. ?>