Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once __DIR__ . '/../../required/db_connect.php';
- $input = file_get_contents("php://input");
- $error=0; $out_json = array(); $out_json['success'] = 1; //assume success
- $sprinkler_status=0; $motion_status=0;
- $rain_status = 0; $photo = 0;
- if ($input) {
- $json = json_decode($input, true); //check if it json input
- if (json_last_error() == JSON_ERROR_NONE)
- {
- if (isset($json["username"]) && isset($json["password"]) && isset($json["SW1"]) && isset($json["SW2"]))
- {
- $in_username = $json["username"];
- $in_password = $json["password"]; //if the expected fields are not null, get them
- $in_sprinkler = $json["SW1"];
- $in_photo = $json["SW2"];
- if ($stmt=$mysqli->prepare("SELECT password FROM webuser WHERE pname = ? LIMIT 1"))
- {
- $stmt->bind_param('s', $in_username);
- $stmt->execute(); $stmt->store_result(); //store_result to get num_rows etc.
- $stmt->bind_result($db_password); //get the hashed password
- $stmt->fetch();
- if ($stmt->num_rows == 1)
- { //if user exists, verify the password
- if (password_verify($in_password, $db_password))
- {
- $stmt->close();
- if ($stmt = $mysqli->prepare("UPDATE device set status=? where devname = 'sprinkler'"))
- { //update LED1
- $stmt->bind_param('i', $in_sprinkler); $stmt->execute();
- }
- else {$error=1;}
- $stmt->close();
- if ($stmt = $mysqli->prepare("UPDATE device set status=? where devname = 'photo'"))
- { //update LED1
- $stmt->bind_param('i', $in_photo); $stmt->execute();
- }
- else {$error=1;}
- $stmt->close();
- if (!$error && ($stmt = $mysqli->prepare("SELECT status FROM device where devname = 'motion'")))
- { //read SW1
- $stmt->execute(); $stmt->bind_result($motion_status); $stmt->fetch();
- }
- else
- {$error=2;}
- $stmt->close();
- if (!$error && ($stmt = $mysqli->prepare("SELECT status FROM device where devname = 'rain'")))
- { //read LED1
- $stmt->execute(); $stmt->bind_result($rain_status); $stmt->fetch();
- } else
- {$error=3;}
- $stmt->close();
- }
- else {$error=4;}
- } else {$error=5;}
- } else {$error=6;}
- } else {$error=7;}
- } else {$error=8;}
- }
- else
- {$error=9;}
- if ($error)
- {
- $out_json['success'] = 0; //flag failure
- }
- $out_json['motion'] = $motion_status;
- $out_json['rain'] = $rain_status;
- $out_json['error'] = $error; //provide error (if any) number for debugging
- echo json_encode($out_json); //encode the data in json format
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement