Files
thetool/application/Device/DeviceController.php
Daniel Spitzer c8e6895d52 Device Bugfix
* Popanzeige in der Device Übersicht gefixt
2025-01-26 10:09:36 +01:00

446 lines
15 KiB
PHP

<?php
class DeviceController extends mfBaseController
{
protected function init()
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction()
{
$deviceManufacturers = array_map(function($deviceManufacturer) {
return [
"id" => $deviceManufacturer->id,
"name" => $deviceManufacturer->name,
"creator" => $deviceManufacturer->creator->name,
"created" => $deviceManufacturer->create,
];
}, DevicemanufactorModel::getAll());
$deviceTypes = array_map(function($deviceType) {
return [
"id" => $deviceType->id,
"name" => $deviceType->name,
"manufacturer" => $deviceType->devicemanufactor->name,
"price" => $deviceType->price,
"power" => $deviceType->power,
"creator" => $deviceType->creator->name,
"created" => $deviceType->create,
];
}, DevicetypeModel::getAll());
$JSGlobals = ["BASE_URL" => self::getUrl(""),
"DASHBOARD_URL" => self::getUrl("Dashboard"),
"MFAPPNAME" => MFAPPNAME_SLUG,
"PAGE_TITLE" => "Devices",
"PATH" => [
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
["text" => "Devices", "href" => self::getUrl("Device")]
],
"DEVICE_MANUFACTURERS" => $deviceManufacturers,
"DEVICE_TYPES" => $deviceTypes,
"DEVICES" => $this->getDevices(),
"ZABBIX_URL" => (defined("ZABBIX_URL")) ? ZABBIX_URL : "",
"GRAFANA_URL" => (defined("GRAFANA_URL")) ? GRAFANA_URL : "",
];
$this->layout()->set("vueViewName", "Device");
$this->layout()->set("JSGlobals", $JSGlobals);
$this->layout()->setTemplate("VueViews/Vue");
}
protected function detailAction()
{
$id = $this->request->id;
if (!is_numeric($id) || !$id) {
$this->layout()->setFlash("Gerät nicht gefunden", "error");
$this->redirect("Device");
}
$device = new Device($id);
if ($device->id != $id) {
$this->layout()->setFlash("Gerät nicht gefunden", "error");
$this->redirect("Device");
}
$this->layout()->setTemplate("Device/Detail");
$devicesconfig = DeviceModel::getconifg($id);
$devices = DeviceModel::getOne($id);
$devicesall= DeviceModel::getAll();
if ($devices->devicetype->olt == "1") {
$customer = DeviceModel::getOltCustomer($device->ip);
} else {
$customer = [];
}
$this->layout()->set("devicesconfig", $devicesconfig);
$this->layout()->set("devices", $devices);
$this->layout()->set("devicesall", $devicesall);
$this->layout()->set("customer", $customer);
}
protected function addAction()
{
$this->layout()->setTemplate("Device/Form");
$this->layout()->set("devicetypes", DevicetypeModel::getAll());
$this->layout()->set("pops", PopModel::getAll());
$this->layout()->set("devices", DeviceModel::getAll());
}
protected function editAction()
{
$id = $this->request->id;
if (!is_numeric($id) || !$id) {
$this->layout()->setFlash("Device nicht gefunden", "error");
$this->redirect("Device");
}
$device = new Device($id);
if ($device->id != $id) {
$this->layout()->setFlash("Device nicht gefunden", "error");
$this->redirect("Device");
}
$this->layout()->set("device", $device);
$this->addAction();
}
protected function saveAction()
{
$r = $this->request;
$id = $r->id;
//var_dump($r->get());exit;
if (is_numeric($id) && $id > 0) {
$mode = "edit";
$device = new Device($id);
if (!$device->id) {
$this->layout()->setFlash("Device nicht gefunden", "error");
$this->redirect("Device");
}
} else {
$mode = "add";
}
$data = [];
$data['name'] = trim($r->name);
$data['devicetype_id'] = $r->devicetype_id;
$data['parent_id'] = $r->parent_id;
$data['autobackup'] = trim($r->autobackup);
if (trim($r->pop_id) == "0") {
$data['pop_id'] = NULL;
} else {
$data['pop_id'] = $r->pop_id;
}
if (!(trim($r->addr_street))) {
$data['addr_street'] = NULL;
$data['addr_number'] = NULL;
$data['addr_extended'] = NULL;
$data['addr_zip'] = NULL;
$data['addr_city'] = NULL;
} else {
$data['addr_street'] = $r->addr_street;
$data['addr_number'] = $r->addr_number;
$data['addr_extended'] = $r->addr_extended;
$data['addr_zip'] = $r->addr_zip;
$data['addr_city'] = $r->addr_city;
}
if (!trim($r->gps_lat) || !trim($r->gps_long)) {
$data['gps_lat'] = NULL;
$data['gps_long'] = NULL;
} else {
$data['gps_lat'] = $r->gps_lat;
$data['gps_long'] = $r->gps_long;
}
if ($data['autobackup'] != "1") {
$data['autobackup'] = "0";
}
if (!$data['parent_id']) {
$data['parent_id'] = NULL;
}
$data['ip'] = $r->ip;
$data['mac'] = $r->mac;
$data['serial'] = $r->serial;
if ($r->snmp_version) {
$data['snmp_version'] = $r->snmp_version;
} else {
$data['snmp_version'] = NULL;
}
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]?)$/";
if (!$data['name']) {
$this->layout()->setFlash("Name darf nicht leer sein", "error");
$this->redirect("Device");
}
if (!$data['ip']) {
$this->layout()->setFlash("IP-Adresse darf nicht leer sein", "error");
$this->redirect("Device");
} else if (!preg_match($ipv4_validation_regex, trim($data['ip']))) {
$this->layout()->setFlash("IP-Adresse ist nicht gültig", "error");
$this->redirect("Device");
}
// var_dump($_FILES);
// var_dump($upload);
// exit;
if ($mode == "edit") {
$device->update($data);
} else {
$device = DeviceModel::create($data);
}
// var_dump($device);
// 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) {
$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);
}
if ($mode == "edit") {
$this->layout()->setFlash("Device erfolgreich geändert", "success");
} else if ($mode = "add") {
$this->layout()->setFlash("Device erfolgreich angelegt", "success");
}
$this->redirect($returnUrl, $returnAction, $returnVariables, $returnAnker);
}
protected function apiAction()
{
if (!$this->me->is(["Admin"])) {
$return = false;
}
$do = $this->request->do;
$format = $this->request->format;
$filename = $this->request->filename;
$id = $this->request->id;
$ip = $this->request->ip;
$portid = $this->request->portid;
$ports = $this->request->ports;
$adv = $this->request->adv;
$ont = $this->request->ont;
$data = [];
switch ($do) {
case "getDevices":
header('Content-Type: application/json');
die(json_encode($this->getDevices()));
case "getconfig":
$this->getConfig($id, $format, $filename);
break;
case "createconfig":
$this->createConfig($ip);
break;
case "getoltinfo":
$this->getoltInfo($ip, $portid, $adv);
break;
case "getontinfo":
$this->getontInfo($ip, $portid, $ont);
break;
case "getontinfomac":
$this->getontInfoMac($ip, $portid, $ont);
break;
case "changeoltsplitter":
$this->changeoltSplitter($id, $portid, $ports);
break;
default:
$return = false;
}
}
protected function deleteAction()
{
$id = $this->request->id;
$device = new Device($id);
if (!$device->id || $device->id != $id) {
$this->layout()->setFlash("Gerätetyp nicht gefunden.", "error");
$this->redirect("Device");
}
$device->delete();
$this->redirect("Device");
}
private function getConfig($id, $format, $filename)
{
$configDownload = DeviceModel::getconifgdownload($id, $format);
//
// header('Content-Type: application/octet-stream');
header('Content-Type: text/plain');
header('Content-disposition: attachment; filename="' . $filename . '"');
echo $configDownload;
exit;
}
private function createConfig($ip)
{
$r = $this->request;
$id = $r->id;
$createConfig = DeviceModel::configcreate($ip);
if ($createConfig->success === "true") {
$this->layout()->setFlash("Backup wurde erfolgreich erstellt", "success");
} elseif (!TT_MBI_API_ENABLE) {
$this->layout()->setFlash("Backup konnte nicht erstellt werden. <b>Fehler</b>: Schnittstellenserver wurde vom Admin deaktiviert.", "error");
} else {
$this->layout()->setFlash("Backup konnte nicht erstellt werden. <b>Fehler</b>: " . $createConfig->error, "error");
}
$returnUrl = "Device";
$returnAction = "Detail";
$returnVariables['id'] = $id;
$this->redirect($returnUrl, $returnAction, $returnVariables);
}
private function changeoltSplitter($id, $portid, $ports)
{
$changeOltSplitter = DeviceModel::changeoltSplitter($id, $portid, $ports);
echo json_encode($changeOltSplitter);
exit;
}
private function getoltInfo($ip, $portid, $adv)
{
$r = $this->request;
$id = $r->id;
$getOltInfo = DeviceModel::getoltInfo($ip, $portid, $adv);
echo json_encode($getOltInfo);
exit;
}
private function getontInfo($ip, $portid, $ont)
{
$r = $this->request;
$id = $r->id;
$getOntInfo = DeviceModel::getontInfo($ip, $portid, $ont);
echo json_encode($getOntInfo);
exit;
}
private function getontInfoMac($ip, $portid, $ont)
{
$r = $this->request;
$id = $r->id;
$getOntInfo = DeviceModel::getontInfoMac($ip, $portid, $ont);
echo json_encode($getOntInfo);
exit;
}
private function getDevices()
{
$devices = DeviceModel::getAll();
foreach ($devices as $device) {
$locationText = "";
$locationUrl = "";
if ($device->pop_id && trim($device->pop->name)) {
$locationText = $device->pop->name;
$locationUrl = self::getUrl("Pop", "Detail", ["id" => $device->pop->id]);
} else if (trim($device->addr_street)) {
$locationText = $device->addr_street . " " . $device->addr_number . ", " . $device->addr_zip . " " . $device->addr_city;
$locationUrl = "http://maps.google.com/?q=" . $locationText;
} else if (trim($device->gps_lat)) {
$locationText = $device->gps_lat . " , " . $device->gps_long;
$locationUrl = "http://maps.google.com/?q=" . $locationText;
}
$backup = 'na';
if ($device->last_config_backup) {
if (time() - $device->last_config_backup <= 172800) {
$backup = 'ok';
if ($device->autobackup==1) {
$backup = 'auto';
}
} else {
$backup = 'aged';
}
}
$data[] = [
"id" => $device->id,
"name" => $device->name,
"devicetype" => $device->devicetype->name,
"devicemanufactor" => $device->devicetype->devicemanufactor->name,
"locationText" => $locationText,
"locationUrl" => $locationUrl,
"ip" => $device->ip,
"mac" => $device->mac,
"serial" => $device->serial,
"zabbix_online" => $device->zabbix_online,
"zabbix_host_id" => $device->zabbix_host_id,
"price" => $device->price != "0.00" ? $device->price : $device->devicetype->price,
"power" => $device->power != "0.00" ? intval($device->power) : intval($device->devicetype->power),
"backup" => $backup,
];
}
return $data ?? [];
}
}