From ebd9babb7bd4bfb474950382f64ffe55c023fea3 Mon Sep 17 00:00:00 2001 From: iBNu Maksum Date: Fri, 20 Jan 2023 10:36:58 +0700 Subject: [PATCH 1/4] UI import --- ui/mikrotik_import.tpl | 35 +++++++++++++++++++++++++++++++++++ ui/mikrotik_import_start.tpl | 18 ++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 ui/mikrotik_import.tpl create mode 100644 ui/mikrotik_import_start.tpl diff --git a/ui/mikrotik_import.tpl b/ui/mikrotik_import.tpl new file mode 100644 index 0000000..d9a33de --- /dev/null +++ b/ui/mikrotik_import.tpl @@ -0,0 +1,35 @@ +{include file="sections/header.tpl"} + +
+
+
+
+
Import User and Packages from Mikrotik
+
+
+ +
+ {$_L['Hotspot_Plans']} + {$_L['PPPOE_Plans']} +
+
+
+ +
+ +
+
+
+
+ +
+
+
+
+
+
+
+{include file="sections/footer.tpl"} \ No newline at end of file diff --git a/ui/mikrotik_import_start.tpl b/ui/mikrotik_import_start.tpl new file mode 100644 index 0000000..63c638e --- /dev/null +++ b/ui/mikrotik_import_start.tpl @@ -0,0 +1,18 @@ +{include file="sections/header.tpl"} +
+
+
+
+
Package import
+
+
    + {foreach $results as $result} +
  1. {$result}
  2. + {/foreach} +
+
+
+
+
+
+{include file="sections/footer.tpl"} \ No newline at end of file From 6ac6e53f9ed3844ed283875c91bd748b9b9ea003 Mon Sep 17 00:00:00 2001 From: iBNu Maksum Date: Mon, 30 Jan 2023 14:14:38 +0700 Subject: [PATCH 2/4] Import hotspot packages and user --- mikrotik_import.php | 40 ++++++++++++++++++++++++++++++++---- ui/mikrotik_import.tpl | 16 ++++++++++++--- ui/mikrotik_import_start.tpl | 3 +++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/mikrotik_import.php b/mikrotik_import.php index 568da31..83cd447 100644 --- a/mikrotik_import.php +++ b/mikrotik_import.php @@ -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'){ + echo "POE"; + } $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' ); @@ -100,5 +105,32 @@ 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; } diff --git a/ui/mikrotik_import.tpl b/ui/mikrotik_import.tpl index d9a33de..0113d44 100644 --- a/ui/mikrotik_import.tpl +++ b/ui/mikrotik_import.tpl @@ -3,6 +3,17 @@
+ +
+
Information
+
+
    +
  1. This Plugin only import Packages and Users
  2. +
  3. Active package will not be imported
  4. +
  5. You must Refill the user or User buy new package
  6. +
+
+
Import User and Packages from Mikrotik
@@ -10,7 +21,7 @@
{$_L['Hotspot_Plans']} - {$_L['PPPOE_Plans']} + {$_L['PPPOE_Plans']}
@@ -23,8 +34,7 @@
- +
diff --git a/ui/mikrotik_import_start.tpl b/ui/mikrotik_import_start.tpl index 63c638e..8386b08 100644 --- a/ui/mikrotik_import_start.tpl +++ b/ui/mikrotik_import_start.tpl @@ -1,7 +1,9 @@ {include file="sections/header.tpl"} +
+
Package import
@@ -15,4 +17,5 @@
+ {include file="sections/footer.tpl"} \ No newline at end of file From 560a4bd62b55b1d70d19e0aba4e830b35077e1cb Mon Sep 17 00:00:00 2001 From: iBNu Maksum Date: Mon, 30 Jan 2023 14:16:12 +0700 Subject: [PATCH 3/4] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3676c6a..11827e3 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ 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/** From 55bdcb0d98047ab5c09bac7bf69ace8f06ebc2a9 Mon Sep 17 00:00:00 2001 From: iBNu Maksum Date: Mon, 30 Jan 2023 14:32:06 +0700 Subject: [PATCH 4/4] Import Mikrotik PPOE --- README.md | 2 - mikrotik_import.php | 97 +++++++++++++++++++++++++++++++++++- ui/mikrotik_import.tpl | 4 +- ui/mikrotik_import_start.tpl | 15 ++++-- 4 files changed, 108 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 11827e3..c1dc439 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # 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.*** diff --git a/mikrotik_import.php b/mikrotik_import.php index 83cd447..f0046d1 100644 --- a/mikrotik_import.php +++ b/mikrotik_import.php @@ -33,7 +33,7 @@ function mikrotik_import_start_ui() if($type=='Hotspot'){ $results = mikrotik_import_mikrotik_hotspot_package($_POST['server'], $mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); }else if($type=='PPPOE'){ - echo "POE"; + $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'); @@ -134,3 +134,98 @@ function mikrotik_import_mikrotik_hotspot_package($router, $ip, $user, $pass) } 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; +} \ No newline at end of file diff --git a/ui/mikrotik_import.tpl b/ui/mikrotik_import.tpl index 0113d44..0283201 100644 --- a/ui/mikrotik_import.tpl +++ b/ui/mikrotik_import.tpl @@ -3,7 +3,6 @@
-
Information
@@ -42,4 +41,5 @@
-{include file="sections/footer.tpl"} \ No newline at end of file + +{include file="sections/footer.tpl"} diff --git a/ui/mikrotik_import_start.tpl b/ui/mikrotik_import_start.tpl index 8386b08..724a9c1 100644 --- a/ui/mikrotik_import_start.tpl +++ b/ui/mikrotik_import_start.tpl @@ -3,14 +3,19 @@
-
+
+
Information
+
+ After import, you need to configure Packages, set time limit +
+
Package import
    - {foreach $results as $result} -
  1. {$result}
  2. - {/foreach} + {foreach $results as $result} +
  3. {$result}
  4. + {/foreach}
@@ -18,4 +23,4 @@
-{include file="sections/footer.tpl"} \ No newline at end of file +{include file="sections/footer.tpl"}