widget_traffic.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class widget_traffic extends TaskbarWidget {
  3. /**
  4. * Caching object placeholder
  5. *
  6. * @var object
  7. */
  8. protected $cache = '';
  9. /**
  10. * Caching timeout in seconds
  11. *
  12. * @var int
  13. */
  14. protected $timeout = 3600;
  15. public function __construct() {
  16. }
  17. /**
  18. * Initalizes system cache object for further usage
  19. *
  20. * @return void
  21. */
  22. protected function initCache() {
  23. $this->cache = new UbillingCache();
  24. }
  25. /**
  26. * Render traffic data
  27. *
  28. * @return string
  29. */
  30. public function getTraffic() {
  31. $queryDown = "SELECT SUM(D0+D1+D2+D3+D4+D5+D6+D7+D8+D9) as `downloaded` from `users`";
  32. $dataDown = simple_query($queryDown);
  33. $queryUp = "SELECT SUM(U0+U1+U2+U3+U4+U5+U6+U7+U8+U9) as `uploaded` from `users`";
  34. $dataUp = simple_query($queryUp);
  35. $result = __('Traffic') . ': ' . __('Downloaded') . ' - ' . stg_convert_size($dataDown['downloaded']) . ', ' . __('Uploaded') . ' - ' . stg_convert_size($dataUp['uploaded']);
  36. $result = $this->widgetContainer($result);
  37. return ($result);
  38. }
  39. /**
  40. * Widget data with caching
  41. *
  42. * @return string
  43. */
  44. public function render() {
  45. $result = '';
  46. $this->initCache();
  47. $obj = $this;
  48. $result = $this->cache->getCallback('WIDGET_TRAFFIC', function() use ($obj) {
  49. return ($obj->getTraffic());
  50. }, $this->timeout);
  51. return ($result);
  52. }
  53. }
  54. ?>