index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. if (cfr('PLDETAILS')) {
  3. $altCfg = $ubillingConfig->getAlter();
  4. if (@$altCfg['DSTAT_ENABLED']) {
  5. if (isset($_GET['username'])) {
  6. $login = $_GET['username'];
  7. $userdata = zb_UserGetStargazerData($login);
  8. $cyear = curyear();
  9. $cmonth = date("m");
  10. $tablename = 'detailstat_' . $cmonth . '_' . $cyear . '';
  11. /**
  12. * Checks is detailstatstable exists
  13. *
  14. * @global string $tablename
  15. * @return bool
  16. */
  17. function ds_CheckTable() {
  18. global $tablename;
  19. $query = "SELECT CASE WHEN (SELECT COUNT(*) AS STATUS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = (SELECT DATABASE()) AND TABLE_NAME = '" . $tablename . "') = 1 THEN (SELECT 1) ELSE (SELECT 0) END AS result;";
  20. $result = simple_query($query);
  21. $result = ($result['result'] == 0) ? false : true;
  22. return ($result);
  23. }
  24. /**
  25. * Returns available days with stats from table
  26. *
  27. * @global string $tablename
  28. * @return array
  29. */
  30. function ds_GetDays() {
  31. global $tablename;
  32. $query = "SELECT DISTINCT `day` from `" . $tablename . "`";
  33. $alldays = simple_queryall($query);
  34. return($alldays);
  35. }
  36. /**
  37. * Returns stats by day for some user
  38. *
  39. * @global string $tablename
  40. * @param string $login
  41. * @param int $day
  42. * @param int $page
  43. * @return array
  44. */
  45. function ds_GetDayStats($login, $day, $page = 0) {
  46. global $tablename;
  47. $pagelimit = 100;
  48. $page = vf($page);
  49. $login = mysql_real_escape_string($login);
  50. $dey = vf($day);
  51. $query = "SELECT * from `" . $tablename . "` WHERE `login`='" . $login . "' AND `day`='" . $day . "' ORDER by `starttime` DESC";
  52. $daystats = simple_queryall($query);
  53. return($daystats);
  54. }
  55. /**
  56. * Returns count of summ dowloaded bytes by day for some user
  57. *
  58. * @global string $tablename
  59. * @param string $login
  60. * @param int $day
  61. * @return int
  62. */
  63. function ds_GetDownSumm($login, $day) {
  64. global $tablename;
  65. $login = vf($login);
  66. $day = vf($day);
  67. $query = "SELECT SUM(`down`) from `" . $tablename . "` WHERE `login`='" . $login . "' AND `day`='" . $day . "'";
  68. $summ = simple_query($query);
  69. return($summ['SUM(`down`)']);
  70. }
  71. /**
  72. * Returns count of summ uploaded bytes by day for some user
  73. *
  74. * @global string $tablename
  75. * @param string $login
  76. * @param int $day
  77. * @return int
  78. */
  79. function ds_GetUpSumm($login, $day) {
  80. global $tablename;
  81. $login = vf($login);
  82. $day = vf($day);
  83. $query = "SELECT SUM(`up`) from `" . $tablename . "` WHERE `login`='" . $login . "' AND `day`='" . $day . "'";
  84. $summ = simple_query($query);
  85. return($summ['SUM(`up`)']);
  86. }
  87. /**
  88. * Returns summ of money for some day used by user
  89. *
  90. * @global string $tablename
  91. * @param string $login
  92. * @param int $day
  93. * @return string
  94. */
  95. function ds_GetCashSumm($login, $day) {
  96. global $tablename;
  97. $login = vf($login);
  98. $day = vf($day);
  99. $query = "SELECT SUM(`cash`) from `" . $tablename . "` WHERE `login`='" . $login . "' AND `day`='" . $day . "'";
  100. $summ = simple_query($query);
  101. return($summ['SUM(`cash`)']);
  102. }
  103. /**
  104. * Renders stats for some user
  105. *
  106. * @param string $login
  107. * @return string
  108. */
  109. function web_DSShow($login) {
  110. $login = vf($login);
  111. $days = ds_GetDays();
  112. $result = '';
  113. $cells = wf_TableCell(__('Day'));
  114. $cells .= wf_TableCell(__('Downloaded'));
  115. $cells .= wf_TableCell(__('Uploaded'));
  116. $cells .= wf_TableCell(__('Cash'));
  117. $rows = wf_TableRow($cells, 'row1');
  118. if (!empty($days)) {
  119. foreach ($days as $io => $eachday) {
  120. $downsumm = ds_GetDownSumm($login, $eachday['day']);
  121. $upsumm = ds_GetUpSumm($login, $eachday['day']);
  122. $cashsumm = ds_GetCashSumm($login, $eachday['day']);
  123. if (!$downsumm) {
  124. $downsumm = 0;
  125. }
  126. if (!$upsumm) {
  127. $upsumm = 0;
  128. }
  129. $dayLink = wf_Link('?module=pl_traffdetails&username=' . $login . '&day=' . $eachday['day'], $eachday['day']);
  130. $cells = wf_TableCell($dayLink);
  131. $cells .= wf_TableCell(stg_convert_size($downsumm), '', '', 'sorttable_customkey="' . $downsumm . '"');
  132. $cells .= wf_TableCell(stg_convert_size($upsumm), '', '', 'sorttable_customkey="' . $upsumm . '"');
  133. $cells .= wf_TableCell(round($cashsumm, 2));
  134. $rows .= wf_TableRow($cells, 'row3');
  135. }
  136. }
  137. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  138. return($result);
  139. }
  140. /**
  141. *
  142. * @param type $login
  143. * @param type $day
  144. * @param int $page
  145. * @return string
  146. */
  147. function web_DSShowDayStats($login, $day, $page = 0) {
  148. $traffclasse_raw = zb_DirectionsGetAll();
  149. $resolveFlag = (wf_CheckGet(array('fastresolve'))) ? true : false;
  150. if ($resolveFlag) {
  151. $cache = new UbillingCache();
  152. $cacheTime = 2592000;
  153. $cachedData = $cache->get('RESOLVE', $cacheTime);
  154. }
  155. $tc = array();
  156. if (!empty($traffclasse_raw)) {
  157. foreach ($traffclasse_raw as $io => $eachtc) {
  158. $tc[$eachtc['rulenumber']] = $eachtc['rulename'];
  159. }
  160. }
  161. $login = mysql_real_escape_string($login);
  162. $page = vf($page);
  163. $day = vf($day);
  164. $daystats = ds_GetDayStats($login, $day, $page);
  165. $result = '';
  166. $cells = wf_TableCell(__('Session start'));
  167. $cells .= wf_TableCell(__('Session end'));
  168. $cells .= wf_TableCell(__('IP'));
  169. if ($resolveFlag) {
  170. $cells .= wf_TableCell(__('Host'));
  171. }
  172. $cells .= wf_TableCell(__('Traffic classes'));
  173. $cells .= wf_TableCell(__('Downloaded') . '/' . __('Uploaded'));
  174. $cells .= wf_TableCell(__('Cash'));
  175. $rows = wf_TableRow($cells, 'row1');
  176. if (!empty($daystats)) {
  177. foreach ($daystats as $io => $eachtraff) {
  178. $cells = wf_TableCell($eachtraff['startTime']);
  179. $cells .= wf_TableCell($eachtraff['endTime']);
  180. $whoisLink = wf_Link('?module=whois&ip=' . $eachtraff['IP'], wf_img('skins/icon_whois_small.png', __('Whois')));
  181. $webLink = wf_Link('http://' . $eachtraff['IP'], $eachtraff['IP']);
  182. $cells .= wf_TableCell($whoisLink . ' ' . $webLink);
  183. if ($resolveFlag) {
  184. if (!empty($cachedData)) {
  185. if (isset($cachedData[$eachtraff['IP']])) {
  186. $resolvedHostname = $cachedData[$eachtraff['IP']];
  187. } else {
  188. $resolvedHostname = gethostbyaddr($eachtraff['IP']);
  189. $cachedData[$eachtraff['IP']] = $resolvedHostname;
  190. }
  191. } else {
  192. $resolvedHostname = gethostbyaddr($eachtraff['IP']);
  193. $cachedData[$eachtraff['IP']] = $resolvedHostname;
  194. }
  195. $cells .= wf_TableCell($resolvedHostname);
  196. }
  197. $cells .= wf_TableCell(@$tc[$eachtraff['dir']]);
  198. $cells .= wf_TableCell(stg_convert_size($eachtraff['down']) . ' / ' . stg_convert_size($eachtraff['up']), '', '', 'sorttable_customkey="' . ($eachtraff['down'] + $eachtraff['up']) . '"');
  199. $cells .= wf_TableCell(round($eachtraff['cash'], 3));
  200. $rows .= wf_TableRow($cells, 'row3');
  201. }
  202. //saving data to cache
  203. if ($resolveFlag) {
  204. $cache->set('RESOLVE', $cachedData, $cacheTime);
  205. }
  206. }
  207. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  208. return ($result);
  209. }
  210. if (ds_CheckTable()) {
  211. show_window(__('Traffic detailed stats'), web_DSShow($login));
  212. } else {
  213. show_error(__('No detailstats database exists'));
  214. }
  215. if (isset($_GET['day'])) {
  216. $day = $_GET['day'];
  217. $resolveControl = wf_Link('?module=pl_traffdetails&username=' . $login . '&day=' . $day . '&fastresolve=true', wf_img('skins/icon_whois_small.png', __('Resolve hostnames')));
  218. show_window(__('Detailed stats by day') . ' ' . $resolveControl, web_DSShowDayStats($login, $day));
  219. }
  220. show_window('', web_UserControls($login));
  221. }
  222. } else {
  223. show_error(__('This module is disabled'));
  224. }
  225. } else {
  226. show_error(__('You cant control this module'));
  227. }
  228. ?>