$notify_privacy = $_GET['notify_privacy'] == 'true' ? true : false; $notify_timeline = $_GET['notify_timeline'] == 'true' ? true : false; $groups = explode(',', $_GET['groups'] == '' ? null : $_GET['groups']); $name = $_GET['name'] != '' ? $_GET['name'] : null; // do not use htmlentities, AngularJS will protect us $update = $DBH->prepare('UPDATE accounts SET name = :name, notify_status = :notify_status, notify_statusmsg = :notify_statusmsg, notify_profilepic = :notify_profilepic, notify_privacy = :notify_privacy, notify_timeline = :notify_timeline WHERE id = :id;'); $update->execute(array(':id' => $number, ':name' => $name, ':notify_status' => (int) $notify_status, ':notify_statusmsg' => (int) $notify_statusmsg, ':notify_profilepic' => (int) $notify_profilepic, ':notify_privacy' => (int) $notify_privacy, ':notify_timeline' => (int) $notify_timeline)); // Update groups $select_group = $DBH->prepare('SELECT gid FROM accounts_to_groups WHERE number = :number'); $select_group->execute(array(':number' => $number)); // Remove groups if they are not listed anymore $processed_groups = []; foreach ($select_group->fetchAll(PDO::FETCH_ASSOC) as $group_in_db) { if (!in_array($group_in_db['gid'], $groups)) { removeUserInGroup($group_in_db['gid'], $number); } else { array_push($processed_groups, $group_in_db['gid']); } } // Add any new groups foreach ($groups as $group) { if (!in_array($group, $processed_groups) && $group != '') { insertUserInGroup($group, $number); } } echo json_encode(['success' => true, 'number' => $number]); } else { echo json_encode(['error' => 'No name or correct phone number supplied!', 'code' => 400]); } break;
/** * Add a new account to the database. * Give a name, a phonenumber (id) and request if you a true/false or a array for JSON syntax (for any errors). */ function addAccount($name, $account_id, $groups, $array_result = false) { global $DBH; $number = $account_id; // Check before insert $check = $DBH->prepare('SELECT "active" FROM accounts WHERE "id"=:id'); $check->execute(array(':id' => $number)); if ($check->rowCount() == 0) { $insert = $DBH->prepare('INSERT INTO accounts (id, active, name) VALUES (:id, true, :name);'); $insert->execute(array(':id' => $number, ':name' => $name)); // Add any new groups foreach ($groups as $group) { if ($group != '') { insertUserInGroup($group, $number); } } if ($array_result) { return ['success' => true]; } else { return true; } } else { // Account already exists, make sure to re-activate if status=false $row = $check->fetch(); if ($row['active'] == true) { if ($array_result) { return ['error' => 'Phone already exists!', 'code' => 400]; } else { return false; } } else { $update = $DBH->prepare('UPDATE accounts SET "active" = true WHERE id = :number;'); $update->execute(array(':number' => $number)); // Remove groups if they are not listed anymore $select_group = $DBH->prepare('SELECT gid FROM accounts_to_groups WHERE number = :number'); $select_group->execute(array(':number' => $number)); $processed_groups = []; foreach ($select_group->fetchAll(PDO::FETCH_ASSOC) as $group_in_db) { if (!in_array($group_in_db['gid'], $groups)) { removeUserInGroup($group_in_db['gid'], $number); } else { array_push($processed_groups, $group_in_db['gid']); } } // Add any new groups foreach ($groups as $group) { if (!in_array($group, $processed_groups) && $group != '') { insertUserInGroup($group, $number); } } if ($array_result) { return ['success' => true]; } else { return true; } } } }