Advertisement
Sirallens

appSync

Jul 6th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. <?php
  2.     require_once __DIR__ . '/../../required/db_connect.php';
  3.     $input = file_get_contents("php://input");
  4.     $error=0; $out_json = array(); $out_json['success'] = 1; //assume success
  5.     $sprinkler_status=0; $motion_status=0;
  6.     $rain_status = 0; $photo = 0;
  7.     if ($input) {
  8.          $json = json_decode($input, true); //check if it json input
  9.          if (json_last_error() == JSON_ERROR_NONE)
  10.           {
  11.              if (isset($json["username"]) && isset($json["password"]) && isset($json["SW1"]) && isset($json["SW2"]))
  12.              {
  13.                  $in_username = $json["username"];
  14.                  $in_password = $json["password"]; //if the expected fields are not null, get them
  15.                  $in_sprinkler = $json["SW1"];
  16.                  $in_photo = $json["SW2"];
  17.                  if ($stmt=$mysqli->prepare("SELECT password FROM webuser WHERE pname = ? LIMIT 1"))
  18.                  {
  19.                      $stmt->bind_param('s', $in_username);
  20.                     $stmt->execute(); $stmt->store_result(); //store_result to get num_rows etc.
  21.                      $stmt->bind_result($db_password); //get the hashed password
  22.                      $stmt->fetch();
  23.                      if ($stmt->num_rows == 1)
  24.                      { //if user exists, verify the password
  25.                          if (password_verify($in_password, $db_password))
  26.                          {
  27.                              $stmt->close();
  28.                              if ($stmt = $mysqli->prepare("UPDATE device set status=? where devname = 'sprinkler'"))
  29.                              { //update LED1
  30.                                 $stmt->bind_param('i', $in_sprinkler); $stmt->execute();
  31.                              }
  32.                              else {$error=1;}
  33.                                 $stmt->close();
  34.  
  35.                              if ($stmt = $mysqli->prepare("UPDATE device set status=? where devname = 'photo'"))
  36.                              { //update LED1
  37.                                 $stmt->bind_param('i', $in_photo); $stmt->execute();
  38.                              }
  39.                              else {$error=1;}
  40.                             $stmt->close();
  41.  
  42.                             if (!$error && ($stmt = $mysqli->prepare("SELECT status FROM device where devname = 'motion'")))
  43.                             {   //read SW1
  44.                              $stmt->execute(); $stmt->bind_result($motion_status); $stmt->fetch();
  45.                             }
  46.                             else
  47.                                 {$error=2;}
  48.                              $stmt->close();
  49.                            
  50.                              if (!$error && ($stmt = $mysqli->prepare("SELECT status FROM device where devname = 'rain'")))
  51.                              { //read LED1
  52.                                 $stmt->execute(); $stmt->bind_result($rain_status); $stmt->fetch();
  53.                              } else
  54.                                 {$error=3;}
  55.                             $stmt->close();
  56.                          }
  57.                          else {$error=4;}
  58.                      } else {$error=5;}
  59.                  } else {$error=6;}
  60.              } else {$error=7;}
  61.          } else {$error=8;}
  62.     }
  63.     else
  64.         {$error=9;}
  65.     if ($error)
  66.     {
  67.         $out_json['success'] = 0; //flag failure
  68.     }
  69.     $out_json['motion'] = $motion_status;
  70.     $out_json['rain'] = $rain_status;
  71.     $out_json['error'] = $error; //provide error (if any) number for debugging
  72.     echo json_encode($out_json); //encode the data in json format
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement