PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/admin/admin_user_ban.php

https://code.google.com/p/torrentpier/
PHP | 403 lines | 355 code | 32 blank | 16 comment | 73 complexity | dff26694efcf9b9e2bc2f1367ca04d5a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // ACP Header - START
  3. if (!empty($setmodules))
  4. {
  5. $module['Users']['Ban_Management'] = basename(__FILE__);
  6. return;
  7. }
  8. require('./pagestart.php');
  9. // ACP Header - END
  10. //
  11. // Start program
  12. //
  13. if ( isset($_POST['submit']) )
  14. {
  15. $user_bansql = '';
  16. $email_bansql = '';
  17. $ip_bansql = '';
  18. $user_list = array();
  19. if ( !empty($_POST['username']) )
  20. {
  21. $this_userdata = get_userdata($_POST['username'], true);
  22. if( !$this_userdata )
  23. {
  24. message_die(GENERAL_MESSAGE, $lang['NO_USER_ID_SPECIFIED'] );
  25. }
  26. $user_list[] = $this_userdata['user_id'];
  27. }
  28. $ip_list = array();
  29. if ( isset($_POST['ban_ip']) )
  30. {
  31. $ip_list_temp = explode(',', $_POST['ban_ip']);
  32. for($i = 0; $i < count($ip_list_temp); $i++)
  33. {
  34. if ( preg_match('/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[ ]*\-[ ]*([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/', trim($ip_list_temp[$i]), $ip_range_explode) )
  35. {
  36. //
  37. // Don't ask about all this, just don't ask ... !
  38. //
  39. $ip_1_counter = $ip_range_explode[1];
  40. $ip_1_end = $ip_range_explode[5];
  41. while ( $ip_1_counter <= $ip_1_end )
  42. {
  43. $ip_2_counter = ( $ip_1_counter == $ip_range_explode[1] ) ? $ip_range_explode[2] : 0;
  44. $ip_2_end = ( $ip_1_counter < $ip_1_end ) ? 254 : $ip_range_explode[6];
  45. if ( $ip_2_counter == 0 && $ip_2_end == 254 )
  46. {
  47. $ip_2_counter = 255;
  48. $ip_2_fragment = 255;
  49. $ip_list[] = encode_ip("$ip_1_counter.255.255.255");
  50. }
  51. while ( $ip_2_counter <= $ip_2_end )
  52. {
  53. $ip_3_counter = ( $ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1] ) ? $ip_range_explode[3] : 0;
  54. $ip_3_end = ( $ip_2_counter < $ip_2_end || $ip_1_counter < $ip_1_end ) ? 254 : $ip_range_explode[7];
  55. if ( $ip_3_counter == 0 && $ip_3_end == 254 )
  56. {
  57. $ip_3_counter = 255;
  58. $ip_3_fragment = 255;
  59. $ip_list[] = encode_ip("$ip_1_counter.$ip_2_counter.255.255");
  60. }
  61. while ( $ip_3_counter <= $ip_3_end )
  62. {
  63. $ip_4_counter = ( $ip_3_counter == $ip_range_explode[3] && $ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1] ) ? $ip_range_explode[4] : 0;
  64. $ip_4_end = ( $ip_3_counter < $ip_3_end || $ip_2_counter < $ip_2_end ) ? 254 : $ip_range_explode[8];
  65. if ( $ip_4_counter == 0 && $ip_4_end == 254 )
  66. {
  67. $ip_4_counter = 255;
  68. $ip_4_fragment = 255;
  69. $ip_list[] = encode_ip("$ip_1_counter.$ip_2_counter.$ip_3_counter.255");
  70. }
  71. while ( $ip_4_counter <= $ip_4_end )
  72. {
  73. $ip_list[] = encode_ip("$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter");
  74. $ip_4_counter++;
  75. }
  76. $ip_3_counter++;
  77. }
  78. $ip_2_counter++;
  79. }
  80. $ip_1_counter++;
  81. }
  82. }
  83. else if ( preg_match('/^([\w\-_]\.?){2,}$/is', trim($ip_list_temp[$i])) )
  84. {
  85. $ip = gethostbynamel(trim($ip_list_temp[$i]));
  86. for($j = 0; $j < count($ip); $j++)
  87. {
  88. if ( !empty($ip[$j]) )
  89. {
  90. $ip_list[] = encode_ip($ip[$j]);
  91. }
  92. }
  93. }
  94. else if ( preg_match('/^([0-9]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})$/', trim($ip_list_temp[$i])) )
  95. {
  96. $ip_list[] = encode_ip(str_replace('*', '255', trim($ip_list_temp[$i])));
  97. }
  98. }
  99. }
  100. $email_list = array();
  101. if ( isset($_POST['ban_email']) )
  102. {
  103. $email_list_temp = explode(',', $_POST['ban_email']);
  104. for($i = 0; $i < count($email_list_temp); $i++)
  105. {
  106. //
  107. // This ereg match is based on one by php@unreelpro.com
  108. // contained in the annotated php manual at php.com (ereg
  109. // section)
  110. //
  111. if (preg_match('/^(([a-z0-9&\'\.\-_\+])|(\*))+@(([a-z0-9\-])|(\*))+\.([a-z0-9\-]+\.)*?[a-z]+$/is', trim($email_list_temp[$i])))
  112. {
  113. $email_list[] = trim($email_list_temp[$i]);
  114. }
  115. }
  116. }
  117. $sql = "SELECT *
  118. FROM " . BANLIST_TABLE;
  119. if ( !($result = $db->sql_query($sql)) )
  120. {
  121. message_die(GENERAL_ERROR, "Couldn't obtain banlist information", "", __LINE__, __FILE__, $sql);
  122. }
  123. $current_banlist = $db->sql_fetchrowset($result);
  124. $db->sql_freeresult($result);
  125. $kill_session_sql = '';
  126. for($i = 0; $i < count($user_list); $i++)
  127. {
  128. $in_banlist = false;
  129. for($j = 0; $j < count($current_banlist); $j++)
  130. {
  131. if ( $user_list[$i] == $current_banlist[$j]['ban_userid'] )
  132. {
  133. $in_banlist = true;
  134. }
  135. }
  136. if ( !$in_banlist )
  137. {
  138. $kill_session_sql .= ( ( $kill_session_sql != '' ) ? ' OR ' : '' ) . "session_user_id = " . $user_list[$i];
  139. $sql = "INSERT INTO " . BANLIST_TABLE . " (ban_userid)
  140. VALUES (" . $user_list[$i] . ")";
  141. if ( !$db->sql_query($sql) )
  142. {
  143. message_die(GENERAL_ERROR, "Couldn't insert ban_userid info into database", "", __LINE__, __FILE__, $sql);
  144. }
  145. }
  146. }
  147. for($i = 0; $i < count($ip_list); $i++)
  148. {
  149. $in_banlist = false;
  150. for($j = 0; $j < count($current_banlist); $j++)
  151. {
  152. if ( $ip_list[$i] == $current_banlist[$j]['ban_ip'] )
  153. {
  154. $in_banlist = true;
  155. }
  156. }
  157. if ( !$in_banlist )
  158. {
  159. if ( preg_match('/(ff\.)|(\.ff)/is', chunk_split($ip_list[$i], 2, '.')) )
  160. {
  161. $kill_ip_sql = "session_ip LIKE '" . str_replace('.', '', preg_replace('/(ff\.)|(\.ff)/is', '%', chunk_split($ip_list[$i], 2, "."))) . "'";
  162. }
  163. else
  164. {
  165. $kill_ip_sql = "session_ip = '" . $ip_list[$i] . "'";
  166. }
  167. $kill_session_sql .= ( ( $kill_session_sql != '' ) ? ' OR ' : '' ) . $kill_ip_sql;
  168. $sql = "INSERT INTO " . BANLIST_TABLE . " (ban_ip)
  169. VALUES ('" . $ip_list[$i] . "')";
  170. if ( !$db->sql_query($sql) )
  171. {
  172. message_die(GENERAL_ERROR, "Couldn't insert ban_ip info into database", "", __LINE__, __FILE__, $sql);
  173. }
  174. }
  175. }
  176. //
  177. // Now we'll delete all entries from the session table with any of the banned
  178. // user or IP info just entered into the ban table ... this will force a session
  179. // initialisation resulting in an instant ban
  180. //
  181. if ( $kill_session_sql != '' )
  182. {
  183. $sql = "DELETE FROM " . SESSIONS_TABLE . "
  184. WHERE $kill_session_sql";
  185. if ( !$db->sql_query($sql) )
  186. {
  187. message_die(GENERAL_ERROR, "Couldn't delete banned sessions from database", "", __LINE__, __FILE__, $sql);
  188. }
  189. }
  190. for($i = 0; $i < count($email_list); $i++)
  191. {
  192. $in_banlist = false;
  193. for($j = 0; $j < count($current_banlist); $j++)
  194. {
  195. if ( $email_list[$i] == $current_banlist[$j]['ban_email'] )
  196. {
  197. $in_banlist = true;
  198. }
  199. }
  200. if ( !$in_banlist )
  201. {
  202. $sql = "INSERT INTO " . BANLIST_TABLE . " (ban_email)
  203. VALUES ('" . str_replace("\'", "''", $email_list[$i]) . "')";
  204. if ( !$db->sql_query($sql) )
  205. {
  206. message_die(GENERAL_ERROR, "Couldn't insert ban_email info into database", "", __LINE__, __FILE__, $sql);
  207. }
  208. }
  209. }
  210. $where_sql = '';
  211. if ( isset($_POST['unban_user']) )
  212. {
  213. $user_list = $_POST['unban_user'];
  214. for($i = 0; $i < count($user_list); $i++)
  215. {
  216. if ( $user_list[$i] != -1 )
  217. {
  218. $where_sql .= ( ( $where_sql != '' ) ? ', ' : '' ) . intval($user_list[$i]);
  219. }
  220. }
  221. }
  222. if ( isset($_POST['unban_ip']) )
  223. {
  224. $ip_list = $_POST['unban_ip'];
  225. for($i = 0; $i < count($ip_list); $i++)
  226. {
  227. if ( $ip_list[$i] != -1 )
  228. {
  229. $where_sql .= ( ( $where_sql != '' ) ? ', ' : '' ) . str_replace("\'", "''", $ip_list[$i]);
  230. }
  231. }
  232. }
  233. if ( isset($_POST['unban_email']) )
  234. {
  235. $email_list = $_POST['unban_email'];
  236. for($i = 0; $i < count($email_list); $i++)
  237. {
  238. if ( $email_list[$i] != -1 )
  239. {
  240. $where_sql .= ( ( $where_sql != '' ) ? ', ' : '' ) . str_replace("\'", "''", $email_list[$i]);
  241. }
  242. }
  243. }
  244. if ( $where_sql != '' )
  245. {
  246. $sql = "DELETE FROM " . BANLIST_TABLE . "
  247. WHERE ban_id IN ($where_sql)";
  248. if ( !$db->sql_query($sql) )
  249. {
  250. message_die(GENERAL_ERROR, "Couldn't delete ban info from database", "", __LINE__, __FILE__, $sql);
  251. }
  252. }
  253. $message = $lang['BAN_UPDATE_SUCESSFUL'] . '<br /><br />' . sprintf($lang['CLICK_RETURN_BANADMIN'], '<a href="' . append_sid("admin_user_ban.php") . '">', '</a>') . '<br /><br />' . sprintf($lang['CLICK_RETURN_ADMIN_INDEX'], '<a href="' . append_sid("index.php?pane=right") . '">', '</a>');
  254. message_die(GENERAL_MESSAGE, $message);
  255. }
  256. else
  257. {
  258. $template->assign_vars(array(
  259. 'L_BAN_TITLE' => $lang['BAN_CONTROL'],
  260. 'L_IP_OR_HOSTNAME' => $lang['IP_HOSTNAME'],
  261. 'S_BANLIST_ACTION' => append_sid("admin_user_ban.php"))
  262. );
  263. $template->assign_vars(array(
  264. 'L_BAN_USER' => $lang['BAN_USERNAME'],
  265. 'L_BAN_USER_EXPLAIN' => $lang['BAN_USERNAME_EXPLAIN'])
  266. );
  267. $userban_count = 0;
  268. $ipban_count = 0;
  269. $emailban_count = 0;
  270. $sql = "SELECT b.ban_id, u.user_id, u.username
  271. FROM " . BANLIST_TABLE . " b, " . USERS_TABLE . " u
  272. WHERE u.user_id = b.ban_userid
  273. AND b.ban_userid <> 0
  274. AND u.user_id <> " . ANONYMOUS . "
  275. ORDER BY u.username ASC";
  276. if ( !($result = $db->sql_query($sql)) )
  277. {
  278. message_die(GENERAL_ERROR, 'Could not select current user_id ban list', '', __LINE__, __FILE__, $sql);
  279. }
  280. $user_list = $db->sql_fetchrowset($result);
  281. $db->sql_freeresult($result);
  282. $select_userlist = '';
  283. for($i = 0; $i < count($user_list); $i++)
  284. {
  285. $select_userlist .= '<option value="' . $user_list[$i]['ban_id'] . '">' . $user_list[$i]['username'] . '</option>';
  286. $userban_count++;
  287. }
  288. if( $select_userlist == '' )
  289. {
  290. $select_userlist = '<option value="-1">' . $lang['NO_BANNED_USERS'] . '</option>';
  291. }
  292. $select_userlist = '<select name="unban_user[]" multiple="multiple" size="5">' . $select_userlist . '</select>';
  293. $sql = "
  294. SELECT ban_id, ban_ip, ban_email
  295. FROM ". BANLIST_TABLE ."
  296. ORDER BY ban_ip
  297. ";
  298. if ( !($result = $db->sql_query($sql)) )
  299. {
  300. message_die(GENERAL_ERROR, 'Could not select current ip ban list', '', __LINE__, __FILE__, $sql);
  301. }
  302. $banlist = $db->sql_fetchrowset($result);
  303. $db->sql_freeresult($result);
  304. $select_iplist = '';
  305. $select_emaillist = '';
  306. for($i = 0; $i < count($banlist); $i++)
  307. {
  308. $ban_id = $banlist[$i]['ban_id'];
  309. if ( !empty($banlist[$i]['ban_ip']) )
  310. {
  311. $ban_ip = str_replace('255', '*', decode_ip($banlist[$i]['ban_ip']));
  312. $select_iplist .= '<option value="' . $ban_id . '">' . $ban_ip . '</option>';
  313. $ipban_count++;
  314. }
  315. else if ( !empty($banlist[$i]['ban_email']) )
  316. {
  317. $ban_email = $banlist[$i]['ban_email'];
  318. $select_emaillist .= '<option value="' . $ban_id . '">' . $ban_email . '</option>';
  319. $emailban_count++;
  320. }
  321. }
  322. if ( $select_iplist == '' )
  323. {
  324. $select_iplist = '<option value="-1">' . $lang['NO_BANNED_IP'] . '</option>';
  325. }
  326. if ( $select_emaillist == '' )
  327. {
  328. $select_emaillist = '<option value="-1">' . $lang['NO_BANNED_EMAIL'] . '</option>';
  329. }
  330. $select_iplist = '<select name="unban_ip[]" multiple="multiple" size="15">' . $select_iplist . '</select>';
  331. $select_emaillist = '<select name="unban_email[]" multiple="multiple" size="10">' . $select_emaillist . '</select>';
  332. $template->assign_vars(array(
  333. 'L_UNBAN_USER' => $lang['UNBAN_USERNAME'],
  334. 'L_UNBAN_USER_EXPLAIN' => $lang['UNBAN_USERNAME_EXPLAIN'],
  335. 'U_SEARCH_USER' => append_sid("./../search.php?mode=searchuser"),
  336. 'S_UNBAN_USERLIST_SELECT' => $select_userlist,
  337. 'S_UNBAN_IPLIST_SELECT' => $select_iplist,
  338. 'S_UNBAN_EMAILLIST_SELECT' => $select_emaillist,
  339. 'S_BAN_ACTION' => append_sid("admin_user_ban.php"))
  340. );
  341. }
  342. print_page('admin_user_ban.tpl', 'admin');