index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * Copyright (C) 2019 Alexander Marston ([email protected])
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // Require includes
  19. require __DIR__ . '/vendor/autoload.php';
  20. require __DIR__ . '/includes/utilities.php';
  21. require __DIR__ . '/includes/vnstat.php';
  22. require __DIR__ . '/includes/config.php';
  23. if (isset($vnstat_config)) {
  24. $vnstat_cmd = $vnstat_bin_dir.' --config '.$vnstat_config;
  25. } else {
  26. $vnstat_cmd = $vnstat_bin_dir;
  27. }
  28. if (empty($graph_type)) {
  29. $graph_type = 'linear';
  30. }
  31. // Initiaite vnStat class
  32. $vnstat = new vnStat($vnstat_cmd);
  33. // Initiate Smarty
  34. $smarty = new Smarty();
  35. // Set the current year
  36. $smarty->assign('year', date("Y"));
  37. // Set the list of interfaces
  38. $interface_list = $vnstat->getInterfaces();
  39. // Set the current interface
  40. $thisInterface = "";
  41. if (isset($_GET['i'])) {
  42. $interfaceChosen = rawurldecode($_GET['i']);
  43. if (in_array($interfaceChosen, $interface_list, true)) {
  44. $thisInterface = $interfaceChosen;
  45. } else {
  46. $thisInterface = reset($interface_list);
  47. }
  48. } else {
  49. // Assume they mean the first interface
  50. $thisInterface = reset($interface_list);
  51. }
  52. $smarty->assign('graph_type', $graph_type);
  53. $smarty->assign('current_interface', $thisInterface);
  54. // Assign interface options
  55. $smarty->assign('interface_list', $interface_list);
  56. // JsonVersion
  57. $smarty->assign('jsonVersion', $vnstat->getVnstatJsonVersion());
  58. // Populate table data
  59. if ($vnstat->getVnstatJsonVersion() > 1) {
  60. $fiveData = $vnstat->getInterfaceData('five', 'table', $thisInterface);
  61. $smarty->assign('fiveTableData', $fiveData);
  62. }
  63. $hourlyData = $vnstat->getInterfaceData('hourly', 'table', $thisInterface);
  64. $smarty->assign('hourlyTableData', $hourlyData);
  65. $dailyData = $vnstat->getInterfaceData('daily', 'table', $thisInterface);
  66. $smarty->assign('dailyTableData', $dailyData);
  67. $monthlyData = $vnstat->getInterfaceData('monthly', 'table', $thisInterface);
  68. $smarty->assign('monthlyTableData', $monthlyData);
  69. $top10Data = $vnstat->getInterfaceData('top10', 'table', $thisInterface);
  70. $smarty->assign('top10TableData', $top10Data);
  71. // Populate graph data
  72. if ($vnstat->getVnstatJsonVersion() > 1) {
  73. $fiveGraphData = $vnstat->getInterfaceData('five', 'graph', $thisInterface);
  74. $smarty->assign('fiveGraphData', $fiveGraphData);
  75. $smarty->assign('fiveLargestPrefix', $fiveGraphData[0]['delimiter']);
  76. $smarty->assign('fiveBase', $fiveGraphData[0]['base']);
  77. }
  78. $hourlyGraphData = $vnstat->getInterfaceData('hourly', 'graph', $thisInterface);
  79. $smarty->assign('hourlyGraphData', $hourlyGraphData);
  80. $smarty->assign('hourlyLargestPrefix', $hourlyGraphData[0]['delimiter']);
  81. $smarty->assign('hourlyBase', $hourlyGraphData[0]['base']);
  82. $dailyGraphData = $vnstat->getInterfaceData('daily', 'graph', $thisInterface);
  83. $smarty->assign('dailyGraphData', $dailyGraphData);
  84. $smarty->assign('dailyLargestPrefix', $dailyGraphData[0]['delimiter']);
  85. $smarty->assign('dailyBase', $dailyGraphData[0]['base']);
  86. $monthlyGraphData = $vnstat->getInterfaceData('monthly', 'graph', $thisInterface);
  87. $smarty->assign('monthlyGraphData', $monthlyGraphData);
  88. $smarty->assign('monthlyLargestPrefix', $monthlyGraphData[0]['delimiter']);
  89. // Display the page
  90. $smarty->display('templates/site_index.tpl');
  91. ?>