Esempio n. 1
0
function addRoom($db, $company, $branch, $room, $max_cap)
{
    $sub_query = "SELECT c.company_id, b.branch_id FROM company AS c\n                    INNER JOIN branch b on b.company_id = c.company_id\n                    WHERE b.branch_address = '{$branch}' AND c.company_name = '{$company}'";
    $results = $db->query($sub_query);
    $ids = $results->fetch_assoc();
    $branch_id = $ids['branch_id'];
    $rand = genRandom();
    $hash = password_hash($rand, PASSWORD_DEFAULT);
    $insert_room = "INSERT INTO room (`branch_id`, `room_number`, `max_capacity`, `secret_key`)\n                      VALUES ('{$branch_id}', '{$room}', '{$max_cap}', '{$hash}')";
    if ($db->query($insert_room) === TRUE) {
        echo "Successfully inserted '{$room}'" . "<br>";
        echo "Your Pi's password is " . $rand . ". Keep it safe and secure. The Pi needs it to submit counts" . "<br>";
    } else {
        echo mysqli_error($db) . "<br />";
    }
}
Esempio n. 2
0
/**
 *  Adds a room to an existing branch
 *  @param mysqli $db the database to add information to
 *  @param string $company the company the branch belongs to
 *  @param string $branch the address of the branch the room belongs to
 *  @param string $room the room number of the room to add
 *  @param int $max_cap the max capacity of the room
 */
function addRoom($db, $company, $branch, $room, $max_cap)
{
    $sub_query = "SELECT c.company_id, b.branch_id, COALESCE(MAX(r.room_id), 0) AS `room_id` FROM company AS c\n                    INNER JOIN branch b on b.company_id = c.company_id\n                    INNER JOIN room r\n                    WHERE b.branch_address = '{$branch}' AND c.company_name = '{$company}'";
    $results = $db->query($sub_query);
    $ids = $results->fetch_assoc();
    $branch_id = $ids['branch_id'];
    $room_id = $ids['room_id'] + 1;
    //Generate random string and then hash it to retrieve the authentication key for the Pi
    $rand = genRandom();
    $hash = password_hash($rand, PASSWORD_DEFAULT);
    $insert_room = "INSERT INTO room (`room_id`, `branch_id`, `room_number`, `max_capacity`, `secret_key`)\n                      VALUES ('{$room_id}','{$branch_id}', '{$room}', '{$max_cap}', '{$hash}')";
    if ($db->query($insert_room) === TRUE) {
        echo "Successfully inserted '{$room}'" . "<br>";
        echo "Your Pi's password is " . $rand . ". Keep it safe and secure. The Pi needs it to submit counts" . "<br>";
        echo "Your Pi's id is " . $room_id . ". Keep it safe and secure. The Pi also needs it to submit counts" . "<br>";
    } else {
        echo "Error inserting '{$room}' <br>";
    }
}