api.uhw.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. /*
  69. $sudo_path = $uconf['SUDO_PATH'];
  70. $cat_path = $uconf['CAT_PATH'];
  71. $logpath = $uconf['LOG_PATH'];
  72. $tail_path = $uconf['TAIL_PATH'];
  73. $grep_path = $uconf['GREP_PATH'];
  74. $unknown_mask = $uconf['UNKNOWN_MASK'];
  75. $unknown_lease = $uconf['UNKNOWN_LEASE'];
  76. *
  77. */
  78. $macField = $uconf['MAC_FIELD'];
  79. $query = 'SELECT `framedipaddress`,`' . $macField . '` FROM `mlg_acct` WHERE `framedipaddress`="' . $ip . '" ORDER BY `radacctid` DESC LIMIT 1';
  80. $raw = simple_query($query);
  81. //$raw = shell_exec($sudo_path . ' ' . $cat_path . ' ' . $logpath . ' | ' . $grep_path . ' "' . $unknown_lease . $ip . ' " | ' . $tail_path . ' -n1');
  82. if (!empty($raw)) {
  83. $mac_detect = uhw_MacParse(preg_replace('/([a-f0-9]{2})(?![\s\]\/])([\.\:\-]?)/', '\1:', $raw[$macField]));
  84. if ($mac_detect) {
  85. return ($mac_detect);
  86. }
  87. }
  88. return(false);
  89. }
  90. function uhw_modal($link, $title, $content, $linkclass = '', $width = '', $height = '') {
  91. $wid = rand(0, 99999);
  92. //setting link class
  93. if ($linkclass != '') {
  94. $link_class = 'class="' . $linkclass . '"';
  95. } else {
  96. $link_class = '';
  97. }
  98. //setting auto width if not specified
  99. if ($width == '') {
  100. $width = '600';
  101. }
  102. //setting auto width if not specified
  103. if ($height == '') {
  104. $height = '400';
  105. }
  106. $dialog = '
  107. <script type="text/javascript">
  108. $(function() {
  109. $( "#dialog-modal_' . $wid . '" ).dialog({
  110. autoOpen: false,
  111. width: ' . $width . ',
  112. height: ' . $height . ',
  113. modal: true,
  114. show: "drop",
  115. hide: "fold"
  116. });
  117. $( "#opener_' . $wid . '" ).click(function() {
  118. $( "#dialog-modal_' . $wid . '" ).dialog( "open" );
  119. return false;
  120. });
  121. });
  122. </script>
  123. <div id="dialog-modal_' . $wid . '" title="' . $title . '" style="display:none; width:1px; height:1px;">
  124. <p>
  125. ' . $content . '
  126. </p>
  127. </div>
  128. <a href="#" id="opener_' . $wid . '" ' . $link_class . '>' . $link . '</a>
  129. ';
  130. return($dialog);
  131. }
  132. function uhw_modal_open($title, $content, $width = '', $height = '') {
  133. $wid = rand(0, 99999);
  134. //setting auto width if not specified
  135. if ($width == '') {
  136. $width = '600';
  137. }
  138. //setting auto width if not specified
  139. if ($height == '') {
  140. $height = '400';
  141. }
  142. $dialog = '
  143. <script type="text/javascript">
  144. $(function() {
  145. $( "#dialog-modal_' . $wid . '" ).dialog({
  146. autoOpen: true,
  147. width: ' . $width . ',
  148. height: ' . $height . ',
  149. modal: true,
  150. show: "drop",
  151. hide: "fold"
  152. });
  153. $( "#opener_' . $wid . '" ).click(function() {
  154. $( "#dialog-modal_' . $wid . '" ).dialog( "open" );
  155. return false;
  156. });
  157. });
  158. </script>
  159. <div id="dialog-modal_' . $wid . '" title="' . $title . '" style="display:none; width:1px; height:1px;">
  160. <p>
  161. ' . $content . '
  162. </p>
  163. </div>
  164. ';
  165. return($dialog);
  166. }
  167. function uhw_PasswordForm($uconf) {
  168. $form = '<form action="" method="POST" class="glamour">';
  169. if ($uconf['USE_LOGIN']) {
  170. $form .= '<label for="loginfield">' . $uconf['SUP_LOGIN'] . '</label> <input type="text" name="login" id="loginfield" size="16" style="margin-left: 12px;"><br /><br />';
  171. }
  172. $form .= '<label for="passfield">' . $uconf['SUP_PASS'] . '</label> <input type="' . $uconf['SELFACT_FIELDTYPE'] . '" name="password" id="passfield" size="16">
  173. <br>
  174. <br>
  175. <input type="submit" value="' . $uconf['SUP_ACTIVATE_QUERY'] . '">
  176. </form>
  177. <div style="clear:both;"></div>
  178. <br><br>
  179. ' . $uconf['SUP_PASSNOTICE'] . '
  180. ';
  181. $result = '<br><br><br>';
  182. $result .= uhw_modal($uconf['SUP_SELFACT'], $uconf['SUP_SELFACT'], $form, 'ubButton', '600', '400');
  183. print($result);
  184. }
  185. function uhw_IsMacUnique($mac) {
  186. $mac = vf($mac);
  187. $mac = strtolower($mac);
  188. $query = "SELECT `id` from `nethosts` WHERE `mac`='" . $mac . "'";
  189. $data = simple_query($query);
  190. if ($mac == '00:00:00:00:00:00') {
  191. return (false);
  192. }
  193. if (empty($data)) {
  194. return (true);
  195. } else {
  196. return (false);
  197. }
  198. }
  199. function uhw_FindUserByPassword($password, $login = '') {
  200. global $uconf;
  201. $result = '';
  202. $password = mysql_real_escape_string($password);
  203. if ($uconf['USE_LOGIN'] and ! empty($login)) {
  204. $login = mysql_real_escape_string($login);
  205. $query = "SELECT `login` from `users` WHERE `Password`='" . $password . "'";
  206. $query .= " AND `login` = '" . $login . "'";
  207. $result = simple_query($query);
  208. } else {
  209. $query = "SELECT `login` from `users` WHERE `Password`='" . $password . "'";
  210. $result = simple_query($query);
  211. }
  212. if (!empty($result)) {
  213. return ($result['login']);
  214. } else {
  215. return(false);
  216. }
  217. }
  218. function uhw_UserGetIp($login) {
  219. $query = "SELECT `IP` from `users` WHERE `login`='" . $login . "'";
  220. $result = simple_query($query);
  221. if (!empty($result)) {
  222. return ($result['IP']);
  223. } else {
  224. return (false);
  225. }
  226. }
  227. function uhw_NethostGetID($ip) {
  228. $query = "SELECT `id` from `nethosts` WHERE `ip`='" . $ip . "'";
  229. $result = simple_query($query);
  230. if (!empty($result)) {
  231. return ($result['id']);
  232. } else {
  233. return (false);
  234. }
  235. }
  236. function uhw_NethostGetMac($nethostid) {
  237. $query = "SELECT `mac` from `nethosts` WHERE `id`='" . $nethostid . "'";
  238. $result = simple_query($query);
  239. if (!empty($result)) {
  240. return ($result['mac']);
  241. } else {
  242. return (false);
  243. }
  244. }
  245. function uhw_ub_log_register($event) {
  246. $admin_login = 'external';
  247. $ip = '127.0.0.1';
  248. $current_time = date("Y-m-d H:i:s");
  249. $event = mysql_real_escape_string($event);
  250. $query = "INSERT INTO `weblogs` (`id`,`date`,`admin`,`ip`,`event`) VALUES(NULL,'" . $current_time . "','" . $admin_login . "','" . $ip . "','" . $event . "')";
  251. nr_query($query);
  252. }
  253. function uhw_LogSelfact($trypassword, $login, $tryip, $nethostid, $oldmac, $newmac) {
  254. $date = date("Y-m-d H:i:s");
  255. $query = "INSERT INTO `uhw_log` (
  256. `id` ,
  257. `date` ,
  258. `password` ,
  259. `login` ,
  260. `ip` ,
  261. `nhid` ,
  262. `oldmac` ,
  263. `newmac`
  264. )
  265. VALUES (
  266. NULL , '" . $date . "', '" . $trypassword . "', '" . $login . "', '" . $tryip . "', '" . $nethostid . "', '" . $oldmac . "', '" . $newmac . "'
  267. );";
  268. nr_query($query);
  269. //put ubilling log entry
  270. uhw_ub_log_register("UHW CHANGE (" . $login . ") MAC FROM " . $oldmac . " ON " . $newmac);
  271. }
  272. function uhw_GetBrute($mac) {
  273. $query = "SELECT COUNT(`id`) from `uhw_brute` WHERE `mac`='" . $mac . "'";
  274. $data = simple_query($query);
  275. return ($data['COUNT(`id`)']);
  276. }
  277. function uhw_LogBrute($password, $mac, $login = '') {
  278. $password = mysql_real_escape_string($password);
  279. $login = mysql_real_escape_string($login);
  280. $date = date("Y-m-d H:i:s");
  281. $query = "INSERT INTO `uhw_brute` (
  282. `id` ,
  283. `date` ,
  284. `password` ,
  285. `mac` ,
  286. `login`
  287. )
  288. VALUES (
  289. NULL , '" . $date . "', '" . $password . "', '" . $mac . "', '" . $login . "'
  290. );";
  291. nr_query($query);
  292. }
  293. function uhw_ChangeMac($nethost_id, $newmac, $oldmac) {
  294. $uconf = uhw_LoadConfig();
  295. $newmac = strtolower($newmac);
  296. $oldmac = strtolower($oldmac);
  297. switch ($uconf['MAC_FORMAT']) {
  298. case 'MAC':
  299. $mlg_mac = $newmac;
  300. $mlg_old_mac = $oldmac;
  301. break;
  302. case 'MACFDL':
  303. $mlg_mac = transformMacDotted($newmac);
  304. $mlg_old_mac = transformMacDotted($oldmac);
  305. break;
  306. case 'MACFML':
  307. $mlg_mac = str_replace('.', '-', transformMacDotted($newmac));
  308. $mlg_old_mac = str_replace('.', '-', transformMacDotted($oldmac));
  309. break;
  310. case 'MACTMU':
  311. $mlg_mac = transformMacMinused($newmac, true);
  312. $mlg_old_mac = transformMacMinused($oldmac, true);
  313. break;
  314. case 'MACTML':
  315. $mlg_mac = transformMacMinused($newmac, false);
  316. $mlg_old_mac = transformMacMinused($oldmac, false);
  317. break;
  318. default :
  319. $mlg_mac = $newmac;
  320. $mlg_old_mac = $oldmac;
  321. break;
  322. }
  323. simple_update_field('mlg_check', 'username', $mlg_mac, 'WHERE `username`="' . $mlg_old_mac . '"');
  324. simple_update_field('mlg_reply', 'username', $mlg_mac, 'WHERE `username`="' . $mlg_old_mac . '"');
  325. simple_update_field('mlg_groupreply', 'username', $mlg_mac, 'WHERE `username`="' . $mlg_old_mac . '"');
  326. simple_update_field('nethosts', 'mac', $newmac, "WHERE `id`='" . $nethost_id . "'");
  327. }
  328. function uhw_RemoteApiPush($url, $serial, $action, $param = '') {
  329. $getdata = http_build_query(
  330. array(
  331. 'module' => 'remoteapi',
  332. 'key' => $serial,
  333. 'action' => $action,
  334. 'param' => $param
  335. )
  336. );
  337. $opts = array('http' =>
  338. array(
  339. 'method' => 'GET',
  340. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  341. 'content' => $getdata
  342. )
  343. );
  344. $context = stream_context_create($opts);
  345. @$result = file_get_contents($url . '?' . $getdata, false, $context);
  346. return ($result);
  347. }
  348. function transformMacDotted($mac) {
  349. $result = implode(".", str_split(str_replace(":", "", $mac), 4));
  350. return ($result);
  351. }
  352. function transformMacMinused($mac, $caps = false) {
  353. $result = str_replace(':', '-', $mac);
  354. if ($caps) {
  355. $result = strtoupper($result);
  356. }
  357. return ($result);
  358. }
  359. ?>