phpnuxbill/system/controllers/services.php

498 lines
19 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
/**
2022-08-23 17:33:21 +08:00
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
2022-08-23 17:33:21 +08:00
**/
2017-03-11 03:51:06 +08:00
_admin();
2022-08-23 17:33:21 +08:00
$ui->assign('_title', $_L['Hotspot_Plans'] . ' - ' . $config['CompanyName']);
2017-03-11 03:51:06 +08:00
$ui->assign('_system_menu', 'services');
$action = $routes['1'];
$admin = Admin::_info();
$ui->assign('_admin', $admin);
2022-08-23 17:33:21 +08:00
if ($admin['user_type'] != 'Admin' and $admin['user_type'] != 'Sales') {
r2(U . "dashboard", 'e', $_L['Do_Not_Access']);
2017-03-11 03:51:06 +08:00
}
use PEAR2\Net\RouterOS;
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
require_once 'system/autoload/PEAR2/Autoload.php';
switch ($action) {
case 'hotspot':
2022-08-23 17:33:21 +08:00
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/hotspot.js"></script>');
$name = _post('name');
if ($name != '') {
$paginator = Paginator::bootstrap('tbl_plans', 'name_plan', '%' . $name . '%', 'type', 'Hotspot');
$d = ORM::for_table('tbl_bandwidth')->join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))->where('tbl_plans.type', 'Hotspot')->where_like('tbl_plans.name_plan', '%' . $name . '%')->offset($paginator['startpoint'])->limit($paginator['limit'])->find_many();
} else {
$paginator = Paginator::bootstrap('tbl_plans', 'type', 'Hotspot');
$d = ORM::for_table('tbl_bandwidth')->join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))->where('tbl_plans.type', 'Hotspot')->offset($paginator['startpoint'])->limit($paginator['limit'])->find_many();
}
$ui->assign('d', $d);
$ui->assign('paginator', $paginator);
2017-03-11 03:51:06 +08:00
$ui->display('hotspot.tpl');
break;
case 'add':
2022-08-23 17:33:21 +08:00
$d = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('d', $d);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
2017-03-11 03:51:06 +08:00
$ui->display('hotspot-add.tpl');
break;
case 'edit':
$id = $routes['2'];
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 17:33:21 +08:00
if ($d) {
$ui->assign('d', $d);
$b = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('b', $b);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
2017-03-11 03:51:06 +08:00
$ui->display('hotspot-edit.tpl');
2022-08-23 17:33:21 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'services/hotspot', 'e', $_L['Account_Not_Found']);
}
break;
case 'delete':
$id = $routes['2'];
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 17:33:21 +08:00
if ($d) {
$mikrotik = Router::_info($d['routers']);
try {
$iport = explode(":", $mikrotik['ip_address']);
$client = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
} catch (Exception $e) {
die("Unable to connect to the router.<br>".$e->getMessage());
}
$printRequest = new RouterOS\Request(
'/ip hotspot user profile print .proplist=name',
RouterOS\Query::where('name', $d['name_plan'])
);
$profileName = $client->sendSync($printRequest)->getProperty('name');
$removeRequest = new RouterOS\Request('/ip/hotspot/user/profile/remove');
$client(
$removeRequest
->setArgument('numbers', $profileName)
2017-03-11 03:51:06 +08:00
);
$d->delete();
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
r2(U . 'services/hotspot', 's', $_L['Delete_Successfully']);
}
break;
case 'add-post':
$name = _post('name');
$typebp = _post('typebp');
2022-08-23 17:33:21 +08:00
$limit_type = _post('limit_type');
$time_limit = _post('time_limit');
$time_unit = _post('time_unit');
$data_limit = _post('data_limit');
$data_unit = _post('data_unit');
$id_bw = _post('id_bw');
$price = _post('pricebp');
$sharedusers = _post('sharedusers');
2017-03-11 03:51:06 +08:00
$validity = _post('validity');
2022-08-23 17:33:21 +08:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
2017-03-11 03:51:06 +08:00
$msg = '';
2022-08-23 17:33:21 +08:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $routers == '') {
$msg .= $_L['All_field_is_required'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
$d = ORM::for_table('tbl_plans')->where('name_plan', $name)->where('type', 'Hotspot')->find_one();
if ($d) {
$msg .= $_L['Plan_already_exist'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
} else {
$unitdown = 'M';
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
} else {
$unitup = 'M';
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
$mikrotik = Router::_info($routers);
try {
$iport = explode(":", $mikrotik['ip_address']);
$client = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
} catch (Exception $e) {
die("Unable to connect to the router.<br>".$e->getMessage());
}
$addRequest = new RouterOS\Request('/ip/hotspot/user/profile/add');
$client->sendSync(
$addRequest
->setArgument('name', $name)
->setArgument('shared-users', $sharedusers)
->setArgument('rate-limit', $rate)
2017-03-11 03:51:06 +08:00
);
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_plans')->create();
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price;
2022-08-23 17:33:21 +08:00
$d->type = 'Hotspot';
2017-03-11 03:51:06 +08:00
$d->typebp = $typebp;
2022-08-23 17:33:21 +08:00
$d->limit_type = $limit_type;
$d->time_limit = $time_limit;
$d->time_unit = $time_unit;
$d->data_limit = $data_limit;
$d->data_unit = $data_unit;
$d->validity = $validity;
2017-03-11 03:51:06 +08:00
$d->validity_unit = $validity_unit;
2022-08-23 17:33:21 +08:00
$d->shared_users = $sharedusers;
$d->routers = $routers;
2017-03-11 03:51:06 +08:00
$d->save();
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
r2(U . 'services/hotspot', 's', $_L['Created_Successfully']);
2022-08-23 17:33:21 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'services/add', 'e', $msg);
}
break;
case 'edit-post':
2022-08-23 17:33:21 +08:00
$id = _post('id');
2017-03-11 03:51:06 +08:00
$name = _post('name');
$id_bw = _post('id_bw');
2022-08-23 17:33:21 +08:00
$typebp = _post('typebp');
2017-03-11 03:51:06 +08:00
$price = _post('price');
2022-08-23 17:33:21 +08:00
$limit_type = _post('limit_type');
$time_limit = _post('time_limit');
$time_unit = _post('time_unit');
$data_limit = _post('data_limit');
$data_unit = _post('data_unit');
$sharedusers = _post('sharedusers');
2017-03-11 03:51:06 +08:00
$validity = _post('validity');
2022-08-23 17:33:21 +08:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
2017-03-11 03:51:06 +08:00
$msg = '';
2022-08-23 17:33:21 +08:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $routers == '') {
$msg .= $_L['All_field_is_required'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
$d = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
if ($d) {
} else {
$msg .= $_L['Data_Not_Found'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
} else {
$unitdown = 'M';
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
} else {
$unitup = 'M';
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
$mikrotik = Router::_info($routers);
try {
$iport = explode(":", $mikrotik['ip_address']);
$client = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
} catch (Exception $e) {
die("Unable to connect to the router.<br>".$e->getMessage());
}
$printRequest = new RouterOS\Request(
'/ip hotspot user profile print .proplist=name',
RouterOS\Query::where('name', $name)
);
$profileName = $client->sendSync($printRequest)->getProperty('name');
$setRequest = new RouterOS\Request('/ip/hotspot/user/profile/set');
$client(
$setRequest
->setArgument('numbers', $profileName)
->setArgument('shared-users', $sharedusers)
->setArgument('rate-limit', $rate)
2017-03-11 03:51:06 +08:00
);
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price;
$d->typebp = $typebp;
2022-08-23 17:33:21 +08:00
$d->limit_type = $limit_type;
$d->time_limit = $time_limit;
$d->time_unit = $time_unit;
$d->data_limit = $data_limit;
$d->data_unit = $data_unit;
$d->validity = $validity;
2017-03-11 03:51:06 +08:00
$d->validity_unit = $validity_unit;
2022-08-23 17:33:21 +08:00
$d->shared_users = $sharedusers;
$d->routers = $routers;
2017-03-11 03:51:06 +08:00
$d->save();
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
r2(U . 'services/hotspot', 's', $_L['Updated_Successfully']);
2022-08-23 17:33:21 +08:00
} else {
r2(U . 'services/edit/' . $id, 'e', $msg);
2017-03-11 03:51:06 +08:00
}
break;
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
case 'pppoe':
2022-08-23 17:33:21 +08:00
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/pppoe.js"></script>');
$name = _post('name');
if ($name != '') {
$paginator = Paginator::bootstrap('tbl_plans', 'name_plan', '%' . $name . '%', 'type', 'Hotspot');
$d = ORM::for_table('tbl_bandwidth')->join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))->where('tbl_plans.type', 'PPPOE')->where_like('tbl_plans.name_plan', '%' . $name . '%')->offset($paginator['startpoint'])->limit($paginator['limit'])->find_many();
} else {
$paginator = Paginator::bootstrap('tbl_plans', 'type', 'Hotspot');
$d = ORM::for_table('tbl_bandwidth')->join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))->where('tbl_plans.type', 'PPPOE')->offset($paginator['startpoint'])->limit($paginator['limit'])->find_many();
}
$ui->assign('d', $d);
$ui->assign('paginator', $paginator);
2017-03-11 03:51:06 +08:00
$ui->display('pppoe.tpl');
break;
case 'pppoe-add':
2022-08-23 17:33:21 +08:00
$d = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('d', $d);
$p = ORM::for_table('tbl_pool')->find_many();
$ui->assign('p', $p);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
2017-03-11 03:51:06 +08:00
$ui->display('pppoe-add.tpl');
break;
case 'pppoe-edit':
$id = $routes['2'];
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 17:33:21 +08:00
if ($d) {
$ui->assign('d', $d);
$b = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('b', $b);
$p = ORM::for_table('tbl_pool')->find_many();
$ui->assign('p', $p);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
2017-03-11 03:51:06 +08:00
$ui->display('pppoe-edit.tpl');
2022-08-23 17:33:21 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'services/pppoe', 'e', $_L['Account_Not_Found']);
}
break;
case 'pppoe-delete':
$id = $routes['2'];
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 17:33:21 +08:00
if ($d) {
$mikrotik = Router::_info($d['routers']);
try {
$iport = explode(":", $mikrotik['ip_address']);
$client = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
} catch (Exception $e) {
die("Unable to connect to the router.<br>".$e->getMessage());
}
$printRequest = new RouterOS\Request(
'/ppp profile print .proplist=name',
RouterOS\Query::where('name', $d['name_plan'])
);
$profileName = $client->sendSync($printRequest)->getProperty('name');
$removeRequest = new RouterOS\Request('/ppp/profile/remove');
$client(
$removeRequest
->setArgument('numbers', $profileName)
2017-03-11 03:51:06 +08:00
);
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d->delete();
r2(U . 'services/pppoe', 's', $_L['Delete_Successfully']);
}
break;
case 'pppoe-add-post':
$name = _post('name_plan');
2022-08-23 17:33:21 +08:00
$id_bw = _post('id_bw');
$price = _post('price');
2017-03-11 03:51:06 +08:00
$validity = _post('validity');
2022-08-23 17:33:21 +08:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
$pool = _post('pool_name');
2017-03-11 03:51:06 +08:00
$msg = '';
2022-08-23 17:33:21 +08:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $routers == '' or $pool == '') {
$msg .= $_L['All_field_is_required'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
$d = ORM::for_table('tbl_plans')->where('name_plan', $name)->find_one();
if ($d) {
$msg .= $_L['Plan_already_exist'] . '<br>';
}
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
} else {
$unitdown = 'M';
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
} else {
$unitup = 'M';
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
$mikrotik = Router::_info($routers);
try {
$iport = explode(":", $mikrotik['ip_address']);
$client = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
} catch (Exception $e) {
die("Unable to connect to the router.<br>".$e->getMessage());
}
$addRequest = new RouterOS\Request('/ppp/profile/add');
$client->sendSync(
$addRequest
->setArgument('name', $name)
->setArgument('local-address', $pool)
->setArgument('remote-address', $pool)
->setArgument('rate-limit', $rate)
2017-03-11 03:51:06 +08:00
);
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_plans')->create();
$d->type = 'PPPOE';
2022-08-23 17:33:21 +08:00
$d->name_plan = $name;
2017-03-11 03:51:06 +08:00
$d->id_bw = $id_bw;
$d->price = $price;
2022-08-23 17:33:21 +08:00
$d->validity = $validity;
2017-03-11 03:51:06 +08:00
$d->validity_unit = $validity_unit;
2022-08-23 17:33:21 +08:00
$d->routers = $routers;
$d->pool = $pool;
2017-03-11 03:51:06 +08:00
$d->save();
r2(U . 'services/pppoe', 's', $_L['Created_Successfully']);
2022-08-23 17:33:21 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'services/pppoe-add', 'e', $msg);
}
break;
case 'edit-pppoe-post':
2022-08-23 17:33:21 +08:00
$id = _post('id');
2017-03-11 03:51:06 +08:00
$name = _post('name_plan');
2022-08-23 17:33:21 +08:00
$id_bw = _post('id_bw');
$price = _post('price');
2017-03-11 03:51:06 +08:00
$validity = _post('validity');
2022-08-23 17:33:21 +08:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
$pool = _post('pool_name');
2017-03-11 03:51:06 +08:00
$msg = '';
2022-08-23 17:33:21 +08:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $routers == '' or $pool == '') {
$msg .= $_L['All_field_is_required'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
$d = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
if ($d) {
} else {
$msg .= $_L['Data_Not_Found'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-08-23 17:33:21 +08:00
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
} else {
$unitdown = 'M';
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
} else {
$unitup = 'M';
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
$mikrotik = Router::_info($routers);
try {
$iport = explode(":", $mikrotik['ip_address']);
$client = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
} catch (Exception $e) {
die("Unable to connect to the router.<br>".$e->getMessage());
}
$printRequest = new RouterOS\Request(
'/ppp profile print .proplist=name',
RouterOS\Query::where('name', $name)
);
$profileName = $client->sendSync($printRequest)->getProperty('name');
$setRequest = new RouterOS\Request('/ppp/profile/set');
$client(
$setRequest
->setArgument('numbers', $profileName)
->setArgument('local-address', $pool)
->setArgument('remote-address', $pool)
->setArgument('rate-limit', $rate)
2017-03-11 03:51:06 +08:00
);
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price;
2022-08-23 17:33:21 +08:00
$d->validity = $validity;
2017-03-11 03:51:06 +08:00
$d->validity_unit = $validity_unit;
2022-08-23 17:33:21 +08:00
$d->routers = $routers;
$d->pool = $pool;
2017-03-11 03:51:06 +08:00
$d->save();
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
r2(U . 'services/pppoe', 's', $_L['Updated_Successfully']);
2022-08-23 17:33:21 +08:00
} else {
r2(U . 'services/pppoe-edit/' . $id, 'e', $msg);
2017-03-11 03:51:06 +08:00
}
break;
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
default:
echo 'action not defined';
2022-08-23 17:33:21 +08:00
}