Esempio n. 1
0
         // 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;
     /**
      *		Update whatsspy config.
      */
 /**
  *		Update whatsspy config.
  */
 case 'updateConfig':
     requireAuth();
Esempio n. 2
0
/**
 *		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;
            }
        }
    }
}