Pada kesempatan kali ini kita akan mencoba menghubungkan/membaca mikrotik dengan menggunakan (PHP) .Pastikan anda terhubung dengan router mikrotik anda, dan setidaknya anda sudah mensetting seperti : IP Address, DHCP Server, DNS, Firewall Nat, Web Proxy, dll
Buatlah folder di Htdocs dengan nama TestMikrotik dan buat sub folder di dalam nya dengan nama mikrotik, dan buat koneksi, address, dhcpserver, dns, firewallnat, webproxy dengan format (PHP)
- connect.php ;
<?php
require '/vendor/autoload.php';
use MikrotikAPI\Talker\Talker;
use \MikrotikAPI\Entity\Auth;
use MikrotikAPI\Commands\IP\Firewall\FirewallFilter;
use MikrotikAPI\Commands\IP\Address;
use MikrotikAPI\Commands\IP\Firewall\FirewallNAT;
use MikrotikAPI\Commands\IP\DNS;
use MikrotikAPI\Commands\IP\DHCPServer;
use MikrotikAPI\Commands\IP\WebProxy;
$auth = new Auth();
$auth->setHost("192.168.88.1");
$auth->setUsername("admin");
$auth->setPassword("");
$auth->setDebug(true);
if(!$auth){
echo"Gagal koneksi ke mikrotik cek ip / user-pass";
}
?>
- address.php
<?php
require'connect.php';
use MikrotikAPI\Talker\Talker;
use \MikrotikAPI\Entity\Auth;
use MikrotikAPI\Commands\IP\Firewall\FirewallFilter;
use MikrotikAPI\Commands\IP\Address;
use MikrotikAPI\Commands\IP\Firewall\FirewallNAT;
use MikrotikAPI\Commands\IP\DNS;
use MikrotikAPI\Commands\IP\DHCPServer;
use MikrotikAPI\Commands\IP\WebProxy;
//use MikrotikAPI\Commands\IP\WebProxy\Access;
$talker = new Talker($auth);
//$filter = new FirewallFilter($talker);
//$a = $filter->getAll();
$ipaddr = new WebProxy($talker);
$listIP = $ipaddr->getAll();
MikrotikAPI\Util\DebugDumper::dump($listIP);
?>
- dhcpserver.php
<?php
require'connect.php';
use MikrotikAPI\Talker\Talker;
use \MikrotikAPI\Entity\Auth;
use MikrotikAPI\Commands\IP\Firewall\FirewallFilter;
use MikrotikAPI\Commands\IP\Address;
use MikrotikAPI\Commands\IP\Firewall\FirewallNAT;
use MikrotikAPI\Commands\IP\DNS;
use MikrotikAPI\Commands\IP\DHCPServer;
use MikrotikAPI\Commands\IP\WebProxy;
$talker = new Talker($auth);
//$filter = new FirewallFilter($talker);
//$a = $filter->getAll();
$ipaddr = new DHCPServer($talker);
$listIP = $ipaddr->getAll();
MikrotikAPI\Util\DebugDumper::dump($listIP);
?>
- dns.php
<?php
require'connect.php';
use MikrotikAPI\Talker\Talker;
use \MikrotikAPI\Entity\Auth;
use MikrotikAPI\Commands\IP\Firewall\FirewallFilter;
use MikrotikAPI\Commands\IP\Address;
use MikrotikAPI\Commands\IP\Firewall\FirewallNAT;
use MikrotikAPI\Commands\IP\DNS;
use MikrotikAPI\Commands\IP\DHCPServer;
use MikrotikAPI\Commands\IP\WebProxy;
$talker = new Talker($auth);
//$filter = new FirewallFilter($talker);
//$a = $filter->getAll();
$ipaddr = new DNS($talker);
$listIP = $ipaddr->getAll();
MikrotikAPI\Util\DebugDumper::dump($listIP);
?>
- firewallnat.php
<?php
require'connect.php';
use MikrotikAPI\Talker\Talker;
use \MikrotikAPI\Entity\Auth;
use MikrotikAPI\Commands\IP\Firewall\FirewallFilter;
use MikrotikAPI\Commands\IP\Address;
use MikrotikAPI\Commands\IP\Firewall\FirewallNAT;
use MikrotikAPI\Commands\IP\DNS;
use MikrotikAPI\Commands\IP\DHCPServer;
use MikrotikAPI\Commands\IP\WebProxy;
$talker = new Talker($auth);
//$filter = new FirewallFilter($talker);
//$a = $filter->getAll();
$ipaddr = new FirewallNAT($talker);
$listIP = $ipaddr->getAll();
MikrotikAPI\Util\DebugDumper::dump($listIP);
?>
- webproxy.php
<?php
require'connect.php';
use MikrotikAPI\Talker\Talker;
use \MikrotikAPI\Entity\Auth;
use MikrotikAPI\Commands\IP\Firewall\FirewallFilter;
use MikrotikAPI\Commands\IP\Address;
use MikrotikAPI\Commands\IP\Firewall\FirewallNAT;
use MikrotikAPI\Commands\IP\DNS;
use MikrotikAPI\Commands\IP\DHCPServer;
use MikrotikAPI\Commands\IP\WebProxy;
$talker = new Talker($auth);
//$filter = new FirewallFilter($talker);
//$a = $filter->getAll();
$ipaddr = new WebProxy($talker);
$listIP = $ipaddr->getAll();
MikrotikAPI\Util\DebugDumper::dump($listIP);
?>
Buatlah sub-sub folder di dalam TestMikrotik tadi dengan nama-nama src/MikrotikAPI/Commands/IP di dalam sub folder IP buatlah script PHP
- Address.php
<?php
namespace MikrotikAPI\Commands\IP;
use MikrotikAPI\Talker\Talker,
MikrotikAPI\Util\SentenceUtil;
/**
* Description of Address
*
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
*/
class Address {
/**
*
* @var type array
*/
private $talker;
function __construct(Talker $talker) {
$this->talker = $talker;
}
/**
* This method is used to add the ip address
* @param type $address string
* @param type $interface string
* @param type $comment string
* @return type array
*/
public function add($param) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/address/add");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display all ip address
* @return type array
*
*/
public function getAll() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/address/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Address To Set, Please Your Add Ip Address";
}
}
/**
* This method is used to activate the ip address by id
* @param type $id is not an array
* @return type array
*
*
*/
public function enable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/address/enable");
$sentence->where(".id", "=", $id);
$enable = $this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to disable ip address by id
* @param type $id string
* @return type array
*
*
*/
public function disable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/address/disable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to remove the ip address by id
* @param type $id is not an array
* @return type array
*
*/
public function delete($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/address/remove");
$sentence->where(".id", "=", $id);
$enable = $this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to set or edit by id
* @param type $param array
* @return type array
*
*/
public function set($param, $id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/address/set");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display one ip address
* in detail based on the id
* @param type $id not string
* @return type array
*
*/
public function detail_address($id) {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/address/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Address With This id = " . $id;
}
}
}
- DHCPServer.php
<?php
namespace MikrotikAPI\Commands\IP;
use MikrotikAPI\Talker\Talker,
MikrotikAPI\Util\SentenceUtil;
/**
* Description of Mapi_Ip_Dhcp_Server
*
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
*/
class DHCPServer {
/**
*
* @var type array
*/
private $talker;
function __construct(Talker $talker) {
$this->talker = $talker;
}
/**
* This methode is used to add ip dhcp server network
* @param type $param array
* @return type array
*/
public function addNetwork($param) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/network/add");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to add ip dhcp server
* @param type $param array
* @return type \array
*/
public function add($param) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/add");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to set ip dhcp server config
* @param type $store_leases_disk string
* @return type array
*/
public function setConfig($store_leases_disk) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/config/set");
$sentence->setAttribute("store-leases-disk", $store_leases_disk);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to set or edit ip dhcp server network
* by id
* @param type $param array
* @param type $id string
* @return type array
*/
public function setNetwork($param, $id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/network/set");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to display all ip dhcp server network
* @return type array
*/
public function getAllNetwork() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dhcp-server/network/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Dhcp-Server Network To Set, Please Your Add Ip Dhcp-Server Network";
}
}
/**
* This methode is used to delete ip dhcp server network by id
* @param type $id string
* @return type array
*/
public function deleteNetwork($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/network/remove");
$sentence->where(".id", "=", $id);
$enable = $this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display one ip dhcp server network
* in detail based on the id
* @param type $id string
* @return type array
*/
public function detail($id) {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dhcp-server/network/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Dhcp-Server Network With This id = " . $id;
}
}
/**
* This methode is used to disable ip dhcp server by id
* @param type $id string
* @return type array
*/
public function disable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/disable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to enable ip dhcp server by id
* @param type $id string
* @return type array
*/
public function enable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/enable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to remove ip dhcp server by id
* @param type $id string
* @return type array
*/
public function delete($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/remove");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to det or edit ip dhcp server by id
* @param type $param array
* @param type $id string
* @return type array
*/
public function set($param, $id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dhcp-server/set");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This methode is used to display all ip dhcp server
* @return type array
*/
public function getAll() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dhcp-server/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Dhcp-Server To Set, Please Your Add Ip Dhcp-Server";
}
}
/**
* This method is used to display one ip dhcp server
* in detail based on the id
* @param type $id string
* @return type array
*/
public function detail2($id) {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dhcp-server/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Dhcp-Server With This id = " . $id;
}
}
public function getAllConfig() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dhcp-server/config/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Dhcp-Server Config To Set, Please Your Add Ip Dhcp-Server Config";
}
}
}
- DNS.php
<?php
namespace MikrotikAPI\Commands\IP;
use MikrotikAPI\Talker\Talker,
MikrotikAPI\Util\SentenceUtil;
/**
* Description of Mapi_Ip_Dns
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
*/
class DNS{
/**
*
* @var type array
*/
private $talker;
function __construct(Talker $talker){
$this->talker = $talker;
}
/**
* This method is used to configure dns
* @param type $servers string example : '192.168.1.1,192.168.2.1'
* @return type array
*
*/
public function set($servers){
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dns/set");
$sentence->setAttribute("servers", $servers);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display
* all dns
* @return type array
*
*/
public function getAll(){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()){
return $rs->getResultArray();
}else{
return "No Ip DNS To Set, Please Your Add Ip DNS";
}
}
/**
* This method is used to add the static dns
* @param type $param array
* @return type array
*
*/
public function addStatic($param){
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dns/static/add");
foreach ($param as $name => $value){
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display
* all static dns
* @return type array
*
*/
public function getAllStatic(){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/static/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()){
return $rs->getResultArray();
}else{
return "No Ip Static DNS To Set, Please Your Add Ip Static DNS";
}
}
/**
* This method is used to display one static dns
* in detail based on the id
* @param type $id string
* @return type array
*
*/
public function detailStatic($id){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/static/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0 ;
if ($i < $rs->size()){
return $rs->getResultArray();
} else {
return "No Ip Static DNS With This Id = ".$id;
}
}
/**
* This method is used to change based on the id
* @param type $param array
* @param type $id string
* @return type array
*
*/
public function setStatic($param, $id){
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/dns/static/set");
foreach ($param as $name => $value){
$sentence->setAttribute($name, $value);
}
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display
* all dns cache
* @return array || string
*
*/
public function getAllCache(){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/cache/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()){
return $rs->getResultArray();
}else{
return "No Ip DNS Cache To Set, Please Your Add Ip DNS Cache";
}
}
/**
* This method is used to display one dns cache
* in detail based on the id
* @param type $id string
* @return type array
*
*/
public function detailCache($id){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/cache/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0 ;
if ($i < $rs->size()){
return $rs->getResultArray();
} else {
return "No Ip DNS Cache With This Id = ".$id;
}
}
/**
* This method is used to display
* all dns cache all cache
* @return type array
*
*/
public function getAllCacheAll(){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/cache/all/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()){
return $rs->getResultArray();
}else{
return "No Ip DNS Cache All To Set, Please Your Add Ip DNS Cache All";
}
}
/**
* This method is used to display one dns cache all
* in detail based on the id
* @param type $id string
* @return type array
*
*/
public function detailCacheAll($id){
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/dns/cache/all/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0 ;
if ($i < $rs->size()){
return $rs->getResultArray();
} else {
return "No Ip DNS Cache All With This Id = ".$id;
}
}
}
- WebProxy.php
<?php
namespace MikrotikAPI\Commands\IP;
use MikrotikAPI\Talker\Talker,
MikrotikAPI\Util\SentenceUtil;
/* Description of WebProxy
*
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
*/
class WebProxy {
private $talker;
function __construct(Talker $talker) {
$this->talker = $talker;
}
/**
* This method used for get all web proxy config
* @return type array
*/
public function getAll() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/proxy/access/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Proxy To Set, Please Your Add Ip Proxy";
}
}
/**
*
* This method used for set Web Proxy configuration
* @param array $param
* @return array
*/
public function set($param) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/proxy/set");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
}
Buatlah sub folder lagi di dalam IP dengan nama Firewall dan buatlah script PHP firewallnat
- FirewallNAT.php
<?php
namespace MikrotikAPI\Commands\IP\Firewall;
use MikrotikAPI\Talker\Talker,
MikrotikAPI\Util\SentenceUtil;
/**
* Description of NAT
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
*
*/
class FirewallNAT {
/**
*
* @var Talker $talker
*/
private $talker;
function __construct(Talker $talker) {
$this->talker = $talker;
}
/**
* This method is used to add the firewall nat
* @param type $param array
* @return type array
*
*/
public function add($param) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/nat/add");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to disable firewall nat by id
* @param type $id string
* @return type array
*
*/
public function disable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/nat/disable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to enable firewall nat by id
* @param type $id string
* @return type array
*
*/
public function enable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/nat/enable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to change firewall nat based on the id
* @param type $param array
* @param type $id string
* @return type array
*
*/
public function set($param, $id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/nat/set");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to remove firewall nat
* @param type $id string
* @return type array
*
*/
public function delete($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/nat/remove");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display all firewall nat
* @return type array
*
*/
public function getAll() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/firewall/nat/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Firewall NAT To Set, Please Your Add Ip Firewall NAT";
}
}
/**
* This method is used to display one firewall nat
* in detail based on the id
* @param type $id string
* @return type array
*
*/
public function detail($id) {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/firewall/nat/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Firewall NAT With This id = " . $id;
}
}
}
- FirewallFilter
<?php
namespace MikrotikAPI\Commands\IP\Firewall;
use MikrotikAPI\Talker\Talker,
MikrotikAPI\Util\SentenceUtil;
/**
* Description of Filter
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
*/
class FirewallFilter {
/**
*
* @var Talker $talker
*/
private $talker;
function __construct(Talker $talker) {
$this->talker = $talker;
}
/**
*
* @param type $param array
* @return type array
* This method is used to add the firewall filter
*/
public function add($param) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/filter/add");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display all firewall filter
* @return type array
*
*/
public function getAll() {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/firewall/filter/getall");
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Firewall Filter To Set, Please Your Add Ip Firewall Filter";
}
}
/**
* This method is used to enable firewall filter by id
* @param type $id string
* @return type array
*
*/
public function enable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/filter/enable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to disable firewall filter by id
* @param type $id string
* @return type array
*
*/
public function disable($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/filter/disable");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to remove firewall filter by id
* @param type $id string
* @return type array
*
*/
public function delete($id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/filter/remove");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to change firewall nat based on the id
* @param type $param array
* @param type $id string
* @return type array
*
*/
public function set($param, $id) {
$sentence = new SentenceUtil();
$sentence->addCommand("/ip/firewall/filter/set");
foreach ($param as $name => $value) {
$sentence->setAttribute($name, $value);
}
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
return "Sucsess";
}
/**
* This method is used to display one firewall filter
* in detail based on the id
* @param type $id string
* @return type array
*
*/
public function detail($id) {
$sentence = new SentenceUtil();
$sentence->fromCommand("/ip/firewall/filter/print");
$sentence->where(".id", "=", $id);
$this->talker->send($sentence);
$rs = $this->talker->getResult();
$i = 0;
if ($i < $rs->size()) {
return $rs->getResultArray();
} else {
return "No Ip Firewall Filter With This id = " . $id;
}
}
}
Dan Buat lagi subfolder di dalam MikrotikAPI dengan nama Entity dan Talker
- Didalam subfolder Entitiy buat source php lagi
- Auth.php
<?php
namespace MikrotikAPI\Entity;
/**
* Description of Auth
*
* @author nunenuh
*/
class Auth {
/**
*
* @var string
*/
private $host;
/**
*
* @var int
*/
private $port = 8728;
/**
*
* @var string
*/
private $username;
/**
*
* @var string
*/
private $password;
/**
*
* @var boolean
*/
private $debug = FALSE;
/**
*
* @var int
*/
private $attempts = 5;
/**
*
* @var int
*/
private $delay = 2;
/**
*
* @var int
*/
private $timeout = 2;
function __construct() {
}
public function set($host, $port, $username, $password, $debug, $attempts, $delay, $timeout) {
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->debug = $debug;
$this->attempts = $attempts;
$this->delay = $delay;
$this->timeout = $timeout;
}
public function getHost() {
return $this->host;
}
public function getPort() {
return $this->port;
}
public function getUsername() {
return $this->username;
}
public function getPassword() {
return $this->password;
}
public function getDebug() {
return $this->debug;
}
public function getAttempts() {
return $this->attempts;
}
public function getDelay() {
return $this->delay;
}
public function getTimeout() {
return $this->timeout;
}
public function setHost($host) {
$this->host = $host;
}
public function setPort($port) {
$this->port = $port;
}
public function setUsername($username) {
$this->username = $username;
}
public function setPassword($password) {
$this->password = $password;
}
public function setDebug($debug) {
$this->debug = $debug;
}
public function setAttempts($attempts) {
$this->attempts = $attempts;
}
public function setDelay($delay) {
$this->delay = $delay;
}
public function setTimeout($timeout) {
$this->timeout = $timeout;
}
}
- Didalam subfolder Talker buat source php lagi
- Talker.php
<?php
namespace MikrotikAPI\Talker;
use MikrotikAPI\Entity\Auth;
use MikrotikAPI\Core\Connector;
/**
* Description of Talker
*
* @author Lalu Erfandi Maula Yusnu nunenuh@gmail.com <http://vthink.web.id>
* @copyright Copyright (c) 2011, Virtual Think Team.
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @category Libraries
* @property TalkerReciever
* @property TalkerSender
*/
class Talker {
private $sender;
private $reciever;
private $auth;
private $connector;
// private $param;
public function __construct(Auth $auth) {
// parent::__construct($auth->getHost(), $auth->getPort(), $auth->getUsername(), $auth->getPassword());
// parent::connect();
$this->auth = $auth;
$this->connector = new Connector($auth->getHost(), $auth->getPort(), $auth->getUsername(), $auth->getPassword());
$this->connector->connect();
$this->sender = new TalkerSender($this->connector);
$this->reciever = new TalkerReciever($this->connector);
}
/**
*
* @return type
*/
public function isLogin() {
// return parent::isLogin();
}
/**
*
* @return type
*/
public function isConnected() {
// return parent::isConnected();
}
/**
*
* @return type
*/
public function isDebug() {
return $this->auth->getDebug();
}
/**
*
* @param type $boolean
*/
public function setDebug($boolean) {
$this->auth->setDebug($boolean);
$this->sender->setDebug($boolean);
$this->reciever->setDebug($boolean);
}
/**
*
* @return type
*/
public function isTrap() {
return $this->reciever->isTrap();
}
/**
*
* @return type
*/
public function isDone() {
return $this->reciever->isDone();
}
/**
*
* @return type
*/
public function isData() {
return $this->reciever->isData();
}
/**
*
* @param type $sentence
*/
public function send($sentence) {
$this->sender->send($sentence);
$this->reciever->doRecieving();
}
/**
*
* @return type
*/
public function getResult() {
return $this->reciever->getResult();
}
}
- Buat subfolder di dalam TestMikrotik dengan nama vendor dan buatlah source phpnya
- autoload.php
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit6e58d18ab647c55b3f15c6772ec323c7::getLoader();
?>
KALAU BINGUNG SILAHKAN DOWNLOAD SOURCE LENGKAPNYA LANGSUNG DIBAWAH INI :
Tidak ada komentar:
Posting Komentar