123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- header('Content-type: application/json');
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- # Gets json from POST
- $json = json_decode(file_get_contents('php://input'), true);
-
- # Input validation for username y password
- $username = $json["username"];
- $password = $json["password"];
- if(strlen($username) == 0 or strlen($password) == 0){
- header("400 Bad Request", true, 400);
- exit();
- }
-
- # Hashed password
- $hash = password_hash($password, PASSWORD_BCRYPT);
-
- include 'db.php';
-
- # Starts a transaction
- $conn->autocommit(FALSE);
- $conn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
-
- # Inserts into login
- $sql = "INSERT INTO login (username, hash) VALUES ('".$username."', '".$hash."')";
- if($conn->query($sql) === FALSE){
- $conn->rollback();
- $conn->close();
- header("500 Internal Server Error", true, 500);
- exit();
- }
-
- # Grabs all data
- $userid = $conn->insert_id;
- $nombre = $json["nombre"];
- $organizacion = $json["organizacion"];
- $puesto = $json["puesto"];
- $urbanizacion = $json["urbanizacion"];
- $calle = $json["calle"];
- $pueblo = $json["pueblo"];
- $cpostal = $json["cpostal"];
- $telefono = $json["telefono"];
- $correo = $json["correo"];
-
- # Checks if necessary data is empty
- if(strlen($nombre) == 0 or strlen($urbanizacion) == 0 or strlen($calle) == 0 or strlen($pueblo) == 0 or
- strlen($cpostal) == 0 or strlen($telefono) == 0 or strlen($correo) == 0 ){
- $conn->rollback();
- $conn->close();
- header("400 Bad Request", true, 400);
- exit();
- }
-
- # Inserts into miembros
- $sql = "INSERT INTO miembros (userid, nombre, organizacion, puesto, urbanizacion, calle, pueblo, cpostal, telefono, correo) VALUES "
- ."('".$userid."', '".$nombre."', '".$organizacion."', '".$puesto."', '".$urbanizacion.
- "', '".$calle."', '".$pueblo."', '".$cpostal."', '".$telefono."', '".$correo."')";
-
- if($conn->query($sql) === FALSE){
- $conn->rollback();
- $conn->close();
- header("500 Internal Server Error", true, 500);
- exit();
- }
-
- # Commits changes
- $conn->commit();
- $conn->close();
-
- # Returns 200
- header("200 OK", true, 200);
- exit();
-
- } else {
- header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);
- exit();
- }
- ?>
|