#!/usr/local/bin/php -f
<?php

/*
 * cp_allow cpzone=.... ip=... username=... [mac=...] [attrs=...] ...
 * output on stdout: sessionid
 */

global $cpzone;

$ip = $cpzone = null;

array_shift($argv);
foreach ($argv as $arg) {
        list($name, $value) = explode('=', $arg, 2);
        switch($name) {
                case 'ip':
                        $ip=$value;
                        break;
                case 'cpzone':
                        $cpzone=$value;
                        break;
        }
}

// check IP format (ipv4 only)
if (! preg_match( '/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)) { 
        exit(2);
}

// check if cpzone is defined
if ( $cpzone == null ) {
        exit(4);
}

require("captiveportal.inc");
$result = captiveportal_read_db("WHERE ip = '{$ip}'");
foreach ($result as $cpentry) {
    captiveportal_disconnect_client($cpentry[5]);
}

?>
