PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/ipsearch.php

https://github.com/Bigjoos/U-232-V1
PHP | 255 lines | 217 code | 29 blank | 9 comment | 32 complexity | f0f9d59e5ee178cfa4caa3bbf001f5cf MD5 | raw file
  1. <?php
  2. /**
  3. * https://09source.kicks-ass.net:8443/svn/installer09/
  4. * Licence Info: GPL
  5. * Copyright (C) 2010 Installer09 v.1
  6. * A bittorrent tracker source based on TBDev.net/tbsource/bytemonsoon.
  7. * Project Leaders: Mindless,putyn,kidvision.
  8. **/
  9. if ( ! defined( 'IN_TBDEV_ADMIN' ) )
  10. {
  11. $HTMLOUT='';
  12. $HTMLOUT .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
  13. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
  14. <html xmlns='http://www.w3.org/1999/xhtml'>
  15. <head>
  16. <title>Error!</title>
  17. </head>
  18. <body>
  19. <div style='font-size:33px;color:white;background-color:red;text-align:center;'>Incorrect access<br />You cannot access this file directly.</div>
  20. </body></html>";
  21. print $HTMLOUT;
  22. exit();
  23. }
  24. require_once(INCL_DIR.'user_functions.php');
  25. require_once(INCL_DIR.'html_functions.php');
  26. require_once(INCL_DIR.'pager_functions.php');
  27. $HTMLOUT ="";
  28. $ip="";
  29. $mask="";
  30. $lang = array_merge( $lang );
  31. require_once(INCL_DIR.'class_check.php');
  32. class_check(UC_STAFF);
  33. //if ($CURUSER['class'] < UC_STAFF)
  34. //stderr("Error", "No Access");
  35. function ratios($up,$down, $color = True)
  36. {
  37. if ($down > 0)
  38. {
  39. $r = number_format($up / $down, 2);
  40. if ($color)
  41. $r = "<font color='".get_ratio_color($r)."'>$r</font>";
  42. }
  43. else
  44. if ($up > 0)
  45. $r = "Inf.";
  46. else
  47. $r = "---";
  48. return $r;
  49. }
  50. $HTMLOUT .= begin_main_frame();
  51. $HTMLOUT .="<h1>Search in IP History</h1>\n
  52. <form method='post' action='staffpanel.php?tool=ipsearch&amp;action=ipsearch'>\n
  53. <table align='center' border='1' cellspacing='0' width='115' cellpadding='5'>\n
  54. <tr>
  55. <td align='left'>IP:</td>\n
  56. <td align='left'>
  57. <input type='text' name='ip' size='40' value='" . htmlspecialchars($ip) . "' />\n
  58. </td></tr>
  59. <tr>
  60. <td align='left'>Mask:</td>
  61. <td align='left'>\n
  62. <input type='text' name='mask' size='40' value='" . htmlspecialchars($mask) . "' /></td></tr>\n
  63. <tr>
  64. <td align='right' colspan='2'><input type='submit' value='Search !' style='height: 20px' />
  65. </td></tr></table></form><br /><br />\n";
  66. $ip = isset($_POST["ip"]) ? htmlspecialchars(trim($_POST["ip"])) : '';
  67. if ($ip)
  68. {
  69. $regex = "/^(((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))(\.\b|$)){4}$/";
  70. if (!preg_match($regex, $ip))
  71. {
  72. $HTMLOUT .= stdmsg("Error", "Invalid IP.");
  73. $HTMLOUT .= end_main_frame();
  74. print stdhead("IP Search") . $HTMLOUT . stdfoot();
  75. die();
  76. }
  77. $mask = isset($_POST["mask"]) ? htmlspecialchars(trim($_POST["mask"])) : '';
  78. if ($mask == "" || $mask == "255.255.255.255")
  79. {
  80. $where1 = "u.ip = '$ip'";
  81. $where2 = "iplog.ip = '$ip'";
  82. $dom = @gethostbyaddr($ip);
  83. if ($dom == $ip || @gethostbyname($dom) != $ip)
  84. $addr = "";
  85. else
  86. $addr = $dom;
  87. }
  88. else
  89. {
  90. if (substr($mask,0,1) == "/")
  91. {
  92. $n = substr($mask, 1, strlen($mask) - 1);
  93. if (!is_numeric($n) or $n < 0 or $n > 32)
  94. {
  95. $HTMLOUT .= stdmsg("Error", "Invalid subnet mask.");
  96. $HTMLOUT .= end_main_frame();
  97. print stdhead("IP Search") . $HTMLOUT . stdfoot();
  98. die();
  99. }
  100. else
  101. $mask = long2ip(pow(2,32) - pow(2,32-$n));
  102. }
  103. elseif (!preg_match($regex, $mask))
  104. {
  105. $HTMLOUT .= stdmsg("Error", "Invalid subnet mask.");
  106. $HTMLOUT .= end_main_frame();
  107. print stdhead("IP Search") . $HTMLOUT . stdfoot();
  108. die();
  109. }
  110. $where1 = "INET_ATON(u.ip) & INET_ATON('$mask') = INET_ATON('$ip') & INET_ATON('$mask')";
  111. $where2 = "INET_ATON(iplog.ip) & INET_ATON('$mask') = INET_ATON('$ip') & INET_ATON('$mask')";
  112. $addr = "Mask: $mask";
  113. }
  114. $queryc = "SELECT COUNT(*) FROM
  115. (
  116. SELECT u.id FROM users AS u WHERE $where1
  117. UNION SELECT u.id FROM users AS u RIGHT JOIN iplog ON u.id = iplog.userid WHERE $where2
  118. GROUP BY u.id
  119. ) AS ipsearch";
  120. $res = sql_query($queryc) or sqlerr(__FILE__, __LINE__);
  121. $row = mysql_fetch_array($res);
  122. $count = $row[0];
  123. if ($count == 0)
  124. {
  125. $HTMLOUT .="<br /><b>No users found</b>\n";
  126. $HTMLOUT .= end_main_frame();
  127. print stdhead("IP sEARCH") . $HTMLOUT . stdfoot();
  128. die;
  129. }
  130. $order= isset($_POST['order']) && $_POST['order'];
  131. $page = isset($_POST['page']) && 0 + $_POST['page'];
  132. $perpage = 20;
  133. $pager = pager($perpage, $count, "staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask&amp;order=$order&amp;");
  134. if ($order == "added")
  135. $orderby = "added DESC";
  136. elseif ($order == "username")
  137. $orderby = "UPPER(username) ASC";
  138. elseif ($order == "email")
  139. $orderby = "email ASC";
  140. elseif ($order == "last_ip")
  141. $orderby = "last_ip ASC";
  142. elseif ($order == "last_access")
  143. $orderby = "last_ip ASC";
  144. else
  145. $orderby = "access DESC";
  146. $query1 = "SELECT * FROM (
  147. SELECT u.id, u.username, u.ip AS ip, u.ip AS last_ip, u.last_access, u.last_access AS access, u.email, u.invitedby, u.added, u.class, u.uploaded, u.downloaded, u.donor, u.enabled, u.warned, u.leechwarn, u.chatpost, u.pirate, u.king
  148. FROM users AS u
  149. WHERE $where1
  150. UNION SELECT u.id, u.username, iplog.ip AS ip, u.ip as last_ip, u.last_access, max(iplog.access) AS access, u.email, u.invitedby, u.added, u.class, u.uploaded, u.downloaded, u.donor, u.enabled, u.warned, u.leechwarn, u.chatpost, u.pirate, u.king
  151. FROM users AS u
  152. RIGHT JOIN iplog ON u.id = iplog.userid
  153. WHERE $where2
  154. GROUP BY u.id ) as ipsearch
  155. GROUP BY id
  156. ORDER BY $orderby
  157. ".$pager['limit']."";
  158. $res = sql_query($query1) or sqlerr(__FILE__, __LINE__);
  159. $HTMLOUT .= begin_frame("".htmlspecialchars($count)." users have used the IP: ".htmlspecialchars($ip)." (".htmlspecialchars($addr).")", True);
  160. if ($count > $perpage)
  161. $HTMLOUT .= $pager['pagertop'];
  162. $HTMLOUT .= "<table border='1' cellspacing='0' cellpadding='5'>\n";
  163. $HTMLOUT .= "<tr>
  164. <td class='colhead'><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask&amp;order=username'>Username</a></td>".
  165. "<td class='colhead'>Ratio</td>".
  166. "<td class='colhead'><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask&amp;order=email'>Email</a></td>".
  167. "<td class='colhead'><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask&amp;order=last_ip'>Last IP</a></td>".
  168. "<td class='colhead'><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask&amp;order=last_access'>Last access</a></td>".
  169. "<td class='colhead'>Num of IP's</td>".
  170. "<td class='colhead'><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask'>Last access on <br />".htmlspecialchars($ip)."</a></td>".
  171. "<td class='colhead'><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=ipsearch&amp;action=ipsearch&amp;ip=$ip&amp;mask=$mask&amp;order=added'>Added</a></td>".
  172. "<td class='colhead'>Invited by</td></tr>";
  173. while ($user = mysql_fetch_array($res))
  174. {
  175. if ($user['added'] == '0')
  176. $user['added'] = '---';
  177. if ($user['last_access'] == '0')
  178. $user['last_access'] = '---';
  179. if ($user['last_ip'])
  180. {
  181. $nip = ip2long($user['last_ip']);
  182. $res1 = sql_query("SELECT COUNT(*) FROM bans WHERE $nip >= first AND $nip <= last") or sqlerr(__FILE__, __LINE__);
  183. $array = mysql_fetch_row($res1);
  184. if ($array[0] == 0)
  185. $ipstr = $user['last_ip'];
  186. else
  187. $ipstr = "<a href='{$TBDEV['baseurl']}/staffpanel.php?tool=testip&amp;action=testip&amp;ip=" . $user['last_ip'] . "'><font color='#FF0000'><b>" .htmlspecialchars( $user["last_ip"]) . "</b></font></a>";
  188. }
  189. else
  190. $ipstr = "---";
  191. $resip = sql_query("SELECT ip FROM iplog WHERE userid=" . sqlesc($user["id"]) . " GROUP BY iplog.ip") or sqlerr(__FILE__, __LINE__);
  192. $iphistory = mysql_num_rows($resip);
  193. if ($user["invitedby"] > 0)
  194. {
  195. $res2 = sql_query("SELECT username FROM users WHERE id=".sqlesc($user["invitedby"])."");
  196. $array = mysql_fetch_array($res2);
  197. $invitedby = $array["username"];
  198. if ($invitedby == "")
  199. $invitedby = "<i>[Deleted]</i>";
  200. else
  201. $invitedby = "<a href='{$TBDEV['baseurl']}/userdetails.php?id=$user[invitedby]'>".htmlspecialchars($invitedby)."</a>";
  202. }
  203. else
  204. $invitedby = "--";
  205. $HTMLOUT .= "<tr>
  206. <td><b><a href='{$TBDEV['baseurl']}/userdetails.php?id=" . $user['id'] . "'></a></b>" . format_username($user) . "</td>".
  207. "<td>" . ratios($user['uploaded'], $user['downloaded']) . "</td>
  208. <td>" . $user['email'] . "</td><td>" . $ipstr . "</td>
  209. <td><div align='center'>" . get_date($user['last_access'],'DATE' ,1,0) . "</div></td>
  210. <td><div align='center'><b><a href='{$TBDEV['baseurl']}/staffpanel.php?tool=iphistory&amp;action=iphistory&amp;id=" . $user['id'] . "'>" . htmlspecialchars($iphistory). "</a></b></div></td>
  211. <td><div align='center'>" . get_date($user['access'],'DATE' ,1,0) . "</div></td>
  212. <td><div align='center'>" . get_date($user['added'],'DATE' ,1,0) . "</div></td>
  213. <td><div align='center'>" . $invitedby . "</div></td>
  214. </tr>\n";
  215. }
  216. $HTMLOUT .= "</table>";
  217. if ($count > $perpage)
  218. $pager['pagerbottom'];
  219. $HTMLOUT .= end_frame();
  220. }
  221. $HTMLOUT .= end_main_frame();
  222. print stdhead("Ip Search") . $HTMLOUT . stdfoot();
  223. die;
  224. ?>