<?php
function gravarequipamento()
{
if($this->data['Equipamento']['form'] == 'gravarequipamento')
{
$this->data = Sanitize::clean($this->data, array('encode' => false));
$this->Equipamento->set($this->data);
if ($this->Equipamento->validates())
{
// DEFINIÇÕES DO SISTEMA
define('USER_PADRAO', 'manut'); // System User
define('GRUPO_PADRAO', 'write'); // Group System User
define('ADMIN_SISTEMA', 'zuca'); // System Admin test
define('SENHA_SISTEMA', '123456'); // Password test
$ip = $this->data['Equipamento']['ip']; // IP of MIKROTIK
$new_password = // Encryption omitted;
$this->data['Equipamento']['senha'] = $new_password;
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ACTION IN MIKROTIK
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = false;
if ($API->connect($ip, ADMIN_SISTEMA, SENHA_SISTEMA))
{
$this->Equipamento->save($this->data); // Save in data together with mikrotik action. If one fail, another fail to.
$API->write('/user/add', false);
$API->write('=group='.GRUPO_PADRAO, false);
$API->write('=name='.USER_PADRAO, false);
$API->write('=password='.$new_password);
$ARRAY = $API->read();
$API->disconnect();
$mensagem = 'SAVE SUCESS!
<br/> USUÁRIO PADRÃO: '.USER_PADRAO.'
<br/>SENHA: '.$new_password;
$this->Session->setFlash($mensagem);
$this->redirect(array('action' => 'gravarequipamento'));
}
else
{
$mensagem = 'FAIL CONECTION WITH MIKROTIK ('.$ip.')';
$this->Session->setFlash($mensagem);
$this->redirect(array('action' => 'gravarequipamento'));
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ACTION IN MIKROTIK
}
else
{
// Tratando as mensagens de erro
$mensagem = $this->Equipamento->invalidFields();
if(isset($mensagem['ip'])){$ip = ">> ".$mensagem['ip']." <<";} else {$ip = '';}
if(isset($mensagem['nome'])){$nome = ">> ".$mensagem['nome']." <<";} else {$nome = '';}
$this->Session->setFlash("Please, read the instructions here:<br/>".$ip."<br/>".$nome);
//if(!empty($mensagem)){$this->Session->setFlash($mensagem);}
$this->redirect(array('action' => 'gravarequipamento'));
}
}
}
?>