index.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. //
  3. // vnStat PHP frontend (c)2006-2010 Bjorge Dijkstra (bjd@jooz.net)
  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 2 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, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. //
  19. //
  20. // see file COPYING or at http://www.gnu.org/licenses/gpl.html
  21. // for more information.
  22. //
  23. require 'config.php';
  24. require 'localize.php';
  25. require 'vnstat.php';
  26. validate_input();
  27. require "./themes/$style/theme.php";
  28. function write_side_bar()
  29. {
  30. global $iface, $page, $graph, $script, $style;
  31. global $iface_list, $iface_title;
  32. global $page_list, $page_title;
  33. $p = "&amp;graph=$graph&amp;style=$style";
  34. print "<ul class=\"iface\">\n";
  35. foreach ($iface_list as $if)
  36. {
  37. if ($iface == $if) {
  38. print "<li class=\"iface active\">";
  39. } else {
  40. print "<li class=\"iface\">";
  41. }
  42. print "<a href=\"$script?if=$if$p\">";
  43. if (isset($iface_title[$if]))
  44. {
  45. print $iface_title[$if];
  46. }
  47. else
  48. {
  49. print $if;
  50. }
  51. print "</a>";
  52. print "<ul class=\"page\">\n";
  53. foreach ($page_list as $pg)
  54. {
  55. print "<li class=\"page\"><a href=\"$script?if=$if$p&amp;page=$pg\">".$page_title[$pg]."</a></li>\n";
  56. }
  57. print "</ul></li>\n";
  58. }
  59. print "</ul>\n";
  60. }
  61. function kbytes_to_string($kb)
  62. {
  63. global $byte_notation;
  64. $units = array('TiB','GiB','MiB','KiB');
  65. $scale = 1024*1024*1024;
  66. $ui = 0;
  67. $custom_size = isset($byte_notation) && in_array($byte_notation, $units);
  68. while ((($kb < $scale) && ($scale > 1)) || $custom_size)
  69. {
  70. $ui++;
  71. $scale = $scale / 1024;
  72. if ($custom_size && $units[$ui] == $byte_notation) {
  73. break;
  74. }
  75. }
  76. return sprintf("%0.2f %s", ($kb/$scale),$units[$ui]);
  77. }
  78. function write_summary()
  79. {
  80. global $summary,$top,$day,$hour,$month;
  81. $trx = $summary['totalrx']*1024+$summary['totalrxk'];
  82. $ttx = $summary['totaltx']*1024+$summary['totaltxk'];
  83. //
  84. // build array for write_data_table
  85. //
  86. $sum = array();
  87. if (count($day) > 0 && count($hour) > 0 && count($month) > 0) {
  88. $sum[0]['act'] = 1;
  89. $sum[0]['label'] = T('This hour');
  90. $sum[0]['rx'] = $hour[0]['rx'];
  91. $sum[0]['tx'] = $hour[0]['tx'];
  92. $sum[1]['act'] = 1;
  93. $sum[1]['label'] = T('This day');
  94. $sum[1]['rx'] = $day[0]['rx'];
  95. $sum[1]['tx'] = $day[0]['tx'];
  96. $sum[2]['act'] = 1;
  97. $sum[2]['label'] = T('This month');
  98. $sum[2]['rx'] = $month[0]['rx'];
  99. $sum[2]['tx'] = $month[0]['tx'];
  100. $sum[3]['act'] = 1;
  101. $sum[3]['label'] = T('All time');
  102. $sum[3]['rx'] = $trx;
  103. $sum[3]['tx'] = $ttx;
  104. }
  105. write_data_table(T('Summary'), $sum);
  106. print "<br/>\n";
  107. write_data_table(T('Top 10 days'), $top);
  108. }
  109. function write_data_table($caption, $tab)
  110. {
  111. print "<table width=\"100%\" cellspacing=\"0\">\n";
  112. print "<caption>$caption</caption>\n";
  113. print "<tr>";
  114. print "<th class=\"label\" style=\"width:120px;\">&nbsp;</th>";
  115. print "<th class=\"label\">".T('In')."</th>";
  116. print "<th class=\"label\">".T('Out')."</th>";
  117. print "<th class=\"label\">".T('Total')."</th>";
  118. print "</tr>\n";
  119. for ($i=0; $i<count($tab); $i++)
  120. {
  121. if ($tab[$i]['act'] == 1)
  122. {
  123. $t = $tab[$i]['label'];
  124. $rx = kbytes_to_string($tab[$i]['rx']);
  125. $tx = kbytes_to_string($tab[$i]['tx']);
  126. $total = kbytes_to_string($tab[$i]['rx']+$tab[$i]['tx']);
  127. $id = ($i & 1) ? 'odd' : 'even';
  128. print "<tr>";
  129. print "<td class=\"label_$id\">$t</td>";
  130. print "<td class=\"numeric_$id\">$rx</td>";
  131. print "<td class=\"numeric_$id\">$tx</td>";
  132. print "<td class=\"numeric_$id\">$total</td>";
  133. print "</tr>\n";
  134. }
  135. }
  136. print "</table>\n";
  137. }
  138. get_vnstat_data();
  139. //
  140. // html start
  141. //
  142. header('Content-type: text/html; charset=utf-8');
  143. print '<?xml version="1.0"?>';
  144. ?>
  145. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  146. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  147. <head>
  148. <title>vnStat - PHP frontend</title>
  149. <link rel="stylesheet" type="text/css" href="themes/<?php echo $style ?>/style.css"/>
  150. <!-- 添加 Bootstrap 5 -->
  151. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  152. <!-- 添加一些现代图标 -->
  153. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
  154. </head>
  155. <body>
  156. <div id="wrap">
  157. <div id="sidebar"><?php write_side_bar(); ?></div>
  158. <div id="content">
  159. <div id="header"><?php print T('Traffic data for').(isset($iface_title[$iface]) ? $iface_title[$iface] : '')." ($iface)";?></div>
  160. <div id="main">
  161. <?php
  162. $graph_params = "if=$iface&amp;page=$page&amp;style=$style";
  163. if ($page != 's')
  164. if ($graph_format == 'svg') {
  165. print "<object type=\"image/svg+xml\" width=\"692\" height=\"297\" data=\"graph_svg.php?$graph_params\"></object>\n";
  166. } else {
  167. print "<img src=\"graph.php?$graph_params\" alt=\"graph\"/>\n";
  168. }
  169. if ($page == 's')
  170. {
  171. write_summary();
  172. }
  173. else if ($page == 'h')
  174. {
  175. write_data_table(T('Last 24 hours'), $hour);
  176. }
  177. else if ($page == 'd')
  178. {
  179. write_data_table(T('Last 30 days'), $day);
  180. }
  181. else if ($page == 'm')
  182. {
  183. write_data_table(T('Last 12 months'), $month);
  184. }
  185. ?>
  186. </div>
  187. <div id="footer"><a href="http://www.sqweek.com/">vnStat PHP frontend</a> 2.0.0 - &copy;2006-2024 Bjorge Dijkstra (bjd _at_ jooz.net)</div>
  188. </div>
  189. </div>
  190. </body></html>