1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?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) or strlen($urbanizacion) or strlen($calle) or strlen($pueblo) or strlen($cpostal) or strlen($telefono) or strlen($correo))){
- $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();
- }
- ?>
|