Skip to content

Instantly share code, notes, and snippets.

@jjcodes78
Last active July 15, 2019 02:56
Show Gist options
  • Save jjcodes78/df2060b6abc61d292a778b7b33f171f3 to your computer and use it in GitHub Desktop.
Save jjcodes78/df2060b6abc61d292a778b7b33f171f3 to your computer and use it in GitHub Desktop.
class MikrotikModel {
// atributos do model
protected $attributes = [];
// instância da API
protected $api;
// comando base
protected $baseComm = "/";
// resposta do comando
protected $response;
public function __construct($api, $data = null)
{
// instancia da classe API
$this->api = $api;
// preenche os dados do Model
$this->attributes = $data;
}
public function save(array $data)
{
$this->response = $this->api->comm($this->baseComm . "/add", $data);
return $this;
}
public function delete($id)
{
$this->response = $this->api->comm($this->baseComm . "/remove", ["numbers" => $id]);
return $this;
}
public function set(array $data)
{
$this->response = $this->api->comm($this->baseComm . "/set, $data);
return $this;
}
public function unset($value)
{
$this->response = $this->api->comm($this->baseComm . "/unset", [
"numbers" => $this->attributes['.id'],
"value-name" => $value
]);
return $this;
}
public function where($attribute, $value)
{
$this->response = $this->api->comm($this->baseComm . "/print", [
"?{$attribute}" => $value
]);
return $this;
}
public function get()
{
if(!isset($this->response[0]) {
throw new \Exception("No response data found.");
}
return new static($this->api, $this->response[0]);
}
public function getResponse()
{
return $this->response;
}
public function __get($property)
{
if(!isset($this->attributes[$property]) {
throw new \Exception("Property not found {$property} in this model.");
}
return $this->attributes[$property];
}
public function __set($property, $value)
{
$this[$property] = $value;
return $this;
}
}
class MikrotikSecret extends MikrotikModel
{
protected $baseComm = "/ppp/secret";
}
require_once('MikrotikModel.php');
require_once('MikrotikSecret.php');
require('../Connections/routeros_api.class.php');
$login_pppoe = $_GET['login_pppoe'];
$velocidade_contratada = $_GET['velocidade_contratada'];
$API = new RouterosAPI();
$API->debug = false;
if ($API->connect($ip_host_rb, $usuario, $senha)) {
$secret = new MikrotikSecret($API)->where('name', $login_pppoe)->get();
$response = $secret->set(array(
"name" => "$login_pppoe",
"password" => "giganetsul",
"service" => "pppoe",
"profile" => "$velocidade_contratada",
))->getResponse();
$API->disconnect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment