<?php
require_once __DIR__ . '/../../api-user-check.php';

const GENERIC_IDENTIFIER_PATTERN = '|^[A-Za-z0-9\.]+$|';

if (isset($_GET['op'])) {
    $op = strtolower($_GET['op']);
}
if (isset($_GET['loc'])) {
    $loc = $_GET['loc'];
}
if (isset($_GET['filterLoc'])) {
    $filterLoc = $_GET['filterLoc'];
} else {
    $filterLoc = '';
}

if (preg_match(GENERIC_IDENTIFIER_PATTERN, $loc) === 1 && ($filterLoc === '' || preg_match(GENERIC_IDENTIFIER_PATTERN, $filterLoc) === 1)) {
    if ($op === 'add') {
        add_favourite($loc, $filterLoc);
    } elseif ($op === 'del') {
        remove_favourite($loc, $filterLoc);
    }
}

$configuration = getConfiguration();

// TODO May be something better to do here than write file and read it back in again. But at least it ensures consistency.

header('Content-Type: application/json');
echo json_encode($configuration);
