api.uhw.php 9.7 KB

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