// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "LICENSE.txt".
require 'guifi_api.php';
/**
 * Configuration of authentication against guifi.net
 */
$username = "";
$password = "";
$gapi = new guifiAPI($username, $password);
if (!empty($_GET['a'])) {
    if ($_GET['a'] == 'getZone' && !empty($_GET['lat']) && !empty($_GET['lon'])) {
        $lat = $_GET['lat'];
        $lon = $_GET['lon'];
        $zones = $gapi->nearestZone($lat, $lon);
        if ($zones) {
            if ($zones->nearest) {
                $nearest = $zones->nearest;
                $candidates = $zones->candidates;
                echo '<select id="ipt-zone" name="zone_id">' . "\n";
                foreach ($candidates as $candidate) {
                    echo '<option value="' . $candidate->zone_id . '"' . ($candidate->zone_id == $nearest->zone_id ? ' selected="selected"' : '') . '>' . htmlspecialchars($candidate->title) . '</option>';
                }
                echo '</select>';
            } else {
                echo "No s'ha pogut trobar cap zona. És possible que no existeixi.<br />" . "\n";
                echo "Si saps quina zona és, introdueix manualment l'identificador de la zona:<br />\n";
                echo '<input type="text" name="zone" id="ipt-zone" />' . "\n";
            }
        } else {
     }
     break;
 case 'delete':
     $zone_id = 27181;
     $removed = $gapi->removeZone($zone_id);
     if ($removed) {
         echo "Zone <strong>{$zone_id}</strong> removed correctly.<br />\n<br />\n";
     } else {
         echo "There was an error deleting the zone.<br />\n";
         echo $gapi->getErrorsStr();
     }
     break;
 case 'nearest':
     $lat = "41.5787648";
     $lon = "1.6171926";
     $nearest = $gapi->nearestZone($lat, $lon);
     if ($nearest) {
         if ($nearest->nearest) {
             $zone = $nearest->nearest;
             echo "Found nearest zone:<br />\n";
             echo "&nbsp;&nbsp;Title: <strong>{$zone->title}</strong><br />\n";
             echo "&nbsp;&nbsp;Zone ID: <strong>{$zone->zone_id}</strong><br />\n";
         }
         if ($nearest->candidates) {
             $candidates = $nearest->candidates;
             echo "<br />\n";
             echo "Found candidate zones:\n";
             echo '<ul>' . "\n";
             foreach ($candidates as $zone) {
                 echo "<li>\n";
                 echo "&nbsp;&nbsp;Title: <strong>{$zone->title}</strong><br />\n";