forked from kevinowino869/mitrobill
simplified Mikrotik Connection
This commit is contained in:
@ -1,56 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
|
||||
**/
|
||||
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
|
||||
**/
|
||||
_admin();
|
||||
$ui->assign('_title', $_L['Network'].' - '. $config['CompanyName']);
|
||||
$ui->assign('_title', $_L['Network'] . ' - ' . $config['CompanyName']);
|
||||
$ui->assign('_system_menu', 'network');
|
||||
|
||||
$action = $routes['1'];
|
||||
$admin = Admin::_info();
|
||||
$ui->assign('_admin', $admin);
|
||||
|
||||
if($admin['user_type'] != 'Admin'){
|
||||
r2(U."dashboard",'e',$_L['Do_Not_Access']);
|
||||
if ($admin['user_type'] != 'Admin') {
|
||||
r2(U . "dashboard", 'e', $_L['Do_Not_Access']);
|
||||
}
|
||||
|
||||
use PEAR2\Net\RouterOS;
|
||||
|
||||
require_once 'system/autoload/PEAR2/Autoload.php';
|
||||
|
||||
switch ($action) {
|
||||
case 'list':
|
||||
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/pool.js"></script>');
|
||||
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/pool.js"></script>');
|
||||
|
||||
$name = _post('name');
|
||||
if ($name != ''){
|
||||
$paginator = Paginator::bootstrap('tbl_pool','pool_name','%'.$name.'%');
|
||||
$d = ORM::for_table('tbl_pool')->where_like('pool_name','%'.$name.'%')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
|
||||
}else{
|
||||
$paginator = Paginator::bootstrap('tbl_pool');
|
||||
$d = ORM::for_table('tbl_pool')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
|
||||
}
|
||||
$name = _post('name');
|
||||
if ($name != '') {
|
||||
$paginator = Paginator::bootstrap('tbl_pool', 'pool_name', '%' . $name . '%');
|
||||
$d = ORM::for_table('tbl_pool')->where_like('pool_name', '%' . $name . '%')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
|
||||
} else {
|
||||
$paginator = Paginator::bootstrap('tbl_pool');
|
||||
$d = ORM::for_table('tbl_pool')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
|
||||
}
|
||||
|
||||
$ui->assign('d',$d);
|
||||
$ui->assign('paginator',$paginator);
|
||||
$ui->assign('d', $d);
|
||||
$ui->assign('paginator', $paginator);
|
||||
run_hook('view_pool'); #HOOK
|
||||
$ui->display('pool.tpl');
|
||||
break;
|
||||
|
||||
case 'add':
|
||||
$r = ORM::for_table('tbl_routers')->find_many();
|
||||
$ui->assign('r',$r);
|
||||
$r = ORM::for_table('tbl_routers')->find_many();
|
||||
$ui->assign('r', $r);
|
||||
run_hook('view_add_pool'); #HOOK
|
||||
$ui->display('pool-add.tpl');
|
||||
$ui->display('pool-add.tpl');
|
||||
break;
|
||||
|
||||
case 'edit':
|
||||
$id = $routes['2'];
|
||||
$d = ORM::for_table('tbl_pool')->find_one($id);
|
||||
if($d){
|
||||
$ui->assign('d',$d);
|
||||
if ($d) {
|
||||
$ui->assign('d', $d);
|
||||
run_hook('view_edit_pool'); #HOOK
|
||||
$ui->display('pool-edit.tpl');
|
||||
}else{
|
||||
} else {
|
||||
r2(U . 'pool/list', 'e', $_L['Account_Not_Found']);
|
||||
}
|
||||
break;
|
||||
@ -59,27 +61,12 @@ switch ($action) {
|
||||
$id = $routes['2'];
|
||||
run_hook('delete_pool'); #HOOK
|
||||
$d = ORM::for_table('tbl_pool')->find_one($id);
|
||||
$mikrotik = Router::_info($d['routers']);
|
||||
if($d){
|
||||
if(!$_c['radius_mode']){
|
||||
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 pool print .proplist=name',
|
||||
RouterOS\Query::where('name', $d['pool_name'])
|
||||
);
|
||||
$poolName = $client->sendSync($printRequest)->getProperty('name');
|
||||
|
||||
$removeRequest = new RouterOS\Request('/ip/pool/remove');
|
||||
$client($removeRequest
|
||||
->setArgument('numbers', $poolName)
|
||||
);
|
||||
$mikrotik = Mikrotik::info($d['routers']);
|
||||
if ($d) {
|
||||
if (!$_c['radius_mode']) {
|
||||
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
|
||||
Mikrotik::removePool($client, $d['pool_name']);
|
||||
}
|
||||
|
||||
$d->delete();
|
||||
|
||||
r2(U . 'pool/list', 's', $_L['Delete_Successfully']);
|
||||
@ -89,44 +76,35 @@ switch ($action) {
|
||||
case 'add-post':
|
||||
$name = _post('name');
|
||||
$ip_address = _post('ip_address');
|
||||
$routers = _post('routers');
|
||||
$routers = _post('routers');
|
||||
run_hook('add_pool'); #HOOK
|
||||
$msg = '';
|
||||
if(Validator::Length($name,30,2) == false){
|
||||
$msg .= 'Name should be between 3 to 30 characters'. '<br>';
|
||||
if (Validator::Length($name, 30, 2) == false) {
|
||||
$msg .= 'Name should be between 3 to 30 characters' . '<br>';
|
||||
}
|
||||
if ($ip_address == '' or $routers == '') {
|
||||
$msg .= $_L['All_field_is_required'] . '<br>';
|
||||
}
|
||||
if ($ip_address == '' OR $routers == ''){
|
||||
$msg .= $_L['All_field_is_required']. '<br>';
|
||||
}
|
||||
|
||||
$d = ORM::for_table('tbl_pool')->where('pool_name',$name)->find_one();
|
||||
if($d){
|
||||
$msg .= $_L['Pool_already_exist']. '<br>';
|
||||
$d = ORM::for_table('tbl_pool')->where('pool_name', $name)->find_one();
|
||||
if ($d) {
|
||||
$msg .= $_L['Pool_already_exist'] . '<br>';
|
||||
}
|
||||
$mikrotik = Router::_info($routers);
|
||||
if($msg == ''){
|
||||
if(!$_c['radius_mode']){
|
||||
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/pool/add');
|
||||
$client->sendSync($addRequest
|
||||
->setArgument('name', $name)
|
||||
->setArgument('ranges', $ip_address)
|
||||
);
|
||||
$mikrotik = Mikrotik::info($routers);
|
||||
if ($msg == '') {
|
||||
if (!$_c['radius_mode']) {
|
||||
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
|
||||
Mikrotik::removePool($client, $name, $ip_address);
|
||||
}
|
||||
|
||||
$b = ORM::for_table('tbl_pool')->create();
|
||||
$b->pool_name = $name;
|
||||
$b->range_ip = $ip_address;
|
||||
$b->routers = $routers;
|
||||
$b->routers = $routers;
|
||||
$b->save();
|
||||
|
||||
r2(U . 'pool/list', 's', $_L['Created_Successfully']);
|
||||
}else{
|
||||
} else {
|
||||
r2(U . 'pool/add', 'e', $msg);
|
||||
}
|
||||
break;
|
||||
@ -138,54 +116,38 @@ switch ($action) {
|
||||
$routers = _post('routers');
|
||||
run_hook('edit_pool'); #HOOK
|
||||
$msg = '';
|
||||
if(Validator::Length($name,30,2) == false){
|
||||
$msg .= 'Name should be between 3 to 30 characters'. '<br>';
|
||||
if (Validator::Length($name, 30, 2) == false) {
|
||||
$msg .= 'Name should be between 3 to 30 characters' . '<br>';
|
||||
}
|
||||
if ($ip_address == '' or $routers == '') {
|
||||
$msg .= $_L['All_field_is_required'] . '<br>';
|
||||
}
|
||||
if ($ip_address == '' OR $routers == ''){
|
||||
$msg .= $_L['All_field_is_required']. '<br>';
|
||||
}
|
||||
|
||||
$id = _post('id');
|
||||
$d = ORM::for_table('tbl_pool')->find_one($id);
|
||||
if($d){
|
||||
|
||||
}else{
|
||||
$msg .= $_L['Data_Not_Found']. '<br>';
|
||||
if ($d) {
|
||||
} else {
|
||||
$msg .= $_L['Data_Not_Found'] . '<br>';
|
||||
}
|
||||
|
||||
$mikrotik = Router::_info($routers);
|
||||
if($msg == ''){
|
||||
if(!$_c['radius_mode']){
|
||||
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 pool print .proplist=name',
|
||||
RouterOS\Query::where('name', $name)
|
||||
);
|
||||
$poolName = $client->sendSync($printRequest)->getProperty('name');
|
||||
|
||||
$setRequest = new RouterOS\Request('/ip/pool/set');
|
||||
$client($setRequest
|
||||
->setArgument('numbers', $poolName)
|
||||
->setArgument('ranges', $ip_address)
|
||||
);
|
||||
$mikrotik = Mikrotik::info($routers);
|
||||
if ($msg == '') {
|
||||
if (!$_c['radius_mode']) {
|
||||
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
|
||||
Mikrotik::setPool($client, $name,$poolName, $ip_address);
|
||||
}
|
||||
|
||||
$d->pool_name = $name;
|
||||
$d->range_ip = $ip_address;
|
||||
$d->routers = $routers;
|
||||
$d->routers = $routers;
|
||||
$d->save();
|
||||
|
||||
r2(U . 'pool/list', 's', $_L['Updated_Successfully']);
|
||||
}else{
|
||||
r2(U . 'pool/edit/'.$id, 'e', $msg);
|
||||
} else {
|
||||
r2(U . 'pool/edit/' . $id, 'e', $msg);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
echo 'action not defined';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user