// REGISTER NEW ACCOUNT
function register(){
$username = $_POST['username'];
$pin = $_POST['pin'];
$role = $_POST['role'];
//check if username already exists
$query = "SELECT * from users where username = '$username'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
if($row != null) {
$json['error'] = "This username already exists!";
}
//create new account
else {
$query = "INSERT INTO users(username, pin, role) VALUES ('$username', '$pin', '$role') ";
$result = mysql_query($query);
if ($result) {
$json['success'] = "Account successfuly created!";
$user["id"] = mysql_insert_id();
$user["username"] = $username;
$user["role"] = $role;
$data[0] = $user;
$json["data"] = $data;
}
else {
$json['error'] = "Database error!";
}
}
return json_encode($json);
}
// LOGIN WITH EXISTING ACCOUNT
function login(){
$username = $_POST['username'];
$pin = $_POST['pin'];
//check if user exists
$query = "SELECT id, username, role from users where username ='$username' and pin = '$pin' ";
$result = mysql_query($query);
if ($result) {
$row = mysql_fetch_assoc($result);
//if something wrong
if ($row == null) {
$json['error'] = "Wrong username/pin!";
}
else {
$json['success'] = "Login successful!";
$json['data'][0] = $row;
}
}
else {
$json['error'] = "Database error";
}
return json_encode($json);
}
// GET USERS with a role
function getUsers(){
$role = $_POST["role"];
//query
if ($role == 0 || $role == 1) {
$query = "SELECT * FROM users WHERE role = '$role' ";
}
else {
$query = "SELECT * FROM users";
}
$result = mysql_query($query);
if ($result){
//fetch results
$i = 0;
while ($row = mysql_fetch_array($result)) {
$data[$i] = $row;
$i++;
}
$json["success"] = "1";
$json["data"] = $data;
}
else {
$json['error'] = "Database error";
}
return json_encode($json);
}
// UPDATE USERS - if id null, add a new one
function updateUser(){
$id = $_POST["id"];
$username = $_POST["username"];
$role = $_POST["role"];
$pin = $_POST["pin"];
if ($id == 0) {
$query = "INSERT INTO users(username, pin, role) VALUES ('$username', '$pin', '$role') ";
}
else {
$query = "UPDATE users SET username = '$username', role = '$role', pin = '$pin' WHERE id = '$id' ";
}
$result = mysql_query($query);
if ($result) {
$json["success"] = "User successfuly ".($id == 0 ? "added!" : "updated!");
}
else {
$json["error"] = "An error occured!";
}
return json_encode($json);
}
// DELETE USER
function deleteUser(){
$id = $_POST["id"];
$result = mysql_query("DELETE FROM users WHERE id = '$id'");
if ($result) {
$json["success"] = "User successfuly deleted!";
}
else {
$json["error"] = "Error deleting user";
}
return json_encode($json);
}
//________________________________________________________________________________________________________________________________________________________________
// GET APARTMENTS - ALL or in a state
function getApartments(){
$state = $_POST["state"];
//get available apartments
if ($state == "0") {
$query = "SELECT * FROM apartments WHERE state = 0";
}
else {
$query = "SELECT * FROM apartments";
}
$result = mysql_query($query);
if ($result) {
$i = 0;
while ($row = mysql_fetch_array($result)) {
$data[$i] = $row;
$i++;
}
$json["data"] = $data;
}
else {
$json["error"] = "Error getting apartments";
}
return json_encode($json);
}
// UPDATE APARTMENT - if id null, than add a new one
function updateApartment(){
$id = $_POST["id"];
$userId = $_POST["userId"];
$state = $_POST["state"];
$dateAdded = $_POST["dateAdded"];
$name = $_POST["name"];
$price = $_POST["price"];
$floorSize = $_POST["floorSize"];
$rooms = $_POST["rooms"];
$description = $_POST["description"];
$address = $_POST["address"];
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
//id null, add a new apartment
if ($id == 0) {
$query = "INSERT INTO apartments(userId, state, dateAdded, name, price, floorSize, rooms, description, address";
if ($latitude && $longitude) {
$query = $query.", latitude, longitude";
}
$query = $query.") ";
$query = $query."VALUES ('$userId', '$state', '$dateAdded', '$name', '$price', '$floorSize', '$rooms', '$description', '$address'";;
if ($latitude && $longitude) {
$query = $query.", '$latitude', '$longitude'";
}
$query = $query.") ";
}
//id not null, update it
else {
$query = "UPDATE apartments SET userId = '$userId', state = '$state', dateAdded = '$dateAdded',
name = '$name', price = '$price', floorSize = '$floorSize', rooms = '$rooms', description = '$description', address = '$address'";
if ($latitude && $longitude) {
$query = $query.", latitude = '$latitude', longitude = '$longitude'";
}
$query = $query."WHERE id = '$id' ";
}
$result = mysql_query($query);
if ($result) {
$json["success"] = "Apartment successfuly ".($id == 0 ? "added!" : "updated!");
//get id if just inserted
if ($id == 0) {
$id = mysql_insert_id();
}
// echo $address." ".$latitude." ".$longitude;
//google geo coding
if ($address != null && ($latitude == null || $longitude == null || $latitude == 0 || $longitude == 0)) {
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=AIzaSyB9dzDY20uU1ebcKg5g7wF16mbbdrMV9Ro";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// var_dump($resp);
// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){
// get the important data
$latitude = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
$longitude = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
$formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
// verify if data is complete
if($latitude && $longitude && $formatted_address){
$query = "UPDATE apartments SET latitude = '$latitude', longitude = '$longitude', address = '$formatted_address' WHERE id = '$id' ";
}
echo $query;
$result = mysql_query($query);
}
}
}
else {
$json["error"] = "An error occured!";
}
return json_encode($json);
}
// DELETE APARTMENT
function deleteApartment(){
$id = $_POST["id"];
$result = mysql_query("DELETE FROM apartments WHERE id = '$id'");
if ($result) {
$json["success"] = "Apartment successfuly deleted!";
}
else {
$json["error"] = "Error deleting apartment";
}
return json_encode($json);
}
Warning: Cannot modify header information - headers already sent by (output started at /var/www/zumzet.ro/html/api/calories/functions.php:1) in /var/www/zumzet.ro/html/api/calories/index.php on line 10
Notice: Undefined index: CONTENT_TYPE in /var/www/zumzet.ro/html/api/calories/index.php on line 12
Warning: Cannot modify header information - headers already sent by (output started at /var/www/zumzet.ro/html/api/calories/functions.php:1) in /var/www/zumzet.ro/html/api/calories/index.php on line 37
Not Found!