<?php

include_once(dirname(__FILE__).'/NetworkModels.php');
include_once(dirname(__FILE__).'/HostList.php');
include_once(dirname(__FILE__).'/Subnet.php');
include_once(dirname(__FILE__).'/Hosting.php');
include_once(dirname(__FILE__).'/UserHosts.php');

class PageFrequencyPlan extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Frekvenční plán';
    $this->Description = 'Výpis obsazení frekvenčních kanálů';
    $this->ParentClass = 'PageNetwork';
  }

  function Show(): string
  {
    // http://en.wikipedia.org/wiki/List_of_WLAN_channels
    //$ChannelList = array(2412 => 1, 2417 => 2, 2422 => 3, 2427 => 4, 2432 => 5, 2437 => 6, 2442 => 7, 2447 => 8, 2452 => 9, 2457 => 10, 2462 => 11, 2467 => 12, 2472 => 13, 5200 => 40, 5205 => 41, 5210 => 42, 5215 => 43, 5220 => 44, 5225 => 45, 5230 => 46, 5235 => 47, 5240 => 48, 5245 => 49, 5250 => 50, 5255 => 51, 5260 => 52, 5265 => 53, 5270 => 54, 5275 => 55, 5280 => 56, 5285 => 57, 5290 => 58, 5295 => 59, 5300 => 60, 5500 => 100, 5520 => 104, 5540 => 108, 5560 => 112, 5580 => 116, 5600 => 120, 5620 => 124, 5640 => 128, 5660 => 132, 5700 => 140, 5720 => 144);
    $Output = '<div align="center">'.
    '<a href="?section=obsazeni_wifi_kanalu&range=a">Pásmo 2,4 GHz (a)</a> '.
    '<a href="?section=obsazeni_wifi_kanalu&amp;range=bc">Pásmo 5 GHz dolní (b, c)</a> '.
    '<a href="?section=obsazeni_wifi_kanalu&amp;range=d">Pásmo 5 GHz horní (d)</a> '.
    '<a href="https://www.ctu.cz/sites/default/files/obsah/vo-r12-032021-3.pdf">VO-R/12/03.2021-3</a><br/>'.
    '<strong>Seznam známých AP a obsazení kmitočtových pásem:</strong></div>'.
    '<table class="WideTable">'.
    '<tr><th/><br/>SSID<br/><br/></th>';
    $ChannelList = array();
    if (!array_key_exists('range', $_GET)) $_GET['range'] = 'a';
    if ($_GET['range'] == 'a')
    {
      $Where = '(Frequency < 5000)';
      for ($Freq = 2402; $Freq <= 2482; $Freq = $Freq + 5) $ChannelList[] = $Freq;
    }
    if ($_GET['range'] == 'bc')
    {
      $Where = '(Frequency >= 5000) AND (Frequency <= 5350)';
      for ($Freq = 5150; $Freq <= 5350; $Freq = $Freq + 5) $ChannelList[] = $Freq;
    }
    if ($_GET['range'] == 'd')
    {
      $Where = '(Frequency >= 5470)';
      for ($Freq = 5470; $Freq <= 5725; $Freq = $Freq + 5) $ChannelList[] = $Freq;
    }

    foreach ($ChannelList as $Frequency)
    {
      $Output .= '<th><div class="RotatedHeader">'.$Frequency.'<div></th>';
    }
    $Output .= '</tr>';
    $DbResult = $this->Database->query('SELECT `Frequency` FROM `NetworkInterfaceWireless` WHERE '.$Where.' AND (`Mode`=0) GROUP BY `Frequency`');
    while ($DbRow = $DbResult->fetch_assoc())
    {
      $DbResult2 = $this->Database->query('SELECT * FROM `NetworkInterfaceWireless` WHERE (`Frequency`='.$DbRow['Frequency'].') AND '.$Where);
      while ($DbRow2 = $DbResult2->fetch_assoc())
      {
        $LowFrequency = $DbRow['Frequency'] - $DbRow2['ChannelWidth'] / 2 - $DbRow2['ChannelWidthLower'];
        $HighFrequency = $DbRow['Frequency'] + $DbRow2['ChannelWidth'] / 2 + $DbRow2['ChannelWidthUpper'];
        $Output .= '<tr><td>'.$DbRow2['SSID'].'</td>';
        foreach ($ChannelList as $Frequency)
        {
          if (($DbRow2['Frequency'] == $Frequency)) $Color = '#408040';
            else if (($LowFrequency <= ($Frequency - 2.5)) and ($HighFrequency >= ($Frequency + 2.5))) $Color = '#50d050';
            else if (($LowFrequency == $Frequency) or ($HighFrequency == $Frequency)) $Color = '#a0d0a0';
            else $Color = '#ffffff';
          $Output .= '<td style="background-color: '.$Color.';">&nbsp;</td>';
        }

        $Output .= '</tr>';
      }
    }
    $Output .= '</table>';
    return $Output;
  }
}

class PageNetwork extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Síť';
    $this->Description = 'Technické informace o síti';
    $this->ParentClass = 'PagePortal';
  }

  function Show(): string
  {
    if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Network', 'ShowInfo'))
      return 'Nemáte oprávnění';

    $Output = '<a href="'.$this->System->Link('/network/frequency-plan/').'">Frekvenční plán</a><br />';
    $Output .= '<a href="'.$this->System->Link('/network/subnet/').'">Výpis registrovaných podsítí</a><br />';
    $Output .= '<a href="'.$this->System->Link('/network/hosts/').'">Registrované zařízení</a><br />';
    return $Output;
  }
}

class ModuleNetwork extends Module
{
  public int $MinNotifyTime;

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'Network';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Networking related tools';
    $this->Dependencies = array(ModuleNotify::GetName(), ModuleIS::GetName());
    $this->Models = array(NetworkDomainAlias::GetClassName(), NetworkDevice::GetClassName(), NetworkDeviceType::GetClassName(),
      NetworkDeviceLog::GetClassName(), NetworkInterface::GetClassName(), NetworkInterfaceType::GetClassName(),
      NetworkSubnet::GetClassName(), NetworkPort::GetClassName(), NetworkSpeedLimit::GetClassName(),
      NetworkInterfaceLatency::GetClassName(), NetworkLink::GetClassName(), NetworkLinkType::GetClassName(),
      NetworkSignal::GetClassName(), NetworkAddressCategory::GetClassName(), NetworkDeviceConfig::GetClassName(),
      NetworkDomain::GetClassName(), NetworkDomainServer::GetClassName(), NetworkDomainView::GetClassName(),
      NetworkDomainItemFilter::GetClassName(), DeviceAPIType::GetClassName(), NetworkInterfaceWireless::GetClassName(),
      NetworkInterfaceUpDown::GetClassName(), NetworkPortUpDown::GetClassName(), NetworkMac::GetClassName());

    // TODO: Make notify time configurable
    $this->MinNotifyTime = 30; // seconds
  }

  function DoStart(): void
  {
    ModuleNotify::Cast($this->System->GetModule('Notify'))->RegisterCheck('NetworkReachability',
      array($this, 'ReachabilityCheck'));
    ModuleNotify::Cast($this->System->GetModule('Notify'))->RegisterCheck('NetworkPort',
      array($this, 'PortCheck'));

    $this->System->RegisterPage(['network'], 'PageNetwork');
    $this->System->RegisterPage(['network', 'administration'], 'PageNetworkAdministration');
    $this->System->RegisterPage(['network', 'subnet'], 'PageSubnet');
    $this->System->RegisterPage(['network', 'user-hosts'], 'PageNetworkHostList');
    $this->System->RegisterPage(['network', 'hosting'],'PageHosting');
    $this->System->RegisterPage(['network', 'hosts'], 'PageHostList');
    $this->System->RegisterPage(['network', 'frequency-plan'], 'PageFrequencyPlan');

    $this->System->RegisterCommandLine('networklog_import', 'Imports network logs from remote server', array($this, 'ImportNetworkLog'));

    $this->System->FormManager->RegisterClass('NetworkDomainAlias', array(
      'Title' => 'Alias domény',
      'Table' => 'NetworkDomainAlias',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Target' => array('Type' => 'String', 'Caption' => 'Cíl', 'Default' => ''),
        'Comment' => array('Type' => 'String', 'Caption' => 'Komentář', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Síťová doména', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainAliasListDomain', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainAlias',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDevice', array(
      'Title' => 'Síťové zařízení',
      'Table' => 'NetworkDevice',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Type' => array('Type' => 'TNetworkDeviceType', 'Caption' => 'Typ', 'Default' => '0'),
        'Member' => array('Type' => 'TMember', 'Caption' => 'Zákazník', 'Default' => '0'),
        'Location' => array('Type' => 'TMember', 'Caption' => 'Umístění', 'Default' => '0'),
        'Service' => array('Type' => 'TServiceCustomerRel', 'Caption' => 'Služba', 'Default' => '', 'Null' => true),
        'Used' => array('Type' => 'Boolean', 'Caption' => 'Použito', 'Default' => '1'),
        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
        'PermanentOnline' => array('Type' => 'Boolean', 'Caption' => 'Běží stále', 'Default' => '0'),
        'Interfaces' => array('Type' => 'TInterfaceList', 'Caption' => 'Rozhraní', 'Default' => ''),
        'MapPosition' => array('Type' => 'TMapPosition', 'Caption' => 'Pozice na mapě', 'Default' => '0', 'Null' => true),
        'Product' => array('Type' => 'TProduct', 'Caption' => 'Produkt', 'Default' => '', 'Null' => true),
        'LoginName' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => '', 'Null' => true, 'NotInList' => true),
        'LoginPassword' => array('Type' => 'String', 'Caption' => 'Přihlašovací heslo', 'Default' => '', 'Null' => true, 'NotInList' => true),
        'API' => array('Type' => 'TDeviceAPIType', 'Caption' => 'API', 'Default' => '', 'Null' => true),
        'Logs' => array('Type' => 'TDeviceLogList', 'Caption' => 'Záznamy', 'Default' => ''),
        'InboundNATPriority' => array('Type' => 'Integer', 'Caption' => 'Priorita příchozího NATu', 'Default' => '1'),
      ),
      'AfterInsert' => array($this, 'AfterInsertNetworkDevice'),
      'AfterModify' => array($this, 'AfterModifyNetworkDevice'),
      'AfterDelete' => array($this, 'AfterModifyNetworkDevice'),
    ));
    $this->System->FormManager->RegisterClass('NetworkDeviceType', array(
      'Title' => 'Typ síťového zařízení',
      'Table' => 'NetworkDeviceType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'ShowOnline' => array('Type' => 'Boolean', 'Caption' => 'Ukázat online', 'Default' => '0'),
        'IconName' => array('Type' => 'String', 'Caption' => 'Jméno ikony', 'Default' => '0'),
        'Devices' => array('Type' => 'TNetworkDeviceListType', 'Caption' => 'Síťová zařízení', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDeviceListType', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Ref' => 'Type',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDeviceLog', array(
      'Title' => 'Zprávy zařízení',
      'Table' => 'NetworkDeviceLog',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Zařízení', 'Default' => ''),
        'Message' => array('Type' => 'String', 'Caption' => 'Zpáva', 'Default' => ''),
        'Tags' => array('Type' => 'String', 'Caption' => 'Značky', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterface', array(
      'Title' => 'Síťové rozhraní',
      'Table' => 'NetworkInterface',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Type' => array('Type' => 'TNetworkInterfaceType', 'Caption' => 'Typ', 'Default' => '0'),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'Fyzická adresa (MAC)', 'Default' => ''),
        'LocalIP' => array('Type' => 'IPv4Address', 'Caption' => 'IPv4', 'Default' => '', 'Null' => true),
        'IPv6' => array('Type' => 'IPv6Address', 'Caption' => 'IPv6', 'Default' => '', 'Null' => true),
        'ExternalIP' => array('Type' => 'IPv4Address', 'Caption' => 'Veřejná IPv4', 'Default' => '', 'Null' => true),
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Zařízení', 'Default' => ''),
        'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '1'),
        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
        'Links' => array('Type' => 'TNetworkLinkListInterface', 'Caption' => 'Propojení', 'Default' => ''),
        'UpDown' => array('Type' => 'TNetworkInterfaceUpDown', 'Caption' => 'Změny stavu', 'Default' => ''),
        'Signal' => array('Type' => 'TNetworkSignalListInterface', 'Caption' => 'Signál', 'Default' => ''),
        'Wireless' => array('Type' => 'TNetworkInterfaceWirelessListInterface', 'Caption' => 'Bezdrátové spoje', 'Default' => ''),
        'Ports' => array('Type' => 'TDevicePortListInterface', 'Caption' => 'Síťové porty', 'Default' => ''),
        'Latency' => array('Type' => 'TDeviceInterfaceLatencyListInterface', 'Caption' => 'Síťová odezva', 'Default' => ''),
      ),
      'AfterInsert' => array($this, 'AfterInsertNetworkInterface'),
      'AfterModify' => array($this, 'AfterModifyNetworkInterface'),
      'BeforeDelete' => array($this, 'BeforeDeleteNetworkInterface'),
      'AfterDelete' => array($this, 'AfterModifyNetworkInterface'),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceType', array(
      'Title' => 'Typ síťového rozhraní',
      'Table' => 'NetworkInterfaceType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'MaxSpeed' => array('Type' => 'Integer', 'Caption' => 'Max. rychlost', 'Default' => '0', 'Suffix' => 'Mbit/s'),
        'FullDuplex' => array('Type' => 'Boolean', 'Caption' => 'Plně duplexní', 'Default' => '0'),
        'Color' => array('Type' => 'Color', 'Caption' => 'Barva', 'Default' => '0'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkSubnet', array(
      'Title' => 'Podsítě',
      'DefaultSortColumn' => 'Name',
      'Table' => 'NetworkSubnet',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'AddressRange' => array('Type' => 'String', 'Caption' => 'Rozsah adres', 'Default' => ''),
        'Mask' => array('Type' => 'Integer', 'Caption' => 'Prefix', 'Default' => ''),
        'DHCP' => array('Type' => 'IPv4Address', 'Caption' => 'DHCP', 'Default' => ''),
        'Gateway' => array('Type' => 'IPv4Address', 'Caption' => 'Brána', 'Default' => ''),
        'WINS' => array('Type' => 'IPv4Address', 'Caption' => 'WINS', 'Default' => ''),
        'DNS' => array('Type' => 'String', 'Caption' => 'DNS', 'Default' => ''),
        'Domain' => array('Type' => 'String', 'Caption' => 'Doména', 'Default' => ''),
        'NTP' => array('Type' => 'String', 'Caption' => 'NTP', 'Default' => ''),
        'Member' => array('Type' => 'TMember', 'Caption' => 'Zákazník', 'Default' => '', 'Null' => true),
        'Service' => array('Type' => 'TServiceCustomerRel', 'Caption' => 'Služba', 'Default' => '', 'Null' => true),
        'ExtAddressRange' => array('Type' => 'String', 'Caption' => 'Vnější rozsah adres', 'Default' => ''),
        'ExtMask' => array('Type' => 'String', 'Caption' => 'Vnější prefix', 'Default' => ''),
        'AddressRangeIPv6' => array('Type' => 'String', 'Caption' => 'Rozsah adres IPv6', 'Default' => ''),
        'MaskIPv6' => array('Type' => 'Integer', 'Caption' => 'Prefix IPv6', 'Default' => ''),
        'Configure' => array('Type' => 'Boolean', 'Caption' => 'Nastavovat', 'Default' => ''),
        'Interfaces' => array('Type' => 'TNetworkSubnetInterfaceList', 'Caption' => 'Rozhraní', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkSubnetInterfaceList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterface',
      'Id' => 'Id',
      'Ref' => null,
      'Filter' => '(`TX`.`LocalIP` != "") AND CompareNetworkPrefix(INET_ATON(`TX`.`LocalIP`), INET_ATON((SELECT `NetworkSubnet`.`AddressRange` FROM `NetworkSubnet` WHERE `NetworkSubnet`.`Id`=#Id)), '.
        '(SELECT `NetworkSubnet`.`Mask` FROM `NetworkSubnet` WHERE `NetworkSubnet`.`Id`=#Id))',
    ));
    $this->System->FormManager->RegisterClass('NetworkPort', array(
      'Title' => 'Síťový port',
      'Table' => 'NetworkPort',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Protocol' => array('Type' => 'TNetworkProtocol', 'Caption' => 'Protokol', 'Default' => '0'),
        'Number' => array('Type' => 'Integer', 'Caption' => 'Číslo', 'Default' => ''),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => ''),
        'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '1'),
        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
        'UpDown' => array('Type' => 'TNetworkPortUpDown', 'Caption' => 'Změny stavu', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkSpeedLimit', array(
      'Title' => 'Omezení přenosové rychlosti',
      'Table' => 'NetworkSpeedLimit',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'SpeedMaxOut' => array('Type' => 'Integer', 'Caption' => 'Max. rychlost odesílání', 'Default' => '0', 'Suffix' => 'bits/s'),
        'SpeedMaxIn' => array('Type' => 'Integer', 'Caption' => 'Max. rychlost přijímání', 'Default' => 0, 'Suffix' => 'bits/s'),
        'Parent' => array('Type' => 'TNetworkSpeedLimit', 'Caption' => 'Rodič', 'Default' => '', 'Null' => true),
        'Items' => array('Type' => 'TNetworkSpeedLimitListParent', 'Caption' => 'Položky'),
        'CustomerServices' => array('Type' => 'TServiceCustomerRelListSpeedLimit', 'Caption' => 'Služby zákazníka'),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkSpeedLimit', array(
      'Type' => 'Reference',
      'Table' => 'NetworkSpeedLimit',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkSpeedLimitListParent', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkSpeedLimit',
      'Id' => 'Id',
      'Ref' => 'Parent',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TServiceCustomerRelListSpeedLimit', array(
      'Type' => 'ManyToOne',
      'Table' => 'ServiceCustomerRel',
      'Id' => 'Id',
      'Ref' => 'SpeedLimit',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceLatency', array(
      'Title' => 'Síťová odezva',
      'Table' => 'NetworkInterfaceLatency',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => ''),
        'Value' => array('Type' => 'Float', 'Caption' => 'Hodnota', 'Default' => '0', 'Suffix' => 'ms'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkLink', array(
      'Title' => 'Síťové propojení',
      'Table' => 'NetworkLink',
      'Items' => array(
        'Type' => array('Type' => 'TNetworkLinkType', 'Caption' => 'Typ', 'Default' => '1'),
        'Interface1' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 1', 'Default' => ''),
        'Interface2' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 2', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkLinkUnion', array(
      'Title' => 'Síťové propojení',
      'BaseTable' => 'NetworkLink',
      'SQL' => '(SELECT `Id`, `Type`, `Interface1` AS `Interface`, `Interface2` AS `InterfaceOther` FROM `NetworkLink`) '.
        'UNION (SELECT `Id`, `Type`, `Interface2` AS `Interface`, `Interface1` AS `InterfaceOther` FROM `NetworkLink`)',
      'Items' => array(
        'Type' => array('Type' => 'TNetworkLinkType', 'Caption' => 'Typ', 'Default' => '1', 'ReadOnly' => true),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 1', 'Default' => '', 'ReadOnly' => true),
        'InterfaceOther' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 2', 'Default' => '', 'ReadOnly' => true),
      ),
    ));

    $this->System->FormManager->RegisterClass('NetworkLinkType', array(
      'Title' => 'Typ síťového propojení',
      'Table' => 'NetworkLinkType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
        'MaxLinkSpeed' => array('Type' => 'Integer', 'Caption' => 'Maximální spojová rychlost', 'Default' => '0', 'Suffix' => 'bits/s'),
        'MaxRealSpeed' => array('Type' => 'Integer', 'Caption' => 'Maximální reálná rychlost', 'Default' => '0', 'Suffix' => 'bits/s'),
        'FullDuplex' => array('Type' => 'Boolean', 'Caption' => 'Plně duplexní', 'Default' => '0'),
        'Color' => array('Type' => 'Color', 'Caption' => 'Barva', 'Default' => '0'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkSignal', array(
      'Title' => 'Signál rozhraní',
      'Table' => 'NetworkSignal',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
        'MAC' => array('Type' => 'TNetworkMac', 'Caption' => 'Fyzická adresa (MAC)', 'Default' => ''),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => '', 'Null' => true),
        'Value' => array('Type' => 'Integer', 'Caption' => 'Signál', 'Default' => '0', 'Suffix' => 'dBm'),
        'Remote' => array('Type' => 'Integer', 'Caption' => 'Vzdálený signál', 'Default' => '0', 'Suffix' => 'dBm'),
        'RateRx' => array('Type' => 'Integer', 'Caption' => 'Rychlost Rx', 'Default' => '0', 'Suffix' => 'Mbps'),
        'RateTx' => array('Type' => 'Integer', 'Caption' => 'Rychlost Tx', 'Default' => '0', 'Suffix' => 'Mbps'),
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Měřeno z', 'Default' => '0'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkAddressCategory', array(
      'Title' => 'Kategorie síťové adresy',
      'Table' => 'NetworkAddressCategory',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkAddressCategory', array(
      'Type' => 'Reference',
      'Table' => 'NetworkAddressCategory',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDeviceConfig', array(
      'Title' => 'Nastavení zařízení',
      'Table' => 'NetworkDeviceConfig',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Zařízení', 'Default' => ''),
        'Time' => array('Type' => 'Date', 'Caption' => 'Čas vytvoření', 'Default' => ''),
        'ConfigFull' => array('Type' => 'Text', 'Caption' => 'Kompletní nastavení', 'Default' => ''),
        'ConfigCompact' => array('Type' => 'Text', 'Caption' => 'Rozdílové nastavení', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkDomain', array(
      'Title' => 'Síťová doména',
      'Table' => 'NetworkDomain',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Parent' => array('Type' => 'TNetworkDomain', 'Caption' => 'Nadřazená doména', 'Default' => '', 'Null' => true),
        'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
        'Serial' => array('Type' => 'Integer', 'Caption' => 'Sériové číslo', 'Default' => '1'),
        'Minimum' => array('Type' => 'Integer', 'Caption' => 'Minimum', 'Default' => '10800'),
        'Retry' => array('Type' => 'Integer', 'Caption' => 'Opakování', 'Default' => '7200'),
        'Expire' => array('Type' => 'Integer', 'Caption' => 'Čas vypršení', 'Default' => '2419200'),
        'Refresh' => array('Type' => 'Integer', 'Caption' => 'Obnovení', 'Default' => '28800'),
        'TTL' => array('Type' => 'Integer', 'Caption' => 'TTL', 'Default' => '86400', 'Suffix' => 'sekund'),
        'Servers' => array('Type' => 'TNetworkDomainServerList', 'Caption' => 'Servery', 'Default' => ''),
        'Views' => array('Type' => 'TNetworkDomainViewListDomain', 'Caption' => 'Pohledy', 'Default' => ''),
        'ItemFilters' => array('Type' => 'TNetworkDomainItemFilterListDomain', 'Caption' => 'Filtry položek', 'Default' => ''),
        'Aliases' => array('Type' => 'TNetworkDomainAliasListDomain', 'Caption' => 'Aliasy', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomain', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDomain',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDomainServer', array(
      'Title' => 'Doménový server',
      'Table' => 'NetworkDomainServer',
      'DefaultSortColumn' => 'Address',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Address' => array('Type' => 'String', 'Caption' => 'Adresa', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Doména', 'Default' => ''),
        'Sequence' => array('Type' => 'Integer', 'Caption' => 'Pořadí', 'Default' => '1'),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainServerList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainServer',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDomainView', array(
      'Title' => 'Pohled síťové domény',
      'Table' => 'NetworkDomainView',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'SysName' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Doména', 'Default' => ''),
        'AddressRange' => array('Type' => 'String', 'Caption' => 'Rozsah adres', 'Default' => ''),
        'ItemFilters' => array('Type' => 'TNetworkDomainItemFilterListView', 'Caption' => 'Filtry položek', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainView', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDomainView',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainViewListDomain', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainView',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDomainItemFilter', array(
      'Title' => 'Filtr doménových položek',
      'Table' => 'NetworkDomainItemFilter',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Domén', 'Default' => ''),
        'AddressCategory' => array('Type' => 'TNetworkAddressCategory', 'Caption' => 'Kategorie adresy', 'Default' => ''),
        'Suffix' => array('Type' => 'String', 'Caption' => 'Přípona jména položek', 'Default' => ''),
        'View' => array('Type' => 'TNetworkDomainView', 'Caption' => 'Pohled', 'Default' => ''),
        'AddressRange' => array('Type' => 'String', 'Caption' => 'Rozsah adres', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainItemFilterListDomain', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainItemFilter',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainItemFilterListView', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainItemFilter',
      'Id' => 'Id',
      'Ref' => 'View',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('DeviceAPIType', array(
      'Title' => 'Typ API zařízení',
      'Table' => 'DeviceAPIType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Devices' => array('Type' => 'TDeviceListAPI', 'Caption' => 'Zařízení', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceWireless', array(
      'Title' => 'Bezdrátová rozhraní',
      'Table' => 'NetworkInterfaceWireless',
      'Items' => array(
        'SSID' => array('Type' => 'String', 'Caption' => 'SSID', 'Default' => ''),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'MAC', 'Default' => ''),
        'NetworkInterface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => ''),
        'TxPower' => array('Type' => 'Integer', 'Caption' => 'Výstupní výkon', 'Default' => '18', 'Suffix' => 'dBm'),
        'CableAttenuation' => array('Type' => 'Integer', 'Caption' => 'Útlum vedení', 'Default' => '0', 'Suffix' => 'dB'),
        'Antenna' => array('Type' => 'TProduct', 'Caption' => 'Anténa', 'Default' => '', 'Null' => true),
        'AntenaGain' => array('Type' => 'Integer', 'Caption' => 'Zisk antény', 'Default' => '', 'Suffix' => 'dBi'),
        'AntennaPolarity' => array('Type' => 'TAntennaPolarity', 'Caption' => 'Polarizace antény', 'Default' => '0'),
        'Frequency' => array('Type' => 'Float', 'Caption' => 'Frekvence', 'Default' => '5600', 'Suffix' => 'MHz'),
        'ChannelWidthLower' => array('Type' => 'Integer', 'Caption' => 'Šířka kanálu dolního', 'Default' => '0', 'Suffix' => 'MHz'),
        'ChannelWidth' => array('Type' => 'Integer', 'Caption' => 'Šířka kanálu', 'Default' => '20', 'Suffix' => 'MHz'),
        'ChannelWidthUpper' => array('Type' => 'Integer', 'Caption' => 'Šířka kanálu horního', 'Default' => '0', 'Suffix' => 'MHz'),
        'Mode' => array('Type'
        => 'TWirelessMode', 'Caption' => 'Režim', 'Default' => '0', 'Suffix' => ''),
        'TotalPower' => array('Type' => 'Integer', 'Caption' => 'Celkový výkon', 'Default' => '20', 'Suffix' => 'dBm',
          'SQL' => '(`TxPower` - `CableAttenuation` + `AntenaGain`)', 'ReadOnly' => true),
        'LimitPower' => array('Type' => 'Integer', 'Caption' => 'Max. limit', 'Default' => '', 'Suffix' => 'dBm',
          'ReadOnly' => true, 'SQL' => '(CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END)'),
        'UnderLimit' => array('Type' => 'Boolean', 'Caption' => 'V limitu', 'Default' => '', 'Suffix' => '',
          'ReadOnly' => true, 'SQL' => '((`TxPower` - `CableAttenuation` + `AntenaGain`) <= (CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END))'),
        'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
      ),
      'Actions' => array(
        array('Caption' => 'Frekvenční plán', 'URL' => '/network/frequency-plan/'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceWirelessCTU', array(
      'Title' => 'Bezdrátová rozhraní pro ČTÚ',
      'Table' => 'NetworkInterfaceWireless',
      'Items' => array(
        'SSID' => array('Type' => 'String', 'Caption' => 'SSID', 'Default' => ''),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'MAC', 'Default' => ''),
        'TxPower' => array('Type' => 'Integer', 'Caption' => 'Výstupní výkon', 'Default' => '18', 'Suffix' => 'dBm'),
        'CableAttenuation' => array('Type' => 'Integer', 'Caption' => 'Útlum vedení', 'Default' => '0', 'Suffix' => 'dB'),
        'AntenaGain' => array('Type' => 'Integer', 'Caption' => 'Zisk antény', 'Default' => '', 'Suffix' => 'dBi'),
        'AntennaPolarity' => array('Type' => 'TAntennaPolarity', 'Caption' => 'Polarizace antény', 'Default' => '0'),
        'Frequency' => array('Type' => 'Float', 'Caption' => 'Frekvence', 'Default' => '5600', 'Suffix' => 'MHz'),
        'ChannelWidthLower' => array('Type' => 'Integer', 'Caption' => 'Šířka kanálu dolního', 'Default' => '0', 'Suffix' => 'MHz'),
        'ChannelWidth' => array('Type' => 'Integer', 'Caption' => 'Šířka kanálu', 'Default' => '20', 'Suffix' => 'MHz'),
        'ChannelWidthUpper' => array('Type' => 'Integer', 'Caption' => 'Šířka kanálu horního', 'Default' => '0', 'Suffix' => 'MHz'),
        'Mode' => array('Type' => 'TWirelessMode', 'Caption' => 'Režim', 'Default' => '0', 'Suffix' => ''),
        'TotalPower' => array('Type' => 'Integer', 'Caption' => 'Celkový výkon', 'Default' => '20', 'Suffix' => 'dBm',
          'SQL' => '(`TxPower` - `CableAttenuation` + `AntenaGain`)', 'ReadOnly' => true),
        'LimitPower' => array('Type' => 'Integer', 'Caption' => 'Max. limit', 'Default' => '', 'Suffix' => 'dBm',
          'ReadOnly' => true, 'SQL' => '(CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END)'),
        'UnderLimit' => array('Type' => 'Boolean', 'Caption' => 'V limitu', 'Default' => '', 'Suffix' => '',
          'ReadOnly' => true, 'SQL' => '((`TxPower` - `CableAttenuation` + `AntenaGain`) <= (CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END))'),
        'Position' => array('Type' => 'String', 'Caption' => 'GPS poloha', 'Default' => '',
          'SQL' => '(SELECT MapPosition.Pos FROM MapPosition WHERE MapPosition.Id=(SELECT MapPosition FROM NetworkDevice WHERE NetworkDevice.Id=(SELECT NetworkInterface.Device FROM NetworkInterface WHERE NetworkInterface.Id=NetworkInterface)))'),
        'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
      ),
      'Actions' => array(
        array('Caption' => 'Frekvenční plán', 'URL' => '/network/frequency-plan/'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceUpDown', array(
      'Title' => 'Změny stavu rozhraní',
      'Table' => 'NetworkInterfaceUpDown',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => '', 'ReadOnly' => true),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => '', 'ReadOnly' => true),
        'State' => array('Type' => 'TOnlineState', 'Caption' => 'Stav', 'Default' => '', 'ReadOnly' => true),
        'Duration' => array('Type' => 'TimeDiff', 'Caption' => 'Trvání', 'Default' => '', 'ReadOnly' => true),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkPortUpDown', array(
      'Title' => 'Změny stavu portů',
      'Table' => 'NetworkPortUpDown',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => '', 'ReadOnly' => true),
        'Port' => array('Type' => 'TNetworkPort', 'Caption' => 'Port', 'Default' => '', 'ReadOnly' => true),
        'State' => array('Type' => 'TOnlineState', 'Caption' => 'Stav', 'Default' => '', 'ReadOnly' => true),
        'Duration' => array('Type' => 'TimeDiff', 'Caption' => 'Trvání', 'Default' => '', 'ReadOnly' => true),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkMac', array(
      'Title' => 'Fyzická síťová adresa',
      'Table' => 'NetworkMac',
      'Items' => array(
        'MAC' => array('Type' => 'String', 'Caption' => 'Fyzická adresa', 'Default' => ''),
        'Signal' => array('Type' => 'TNetworkSignalListMac', 'Caption' => 'Signál rozhraní', 'Default' => ''),        
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkMac', array(
      'Type' => 'Reference',
      'Table' => 'NetworkMac',
      'Id' => 'Id',
      'Name' => 'MAC',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkSignalListMac', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkSignal',
      'Id' => 'Id',
      'Ref' => 'Mac',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkProtocol', array(
      'Type' => 'Enumeration',
      'States' => array('TCP', 'UDP'),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDevice', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDeviceType', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDeviceType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterface', array(
      'Type' => 'Reference',
      'Table' => 'NetworkInterface',
      'View' => '(SELECT NetworkInterface.*, CONCAT_WS("-", NetworkDevice.Name, NULLIF(NetworkInterface.Name, "")) AS DeviceName FROM NetworkInterface '.
      'LEFT JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device) AS T',
      'Id' => 'Id',
      'Name' => 'DeviceName',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkPort', array(
      'Type' => 'Reference',
      'Table' => 'NetworkPort',
      'View' => '(SELECT `NetworkPort`.*, CONCAT(CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")), ":", `NetworkPort`.`Number`) AS `PortName` FROM `NetworkPort` '.
      'LEFT JOIN `NetworkInterface` ON `NetworkInterface`.`Id` = `NetworkPort`.`Interface` '.
      'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device`) AS `T`',
      'Id' => 'Id',
      'Name' => 'PortName',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterfaceType', array(
      'Type' => 'Reference',
      'Table' => 'NetworkInterfaceType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Ref' => 'Member',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceListAPI', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Ref' => 'API',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDevicePortListInterface', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkPort',
      'Id' => 'Id',
      'Ref' => 'Interface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceInterfaceLatencyListInterface', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterfaceLatency',
      'Id' => 'Id',
      'Ref' => 'Interface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkType', array(
      'Type' => 'Reference',
      'Table' => 'NetworkLinkType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceAPIType', array(
      'Type' => 'Reference',
      'Table' => 'DeviceAPIType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterfaceWirelessListInterface', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterfaceWireless',
      'Id' => 'Id',
      'Ref' => 'NetworkInterface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TInterfaceList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterface',
      'Id' => 'Id',
      'Ref' => 'Device',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceLogList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDeviceLog',
      'Id' => 'Id',
      'Ref' => 'Device',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkListInterface', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkLinkUnion',
      'Id' => 'Id',
      'Ref' => 'Interface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkListInterface1', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkLink',
      'Id' => 'Id',
      'Ref' => 'Interface1',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkListInterface2', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkLink',
      'Id' => 'Id',
      'Ref' => 'Interface2',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterfaceUpDown', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterfaceUpDown',
      'Id' => 'Id',
      'Ref' => 'Interface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkPortUpDown', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkPortUpDown',
      'Id' => 'Id',
      'Ref' => 'Port',
      'Filter' => '1',
    ));

    ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('Network',
      array($this, 'ShowDashboardItem'));

    $this->System->RegisterModel('NetworkDevice', array(
      'Title' => 'Síťové zařízení',
    ));
    $this->System->RegisterModel('NetworkInterface', array(
      'Title' => 'Síťové rozhraní',
    ));
  }

  function AfterInsertNetworkDevice(Form $Form, string $Id): array
  {
    $this->System->Models['NetworkDevice']->DoOnChange();
    return $Form->Values;
  }

  function AfterModifyNetworkDevice(Form $Form, string $Id): array
  {
    $this->System->Models['NetworkDevice']->DoOnChange();
    return $Form->Values;
  }

  function AfterInsertNetworkInterface(Form $Form, string $Id): array
  {
    $this->System->Models['NetworkInterface']->DoOnChange();
    return $Form->Values;
  }

  function AfterModifyNetworkInterface(Form $Form, string $Id): array
  {
    $this->System->Models['NetworkInterface']->DoOnChange();
    return $Form->Values;
  }

  function BeforeDeleteNetworkInterface(Form $Form, string $Id): void
  {
    $this->Database->query('DELETE FROM `NetworkInterfaceUpDown` WHERE `Interface`='.$Id);
    $this->Database->query('DELETE FROM `NetworkLink` WHERE `Interface1`='.$Id.' OR `Interface2`='.$Id);
    $this->Database->query('DELETE FROM `NetworkPort` WHERE `Interface`='.$Id);
    $this->Database->query('DELETE FROM `NetworkSignal` WHERE `Interface`='.$Id);
    $this->Database->query('DELETE FROM `NetworkInterfaceWireless` WHERE `NetworkInterface`='.$Id);
    $this->Database->query('DELETE FROM `NetworkInterfaceLatency` WHERE `Interface`='.$Id);
  }

  function ImportNetworkLog(array $Parameters): void
  {
    global $Config;

    $StartTime = time();

    $DbResult = $this->System->Database->select('NetworkDeviceLog', 'Time',
      '1 ORDER BY Time DESC LIMIT 1');
    if ($DbResult->num_rows > 0)
    {
      $DbRow = $DbResult->fetch_assoc();
      $WhereTime = ' AND (`Time` > "'.$DbRow['Time'].'")';
    } else $WhereTime = '';

    $RemoteDb = new Database();
    $RemoteDb->Connect($Config['NetworkLog']['Host'], $Config['NetworkLog']['User'],
      $Config['NetworkLog']['Password'], $Config['NetworkLog']['Database']);
    $DbResult = $RemoteDb->select('SystemEvents', '*', '`ReceivedAt`>"'. TimeToMysqlDate($StartTime).'"'.$WhereTime.' ORDER BY `Time` DESC');
    while ($DbRow = $DbResult->fetch_array())
    {
      $DbResult2 = $this->System->Database->select('NetworkInterface', 'Device, CONCAT(CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")), ".") AS `Name` ', '`Name` LIKE "'.$DbRow['FromHost'].'"');
      if ($DbResult2->num_rows > 0)
      {
        $DbRow2 = $DbResult2->fetch_assoc();
        $DeviceId = $DbRow2['Device'];
        $this->System->Database->insert('NetworkDeviceLog', array('Time' => $DbRow['ReceivedAt'],
          'Device' => $DeviceId, 'Message' => $DbRow['Message'], 'Tags' => $DbRow['SysLogTag']));
      } else echo('Host '.$DbRow['Host'].' not paired with network device.'."\n");
    }
  }

  function ShowDashboardItem(): string
  {
    $Output = '';

    // Devices
    $DbResult = $this->Database->select('NetworkDevice', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových zařízení: registrovaných: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkDevice').'">'.$DbRow['0'].'</a>';

    $DbResult = $this->Database->select('NetworkDevice', 'COUNT(*)', '`Used`=1');
    $DbRow = $DbResult->fetch_row();
    $Output .= ' použitých: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkDevice&amp;filter=1&amp;FilterUsed=1').'">'.$DbRow['0'].'</a>';

    $DbResult = $this->Database->select('NetworkDevice', 'COUNT(*)', '`Used`=1 AND `PermanentOnline`=1 AND `Online`=0');
    $DbRow = $DbResult->fetch_row();
    $Output .= ' nedostupných: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkDevice&amp;filter=1&amp;FilterUsed=1&amp;FilterOnline=0&amp;FilterPermanentOnline=1').'">'.$DbRow['0'].'</a>';

    $Output .= '<br/>';

    // Interfaces
    $DbResult = $this->Database->select('NetworkInterface', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových rozhraní: registrovaných: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkDevice').'">'.$DbRow['0'].'</a>';

    $DbResult = $this->Database->select('NetworkInterface', 'COUNT(*)', '`Enabled`=1');
    $DbRow = $DbResult->fetch_row();
    $Output .= ' použitých: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkInterface&amp;filter=1&amp;FilterEnabled=1').'">'.$DbRow['0'].'</a>';

    //$DbResult = $this->Database->select('NetworkInterface', 'COUNT(*)', '`Enabled`=1 AND `PermanentOnline`=1 AND `Online`=0');
    //$DbRow = $DbResult->fetch_row();
    //$Output .= ' nedostupných: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkInterface&amp;filter=1&amp;FilterEnabled=1&amp;FilterOnline=0&amp;FilterPermanentOnline=1').'">'.$DbRow['0'].'</a>';

    $Output .= '<br/>';

    // Ports
    $DbResult = $this->Database->select('NetworkPort', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových portů: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkPort').'">'.$DbRow['0'].'</a>';

    $DbResult = $this->Database->select('NetworkPort', 'COUNT(*)', '`Enabled`=1');
    $DbRow = $DbResult->fetch_row();
    $Output .= ' použitých: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkPort&amp;filter=1&amp;FilterEnabled=1').'">'.$DbRow['0'].'</a>';

    $DbResult = $this->Database->select('NetworkPort', 'COUNT(*)', '`Enabled`=1 AND `Online`=0');
    $DbRow = $DbResult->fetch_row();
    $Output .= ' nedostupných: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkPort&amp;filter=1&amp;FilterEnabled=1&amp;FilterOnline=0').'">'.$DbRow['0'].'</a>';

    $Output .= '<br/>';

    // Subnets
    $DbResult = $this->Database->select('NetworkSubnet', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových podsítí: <a href="'.$this->System->Link('/is/?a=list&amp;t=NetworkSubnet').'">'.$DbRow['0'].'</a><br/>';

    return $Output;
  }

  function OnlineList(string $Title, int $OnlineNow, int $OnlinePrevious, int $MinDuration): array
  {
    $Time = time();
    $OnlineText = array('<span style="color:red;">Nedostupný</span>', '<span style="color:green;">Dostupný</span>');
    $Output = '';
    $Condition = 'WHERE (`NetworkInterface`.`LastOnline` <= "'.TimeToMysqlDateTime($Time - $MinDuration).'")';
    if ($OnlineNow >= 0) $Condition .= ' AND (`NetworkInterface`.`Online` = '.$OnlineNow.')';
    if ($OnlinePrevious >= 0) $Condition .= ' AND (`NetworkInterface`.`OnlineNotify` = '.$OnlinePrevious.')';  
    $DbResult3 = $this->Database->query('SELECT CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")) AS `Name`, '.
      '`NetworkInterface`.`Online`, `NetworkInterface`.`LastOnline` FROM `NetworkInterface` '.
      'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
      $Condition.' AND (`NetworkDevice`.`PermanentOnline`=1) AND (`NetworkDevice`.`Used`=1) AND '.
      '(`NetworkInterface`.`LocalIP` != "") AND (`NetworkInterface`.`Enabled`=1)'.
      'ORDER BY `Name` ASC');
    if ($DbResult3->num_rows > 0)
    {
      $Output .= $Title.'<br/>';
      $Output .= '<table>'.
        '<tr><th align="center">Jméno</th><th align="center">Stav</th><th align="center">Čas</th><th align="center">Trvání</th></tr>'."\n";
      while ($Item = $DbResult3->fetch_assoc())
      {
        $Duration = $Time - MysqlDateTimeToTime($Item['LastOnline']);
        $DurationText = sprintf('%02d', floor($Duration / 3600) % 24).':'.
          sprintf('%02d', floor($Duration / 60) % 60).':'.
          sprintf('%02d', floor($Duration) % 60);
        $Days = floor($Duration / (60 * 60 * 24));
        if ($Days > 0) $DurationText = $Days.' dnů '.$DurationText;

        $Output .= '<tr><td>'.$Item['Name'].'</td><td>'.$OnlineText[$Item['Online']].
          '</td><td>'.$Item['LastOnline'].'</td><td>'.$DurationText.'</td></tr>'."\n";
      }
      $Output .= '</table><br/>'."\n";
    }

    if (($OnlineNow >= 0) and ($OnlinePrevious >= 0))
    {
      $this->Database->query('UPDATE `NetworkInterface` SET `OnlineNotify` = `Online` '.$Condition);
    }
    return array('Report' => $Output, 'Count' => $DbResult3->num_rows);
  }

  function ReachabilityCheck(): array
  {
    $NewOnline = $this->OnlineList('Nově online', 1, 0, 0);
    $Output = $NewOnline['Report'];
    $NewOffline = $this->OnlineList('Nově offline', 0, 1, $this->MinNotifyTime);
    $Output .= $NewOffline['Report'];
    if ($Output != '')
    {
      $StillOffline = $this->OnlineList('Stále offline', 0, 0, 0);
      $Output .= $StillOffline['Report'];
    }
    $Offline = $this->OnlineList('Offline', 0, -1, 0);
    return array('Report' => $Output, 'Count' => $Offline['Count'], 'Title' => 'Odezva');
  }

  function PortCheckList(string $Title, int $OnlineNow, int $OnlinePrevious, int $MinDuration): array
  {
    $Time = time();
    $OnlineText = array('<span style="color:red;">Nedostupný</span>', '<span style="color:green;">Dostupný</span>');
    $Output = '';
    $Condition = 'WHERE (`NetworkPort`.`LastOnline` <= "'.TimeToMysqlDateTime($Time - $MinDuration).'")';
    if ($OnlineNow >= null) $Condition .= ' AND (`NetworkPort`.`Online` = '.$OnlineNow.')';
    if ($OnlinePrevious >= null) $Condition .= ' AND (`NetworkPort`.`OnlineNotify` = '.$OnlinePrevious.')';
    $DbResult3 = $this->Database->query('SELECT CONCAT(CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")), ":", `NetworkPort`.`Number`, "(", `NetworkPort`.`Name`, ")") AS `Name`, '.
      '`NetworkPort`.`Online`, `NetworkPort`.`LastOnline` FROM `NetworkPort` '.
      'LEFT JOIN `NetworkInterface` ON `NetworkInterface`.`Id` = `NetworkPort`.`Interface` '.
      'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
      $Condition.' AND (`NetworkDevice`.`PermanentOnline`=1) AND (`NetworkDevice`.`Used`=1) AND '.
      '(`NetworkInterface`.`LocalIP` != "") AND (`NetworkPort`.`Enabled` = 1)'.
      'ORDER BY `Name` ASC');
    if ($DbResult3->num_rows > 0)
    {
      $Output .= $Title.'<br/>';
      $Output .= '<table>'.
        '<tr><th align="center">Jméno</th><th align="center">Stav</th><th align="center">Čas</th><th align="center">Trvání</th></tr>'."\n";
      while ($Item = $DbResult3->fetch_assoc())
      {
        $Duration = $Time - MysqlDateTimeToTime($Item['LastOnline']);
        $DurationText = sprintf('%02d', floor($Duration / 3600) % 24).':'.
          sprintf('%02d', floor($Duration / 60) % 60).':'.
          sprintf('%02d', floor($Duration) % 60);
        $Days = floor($Duration / (60 * 60 * 24));
        if ($Days > 0) $DurationText = $Days.' dnů '.$DurationText;

        $Output .= '<tr><td>'.$Item['Name'].'</td><td>'.$OnlineText[$Item['Online']].
          '</td><td>'.$Item['LastOnline'].'</td><td>'.$DurationText.'</td></tr>'."\n";
      }
      $Output .= '</table><br/>'."\n";
    }

    if (($OnlineNow >= 0) and ($OnlinePrevious >= 0))
    {
      $this->Database->query('UPDATE `NetworkPort` SET `OnlineNotify` = `Online` '.$Condition);
    }
    return array('Report' => $Output, 'Count' => $DbResult3->num_rows);
  }

  function PortCheck(): array
  {
    $Output = '';
    $NewOnline = $this->PortCheckList('Nově online', 1, 0, 0);
    $Output .= $NewOnline['Report'];
    $NewOffline = $this->PortCheckList('Nově offline', 0, 1, $this->MinNotifyTime);
    $Output .= $NewOffline['Report'];
    if ($Output != '')
    {
      $StillOffline = $this->PortCheckList('Stále offline', 0, 0, 0);
      $Output .= $StillOffline['Report'];
    }
    $Offline = $this->PortCheckList('Offline', 0, -1, 0);
    return array('Report' => $Output, 'Count' => $Offline['Count'], 'Title' => 'Port');
  }
}