/src/utilities/login_multi_ip.php

https://github.com/extraaa/Game · PHP · 263 lines · 154 code · 50 blank · 59 comment · 20 complexity · 4b0173a1212372c16089cd92ed92495d MD5 · raw file

  1. <?php
  2. /*
  3. * login_multi_ip.php - Finding users logging in using the same IP
  4. * Copyright (c) 2005 Marcus Lunzenauer
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. */
  11. /***** COMMAND LINE *****/
  12. // include PEAR::Console_Getopt
  13. require_once('Console/Getopt.php');
  14. // check for command line options
  15. $options = Console_Getopt::getOpt(Console_Getopt::readPHPArgv(), 'dht:');
  16. if (PEAR::isError($options)) {
  17. multiip_usage();
  18. exit(1);
  19. }
  20. // check for options
  21. $debugFlag = FALSE;
  22. $time_intervall = 12;
  23. foreach ($options[0] as $option) {
  24. // option h
  25. if ('h' == $option[0]) {
  26. multiip_usage(); exit(1);
  27. // option d
  28. } else if ('d' == $option[0]) {
  29. $debugFlag = TRUE;
  30. // option t
  31. } else if ('t' == $option[0]) {
  32. $time_intervall = $option[1];
  33. }
  34. }
  35. /***** INIT *****/
  36. // include necessary files
  37. include "util.inc.php";
  38. include INC_DIR . "config.inc.php";
  39. include INC_DIR . "db.inc.php";
  40. include INC_DIR . "basic.lib.php";
  41. // get globals
  42. $config = new Config();
  43. // show header
  44. multiip_showHeader();
  45. // connect to databases
  46. $db_login = db_connectToLoginDB();
  47. /***** GET GOING *****/
  48. // ** IP **/
  49. // get distinct ip's
  50. $string = "ip";
  51. multiip_showBetween($string);
  52. $ips = multiip_getDistinct_($string);
  53. // check each ip
  54. foreach ($ips as $ip) {
  55. $users = multiip_check_($ip, $string);
  56. if (sizeof($users) > 1) {
  57. multiip_log("%s: (%s)", $ip, implode(',', $users));
  58. }
  59. }
  60. /**Passwords**/
  61. //get distinct passwords
  62. $string = "password";
  63. multiip_showBetween($string);
  64. $passwords = multiip_getDistinct_($string);
  65. //check each password
  66. foreach ($passwords as $pass) {
  67. $users = multiip_check_($pass, $string);
  68. if(sizeof($users) > 1) {
  69. multiip_log("%s: (%s)", $pass, implode(',', $users));
  70. }
  71. }
  72. /**PollID**/
  73. //get distinct pollIDs
  74. $string = "pollID";
  75. multiip_showBetween($string);
  76. $pollIDs = multiip_getDistinct_($string);
  77. //check each pollID
  78. foreach ($pollIDs as $poll) {
  79. $users = multiip_check_($poll, $string);
  80. if(sizeof($users) > 1) {
  81. multiip_log("%s: (%s)", $poll, implode(', ', $users));
  82. }
  83. }
  84. // ***** FUNCTIONS ***** *******************************************************
  85. /**
  86. * Shows usage
  87. */
  88. function multiip_usage() {
  89. echo
  90. "Usage: php login_multi_ip.php [-d] [-h] [-t time_interval]\n".
  91. " -d Debug\n".
  92. " -h This help\n".
  93. " -t time_interval Only consider ips of the last time_interval hours\n";
  94. }
  95. /**
  96. * Logging function with printf syntax
  97. */
  98. function multiip_log($format /* , .. */) {
  99. // get args
  100. $args = func_get_args();
  101. // get format string
  102. $format = array_shift($args);
  103. // do something
  104. echo vsprintf($format, $args) . "\n";
  105. }
  106. /**
  107. * Shows header
  108. */
  109. function multiip_showHeader() {
  110. multiip_log('------------------------------------------------------------');
  111. multiip_log('- FINDING MULTIS -----------------------------------------');
  112. multiip_log('- from %s', date('r'));
  113. multiip_log('------------------------------------------------------------');
  114. }
  115. /**
  116. * Show Between
  117. */
  118. function multiip_showBetween($string){
  119. multiip_log('');
  120. multiip_log('------------------------------------------------------------');
  121. multiip_log('-- searching %s --', $string);
  122. multiip_log('------------------------------------------------------------');
  123. multiip_log('');
  124. }
  125. function multiip_getDistinctIPs() {
  126. // prepare query
  127. global $time_intervall, $db_login;
  128. $sql = $db_login->prepare("SELECT ip FROM LoginLog
  129. WHERE success = 1
  130. AND stamp > NOW() - INTERVAL :time_intervall HOUR
  131. GROUP BY ip
  132. HAVING COUNT(*) > 1");
  133. $sql->bindValue('time_intervall', $time_intervall, PDO::PARAM_INT);
  134. // on error
  135. if (!$sql->execute()) {
  136. multiip_log('Could not retrieve ips from LoginLog');
  137. exit(1);
  138. }
  139. // collect records
  140. $ips = array();
  141. while ($row = $sql->fetch())
  142. $ips[] = $row['ip'];
  143. $sql->closeCursor();
  144. return $ips;
  145. }
  146. function multiip_checkIP($ip) {
  147. // prepare query
  148. global $time_intervall, $db_login;
  149. $sql = $db_login->prepare("SELECT user
  150. FROM LoginLog
  151. WHERE ip = :ip
  152. AND stamp > NOW() - INTERVAL :time_intervall HOUR
  153. AND success = 1
  154. GROUP BY user");
  155. $sql->bindValue('ip', $ip, PDO::PARAM_STR);
  156. $sql->bindValue('time_intervall', $time_intervall, PDO::PARAM_INT);
  157. // on error
  158. if (!$sql->execute()) {
  159. multiip_log('Could not check ip from LoginLog');
  160. exit(1);
  161. }
  162. // collect records
  163. $users = array();
  164. while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
  165. $users[] = $row['user'];
  166. }
  167. $sql->closeCursor();
  168. return $users;
  169. }
  170. function multiip_getDistinct_($string) {
  171. // prepare query
  172. global $time_intervall, $db_login;
  173. $sql = $db_login->prepare("SELECT $string
  174. FROM LoginLog
  175. WHERE success = 1
  176. AND stamp > NOW() - INTERVAL :time_intervall HOUR
  177. GROUP BY {$string} HAVING COUNT(*) > 1");
  178. $sql->bindValue('time_intervall', $time_intervall, PDO::PARAM_INT);
  179. // on error
  180. if (!$sql->execute()) {
  181. multiip_log('Could not retrieve %ss from LoginLog', $string);
  182. exit(1);
  183. }
  184. // collect records
  185. $result = array();
  186. while ($row = $sql->fetch()) {
  187. $result[] = $row[$string];
  188. }
  189. return $result;
  190. }
  191. function multiip_check_($search, $string) {
  192. // prepare query
  193. global $time_intervall, $db_login;
  194. $sql = $db_login->prepare("SELECT user
  195. FROM LoginLog
  196. WHERE {$string} = :search
  197. AND stamp > NOW() - INTERVAL :time_intervall HOUR
  198. AND success = 1
  199. GROUP BY user");
  200. $sql->bindValue('search', $search, PDO::PARAM_STR);
  201. $sql->bindValue('time_intervall', $time_intervall, PDO::PARAM_INT);
  202. // on error
  203. if (!$sql->execute()) {
  204. multiip_log('Could not check %s from LoginLog', $string);
  205. exit(1);
  206. }
  207. // collect records
  208. $users = array();
  209. while ($row = $sql->fetch())
  210. $users[] = $row['user'];
  211. return $users;
  212. }
  213. ?>