<?php

class ConfigRouterOSDNS extends NetworkConfigItem
{
  function Run()
  {
    $Path = array('ip', 'dns', 'static');

    $Routerboard = new Routerboard($this->System->Config['MainRouter']['HostName']);
    $Routerboard->UserName = $this->System->Config['MainRouter']['UserName'];
    $Routerboard->Timeout = $this->System->Config['MainRouter']['ConnectTimeout'];
    $Routerboard->Debug = true;

    $DbResult = $this->Database->query('SELECT * FROM `NetworkDomain`');
    while($Domain = $DbResult->fetch_assoc())
    {
      $DomainName = $Domain['Name'];

      // Get full domain name from parent items
      $CurrentDomain = $Domain;
      while($CurrentDomain['Parent'] > 0)
      {
        $DbResult2 = $this->Database->query('SELECT * FROM `NetworkDomain` WHERE `Id`='.$CurrentDomain['Parent']);
        $CurrentDomain = $DbResult2->fetch_assoc();
        $DomainName .= '.'.$CurrentDomain['Name'];
      }

      $Items = array();

      // Devices, Interfaces
      $DbResult2 = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName` FROM `NetworkInterface` '.
          'JOIN `NetworkDevice` ON `NetworkInterface`.`Device`=`NetworkDevice`.`Id` '.
          'WHERE (`NetworkDevice`.`Used`=1)');
      while($Interface = $DbResult2->fetch_assoc())
      {
        $Name = $Interface['DeviceName'];
        if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
        $NameFull = $Name.'.'.$DomainName;
        $NameExtFull = $Name.'-ext.'.$DomainName;
        if($Interface['LocalIP'] != '')
          $Items[] = array('name' => $NameFull, 'address' => $Interface['LocalIP']);
        if($Interface['IPv6'] != '')
          $Items[] = array('name' => $NameFull, 'address' => $Interface['IPv6']);
        if($Interface['ExternalIP'] != '')
          $Items[] = array('name' => $NameExtFull, 'address' => $Interface['ExternalIP']);
      }

      // Domain aliases
      $DbResult2 = $this->Database->query('SELECT `NetworkDomainAlias`.*, `NetworkInterface`.`LocalIP` AS `LocalIP`, '.
          '`NetworkInterface`.`IPv6` AS `IPv6`, `NetworkInterface`.`ExternalIP` AS `ExternalIP` FROM `NetworkDomainAlias` '.
          'JOIN `NetworkDevice` ON SUBSTR(`NetworkDomainAlias`.`Target`, 1, LENGTH(`NetworkDevice`.`Name` ))=`NetworkDevice`.`Name` '.
          'JOIN `NetworkInterface` ON `NetworkInterface`.`Device`=`NetworkDevice`.`Id` '.
          'WHERE (`NetworkDevice`.`Used`=1) AND '.
          '(CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")) = `NetworkDomainAlias`.`Target`)');
      while($Alias = $DbResult2->fetch_assoc())
      {
        $Name = $Alias['Name'];
        $NameFull = $Name.'.'.$DomainName;
        $NameExtFull = $Name.'-ext.'.$DomainName;
        if($Alias['LocalIP'] != '')
          $Items[] = array('name' => $NameFull, 'address' => $Alias['LocalIP']);
        if($Alias['IPv6'] != '')
          $Items[] = array('name' => $NameFull, 'address' => $Alias['IPv6']);
        if($Alias['ExternalIP'] != '')
          $Items[] = array('name' => $NameExtFull, 'address' => $Alias['ExternalIP']);
      }

      $DbResult2 = $this->Database->query('SELECT * FROM `NetworkDomainServer` WHERE `Domain`='.$Domain['Id']);
      while($Server = $DbResult2->fetch_assoc())
      {
        $Routerboard->HostName = $Server['Address'];
        $Routerboard->ListUpdate($Path, array('name', 'address'), $Items);
      }
    }
  }
}
