api.uhw.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. //reads uhw config file
  3. function uhw_LoadConfig() {
  4. $path = "config/uhw.ini";
  5. $result = parse_ini_file($path);
  6. return ($result);
  7. }
  8. /**
  9. * Checks for substring in string
  10. *
  11. * @param string $string
  12. * @param string $search
  13. * @return bool
  14. */
  15. function ispos($string, $search) {
  16. if (strpos($string, $search) === false) {
  17. return(false);
  18. } else {
  19. return(true);
  20. }
  21. }
  22. //parse mac from a string
  23. function uhw_MacParse($string) {
  24. preg_match('/[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}/i', $string, $macdetect);
  25. if (!empty($macdetect)) {
  26. return ($macdetect[0]);
  27. } else {
  28. return (false);
  29. }
  30. }
  31. // butify mac that will be displayed
  32. function uhw_MacDisplay($mac) {
  33. $uconf = uhw_LoadConfig();
  34. if ($uconf['HIDE_DOTS']) {
  35. $mac = str_replace(':', '', $mac);
  36. }
  37. if ($uconf['SHOW_MAC_SIZE']) {
  38. $mac = substr($mac, '-' . $uconf['SHOW_MAC_SIZE']);
  39. }
  40. print('<font color="#FF0000">' . $mac . '</font>');
  41. }
  42. //isp site redirect
  43. function uhw_redirect($url) {
  44. $redirect = '<script type="text/javascript">
  45. <!--
  46. window.location = "' . $url . '"
  47. //-->
  48. </script>
  49. ';
  50. die($redirect);
  51. }
  52. function uhw_IsAllPasswordsUnique() {
  53. $query_u = "SELECT COUNT(`login`) from `users`";
  54. $userdata = simple_query($query_u);
  55. $usercount = $userdata['COUNT(`login`)'];
  56. $query_p = "SELECT DISTINCT `Password` from `users`";
  57. $passwdata = simple_queryall($query_p);
  58. $passwordcount = sizeof($passwdata);
  59. if ($usercount == $passwordcount) {
  60. return (true);
  61. } else {
  62. return (false);
  63. }
  64. }
  65. //find mac for current user ip by mask
  66. function uhw_FindMac($ip) {
  67. $uconf = uhw_LoadConfig();
  68. $sudo_path = $uconf['SUDO_PATH'];
  69. $cat_path = $uconf['CAT_PATH'];
  70. $logpath = $uconf['LOG_PATH'];
  71. $tail_path = $uconf['TAIL_PATH'];
  72. $grep_path = $uconf['GREP_PATH'];
  73. $unknown_mask = $uconf['UNKNOWN_MASK'];
  74. $unknown_lease = $uconf['UNKNOWN_LEASE'];
  75. $raw = shell_exec($sudo_path . ' ' . $cat_path . ' ' . $logpath . ' | ' . $grep_path . ' "' . $unknown_lease . $ip . ' " | ' . $tail_path . ' -n1');
  76. if (!empty($raw)) {
  77. $mac_detect = uhw_MacParse($raw);
  78. if ($mac_detect) {
  79. return ($mac_detect);
  80. } else {
  81. return (false);
  82. }
  83. } else {
  84. return (false);
  85. }
  86. }
  87. function uhw_modal($link, $title, $content, $linkclass = '', $width = '', $height = '') {
  88. $wid = rand(0, 99999);
  89. //setting link class
  90. if ($linkclass != '') {
  91. $link_class = 'class="' . $linkclass . '"';
  92. } else {
  93. $link_class = '';
  94. }
  95. //setting auto width if not specified
  96. if ($width == '') {
  97. $width = '600';
  98. }
  99. //setting auto width if not specified
  100. if ($height == '') {
  101. $height = '400';
  102. }
  103. $dialog = '
  104. <script type="text/javascript">
  105. $(function() {
  106. $( "#dialog-modal_' . $wid . '" ).dialog({
  107. autoOpen: false,
  108. width: ' . $width . ',
  109. height: ' . $height . ',
  110. modal: true,
  111. show: "drop",
  112. hide: "fold"
  113. });
  114. $( "#opener_' . $wid . '" ).click(function() {
  115. $( "#dialog-modal_' . $wid . '" ).dialog( "open" );
  116. return false;
  117. });
  118. });
  119. </script>
  120. <div id="dialog-modal_' . $wid . '" title="' . $title . '" style="display:none; width:1px; height:1px;">
  121. <p>
  122. ' . $content . '
  123. </p>
  124. </div>
  125. <a href="#" id="opener_' . $wid . '" ' . $link_class . '>' . $link . '</a>
  126. ';
  127. return($dialog);
  128. }
  129. function uhw_modal_open($title, $content, $width = '', $height = '') {
  130. $wid = rand(0, 99999);
  131. //setting auto width if not specified
  132. if ($width == '') {
  133. $width = '600';
  134. }
  135. //setting auto width if not specified
  136. if ($height == '') {
  137. $height = '400';
  138. }
  139. $dialog = '
  140. <script type="text/javascript">
  141. $(function() {
  142. $( "#dialog-modal_' . $wid . '" ).dialog({
  143. autoOpen: true,
  144. width: ' . $width . ',
  145. height: ' . $height . ',
  146. modal: true,
  147. show: "drop",
  148. hide: "fold"
  149. });
  150. $( "#opener_' . $wid . '" ).click(function() {
  151. $( "#dialog-modal_' . $wid . '" ).dialog( "open" );
  152. return false;
  153. });
  154. });
  155. </script>
  156. <div id="dialog-modal_' . $wid . '" title="' . $title . '" style="display:none; width:1px; height:1px;">
  157. <p>
  158. ' . $content . '
  159. </p>
  160. </div>
  161. ';
  162. return($dialog);
  163. }
  164. function uhw_PasswordForm($uconf) {
  165. $form = '<form action="" method="POST" class="glamour">';
  166. if ($uconf['USE_LOGIN']) {
  167. $form .= '<label for="loginfield">' . $uconf['SUP_LOGIN'] . '</label> <input type="text" name="login" id="loginfield" size="16" style="margin-left: 12px;"><br /><br />';
  168. }
  169. $form .= '<label for="passfield">' . $uconf['SUP_PASS'] . '</label> <input type="' . $uconf['SELFACT_FIELDTYPE'] . '" name="password" id="passfield" size="16">
  170. <br>
  171. <br>
  172. <input type="submit" value="' . $uconf['SUP_ACTIVATE_QUERY'] . '">
  173. </form>
  174. <div style="clear:both;"></div>
  175. <br><br>
  176. ' . $uconf['SUP_PASSNOTICE'] . '
  177. ';
  178. $result = '<br><br><br>';
  179. $result .= uhw_modal($uconf['SUP_SELFACT'], $uconf['SUP_SELFACT'], $form, 'ubButton', '600', '400');
  180. print($result);
  181. }
  182. function uhw_IsMacUnique($mac) {
  183. $mac = vf($mac);
  184. $mac = strtolower($mac);
  185. $query = "SELECT `id` from `nethosts` WHERE `mac`='" . $mac . "'";
  186. $data = simple_query($query);
  187. if ($mac == '00:00:00:00:00:00') {
  188. return (false);
  189. }
  190. if (empty($data)) {
  191. return (true);
  192. } else {
  193. return (false);
  194. }
  195. }
  196. function uhw_FindUserByPassword($password, $login = '') {
  197. global $uconf;
  198. $result = '';
  199. $password = mysql_real_escape_string($password);
  200. if ($uconf['USE_LOGIN'] and ! empty($login)) {
  201. $login = mysql_real_escape_string($login);
  202. $query = "SELECT `login` from `users` WHERE `Password`='" . $password . "'";
  203. $query .= " AND `login` = '" . $login . "'";
  204. $result = simple_query($query);
  205. } else {
  206. $query = "SELECT `login` from `users` WHERE `Password`='" . $password . "'";
  207. $result = simple_query($query);
  208. }
  209. if (!empty($result)) {
  210. return ($result['login']);
  211. } else {
  212. return(false);
  213. }
  214. }
  215. function uhw_UserGetIp($login) {
  216. $query = "SELECT `IP` from `users` WHERE `login`='" . $login . "'";
  217. $result = simple_query($query);
  218. if (!empty($result)) {
  219. return ($result['IP']);
  220. } else {
  221. return (false);
  222. }
  223. }
  224. function uhw_NethostGetID($ip) {
  225. $query = "SELECT `id` from `nethosts` WHERE `ip`='" . $ip . "'";
  226. $result = simple_query($query);
  227. if (!empty($result)) {
  228. return ($result['id']);
  229. } else {
  230. return (false);
  231. }
  232. }
  233. function uhw_NethostGetMac($nethostid) {
  234. $query = "SELECT `mac` from `nethosts` WHERE `id`='" . $nethostid . "'";
  235. $result = simple_query($query);
  236. if (!empty($result)) {
  237. return ($result['mac']);
  238. } else {
  239. return (false);
  240. }
  241. }
  242. function uhw_ub_log_register($event) {
  243. $admin_login = 'external';
  244. $ip = '127.0.0.1';
  245. $current_time = date("Y-m-d H:i:s");
  246. $event = mysql_real_escape_string($event);
  247. $query = "INSERT INTO `weblogs` (`id`,`date`,`admin`,`ip`,`event`) VALUES(NULL,'" . $current_time . "','" . $admin_login . "','" . $ip . "','" . $event . "')";
  248. nr_query($query);
  249. }
  250. function uhw_LogSelfact($trypassword, $login, $tryip, $nethostid, $oldmac, $newmac) {
  251. $date = date("Y-m-d H:i:s");
  252. $query = "INSERT INTO `uhw_log` (
  253. `id` ,
  254. `date` ,
  255. `password` ,
  256. `login` ,
  257. `ip` ,
  258. `nhid` ,
  259. `oldmac` ,
  260. `newmac`
  261. )
  262. VALUES (
  263. NULL , '" . $date . "', '" . $trypassword . "', '" . $login . "', '" . $tryip . "', '" . $nethostid . "', '" . $oldmac . "', '" . $newmac . "'
  264. );";
  265. nr_query($query);
  266. //put ubilling log entry
  267. uhw_ub_log_register("UHW CHANGE (" . $login . ") MAC FROM " . $oldmac . " ON " . $newmac);
  268. }
  269. function uhw_GetBrute($mac) {
  270. $query = "SELECT COUNT(`id`) from `uhw_brute` WHERE `mac`='" . $mac . "'";
  271. $data = simple_query($query);
  272. return ($data['COUNT(`id`)']);
  273. }
  274. function uhw_LogBrute($password, $mac, $login = '') {
  275. $password = mysql_real_escape_string($password);
  276. $login = mysql_real_escape_string($login);
  277. $date = date("Y-m-d H:i:s");
  278. $query = "INSERT INTO `uhw_brute` (
  279. `id` ,
  280. `date` ,
  281. `password` ,
  282. `mac` ,
  283. `login`
  284. )
  285. VALUES (
  286. NULL , '" . $date . "', '" . $password . "', '" . $mac . "', '" . $login . "'
  287. );";
  288. nr_query($query);
  289. }
  290. function uhw_ChangeMac($nethost_id, $newmac) {
  291. $newmac = strtolower($newmac);
  292. simple_update_field('nethosts', 'mac', $newmac, "WHERE `id`='" . $nethost_id . "'");
  293. }
  294. function uhw_RemoteApiPush($url, $serial, $action, $param = '') {
  295. $getdata = http_build_query(
  296. array(
  297. 'module' => 'remoteapi',
  298. 'key' => $serial,
  299. 'action' => $action,
  300. 'param' => $param
  301. )
  302. );
  303. $opts = array('http' =>
  304. array(
  305. 'method' => 'GET',
  306. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  307. 'content' => $getdata
  308. )
  309. );
  310. $context = stream_context_create($opts);
  311. @$result = file_get_contents($url . '?' . $getdata, false, $context);
  312. return ($result);
  313. }
  314. ?>