diff --git a/iot/active.php b/iot/active.php index 4a9c3c4..5bfd56d 100644 --- a/iot/active.php +++ b/iot/active.php @@ -1,25 +1,34 @@ -conn->query("UPDATE `user` SET `active`= 1"); - if($query){ - echo json_encode(array('status' => 'all ID active')); - }else{ - echo json_encode(array('status' => 'active not update')); - - } - - $db->conn->close(); - }else{ - echo json_encode(array('status' => 'tkn false')); - - } - - -} else { - echo json_encode(array('status' => 'Invalid information entered')); -} \ No newline at end of file +conn->prepare("SELECT * FROM `user` WHERE `token` = ?"); + $check_token_query->bind_param('s', $token); + $check_token_query->execute(); + $result = $check_token_query->get_result(); + + if ($result->num_rows == 1) { + $update_query = $db->conn->prepare("UPDATE `user` SET `active` = 1 WHERE `token` = ?"); + $update_query->bind_param('s', $token); + $update_result = $update_query->execute(); + + if ($update_result) { + echo json_encode(array('status' => 'success', 'message' => 'All IDs activated')); + } else { + echo json_encode(array('status' => 'error', 'message' => 'Failed to update active status')); + } + } else { + echo json_encode(array('status' => 'error', 'message' => 'Invalid token')); + } + + $check_token_query->close(); + $db->conn->close(); +} else { + echo json_encode(array('status' => 'error', 'message' => 'Token not provided')); +} diff --git a/iot/createID.php b/iot/createID.php index eaa32a9..915af8a 100644 --- a/iot/createID.php +++ b/iot/createID.php @@ -1,86 +1,89 @@ - 0 && $len < count($a)) { - for ($i = 0; $i < $len; $i++) { - $res .= $a[rand(0, $len - 1)]; - } - } - return $res; -} - - -if (isset($_GET['id']) ) { -$clientID=$_GET['id']; - - - - $token = straRandom(20); - $username = "user".$clientID; - - $tableName = "id_" . $clientID; - - - - - $findTable = $db->conn->query("SHOW TABLES LIKE '$tableName' "); - if (mysqli_num_rows($findTable) > 0) { - - echo json_encode(array('status' => 'The entered ID is duplicate')); - } else { - $createTable = $db->conn->query("CREATE TABLE " . $tableName . "( - ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, - clientID VARCHAR(255) NOT NULL, - ip VARCHAR(255), - ina INT DEFAULT 0, - inb INT DEFAULT 0, - inc INT DEFAULT 0, - ind INT DEFAULT 0, - ine INT DEFAULT 0, - inf INT DEFAULT 0, - ing INT DEFAULT 0, - inh INT DEFAULT 0, - sena VARCHAR(4) DEFAULT 'off', - senb VARCHAR(4) DEFAULT 'off', - senc VARCHAR(4) DEFAULT 'off', - send VARCHAR(4) DEFAULT 'off', - sene VARCHAR(4) DEFAULT 'off', - senf VARCHAR(4) DEFAULT 'off', - seng VARCHAR(4) DEFAULT 'off', - senh VARCHAR(4) DEFAULT 'off', - timestamp INT , - time_date timestamp DEFAULT CURRENT_TIMESTAMP)"); - if ($createTable) { - $craeteUser = $db->conn->query("INSERT INTO `user`(`clientID`, `username`, `token`) VALUES ('$clientID','$username','$token')"); - - if ($craeteUser) { - - echo json_encode(array( - 'status' => 'data inserted', - 'clientID' => $clientID, - 'token' => $token, - 'username' => $username, - 'date' => date("Y-m-d"), - 'time' => date('H:i:s') - )); - } else { - echo json_encode(array('status' => 'User not created')); - } - } else { - echo json_encode(array("status" => 'Failed to create table')); - } - } - - -} - else { - echo json_encode(array('status' => 'The requested information was not entered correctly')); -} \ No newline at end of file +conn->query("SHOW TABLES LIKE '$tableName' "); + + if (mysqli_num_rows($findTable) > 0) { + echo json_encode(array('status' => 'The entered ID is duplicate')); + } else { + $createTable = $db->conn->query("CREATE TABLE $tableName ( + ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + clientID VARCHAR(255) NOT NULL, + username VARCHAR(255) NOT NULL, + password VARCHAR(255) NOT NULL, + token VARCHAR(255) NOT NULL, + ip VARCHAR(255), + ina INT DEFAULT 0, + inb INT DEFAULT 0, + inc INT DEFAULT 0, + ind INT DEFAULT 0, + ine INT DEFAULT 0, + inf INT DEFAULT 0, + ing INT DEFAULT 0, + inh INT DEFAULT 0, + sena VARCHAR(4) DEFAULT 'off', + senb VARCHAR(4) DEFAULT 'off', + senc VARCHAR(4) DEFAULT 'off', + send VARCHAR(4) DEFAULT 'off', + sene VARCHAR(4) DEFAULT 'off', + senf VARCHAR(4) DEFAULT 'off', + seng VARCHAR(4) DEFAULT 'off', + senh VARCHAR(4) DEFAULT 'off', + timestamp INT, + time_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )"); + + if ($createTable) { + $createUser = $db->conn->query("INSERT INTO `user` (`clientID`, `username`, `password`, `token`) + VALUES ('$clientID', '$username', '$hashedPassword', '$token')"); + + if ($createUser) { + echo json_encode( + array( + 'status' => 'Data inserted', + 'clientID' => $clientID, + 'token' => $token, + 'username' => $username, + 'date' => date("Y-m-d"), + 'time' => date('H:i:s') + ) + ); + } else { + echo json_encode(array('status' => 'User not created')); + } + } else { + echo json_encode(array('status' => 'Failed to create table')); + } + } + +} else { + echo json_encode(array('status' => 'Invalid request')); +} + +$db->conn->close(); diff --git a/iot/generateToken.php b/iot/generateToken.php new file mode 100644 index 0000000..452f87c --- /dev/null +++ b/iot/generateToken.php @@ -0,0 +1,59 @@ +conn->prepare("SELECT * FROM `user` WHERE `username` = ?"); + $query->bind_param('s', $username); + $query->execute(); + $result = $query->get_result(); + + if ($result->num_rows == 1) { + $user = $result->fetch_assoc(); + + // Verify password hash + if (password_verify($password, $user['password'])) { + // Password is correct, generate new token + $token = generateToken(); + + // Update user token in the database + $update_query = $db->conn->prepare("UPDATE `user` SET `token` = ? WHERE `username` = ?"); + $update_query->bind_param('ss', $token, $username); + $update_result = $update_query->execute(); + + if ($update_result) { + echo json_encode( + array( + 'status' => 'success', + 'username' => $username, + 'token' => $token + ) + ); + } else { + echo json_encode(array('status' => 'Failed to update token')); + } + } else { + echo json_encode(array('status' => 'Invalid password')); + } + } else { + echo json_encode(array('status' => 'User not found')); + } + + $query->close(); + +} else { + echo json_encode(array('status' => 'Invalid request')); +} + +$db->conn->close();