api.uhw.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Unknown Hardware Helper
  4. */
  5. class UHW {
  6. /**
  7. * log database abstraction layer
  8. *
  9. * @var object
  10. */
  11. protected $logDb = '';
  12. /**
  13. * Brute attempts database abstraction layer
  14. *
  15. * @var object
  16. */
  17. protected $bruteDb = '';
  18. /**
  19. * System message helper placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. //some predefined stuff
  25. const TABLE_LOG = 'uhw_log';
  26. const TABLE_BRUTE = 'uhw_brute';
  27. const URL_ME = '?module=uhw';
  28. const ROUTE_BRUTE_SHOW = 'showbrute';
  29. const ROUTE_BRUTE_DEL = 'delbrute';
  30. const ROUTE_BRUTE_FLUSH = 'cleanallbrute';
  31. const ROUTE_LOGIN = 'username';
  32. const ROUTE_AJAX_LOG='ajax';
  33. public function __construct() {
  34. $this->initMessages();
  35. $this->initDbs();
  36. }
  37. /**
  38. * Inits message helper instance
  39. *
  40. * @return void
  41. */
  42. protected function initMessages() {
  43. $this->messages = new UbillingMessageHelper();
  44. }
  45. /**
  46. * Inits some database abstraction layers
  47. *
  48. * @return void
  49. */
  50. protected function initDbs() {
  51. $this->logDb = new NyanORM(self::TABLE_LOG);
  52. $this->bruteDb = new NyanORM(self::TABLE_BRUTE);
  53. }
  54. /**
  55. * Returns UHW control panel widget
  56. *
  57. * @return string
  58. */
  59. public function panel() {
  60. $result = '';
  61. if (!ubRouting::checkGet(self::ROUTE_LOGIN)) {
  62. $result = wf_Link(self::URL_ME, wf_img('skins/ukv/report.png') . ' ' . __('Usage report'), false, 'ubButton');
  63. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_BRUTE_SHOW . '=true', wf_img('skins/icon_key.gif') . ' ' . __('Brute attempts'), false, 'ubButton');
  64. }
  65. return ($result);
  66. }
  67. /**
  68. * Returns JSON reply for jquery datatables with full list of available UHW usages
  69. *
  70. * @param string $loginFilter
  71. *
  72. * @return void
  73. */
  74. public function ajaxGetData($loginFilter = '') {
  75. $loginFilter = ubRouting::filters($loginFilter, 'login');
  76. if (!empty($loginFilter)) {
  77. $this->logDb->where('login', '=', $loginFilter);
  78. }
  79. $this->logDb->orderBy('id', 'DESC');
  80. $all = $this->logDb->getAll();
  81. $alladdress = zb_AddressGetFulladdresslist();
  82. $allrealnames = zb_UserGetAllRealnames();
  83. $json = new wf_JqDtHelper();
  84. if (!empty($all)) {
  85. foreach ($all as $io => $each) {
  86. $profileLink = wf_Link(UserProfile::URL_PROFILE . $each['login'], web_profile_icon() . ' ' . $each['login'], false);
  87. $userAddress = @$alladdress[$each['login']];
  88. $userRealname = @$allrealnames[$each['login']];
  89. $data[] = $each['id'];
  90. $data[] = $each['date'];
  91. $data[] = $each['password'];
  92. $data[] = $profileLink;
  93. $data[] = $userAddress;
  94. $data[] = $userRealname;
  95. $data[] = $each['ip'];
  96. $data[] = $each['nhid'];
  97. $data[] = $each['oldmac'];
  98. $data[] = $each['newmac'];
  99. $json->addRow($data);
  100. unset($data);
  101. }
  102. }
  103. $json->getJson();
  104. }
  105. /**
  106. * Returns container of successful UHW usages
  107. *
  108. * @param string $searchLogin
  109. *
  110. * @return string
  111. */
  112. public function renderUsageList($searchLogin = '') {
  113. $result = '';
  114. $columns = array('ID', 'Date', 'Password', 'Login', 'Address', 'Real name', 'IP', 'NHID', 'Old MAC', 'New MAC');
  115. $opts = '"order": [[ 0, "desc" ]]';
  116. $loginFilter = (!empty($searchLogin)) ? '&'.self::ROUTE_LOGIN.'=' . $searchLogin : '';
  117. $result = wf_JqDtLoader($columns, self::URL_ME.'&'.self::ROUTE_AJAX_LOG.'=true' . $loginFilter, false, 'users', 100, $opts);
  118. return ($result);
  119. }
  120. /**
  121. * Deletes uhw brute attempt from DB by its id
  122. *
  123. * @param int $bruteid
  124. *
  125. * @return void
  126. */
  127. public function deleteBrute($bruteid) {
  128. $bruteid = ubRouting::filters($bruteid, 'int');
  129. $this->bruteDb->where('id','=',$bruteid);
  130. $this->bruteDb->delete();
  131. log_register('UHW BRUTE DELETE [' . $bruteid . ']');
  132. }
  133. /**
  134. * Flushes all UHW brute attempts
  135. *
  136. * @retrun void
  137. */
  138. public function flushAllBrute() {
  139. $query = "TRUNCATE TABLE `uhw_brute` ;";
  140. nr_query($query);
  141. log_register('UHW CLEANUP BRUTE');
  142. }
  143. /**
  144. * Shows list of available UHW brute attempts with cleanup controls
  145. *
  146. * @return string
  147. */
  148. public function renderBruteAttempts() {
  149. $this->bruteDb->orderBy('id','ASC');
  150. $allbrutes=$this->bruteDb->getAll();
  151. $tablecells = wf_TableCell(__('ID'));
  152. $tablecells .= wf_TableCell(__('Date'));
  153. $tablecells .= wf_TableCell(__('Password'));
  154. $tablecells .= wf_TableCell(__('Login'));
  155. $tablecells .= wf_TableCell(__('MAC'));
  156. $tablecells .= wf_TableCell(__('Actions'));
  157. $tablerows = wf_TableRow($tablecells, 'row1');
  158. if (!empty($allbrutes)) {
  159. foreach ($allbrutes as $io => $each) {
  160. $tablecells = wf_TableCell($each['id']);
  161. $tablecells .= wf_TableCell($each['date']);
  162. $tablecells .= wf_TableCell(strip_tags($each['password']));
  163. $tablecells .= wf_TableCell(strip_tags($each['login']));
  164. $tablecells .= wf_TableCell($each['mac']);
  165. $actlinks = wf_JSAlert(self::URL_ME.'&'.self::ROUTE_BRUTE_SHOW.'=true&'.self::ROUTE_BRUTE_DEL.'=' . $each['id'], web_delete_icon(), $this->messages->getEditAlert());
  166. $tablecells .= wf_TableCell($actlinks);
  167. $tablerows .= wf_TableRow($tablecells, 'row3');
  168. }
  169. }
  170. $result = wf_TableBody($tablerows, '100%', 0, 'sortable');
  171. return ($result);
  172. }
  173. }