Merge branch 'devbyspi'

This commit is contained in:
Frank Schubert
2023-03-22 15:39:03 +01:00
9 changed files with 294 additions and 39 deletions

View File

@@ -36,11 +36,17 @@
<!-- end page title -->
<?php
if (!empty($devices->price)) {
if ($devices->price != "0.00") {
$price = $devices->price;
} else {
$price = $devices->devicetype->price;
}
if ($devices->power != "0.0") {
$power = $devices->power;
} else {
$power = $devices->devicetype->power;
}
?>
<div class="row">
<div class="col-12">
@@ -83,6 +89,10 @@ if (!empty($devices->price)) {
<th>Preis</th>
<td><?= $price ?> €</td>
</tr>
<tr>
<th>Leistung</th>
<td><?= $power ?> Watt</td>
</tr>
<tr>
<th>Bemerkung</th>
<td><?= nl2br($devices->comment) ?> </td>
@@ -152,11 +162,21 @@ if (!empty($devices->price)) {
</div>
<div class="col-3 card-border">
<div>
<h4>Config Backups</h4>
<div class="overflow-auto">
<h4 class="float-left">Config Backups</h4>
<?php if ($devices->devicetype->devicemanufactor->config_backup > count()): ?>
<span><i title="Switch config" class="fa-light fa-rectangle-code code-ico"
data-toggle="modal" data-target="#configCode"></i></span>
<?php endif; ?>
<div class="float-right">
<a id="create-backup-href"
href="https://<?= $_SERVER['SERVER_NAME']; ?>/Device/api?do=createconfig&ip=<?= $devices->ip; ?>&id=<?= $devices->id; ?>">
<button class="btn btn-primary "><span id="create-backup-button-text">Backup erstellen</span>
<span id="create-backup-load"></i></span></button>
</a></div>
</div>
<?php
if (!empty($configs)) {
if ($devicesconfig->success == "true" && $devicesconfig->data > count()) {
?>
<div>
<table class="table table-sm">
@@ -164,21 +184,35 @@ if (!empty($devices->price)) {
<tr>
<th>Datum/Uhrzeit</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
<?php foreach ($devicesconfig->data as $config) :
$configfileCleartext = $config->config_cleartext;
$configfileCompressed = $config->config_compressed;
$configid = $config->id;
?>
<tr>
<td><?= date("d.m.Y/H:i", $config->config_timestamp); ?></td>
<td>
<a href="https://<?= $_SERVER['SERVER_NAME']; ?>/Device/api?do=getconfig&id=<?= $configid; ?>&format=txt&filename=<?= $configfileCleartext; ?>">
TXT</a> / <a
href="https://<?= $_SERVER['SERVER_NAME']; ?>/Device/api?do=getconfig&id=<?= $configid; ?>&format=xml&filename=<?= $configfileCompressed; ?>">
XML</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
} else {
?>
<h5 class="text-center">Keine Configs vorhanden</h5>
<div class="mt-1">
<h5 class="text-center">Keine Configs vorhanden</h5>
</div>
<?php
}
?>
@@ -191,10 +225,48 @@ if (!empty($devices->price)) {
</div>
</div>
</div>
<?php if ($devices->devicetype->devicemanufactor->config_backup > count()):
$year = date("Y", time());
$month = date("n", time());
$day = date("d", time());
$config = $devices->devicetype->devicemanufactor->config_backup;
$config = str_replace("&&YEAR&&", $year, $config);
$config = str_replace("&&MONTH&&", $month, $config);
$config = str_replace("&&DAY&&", $day, $config);
?>
<div class="modal fade" id="configCode" tabindex="-1" role="dialog" aria-labelledby="configCodeLabel"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="configCodeLabel">SwitchConfig Autobackup</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<?= nl2br($config) ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Schließen</button>
</div>
</div>
</div>
</div>
<?php endif; ?>
<script type="text/javascript">
$(document).ready(function () {
$('#create-backup-href').click(function () {
$('#create-backup-load').html('<i class="fas fa-spinner fa-spin spinner-ico"></i>');
$('#create-backup-button-text').text('Backup wird erstellt.')
});
});
</script>

View File

@@ -34,7 +34,6 @@ asort($deviceTypes);
<div class="card">
<div class="card-body">
<h4 class="header-title mb-2"><?= ($device->id) ? "Device bearbeiten" : "Neues Gerät" ?></h4>
<form class="form-horizontal" method="post"
action="<?= self::getUrl("Device", "save", ["returnto" => $_GET["returnto"]]) ?>">
<div class="card">
@@ -93,6 +92,22 @@ asort($deviceTypes);
value="<?= $device->serial ?>">
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="serial">Preis</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="price" id="price"
placeholder="<?= $device->devicetype->price ?>"
value="<?= $price ?>">
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="serial">Leistung</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="power" id="power"
placeholder="<?= $device->devicetype->power ?>"
value="<?= $power ?>">
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="comment">Bemerkung</label>
<div class="col-lg-10">
@@ -109,6 +124,10 @@ asort($deviceTypes);
<label class="col-lg-2"></label>
<div class="col-lg-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a href="<?= $cancelUrl ?>">
<button type="button" class="btn btn-secondary">Abbrechen</button>
</a>
</div>
</div>

View File

@@ -74,11 +74,16 @@ $pagination_entity_name = "Device";
<?php
foreach ($devices as $device):
if (!empty($device->price)) {
if ($device->price != "0.00") {
$price = $device->price;
} else {
$price = $device->devicetype->price;
}
if ($device->power != "0.0") {
$power = $device->power;
} else {
$power = $device->devicetype->power;
}
?>
<tr>
@@ -94,7 +99,7 @@ $pagination_entity_name = "Device";
<td class="text-center"><?= $device->mac ?></td>
<td class="text-center"><?= $device->serial ?></td>
<td class="text-right"><?= $price ?> €</td>
<td class="text-right"><?= $device->devicetype->power ?> Watt</td>
<td class="text-right"><?= $power ?> Watt</td>
<td style="text-align: left; letter-spacing: 4px; font-size: 1.1em; width: 80px">

View File

@@ -42,6 +42,13 @@
value="<?= $devicemanufactors->name ?>">
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="comment">Backup Config</label>
<div class="col-lg-10">
<textarea class="form-control" id="config_backup" name="config_backup"
rows="10"><?= $devicemanufactors->config_backup ?></textarea>
</div>
</div>
</div>

View File

@@ -0,0 +1,25 @@
<?php
class DeviceApicontroller extends mfBaseApicontroller
{
protected function init()
{
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$this->addRoute("/device/getDevices", "getDevices", "GET");
}
protected function getDevices()
{
$devices = DeviceModel::getAll();
foreach ($devices as $key => $device) {
$deviceReturn[$key]['id'] = $device->id;
$deviceReturn[$key]['name'] = $device->name;
$deviceReturn[$key]['ip'] = $device->ip;
$deviceReturn[$key]['serial'] = $device->serial;
}
return mfResponse::Ok($deviceReturn);
}
}

View File

@@ -42,7 +42,9 @@ class DeviceController extends mfBaseController
}
$this->layout()->setTemplate("Device/Detail");
$devicesconfig = DeviceModel::getconifg($id);
$devices = DeviceModel::getOne($id);
$this->layout()->set("devicesconfig", $devicesconfig);
$this->layout()->set("devices", $devices);
}
@@ -60,13 +62,13 @@ class DeviceController extends mfBaseController
$id = $this->request->id;
if (!is_numeric($id) || !$id) {
$this->layout()->setFlash("Gerät nicht gefunden", "error");
$this->layout()->setFlash("Device nicht gefunden", "error");
$this->redirect("Device");
}
$device = new Device($id);
if ($device->id != $id) {
$this->layout()->setFlash("Gerät nicht gefunden", "error");
$this->layout()->setFlash("Device nicht gefunden", "error");
$this->redirect("Device");
}
@@ -101,6 +103,16 @@ class DeviceController extends mfBaseController
$data['ip'] = $r->ip;
$data['mac'] = $r->mac;
$data['serial'] = $r->serial;
if (empty(trim($r->price))) {
$data['price'] = "0.00";
} else {
$data['price'] = $r->price;
}
if (empty(trim($r->power))) {
$data['power'] = "0.0";
} else {
$data['power'] = $r->power;
}
$data['comment'] = $r->comment;
$ipv4_validation_regex = "/^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/";
@@ -133,9 +145,41 @@ class DeviceController extends mfBaseController
// exit;
$id = $device->save();
$returnUrl = "Device";
$returnAction = "Index";
$returnVariables = array();
$returnAnker = "";
if ($this->request->returnto) {
if (strpos($this->request->returnto, "-") !== false) {
$urls = explode('-', $this->request->returnto);
$urlCounter = 0;
$returnUrlGen = "";
foreach ($urls as $url) {
if ($urlCounter > 0) {
$returnUrlGen .= "/";
}
$returnUrlGen .= ucfirst($url);
$urlCounter++;
}
$returnAction = "";
$returnVariables['id'] = $id;
$returnUrl = $returnUrlGen;
} else {
$returnUrl = ucfirst($this->request->returnto);
}
}
if (!$id) {
$this->layout()->setFlash("Device konnte nicht angelegt werden", "error");
$this->redirect("Device");
$returnVariables['id'] = $r->id;
if ($mode == "edit") {
$this->layout()->setFlash("Device konnte nicht gespeichert werden", "error");
} else if ($mode = "add") {
$this->layout()->setFlash("Device konnte nicht angelegt werden", "error");
}
$this->redirect($returnUrl, $returnAction, $returnVariables, $returnAnker);
}
$returnUrl="Device";
$returnAction = "Index";
@@ -171,4 +215,5 @@ class DeviceController extends mfBaseController
}
$this->redirect($returnUrl, $returnAction, $returnVariables, $returnAnker);
}
}

View File

@@ -5,8 +5,8 @@ class DeviceModel
public $name = null;
public $ip = null;
public $mac = null;
public $serial= null;
public $comment= null;
public $serial = null;
public $comment = null;
public $devicetype_id = null;
public $pop_id = null;
@@ -16,16 +16,18 @@ class DeviceModel
public $create = null;
public $edit = null;
public static function find($data) {
public static function find($data)
{
}
public static function create(Array $data) {
public static function create(array $data)
{
$model = new Device();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
if(substr($field, 0, 5) == "vlan_" && !$value) {
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
if (substr($field, 0, 5) == "vlan_" && !$value) {
$model->$field = null;
continue;
}
@@ -34,45 +36,47 @@ class DeviceModel
}
$me = mfValuecache::singleton()->get("me");
if(!$me) {
if (!$me) {
$me = new User();
$me->loadMe();
mfValuecache::singleton()->set("me", $me);
}
if($model->create_by === null) {
if ($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
if ($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getOne($id) {
if(!is_numeric($id) || !$id) {
public static function getOne($id)
{
if (!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("Device", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Device($data);
}
return $item;
}
public static function getAll() {
public static function getAll()
{
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Device", "*");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Device($data);
}
}
@@ -80,15 +84,16 @@ class DeviceModel
}
public static function getFirst() {
public static function getFirst()
{
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Device", "*", "$where ORDER BY name, network_id");
if($db->num_rows($res)) {
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Device($data);
if($item->id) {
if ($item->id) {
return $item;
} else {
return null;
@@ -97,7 +102,8 @@ class DeviceModel
return null;
}
public static function search($filter) {
public static function search($filter)
{
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
@@ -110,7 +116,8 @@ class DeviceModel
return $items;
}
private static function getSqlFilter($filter) {
private static function getSqlFilter($filter)
{
$where = "1=1 ";
//var_dump($filter);exit;
@@ -124,4 +131,78 @@ class DeviceModel
//var_dump($filter, $where);exit;
return $where;
}
public static function getconifg($id)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://172.16.5.56/api/deviceconfigs/' . $id,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 4|6l5ixx3CYBP7xClqEfVAC3zrBbQlxusAtu4zNwQp'),
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response);
}
public static function getconifgdownload($id, $format)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://172.16.5.56/api/deviceconfigsdownload/' . $id . '/' . $format,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 5|3QyhSkLgzrHwdVt05wQFUp2sFciiFhhGzucJirnI'),
));
$response = curl_exec($curl);
curl_close($curl);
// echo $response;
// var_dump($response);
return ($response);
}
public static function configcreate($ip)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://172.16.5.56/api/deviceconfigscreate/' . $ip ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 5|3QyhSkLgzrHwdVt05wQFUp2sFciiFhhGzucJirnI'),
));
$response = curl_exec($curl);
return json_decode($response);
}
}

View File

@@ -68,6 +68,7 @@ class DevicemanufactorController extends mfBaseController
$data = [];
$data['name'] = trim($r->name);
$data['config_backup'] = trim($r->config_backup);
if (!$data['name']) {

View File

@@ -39,4 +39,4 @@
.font-weight-500 {
font-weight: 500;
}
}