Skip to content

Commit

Permalink
Merge pull request #2 from hotspotbilling/development
Browse files Browse the repository at this point in the history
Import Hotspot and PPOE Package and Users
  • Loading branch information
ibnux authored Jan 30, 2023
2 parents e3538f9 + 55bdcb0 commit fb2d382
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# PHPNuxBill Plugin: Mikrotik Import

***UNFINISHED - STILL ON PROGRESS***

Import existing Mikrotik Hotspot Package and User to PHPNuxBill

***This script will import Users and Profiles, but will not add active packages to user.***

# Installation

Copy **mikrotik_import.php** to **/system/plugin/**
Expand Down
135 changes: 131 additions & 4 deletions mikrotik_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,23 @@ function mikrotik_import_start_ui()
$admin = Admin::_info();
$ui->assign('_admin', $admin);

$type = $_POST['type'];

// get mikrotik info
$mikrotik = ORM::for_table('tbl_routers')->where('name', $_POST['server'])->find_one();
$results = mikrotik_import_mikrotik_package($_POST['server'], $mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);


if($type=='Hotspot'){
$results = mikrotik_import_mikrotik_hotspot_package($_POST['server'], $mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
}else if($type=='PPPOE'){
$results = mikrotik_import_mikrotik_ppoe_package($_POST['server'], $mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
}
$ui->assign('results', $results);
$ui->display('mikrotik_import_start.tpl');
}

function mikrotik_import_mikrotik_package($router, $ip, $user, $pass)
function mikrotik_import_mikrotik_hotspot_package($router, $ip, $user, $pass)
{
$client = Mikrotik::getClient($ip, $user, $pass);
// import Hotspot Profile to package
$printRequest = new RouterOS\Request(
'/ip hotspot user profile print'
);
Expand Down Expand Up @@ -100,5 +105,127 @@ function mikrotik_import_mikrotik_package($router, $ip, $user, $pass)
}
}
}
// Import user
$userRequest = new RouterOS\Request(
'/ip hotspot user print'
);
$users = $client->sendSync($userRequest)->toArray();
foreach ($users as $u) {
$username = $u->getProperty('name');
if(!empty($username) && !empty($u->getProperty('password'))){
$d = ORM::for_table('tbl_customers')->where('username', $username)->find_one();
if($d){
$results[] = "Username Exists: $username";
}else{
$d = ORM::for_table('tbl_customers')->create();
$d->username = $username;
$d->password = $u->getProperty('password');
$d->fullname = $username;
$d->address = '';
$d->email = $u->getProperty('email');
$d->phonenumber = '';
if ($d->save()) {
$results[] = "$username added successfully";
}else{
$results[] = "$username Failed to be added";
}
}
}
}
return $results;
}


function mikrotik_import_mikrotik_ppoe_package($router, $ip, $user, $pass)
{
$client = Mikrotik::getClient($ip, $user, $pass);
// import Hotspot Profile to package
$printRequest = new RouterOS\Request(
'/ppp profile print'
);
$results = [];
$profiles = $client->sendSync($printRequest)->toArray();
foreach ($profiles as $p) {
$name = $p->getProperty('name');
$rateLimit = $p->getProperty('rate-limit');

// 10M/10M
$rateLimit = explode(" ", $rateLimit)[0];
if (strlen($rateLimit) > 1) {
// Create Bandwidth profile
$rate = explode("/", $rateLimit);
$unit_up = preg_replace("/[^a-zA-Z]+/", "", $rate[0]) . "bps";
$unit_down = preg_replace("/[^a-zA-Z]+/", "", $rate[1]) . "bps";
$rate_up = preg_replace("/[^0-9]+/", "", $rate[0]);
$rate_down = preg_replace("/[^0-9]+/", "", $rate[1]);
$bw_name = str_replace("/", "_", $rateLimit);
$bw = ORM::for_table('tbl_bandwidth')->where('name_bw', $bw_name)->find_one();
if (!$bw) {
$results[] = "Bandwith Created: $bw_name";
$d = ORM::for_table('tbl_bandwidth')->create();
$d->name_bw = $bw_name;
$d->rate_down = $rate_down;
$d->rate_down_unit = $unit_down;
$d->rate_up = $rate_up;
$d->rate_up_unit = $unit_up;
$d->save();
$bw_id = $d->id();
}else{
$results[] = "Bandwith Exists: $bw_name";
$bw_id = $bw->id;
}

// Create Packages
$pack = ORM::for_table('tbl_plans')->where('name_plan', $name)->find_one();
if(!$pack){
$results[] = "Packages Created: $name";
$d = ORM::for_table('tbl_plans')->create();
$d->name_plan = $name;
$d->id_bw = $bw_id;
$d->price = '10000';
$d->type = 'PPPOE';
$d->typebp = 'Unlimited';
$d->limit_type = 'Time_Limit';
$d->time_limit = 0;
$d->time_unit = 'Hrs';
$d->data_limit = 0;
$d->data_unit = 'MB';
$d->validity = '30';
$d->validity_unit = 'Days';
$d->routers = $router;
$d->enabled = 1;
$d->save();
}else{
$results[] = "Packages Exists: $name";
}
}
}
// Import user
$userRequest = new RouterOS\Request(
'/ppp secret print'
);
$users = $client->sendSync($userRequest)->toArray();
foreach ($users as $u) {
$username = $u->getProperty('name');
if(!empty($username) && !empty($u->getProperty('password'))){
$d = ORM::for_table('tbl_customers')->where('username', $username)->find_one();
if($d){
$results[] = "Username Exists: $username";
}else{
$d = ORM::for_table('tbl_customers')->create();
$d->username = $username;
$d->password = $u->getProperty('password');
$d->fullname = $username;
$d->address = '';
$d->email = '';
$d->phonenumber = '';
if ($d->save()) {
$results[] = "$username added successfully";
}else{
$results[] = "$username Failed to be added";
}
}
}
}
return $results;
}
45 changes: 45 additions & 0 deletions ui/mikrotik_import.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{include file="sections/header.tpl"}

<form class="form-horizontal" method="post" role="form" action="{$_url}plugin/mikrotik_import_start_ui">
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="panel panel-primary panel-hovered panel-stacked mb30">
<div class="panel-heading">Information</div>
<div class="panel-body">
<ol>
<li>This Plugin only import Packages and Users</li>
<li>Active package will not be imported</li>
<li>You must Refill the user or User buy new package</li>
</ol>
</div>
</div>
<div class="panel panel-primary panel-hovered panel-stacked mb30">
<div class="panel-heading">Import User and Packages from Mikrotik</div>
<div class="panel-body">
<div class="form-group">
<label class="col-md-2 control-label">{$_L['Type']}</label>
<div class="col-md-6">
<input type="radio" id="Hot" name="type" value="Hotspot"> {$_L['Hotspot_Plans']}
<input type="radio" id="POE" name="type" value="PPPOE"> {$_L['PPPOE_Plans']}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{$_L['Routers']}</label>
<div class="col-md-6">
<select id="server" required name="server" class="form-control">
<option value=''>{$_L['Select_Routers']}</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success waves-effect waves-light" type="submit">Import User</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>

{include file="sections/footer.tpl"}
26 changes: 26 additions & 0 deletions ui/mikrotik_import_start.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{include file="sections/header.tpl"}

<form class="form-horizontal" method="post" role="form" action="{$_url}settings/app-post">
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="panel panel-primary panel-hovered panel-stacked mb30">
<div class="panel-heading">Information</div>
<div class="panel-body">
After import, you need to configure Packages, set time limit
</div>
</div>
<div class="panel panel-primary panel-hovered panel-stacked mb30">
<div class="panel-heading">Package import</div>
<div class="panel-body">
<ol>
{foreach $results as $result}
<li>{$result}</li>
{/foreach}
</ol>
</div>
</div>
</div>
</div>
</form>

{include file="sections/footer.tpl"}

0 comments on commit fb2d382

Please sign in to comment.