Skip to content

Commit

Permalink
Stable PPPoE API, enhanced MySQL driver
Browse files Browse the repository at this point in the history
  • Loading branch information
simultsop committed Oct 24, 2018
1 parent ff1d314 commit e7ea4af
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 11 deletions.
140 changes: 134 additions & 6 deletions pppoe/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,95 @@

Class PPPoE_MySQL_Driver extends MySQL_Driver {

public function update() {
$username = requestParam("username");
$password = requestParam("password");
$albismart_id = requestParam("albismart_id");
$limit = requestParam("limit");
$simuse = requestParam("simuse");
$mac = requestParam("mac");
$pool = requestParam("pool");
$cpeip = requestParam("cpeip");

if($username && $password) {
$this->remove("radcheck")->where("username", $username)->where("attribute", "Cleartext-Password")->all();
$authColumns = array("username" => $username, "attribute" => "Cleartext-Password", "op" => ":=", "value" => $password);
$this->create("radcheck")->columns($authColumns)->save();
}

if($username && $limit) {
$this->remove("radreply")->where("username", $username)->all();
$limitColumns = array("username" => $username, "attribute" => "Mikrotik-Rate-Limit", "op" => "=", "value" => $limit);
$this->create("radreply")->columns($limitColumns)->save();
}

if($username && $albismart_id) {
$this->remove("aid")->where("username", $username)->all();
$aidColumns = array("username" => $username, "id" => $albismart_id);
$this->create("aid")->columns($aidColumns)->save();
}

if($username && $simuse) {
$this->remove("radcheck")->where("username", $username)->where("attribute", "Simultaneous-Use")->all();
$simUseColumns = array("username" => $username, "attribute" => "Simultaneous-Use", "op" => ":=", "value" => $simuse);
$this->create("radcheck")->columns($simUseColumns)->save();
}

if($username && $pool) {
$this->remove("radreply")->where("username", $username)->where("attribute", "Framed-Pool")->all();
$poolColumns = array("username" => $username, "attribute" => "Framed-Pool", "op" => "=", "value" => $pool);
$this->create("radreply")->columns($poolColumns)->save();
}

if($username && isValidMacAddress($mac)) {
$this->remove("radcheck")->where("username", $username)->where("attribute", "Calling-Station-Id")->all();
$macColumns = array("username" => $username, "attribute" => "Calling-Station-Id", "op" => ":=", "value" => $mac);
$this->create("radcheck")->columns($macColumns)->save();
}

if($username && isValidIPAddress($cpeip)) {
$this->remove("radreply")->where("username", $username)->where("attribute", "Framed-IP-Address")->all();
$cpeipColumns = array("username" => $username, "attribute" => "Framed-IP-Address", "op" => "=", "value" => $cpeip);
$this->create("radreply")->columns($cpeipColumns)->save();
}

$this->disconnect(false);

returnJson(true);
}

public function remove() {
$username = requestParam("username");

if($username) {
$this->remove("radcheck")->where("username", $username)->all();
$this->remove("radreply")->where("username", $username)->all();
$this->remove("aid")->where("username", $username)->all();

$this->disconnect(false);
returnJson(true);
}

returnJson("");
}

public function disconnect($returnJson = true) {
$username = requestParam("username");

if($username) {
$activeConnections = $this->get("nas")->innerJoin("radacct ON nas.nasname = radacct.nasipaddress")->select("radacct.nasipaddress as nasip, nas.secret as secret")->isNull("acctstoptime")->where("radacct.username", $username)->all();
if(is_array($activeConnections)) {
foreach($activeConnections as $activeConnection) {
$disconnectCommand = "echo User-Name={$username} | radclient -x ";
$disconnectCommand.= $activeConnection->nasip . ":1700 disconnect " . $activeConnection->secret;
exec($disconnectCommand);
}
}

if($returnJson) { returnJson(true); }
} else { returnJson(""); }
}

public function online() {
$username = (isset($_GET['username'])) ? urldecode($_GET['username']) : null;
if($username) {
Expand All @@ -32,14 +121,14 @@ public function online() {
returnJson($result);
}

public function nases() {
returnJson($this->get("nas")->all());
}

public function nas() {
$method = (isset($_POST["ip"])) ? "create" : "get";
if(!requestParam("ip")) {
returnJson($this->get("nas")->all());
}

$nas = $this->get("nas")->where("nasname", requestParam("ip"))->first();
if($method=="get") { returnJson($nas); }
if(!isset($_POST["ip"])) { returnJson($nas); }

if(requestParam("delete") && $nas) {
$result = ($this->remove("nas")->where("nasname", $nas->nasname)->save()) ? $nas : null;
returnJson($result);
Expand All @@ -64,6 +153,45 @@ public function nas() {
}
}

public function traffic() {
$username = requestParam("username");
$timeframe = (requestParam("year")) ? requestParam("year") : date("Y");
if(requestParam("month")) { $timeframe .= "-" . requestParam("month"); }
if(requestParam("day")) { $timeframe .= "-" . requestParam("day"); }
$group = (requestParam("month")) ? "DAY" : (requestParam("year") ? "MONTH" : "YEAR");
$timeframeSubstr = (requestParam("month")) ? "1,10" : (requestParam("year") ? "1,7" : "1,4");

$columns = array(
"acctstarttime AS connectedAt",
"SUM(acctsessiontime) AS onlineTime",
"SUM(acctinputoctets) AS upload",
"SUM(acctoutputoctets) AS download",
"SUM(acctinputoctets) + SUM(acctoutputoctets) as total",
"SUBSTR(acctstarttime, {$timeframeSubstr}) as timeframe",
);

$this->get("radacct")->columns($columns)->where("username", $username)->where("acctstarttime", $timeframe, "RLIKE")->group($group . "(acctstarttime)");

if(requestParam("day")) {
$columns = array(
"acctstarttime AS connectedAt",
"acctstoptime AS disconnectedAt",
"acctsessiontime AS onlineTime",
"acctinputoctets AS upload",
"acctoutputoctets AS download",
"acctinputoctets + acctoutputoctets as total",
"framedipaddress as ip",
"callingstationid as mac",
"nasipaddress as nas",
"calledstationid as interface",
"CONCAT(acctstarttime, ' - ', acctstoptime) as timeframe",
);
$this->get("radacct")->columns($columns)->where("username", $username)->where("acctstarttime", $timeframe, "RLIKE");
}

returnJson($this->fetchAll());
}

}

$action = (isset($_GET["action"])) ? $_GET["action"] : "info";
Expand Down
26 changes: 21 additions & 5 deletions pppoe/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Class MySQL_Driver {

protected $db, $method, $table, $operation, $columns, $condition, $order;
protected $db, $method, $table, $operation, $columns, $condition, $order, $group, $limit;

public function __construct() {
$host = config("database.host");
Expand Down Expand Up @@ -81,6 +81,12 @@ public function isNull($column, $isNull = true) {
return $this;
}

public function fetch() {
$result = $this->query();
if($result) { $result = $result->fetch(PDO::FETCH_OBJ); }
return ($result!=false) ? $result : null;
}

public function first($column = "id", $order = "DESC") {
if(substr($this->operation, 0, 6)!="SELECT") { return false; }
$this->order($column, $order);
Expand All @@ -97,7 +103,10 @@ public function latest($limit = 10, $column = "id", $order = "DESC") {
return $this->all();
}

/* Alias and columns method */
public function fetchAll() { return $this->all(); }
public function all() {
if(substr($this->operation, 0, 6)!="DELETE") { return $this->query(); }
if(substr($this->operation, 0, 6)!="SELECT") { return false; }
$result = $this->query();
if($result) { $result = $result->fetchAll(PDO::FETCH_OBJ); }
Expand All @@ -114,17 +123,22 @@ public function now() {
}

public function innerJoin($statement) {
$this->operation .= "INNER JOIN " . $statement;
if($statement) { $this->operation .= "INNER JOIN " . $statement; }
return $this;
}

public function order($column = "id", $order = "DESC") {
$this->order = " ORDER BY {$column} {$order} ";
if($column!="") { $this->order = " ORDER BY {$column} {$order} "; }
return $this;
}

public function group($column = "id") {
if($column!="") { $this->group = " GROUP BY {$column} "; }
return $this;
}

public function limit($limit = 1) {
$this->order .= " LIMIT {$limit} ";
if($limit!=0) { $this->limit = " LIMIT {$limit} "; }
return $this;
}

Expand All @@ -133,6 +147,8 @@ protected function table($table) {
$this->columns = "*";
$this->condition = "";
$this->order = "";
$this->group = "";
$this->limit = "";
}

protected function operation($operation = "SELECT") {
Expand All @@ -145,7 +161,7 @@ protected function operation($operation = "SELECT") {
}

protected function query() {
$query = $this->db->{$this->method}( $this->operation . $this->condition . $this->order );
$query = $this->db->{$this->method}( $this->operation . $this->condition . $this->order . $this->group . $this->limit );
return ($query!=false) ? $query : null;
}

Expand Down

0 comments on commit e7ea4af

Please sign in to comment.