#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2023, Lime Technology
 * Copyright 2012-2023, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot = '/usr/local/emhttp';
$net     = '/sys/class/net';
$bond    = '/proc/net/bonding';
$data    = [];

require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/publish.php";
extract(parse_plugin_cfg('dynamix',true));

// add translations
$_SERVER['REQUEST_URI'] = 'dashboard/main';
$login_locale = _var($display,'locale');
require_once "$docroot/webGui/include/Translations.php";

function update_translation($locale) {
  global $docroot,$language;
  $language = [];
  if ($locale) {
    $text = "$docroot/languages/$locale/translations.txt";
    if (file_exists($text)) {
      $store = "$docroot/languages/$locale/translations.dot";
      if (!file_exists($store)) file_put_contents($store,serialize(parse_lang_file($text)));
      $language = unserialize(file_get_contents($store));
    }
    $text = "$docroot/languages/$locale/dashboard.txt";
    if (file_exists($text)) {
      $store = "$docroot/languages/$locale/dashboard.dot";
      if (!file_exists($store)) file_put_contents($store,serialize(parse_lang_file($text)));
      $language = array_merge($language,unserialize(file_get_contents($store)));
    }
  }
}
function ports() {
  global $net;
  exec("ls --indicator-style=none $net|grep -Po '^(lo|(bond|eth)\d+)$'",$ports);
  return $ports;
}

function port_get_contents($port) {
  return file_exists($port) ? @file_get_contents($port) : 0;
}

// initialize variables
$time0 = $time = microtime(true);
foreach (ports() as $port) {
  $data[$port]['rx'] = (float)port_get_contents("$net/$port/statistics/rx_bytes");
  $data[$port]['tx'] = (float)port_get_contents("$net/$port/statistics/tx_bytes");
}
// loop
while (true) {
  // check for language changes
  extract(parse_plugin_cfg('dynamix',true));
  if (_var($display,'locale') != $locale_init) {
    $locale_init = _var($display,'locale');
    update_translation($locale_init);
  }
  $echo = $mode = $rxtx = $stat = [];
  $ts = $time - $time0;
  foreach (ports() as $port) {
    // inbound + outbound speed
    $rx = (float)port_get_contents("$net/$port/statistics/rx_bytes");
    $tx = (float)port_get_contents("$net/$port/statistics/tx_bytes");
    if ($ts > 0) {
      $rxd = ($rx-_var($data[$port],'rx',0))/$ts*8;
      $txd = ($tx-_var($data[$port],'tx',0))/$ts*8;
      $rx_speed = my_scale($rxd,$unit,1,-1).' '.str_replace('B','b',$unit).'ps';
      $tx_speed = my_scale($txd,$unit,1,-1).' '.str_replace('B','b',$unit).'ps';
    } else {
      $rxd = $txd = 0;
      $rx_speed = '---';
      $tx_speed = '---';
    }
    $data[$port]['rx'] = $rx;
    $data[$port]['tx'] = $tx;
    $echo[] = "$port\0$rx_speed\0$tx_speed\0$rxd\0$txd";
    // interface general information
    $mtu = port_get_contents("$net/$port/mtu");
    $link = port_get_contents("$net/$port/carrier")==1;
    if (substr($port,0,4)=='bond') {
      if ($link) {
        $bond_mode = file_exists("$bond/$port") ? str_replace('Bonding Mode: ','',@file("$bond/$port",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)[1]) : '---';
        $mode[] = "$bond_mode, mtu $mtu";
      } else $mode[] = "bond down";
    } elseif ($port=='lo') {
      $mode[] = $link ? "loopback" : "not set";
    } else {
      if ($link) {
        $speed = port_get_contents("$net/$port/speed");
        $duplex = port_get_contents("$net/$port/duplex");
        $mode[] = "$speed Mbps, $duplex duplex, mtu $mtu";
      } else $mode[] = "interface down";
    }
    // interface counters
    $rxtx[] = "$rx\0$tx";
    // interface errors
    $rx_errors = port_get_contents("$net/$port/statistics/rx_errors");
    $rx_drops  = port_get_contents("$net/$port/statistics/rx_dropped");
    $rx_fifo   = port_get_contents("$net/$port/statistics/rx_fifo_errors");
    $tx_errors = port_get_contents("$net/$port/statistics/tx_errors");
    $tx_drops  = port_get_contents("$net/$port/statistics/tx_dropped");
    $tx_fifo   = port_get_contents("$net/$port/statistics/tx_fifo_errors");
    $stat[] = "Errors: {$rx_errors}<br>Drops: {$rx_drops}<br>Overruns: {$rx_fifo}\0Errors: {$tx_errors}<br>Drops: {$tx_drops}<br>Overruns: {$tx_fifo}";
  }
  // current date and time
  $now = time();
  if (_var($display,'time')=='%R') {
    // 24 hour format
    $clock = date('H:i',$now); $ampm = '';
  } else {
    // 12 hour format wih am/pm
    $clock = date('h:i',$now); $ampm = '<span class="ampm">'.date('a',$now).'</span>';
  }
  $timedate = "$clock $ampm\n"._(date('l, F j, Y',$now),0);
  publish('update3',implode("\n",$echo)."\1".implode("\0",$mode)."\1".implode("\0",$rxtx)."\1".implode("\0",$stat)."\1".$timedate);
  $time0 = $time;
  sleep(1);
  $time = microtime(true);
}
