<?php

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

class PageFrequencyPlan extends Page
{
  var $FullTitle = 'Výpis obsazení frekvenčních kanálů';
  var $ShortTitle = 'Frekvenční plán';
  var $ParentClass = 'PageNetwork';

  function Show()
  {
        // 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="http://www.ctu.cz/1/download/Opatreni%20obecne%20povahy/VO_R_12_08_2005_34.pdf">VO_R_12_08_2005_34</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 = '#000000';
                else if(($LowFrequency <= ($Frequency - 2.5)) and ($HighFrequency >= ($Frequency + 2.5))) $Color = '#808080';
                else if(($LowFrequency == $Frequency) or ($HighFrequency == $Frequency)) $Color = '#c0c0c0';
                
                else $Color = '#ffffff';
              $Output .= '<td style="background-color: '.$Color.';">&nbsp;</td>';
            }

            $Output .= '</tr>';  
          }          
        }
        $Output .= '</table>';
    return($Output);
  }  
}
  
class PageNetwork extends Page
{
  var $FullTitle = 'Technické informace o síti';
  var $ShortTitle = 'Síť';
  var $ParentClass = 'PagePortal';

  function Show()
  {
    if(count($this->System->PathItems) > 1)
    {
      $Output = $this->PageNotFound();
    } else $Output = $this->ShowInformation();
    return($Output);  
  }
  
  function ShowInformation()
  {
    $Output = '<a href="'.$this->System->Link('/network/frequency-plan/').'">Frekvenční plán</a><br />';
    $Output .= '<a href="'.$this->System->Link('/network/availability/').'">Měření dostupnosti zařízení</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 AppModule
{
  function __construct($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();
  }  

  function DoInstall()
  {
  }
  
  function DoUninstall()
  {     
  }
  
  function DoStart()
  {
    $this->System->RegisterPage('network', 'PageNetwork');    
    $this->System->RegisterPage(array('network', 'administration'), 'PageNetworkAdministration');
    $this->System->RegisterPage(array('network', 'availability'), 'PageAvailability');
    $this->System->RegisterPage(array('network', 'subnet'), 'PageSubnet');
    $this->System->RegisterPage(array('network', 'user-hosts'), 'PageNetworkHostList');
    $this->System->RegisterPage(array('network', 'hosting'),'PageHosting');
    $this->System->RegisterPage(array('network', 'hosts'), 'PageHostList');
    $this->System->RegisterPage(array('network', 'frequency-plan'), 'PageFrequencyPlan');
    
    $this->System->FormManager->RegisterClass('Subject', array(
      'Title' => 'Subjekty',
      'Table' => 'Subject',
      'DefaultSortColumn' => 'Name',
      'Items' => array(
        'Id' => array('Type' => 'Integer', 'Caption' => 'Identifikace', 'Default' => '', 'ReadOnly' => true),
        'Name' => array('Type' => 'String', 'Caption' => 'Celé jméno', 'Default' => ''),
        'AddressStreet' => array('Type' => 'String', 'Caption' => 'Ulice', 'Default' => ''),
        'AddressTown' => array('Type' => 'String', 'Caption' => 'Město', 'Default' => ''),
        'AddressPSC' => array('Type' => 'String', 'Caption' => 'PSČ', 'Default' => ''),
        'AddressCountry' => array('Type' => 'TCountry', 'Caption' => 'Země', 'Default' => ''),
        'IC' => array('Type' => 'String', 'Caption' => 'IČ', 'Default' => ''),
        'DIC' => array('Type' => 'String', 'Caption' => 'DIČ', 'Default' => ''),
        'MapPosition' => array('Type' => 'TMapPosition', 'Caption' => 'Pozice na mapě', 'Default' => '', 'Null' => true),
        'WWW' => array('Type' => 'Hyperlink', 'Caption' => 'WWW', 'Default' => ''),
        'Note' => array('Type' => 'String', 'Caption' => 'Poznámka', 'Default' => ''),
        'Customer' => array('Type' => 'TMemberListSubject', 'Caption' => 'Členové', 'Default' => ''),
        'Operations' => array('Type' => 'TFinanceOperationListSubject', 'Caption' => 'Platby', 'Default' => ''),
        'Invoices' => array('Type' => 'TFinanceInvoiceListSubject', 'Caption' => 'Faktury', 'Default' => ''),
        'Payment' => array('Type' => 'Float', 'Caption' => 'Placení', 'Default' => '', 
          'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value`) FROM `FinanceOperation` '.
          'WHERE `FinanceOperation`.`Subject`=#Id) - (SELECT SUM(`FinanceInvoice`.`Value`) FROM `FinanceInvoice` '.
          'WHERE `FinanceInvoice`.`Subject`=#Id)'),
      ),
    ));
    $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' => ''),
      ),
    ));
    $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' => 'Boolean', '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),
        'LoginName' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => '', 'Null' => true),
        'LoginPassword' => array('Type' => 'String', 'Caption' => 'Přihlašovací heslo', 'Default' => '', 'Null' => true),
      ),
      'Actions' => array(
        array('Caption' => 'Dostupnost zařízení', 'URL' => '/network/availability/'),
      ),
    ));
    $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'),
      ),
    ));
    $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' => ''),
        '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' => ''),
        'Online' => array('Type' => 'Boolean', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
        'Links1' => array('Type' => 'TNetworkLinkListInterface1', 'Caption' => 'Propojení 1', 'Default' => ''),
        'Links2' => array('Type' => 'TNetworkLinkListInterface2', 'Caption' => 'Propojení 2', 'Default' => ''),
      ),
    ));
    $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),
        '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' => ''),
        'Configure' => array('Type' => 'Boolean', 'Caption' => 'Nastavovat', 'Default' => ''),
      ),
    ));    
    $this->System->FormManager->RegisterClass('NetworkLink', array(
      'Title' => 'Síťové propojení',
      'Table' => 'NetworkLink',
      'Items' => array(
        'Type' => array('Type' => 'Integer', 'Caption' => 'Typ', 'Default' => '1'),
        'Interface1' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 1', 'Default' => ''),
        'Interface2' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 2', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkDeviceConfig',array(
      'Title' => 'Nastavení zařízení',
      'Table' => 'NetworkDeviceConfig',
      'DefaultSortColumn' => 'Time',
      '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('NetworkSegment', array(
      'Title' => 'Úsek sítě',
      'Table' => 'NetworkSegment',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Price' => array('Type' => 'Integer', 'Caption' => 'Cena', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true),
        'Parent' => array('Type' => 'TNetworkSegment', 'Caption' => 'Nadřazený', 'Default' => '', 'Null' => true),
        'Users' => array('Type' => 'Integer', 'Caption' => 'Uživatelů', 'Default' => '0', 'ReadOnly' => true),
        'Consumption' => array('Type' => 'Integer', 'Caption' => 'Spotřeba', 'Default' => '0', 'ReadOnly' => true, 'Suffix' => 'Wattů'),
        'UsersOverheads' => array('Type' => 'Integer', 'Caption' => 'Podílníků', 'Default' => '0', 'ReadOnly' => true),
      ),
    ));
    $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' => 'Šírka kanálu dolního', 'Default' => '0', 'Suffix' => 'MHz'),
        'ChannelWidth' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu', 'Default' => '20', 'Suffix' => 'MHz'),
        'ChannelWidthUpper' => array('Type' => 'Integer', 'Caption' => 'Šírka 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/'),
      ),
    ));
  }  
  
  function DoStop()
  { 
  } 
}
