<?php

include_once('SSH.php');

class Routerboard extends SSH
{
  public array $Methods = array(
    'kex' => 'diffie-hellman-group1-sha1',
    'client_to_server' => array('crypt' => '3des-cbc', 'comp' => 'none'),
    'server_to_client' => array('crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc', 'comp' => 'none')
  );

  function Execute(array $Commands): array
  {
    if (is_array($Commands)) $Commands = implode(';', $Commands);
    return parent::Execute($Commands);
  }

  function GetItem($Command): array
  {
    $Result = $this->Execute($Command);
    array_pop($Result);
    $List = array();
    foreach ($Result as $ResultLine)
    {
      $ResultLineParts = explode(' ', trim($ResultLine));
      if ($ResultLineParts[1][0] == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
      $List[substr($ResultLineParts[0], 0, -1)] = $ResultLineParts[1];
    }
    return $List;
  }

  function GetList($Command, $Properties): array
  {
    $PropertyList = '"';
    foreach ($Properties as $Property)
    {
      $PropertyList .= $Property.'=".[get $i '.$Property.']." ';
    }
    $PropertyList = substr($PropertyList, 0, -3);
    $Result = $this->Execute($Command.' {:foreach i in=[find] do={:put ('.$PropertyList.')}}');
    $List = array();
    foreach ($Result as $ResultLine)
    {
      $ResultLineParts = explode(' ', $ResultLine);
      $ListItem = array();
      foreach ($ResultLineParts as $ResultLinePart)
      {
        $Value = explode('=', $ResultLinePart);
        $ListItem[$Value[0]] = $Value[1];
      }
      $List[] = $ListItem;
    }
    return $List;
  }

  function GetSystemResource(): array
  {
    return $this->GetItem('/system resource print');
  }

  function GetFirewallFilterList(): array
  {
    return $this->GetList('/ip firewall nat', array('src-address', 'dst-address', 'bytes'));
  }

  function GetDHCPServerLeasesList(): array
  {
    return $this->GetList('/ip dhcp-server lease', array('address', 'active-address', 'comment', 'lease-time', 'status', 'host-name'));
  }
}
