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

/sources/admin/ManageBans.php

https://github.com/Arantor/Elkarte
PHP | 1871 lines | 1522 code | 148 blank | 201 comment | 171 complexity | 4ae232d467bf8817c55c5024b94b09a7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file contains all the functions used for the ban center.
  16. * @todo refactor as controller-model
  17. *
  18. */
  19. if (!defined('ELKARTE'))
  20. die('No access...');
  21. /**
  22. * Ban center. The main entrance point for all ban center functions.
  23. * It is accesssed by ?action=admin;area=ban.
  24. * It choses a function based on the 'sa' parameter, like many others.
  25. * The default sub-action is action_list().
  26. * It requires the ban_members permission.
  27. * It initializes the admin tabs.
  28. *
  29. * @uses ManageBans template.
  30. */
  31. function Ban()
  32. {
  33. global $context, $txt, $scripturl;
  34. isAllowedTo('manage_bans');
  35. loadTemplate('ManageBans');
  36. $subActions = array(
  37. 'add' => 'BanEdit',
  38. 'browse' => 'action_browse',
  39. 'edittrigger' => 'action_edittrigger',
  40. 'edit' => 'BanEdit',
  41. 'list' => 'action_list',
  42. 'log' => 'action_log',
  43. );
  44. call_integration_hook('integrate_manage_bans', array($subActions));
  45. // Default the sub-action to 'view ban list'.
  46. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'list';
  47. $context['page_title'] = $txt['ban_title'];
  48. $context['sub_action'] = $_REQUEST['sa'];
  49. // Tabs for browsing the different ban functions.
  50. $context[$context['admin_menu_name']]['tab_data'] = array(
  51. 'title' => $txt['ban_title'],
  52. 'help' => 'ban_members',
  53. 'description' => $txt['ban_description'],
  54. 'tabs' => array(
  55. 'list' => array(
  56. 'description' => $txt['ban_description'],
  57. 'href' => $scripturl . '?action=admin;area=ban;sa=list',
  58. 'is_selected' => $_REQUEST['sa'] == 'list' || $_REQUEST['sa'] == 'edit' || $_REQUEST['sa'] == 'edittrigger',
  59. ),
  60. 'add' => array(
  61. 'description' => $txt['ban_description'],
  62. 'href' => $scripturl . '?action=admin;area=ban;sa=add',
  63. 'is_selected' => $_REQUEST['sa'] == 'add',
  64. ),
  65. 'browse' => array(
  66. 'description' => $txt['ban_trigger_browse_description'],
  67. 'href' => $scripturl . '?action=admin;area=ban;sa=browse',
  68. 'is_selected' => $_REQUEST['sa'] == 'browse',
  69. ),
  70. 'log' => array(
  71. 'description' => $txt['ban_log_description'],
  72. 'href' => $scripturl . '?action=admin;area=ban;sa=log',
  73. 'is_selected' => $_REQUEST['sa'] == 'log',
  74. 'is_last' => true,
  75. ),
  76. ),
  77. );
  78. // Call the right function for this sub-acton.
  79. $subActions[$_REQUEST['sa']]();
  80. }
  81. /**
  82. * Shows a list of bans currently set.
  83. * It is accesssed by ?action=admin;area=ban;sa=list.
  84. * It removes expired bans.
  85. * It allows sorting on different criteria.
  86. * It also handles removal of selected ban items.
  87. *
  88. * @uses the main ManageBans template.
  89. */
  90. function action_list()
  91. {
  92. global $txt, $context, $ban_request, $ban_counts, $scripturl;
  93. global $user_info, $smcFunc;
  94. // User pressed the 'remove selection button'.
  95. if (!empty($_POST['removeBans']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  96. {
  97. checkSession();
  98. // Make sure every entry is a proper integer.
  99. foreach ($_POST['remove'] as $index => $ban_id)
  100. $_POST['remove'][(int) $index] = (int) $ban_id;
  101. // Unban them all!
  102. $smcFunc['db_query']('', '
  103. DELETE FROM {db_prefix}ban_groups
  104. WHERE id_ban_group IN ({array_int:ban_list})',
  105. array(
  106. 'ban_list' => $_POST['remove'],
  107. )
  108. );
  109. $smcFunc['db_query']('', '
  110. DELETE FROM {db_prefix}ban_items
  111. WHERE id_ban_group IN ({array_int:ban_list})',
  112. array(
  113. 'ban_list' => $_POST['remove'],
  114. )
  115. );
  116. // No more caching this ban!
  117. updateSettings(array('banLastUpdated' => time()));
  118. // Some members might be unbanned now. Update the members table.
  119. updateBanMembers();
  120. }
  121. // Create a date string so we don't overload them with date info.
  122. if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0]))
  123. $context['ban_time_format'] = $user_info['time_format'];
  124. else
  125. $context['ban_time_format'] = $matches[0];
  126. $listOptions = array(
  127. 'id' => 'ban_list',
  128. 'title' => $txt['ban_title'],
  129. 'items_per_page' => 20,
  130. 'base_href' => $scripturl . '?action=admin;area=ban;sa=list',
  131. 'default_sort_col' => 'added',
  132. 'default_sort_dir' => 'desc',
  133. 'get_items' => array(
  134. 'function' => 'list_getBans',
  135. ),
  136. 'get_count' => array(
  137. 'function' => 'list_getNumBans',
  138. ),
  139. 'no_items_label' => $txt['ban_no_entries'],
  140. 'columns' => array(
  141. 'name' => array(
  142. 'header' => array(
  143. 'value' => $txt['ban_name'],
  144. ),
  145. 'data' => array(
  146. 'db' => 'name',
  147. ),
  148. 'sort' => array(
  149. 'default' => 'bg.name',
  150. 'reverse' => 'bg.name DESC',
  151. ),
  152. ),
  153. 'notes' => array(
  154. 'header' => array(
  155. 'value' => $txt['ban_notes'],
  156. ),
  157. 'data' => array(
  158. 'db' => 'notes',
  159. 'class' => 'smalltext',
  160. ),
  161. 'sort' => array(
  162. 'default' => 'LENGTH(bg.notes) > 0 DESC, bg.notes',
  163. 'reverse' => 'LENGTH(bg.notes) > 0, bg.notes DESC',
  164. ),
  165. ),
  166. 'reason' => array(
  167. 'header' => array(
  168. 'value' => $txt['ban_reason'],
  169. ),
  170. 'data' => array(
  171. 'db' => 'reason',
  172. 'class' => 'smalltext',
  173. ),
  174. 'sort' => array(
  175. 'default' => 'LENGTH(bg.reason) > 0 DESC, bg.reason',
  176. 'reverse' => 'LENGTH(bg.reason) > 0, bg.reason DESC',
  177. ),
  178. ),
  179. 'added' => array(
  180. 'header' => array(
  181. 'value' => $txt['ban_added'],
  182. ),
  183. 'data' => array(
  184. 'function' => create_function('$rowData', '
  185. global $context;
  186. return timeformat($rowData[\'ban_time\'], empty($context[\'ban_time_format\']) ? true : $context[\'ban_time_format\']);
  187. '),
  188. ),
  189. 'sort' => array(
  190. 'default' => 'bg.ban_time',
  191. 'reverse' => 'bg.ban_time DESC',
  192. ),
  193. ),
  194. 'expires' => array(
  195. 'header' => array(
  196. 'value' => $txt['ban_expires'],
  197. ),
  198. 'data' => array(
  199. 'function' => create_function('$rowData', '
  200. global $txt;
  201. // This ban never expires...whahaha.
  202. if ($rowData[\'expire_time\'] === null)
  203. return $txt[\'never\'];
  204. // This ban has already expired.
  205. elseif ($rowData[\'expire_time\'] < time())
  206. return sprintf(\'<span style="color: red">%1$s</span>\', $txt[\'ban_expired\']);
  207. // Still need to wait a few days for this ban to expire.
  208. else
  209. return sprintf(\'%1$d&nbsp;%2$s\', ceil(($rowData[\'expire_time\'] - time()) / (60 * 60 * 24)), $txt[\'ban_days\']);
  210. '),
  211. ),
  212. 'sort' => array(
  213. 'default' => 'IFNULL(bg.expire_time, 1=1) DESC, bg.expire_time DESC',
  214. 'reverse' => 'IFNULL(bg.expire_time, 1=1), bg.expire_time',
  215. ),
  216. ),
  217. 'num_triggers' => array(
  218. 'header' => array(
  219. 'value' => $txt['ban_triggers'],
  220. ),
  221. 'data' => array(
  222. 'db' => 'num_triggers',
  223. ),
  224. 'sort' => array(
  225. 'default' => 'num_triggers DESC',
  226. 'reverse' => 'num_triggers',
  227. ),
  228. ),
  229. 'actions' => array(
  230. 'header' => array(
  231. 'value' => $txt['ban_actions'],
  232. 'class' => 'centercol',
  233. ),
  234. 'data' => array(
  235. 'sprintf' => array(
  236. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">' . $txt['modify'] . '</a>',
  237. 'params' => array(
  238. 'id_ban_group' => false,
  239. ),
  240. ),
  241. 'class' => 'centercol',
  242. ),
  243. ),
  244. 'check' => array(
  245. 'header' => array(
  246. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  247. 'class' => 'centercol',
  248. ),
  249. 'data' => array(
  250. 'sprintf' => array(
  251. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  252. 'params' => array(
  253. 'id_ban_group' => false,
  254. ),
  255. ),
  256. 'class' => 'centercol',
  257. ),
  258. ),
  259. ),
  260. 'form' => array(
  261. 'href' => $scripturl . '?action=admin;area=ban;sa=list',
  262. ),
  263. 'additional_rows' => array(
  264. array(
  265. 'position' => 'bottom_of_list',
  266. 'value' => '<input type="submit" name="removeBans" value="' . $txt['ban_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_remove_selected_confirm'] . '\');" class="button_submit" />',
  267. ),
  268. ),
  269. );
  270. require_once(SUBSDIR . '/List.subs.php');
  271. createList($listOptions);
  272. $context['sub_template'] = 'show_list';
  273. $context['default_list'] = 'ban_list';
  274. }
  275. /**
  276. * Get bans, what else? For the given options.
  277. *
  278. * @param int $start
  279. * @param int $items_per_page
  280. * @param string $sort
  281. * @return array
  282. */
  283. function list_getBans($start, $items_per_page, $sort)
  284. {
  285. global $smcFunc;
  286. $request = $smcFunc['db_query']('', '
  287. SELECT bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, COUNT(bi.id_ban) AS num_triggers
  288. FROM {db_prefix}ban_groups AS bg
  289. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  290. GROUP BY bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes
  291. ORDER BY {raw:sort}
  292. LIMIT {int:offset}, {int:limit}',
  293. array(
  294. 'sort' => $sort,
  295. 'offset' => $start,
  296. 'limit' => $items_per_page,
  297. )
  298. );
  299. $bans = array();
  300. while ($row = $smcFunc['db_fetch_assoc']($request))
  301. $bans[] = $row;
  302. $smcFunc['db_free_result']($request);
  303. return $bans;
  304. }
  305. /**
  306. * Get the total number of ban from the ban group table
  307. *
  308. * @return int
  309. */
  310. function list_getNumBans()
  311. {
  312. global $smcFunc;
  313. $request = $smcFunc['db_query']('', '
  314. SELECT COUNT(*) AS num_bans
  315. FROM {db_prefix}ban_groups',
  316. array(
  317. )
  318. );
  319. list ($numBans) = $smcFunc['db_fetch_row']($request);
  320. $smcFunc['db_free_result']($request);
  321. return $numBans;
  322. }
  323. /**
  324. * This function is behind the screen for adding new bans and modifying existing ones.
  325. * Adding new bans:
  326. * - is accesssed by ?action=admin;area=ban;sa=add.
  327. * - uses the ban_edit sub template of the ManageBans template.
  328. * Modifying existing bans:
  329. * - is accesssed by ?action=admin;area=ban;sa=edit;bg=x
  330. * - uses the ban_edit sub template of the ManageBans template.
  331. * - shows a list of ban triggers for the specified ban.
  332. * - handles submitted forms that add, modify or remove ban triggers.
  333. *
  334. * @todo insane number of writing to superglobals here...
  335. */
  336. function BanEdit()
  337. {
  338. global $txt, $modSettings, $context, $ban_request, $scripturl, $smcFunc;
  339. $_REQUEST['bg'] = empty($_REQUEST['bg']) ? 0 : (int) $_REQUEST['bg'];
  340. // Adding or editing a ban trigger?
  341. if (!empty($_POST['add_new_trigger']) || !empty($_POST['edit_trigger']))
  342. {
  343. checkSession();
  344. validateToken('admin-bet');
  345. $newBan = !empty($_POST['add_new_trigger']);
  346. $values = array(
  347. 'id_ban_group' => $_REQUEST['bg'],
  348. 'hostname' => '',
  349. 'email_address' => '',
  350. 'id_member' => 0,
  351. 'ip_low1' => 0,
  352. 'ip_high1' => 0,
  353. 'ip_low2' => 0,
  354. 'ip_high2' => 0,
  355. 'ip_low3' => 0,
  356. 'ip_high3' => 0,
  357. 'ip_low4' => 0,
  358. 'ip_high4' => 0,
  359. 'ip_low5' => 0,
  360. 'ip_high5' => 0,
  361. 'ip_low6' => 0,
  362. 'ip_high6' => 0,
  363. 'ip_low7' => 0,
  364. 'ip_high7' => 0,
  365. 'ip_low8' => 0,
  366. 'ip_high8' => 0,
  367. );
  368. // Preset all values that are required.
  369. if ($newBan)
  370. {
  371. $insertKeys = array(
  372. 'id_ban_group' => 'int',
  373. 'hostname' => 'string',
  374. 'email_address' => 'string',
  375. 'id_member' => 'int',
  376. 'ip_low1' => 'int',
  377. 'ip_high1' => 'int',
  378. 'ip_low2' => 'int',
  379. 'ip_high2' => 'int',
  380. 'ip_low3' => 'int',
  381. 'ip_high3' => 'int',
  382. 'ip_low4' => 'int',
  383. 'ip_high4' => 'int',
  384. 'ip_low5' => 'int',
  385. 'ip_high5' => 'int',
  386. 'ip_low6' => 'int',
  387. 'ip_high6' => 'int',
  388. 'ip_low7' => 'int',
  389. 'ip_high7' => 'int',
  390. 'ip_low8' => 'int',
  391. 'ip_high8' => 'int',
  392. );
  393. }
  394. else
  395. $updateString = '
  396. hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
  397. ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
  398. ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
  399. ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
  400. ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4},
  401. ip_low5 = {int:ip_low5}, ip_high5 = {int:ip_high5},
  402. ip_low6 = {int:ip_low6}, ip_high6 = {int:ip_high6},
  403. ip_low7 = {int:ip_low7}, ip_high7 = {int:ip_high7},
  404. ip_low8 = {int:ip_low8}, ip_high8 = {int:ip_high8}';
  405. if ($_POST['bantype'] == 'ip_ban')
  406. {
  407. $ip = trim($_POST['ip']);
  408. $ip_parts = ip2range($ip);
  409. $ip_check = checkExistingTriggerIP($ip_parts, $ip);
  410. if (!$ip_check)
  411. fatal_lang_error('invalid_ip', false);
  412. $values = array_merge($values, $ip_check);
  413. $modlogInfo['ip_range'] = $_POST['ip'];
  414. }
  415. elseif ($_POST['bantype'] == 'hostname_ban')
  416. {
  417. if (preg_match('/[^\w.\-*]/', $_POST['hostname']) == 1)
  418. fatal_lang_error('invalid_hostname', false);
  419. // Replace the * wildcard by a MySQL compatible wildcard %.
  420. $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
  421. $values['hostname'] = $_POST['hostname'];
  422. $modlogInfo['hostname'] = $_POST['hostname'];
  423. }
  424. elseif ($_POST['bantype'] == 'email_ban')
  425. {
  426. if (preg_match('/[^\w.\-\+*@]/', $_POST['email']) == 1)
  427. fatal_lang_error('invalid_email', false);
  428. $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
  429. // Check the user is not banning an admin.
  430. $request = $smcFunc['db_query']('', '
  431. SELECT id_member
  432. FROM {db_prefix}members
  433. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  434. AND email_address LIKE {string:email}
  435. LIMIT 1',
  436. array(
  437. 'admin_group' => 1,
  438. 'email' => $_POST['email'],
  439. )
  440. );
  441. if ($smcFunc['db_num_rows']($request) != 0)
  442. fatal_lang_error('no_ban_admin', 'critical');
  443. $smcFunc['db_free_result']($request);
  444. $values['email_address'] = $_POST['email'];
  445. $modlogInfo['email'] = $_POST['email'];
  446. }
  447. elseif ($_POST['bantype'] == 'user_ban')
  448. {
  449. $_POST['user'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
  450. $request = $smcFunc['db_query']('', '
  451. SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
  452. FROM {db_prefix}members
  453. WHERE member_name = {string:user_name} OR real_name = {string:user_name}
  454. LIMIT 1',
  455. array(
  456. 'admin_group' => 1,
  457. 'user_name' => $_POST['user'],
  458. )
  459. );
  460. if ($smcFunc['db_num_rows']($request) == 0)
  461. fatal_lang_error('invalid_username', false);
  462. list ($memberid, $isAdmin) = $smcFunc['db_fetch_row']($request);
  463. $smcFunc['db_free_result']($request);
  464. if ($isAdmin && $isAdmin != 'f')
  465. fatal_lang_error('no_ban_admin', 'critical');
  466. $values['id_member'] = $memberid;
  467. $modlogInfo['member'] = $memberid;
  468. }
  469. else
  470. fatal_lang_error('no_bantype_selected', false);
  471. if ($newBan)
  472. $smcFunc['db_insert']('',
  473. '{db_prefix}ban_items',
  474. $insertKeys,
  475. $values,
  476. array('id_ban')
  477. );
  478. else
  479. $smcFunc['db_query']('', '
  480. UPDATE {db_prefix}ban_items
  481. SET ' . $updateString . '
  482. WHERE id_ban = {int:ban_item}
  483. AND id_ban_group = {int:id_ban_group}',
  484. array_merge($values, array(
  485. 'ban_item' => (int) $_REQUEST['bi'],
  486. ))
  487. );
  488. // Log the addion of the ban entry into the moderation log.
  489. logAction('ban', $modlogInfo + array(
  490. 'new' => $newBan,
  491. 'type' => $_POST['bantype'],
  492. ));
  493. // Register the last modified date.
  494. updateSettings(array('banLastUpdated' => time()));
  495. // Update the member table to represent the new ban situation.
  496. updateBanMembers();
  497. }
  498. // The user pressed 'Remove selected ban entries'.
  499. elseif (!empty($_POST['remove_selection']) && !empty($_POST['ban_items']) && is_array($_POST['ban_items']))
  500. {
  501. checkSession();
  502. validateToken('admin-bet');
  503. // Making sure every deleted ban item is an integer.
  504. foreach ($_POST['ban_items'] as $key => $value)
  505. $_POST['ban_items'][$key] = (int) $value;
  506. $smcFunc['db_query']('', '
  507. DELETE FROM {db_prefix}ban_items
  508. WHERE id_ban IN ({array_int:ban_list})
  509. AND id_ban_group = {int:ban_group}',
  510. array(
  511. 'ban_list' => $_POST['ban_items'],
  512. 'ban_group' => $_REQUEST['bg'],
  513. )
  514. );
  515. // It changed, let the settings and the member table know.
  516. updateSettings(array('banLastUpdated' => time()));
  517. updateBanMembers();
  518. }
  519. // Modify OR add a ban.
  520. elseif (!empty($_POST['modify_ban']) || !empty($_POST['add_ban']))
  521. {
  522. checkSession();
  523. validateToken('admin-bet');
  524. $addBan = !empty($_POST['add_ban']);
  525. if (empty($_POST['ban_name']))
  526. fatal_lang_error('ban_name_empty', false);
  527. // Let's not allow HTML in ban names, it's more evil than beneficial.
  528. $_POST['ban_name'] = $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES);
  529. // Check whether a ban with this name already exists.
  530. $request = $smcFunc['db_query']('', '
  531. SELECT id_ban_group
  532. FROM {db_prefix}ban_groups
  533. WHERE name = {string:new_ban_name}' . ($addBan ? '' : '
  534. AND id_ban_group != {int:ban_group}') . '
  535. LIMIT 1',
  536. array(
  537. 'ban_group' => $_REQUEST['bg'],
  538. 'new_ban_name' => $_POST['ban_name'],
  539. )
  540. );
  541. if ($smcFunc['db_num_rows']($request) == 1)
  542. fatal_lang_error('ban_name_exists', false, array($_POST['ban_name']));
  543. $smcFunc['db_free_result']($request);
  544. $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
  545. $_POST['notes'] = $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES);
  546. $_POST['notes'] = str_replace(array("\r", "\n", ' '), array('', '<br />', '&nbsp; '), $_POST['notes']);
  547. $_POST['expiration'] = $_POST['expiration'] == 'never' ? 'NULL' : ($_POST['expiration'] == 'expired' ? '0' : ($_POST['expire_date'] != $_POST['old_expire'] ? time() + 24 * 60 * 60 * (int) $_POST['expire_date'] : 'expire_time'));
  548. $_POST['full_ban'] = empty($_POST['full_ban']) ? '0' : '1';
  549. $_POST['cannot_post'] = !empty($_POST['full_ban']) || empty($_POST['cannot_post']) ? '0' : '1';
  550. $_POST['cannot_register'] = !empty($_POST['full_ban']) || empty($_POST['cannot_register']) ? '0' : '1';
  551. $_POST['cannot_login'] = !empty($_POST['full_ban']) || empty($_POST['cannot_login']) ? '0' : '1';
  552. if ($addBan)
  553. {
  554. // Adding some ban triggers?
  555. if ($addBan && !empty($_POST['ban_suggestion']) && is_array($_POST['ban_suggestion']))
  556. {
  557. $ban_triggers = array();
  558. $ban_logs = array();
  559. if (in_array('main_ip', $_POST['ban_suggestion']) && !empty($_POST['main_ip']))
  560. {
  561. $ip = trim($_POST['main_ip']);
  562. $ip_parts = ip2range($ip);
  563. if (!checkExistingTriggerIP($ip_parts, $ip))
  564. fatal_lang_error('invalid_ip', false);
  565. $ban_triggers[] = array(
  566. $ip_parts[0]['low'],
  567. $ip_parts[0]['high'],
  568. $ip_parts[1]['low'],
  569. $ip_parts[1]['high'],
  570. $ip_parts[2]['low'],
  571. $ip_parts[2]['high'],
  572. $ip_parts[3]['low'],
  573. $ip_parts[3]['high'],
  574. $ip_parts[4]['low'],
  575. $ip_parts[4]['high'],
  576. $ip_parts[5]['low'],
  577. $ip_parts[5]['high'],
  578. $ip_parts[6]['low'],
  579. $ip_parts[6]['high'],
  580. $ip_parts[7]['low'],
  581. $ip_parts[7]['high'],
  582. '',
  583. '',
  584. 0,
  585. );
  586. $ban_logs[] = array(
  587. 'ip_range' => $_POST['main_ip'],
  588. );
  589. }
  590. if (in_array('hostname', $_POST['ban_suggestion']) && !empty($_POST['hostname']))
  591. {
  592. if (preg_match('/[^\w.\-*]/', $_POST['hostname']) == 1)
  593. fatal_lang_error('invalid_hostname', false);
  594. // Replace the * wildcard by a MySQL wildcard %.
  595. $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
  596. $ban_triggers[] = array(
  597. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  598. substr($_POST['hostname'], 0, 255),
  599. '',
  600. 0,
  601. );
  602. $ban_logs[] = array(
  603. 'hostname' => $_POST['hostname'],
  604. );
  605. }
  606. if (in_array('email', $_POST['ban_suggestion']) && !empty($_POST['email']))
  607. {
  608. if (preg_match('/[^\w.\-\+*@]/', $_POST['email']) == 1)
  609. fatal_lang_error('invalid_email', false);
  610. $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
  611. $ban_triggers[] = array(
  612. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  613. '',
  614. substr($_POST['email'], 0, 255),
  615. 0,
  616. );
  617. $ban_logs[] = array(
  618. 'email' => $_POST['email'],
  619. );
  620. }
  621. if (in_array('user', $_POST['ban_suggestion']) && (!empty($_POST['bannedUser']) || !empty($_POST['user'])))
  622. {
  623. // We got a username, let's find its ID.
  624. if (empty($_POST['bannedUser']))
  625. {
  626. $_POST['user'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
  627. $request = $smcFunc['db_query']('', '
  628. SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
  629. FROM {db_prefix}members
  630. WHERE member_name = {string:username} OR real_name = {string:username}
  631. LIMIT 1',
  632. array(
  633. 'admin_group' => 1,
  634. 'username' => $_POST['user'],
  635. )
  636. );
  637. if ($smcFunc['db_num_rows']($request) == 0)
  638. fatal_lang_error('invalid_username', false);
  639. list ($_POST['bannedUser'], $isAdmin) = $smcFunc['db_fetch_row']($request);
  640. $smcFunc['db_free_result']($request);
  641. if ($isAdmin && $isAdmin != 'f')
  642. fatal_lang_error('no_ban_admin', 'critical');
  643. }
  644. $ban_triggers[] = array(
  645. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  646. '',
  647. '',
  648. (int) $_POST['bannedUser'],
  649. );
  650. $ban_logs[] = array(
  651. 'member' => $_POST['bannedUser'],
  652. );
  653. }
  654. if (!empty($_POST['ban_suggestion']['ips']) && is_array($_POST['ban_suggestion']['ips']))
  655. {
  656. $_POST['ban_suggestion']['ips'] = array_unique($_POST['ban_suggestion']['ips']);
  657. // Don't add the main IP again.
  658. if (in_array('main_ip', $_POST['ban_suggestion']))
  659. $_POST['ban_suggestion']['ips'] = array_diff($_POST['ban_suggestion']['ips'], array($_POST['main_ip']));
  660. foreach ($_POST['ban_suggestion']['ips'] as $ip)
  661. {
  662. $ip_parts = ip2range($ip);
  663. // They should be alright, but just to be sure...
  664. if (count($ip_parts) != 4 || count($ip_parts) != 8)
  665. fatal_lang_error('invalid_ip', false);
  666. $ban_triggers[] = array(
  667. $ip_parts[0]['low'],
  668. $ip_parts[0]['high'],
  669. $ip_parts[1]['low'],
  670. $ip_parts[1]['high'],
  671. $ip_parts[2]['low'],
  672. $ip_parts[2]['high'],
  673. $ip_parts[3]['low'],
  674. $ip_parts[3]['high'],
  675. $ip_parts[4]['low'],
  676. $ip_parts[4]['high'],
  677. $ip_parts[5]['low'],
  678. $ip_parts[5]['high'],
  679. $ip_parts[6]['low'],
  680. $ip_parts[6]['high'],
  681. $ip_parts[7]['low'],
  682. $ip_parts[7]['high'],
  683. '',
  684. '',
  685. 0,
  686. );
  687. $ban_logs[] = array(
  688. 'ip_range' => $ip,
  689. );
  690. }
  691. }
  692. }
  693. // Yes yes, we're ready to add now.
  694. $smcFunc['db_insert']('',
  695. '{db_prefix}ban_groups',
  696. array(
  697. 'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
  698. 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
  699. ),
  700. array(
  701. $_POST['ban_name'], time(), $_POST['expiration'], $_POST['full_ban'], $_POST['cannot_register'],
  702. $_POST['cannot_post'], $_POST['cannot_login'], $_POST['reason'], $_POST['notes'],
  703. ),
  704. array('id_ban_group')
  705. );
  706. $_REQUEST['bg'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
  707. // Now that the ban group is added, add some triggers as well.
  708. if (!empty($ban_triggers) && !empty($_REQUEST['bg']))
  709. {
  710. // Put in the ban group ID.
  711. foreach ($ban_triggers as $k => $trigger)
  712. array_unshift($ban_triggers[$k], $_REQUEST['bg']);
  713. // Log what we are doing!
  714. foreach ($ban_logs as $log_details)
  715. logAction('ban', $log_details + array('new' => 1));
  716. $smcFunc['db_insert']('',
  717. '{db_prefix}ban_items',
  718. array(
  719. 'id_ban_group' => 'int', 'ip_low1' => 'int', 'ip_high1' => 'int', 'ip_low2' => 'int', 'ip_high2' => 'int',
  720. 'ip_low3' => 'int', 'ip_high3' => 'int', 'ip_low4' => 'int', 'ip_high4' => 'int', 'ip_low5' => 'int',
  721. 'ip_high5' => 'int', 'ip_low6' => 'int', 'ip_high6' => 'int', 'ip_low7' => 'int', 'ip_high7' => 'int',
  722. 'ip_low8' => 'int', 'ip_high8' => 'int', 'hostname' => 'string-255', 'email_address' => 'string-255', 'id_member' => 'int',
  723. ),
  724. $ban_triggers,
  725. array('id_ban')
  726. );
  727. }
  728. }
  729. else
  730. $smcFunc['db_query']('', '
  731. UPDATE {db_prefix}ban_groups
  732. SET
  733. name = {string:ban_name},
  734. reason = {string:reason},
  735. notes = {string:notes},
  736. expire_time = {raw:expiration},
  737. cannot_access = {int:cannot_access},
  738. cannot_post = {int:cannot_post},
  739. cannot_register = {int:cannot_register},
  740. cannot_login = {int:cannot_login}
  741. WHERE id_ban_group = {int:id_ban_group}',
  742. array(
  743. 'expiration' => $_POST['expiration'],
  744. 'cannot_access' => $_POST['full_ban'],
  745. 'cannot_post' => $_POST['cannot_post'],
  746. 'cannot_register' => $_POST['cannot_register'],
  747. 'cannot_login' => $_POST['cannot_login'],
  748. 'id_ban_group' => $_REQUEST['bg'],
  749. 'ban_name' => $_POST['ban_name'],
  750. 'reason' => $_POST['reason'],
  751. 'notes' => $_POST['notes'],
  752. )
  753. );
  754. // No more caching, we have something new here.
  755. updateSettings(array('banLastUpdated' => time()));
  756. updateBanMembers();
  757. }
  758. // If we're editing an existing ban, get it from the database.
  759. if (!empty($_REQUEST['bg']))
  760. {
  761. $context['ban_items'] = array();
  762. $request = $smcFunc['db_query']('', '
  763. SELECT
  764. bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
  765. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  766. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  767. bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, bg.cannot_access, bg.cannot_register, bg.cannot_login, bg.cannot_post,
  768. IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
  769. FROM {db_prefix}ban_groups AS bg
  770. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  771. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  772. WHERE bg.id_ban_group = {int:current_ban}',
  773. array(
  774. 'current_ban' => $_REQUEST['bg'],
  775. )
  776. );
  777. if ($smcFunc['db_num_rows']($request) == 0)
  778. fatal_lang_error('ban_not_found', false);
  779. while ($row = $smcFunc['db_fetch_assoc']($request))
  780. {
  781. if (!isset($context['ban']))
  782. {
  783. $context['ban'] = array(
  784. 'id' => $row['id_ban_group'],
  785. 'name' => $row['name'],
  786. 'expiration' => array(
  787. 'status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'still_active_but_we_re_counting_the_days'),
  788. 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0
  789. ),
  790. 'reason' => $row['reason'],
  791. 'notes' => $row['notes'],
  792. 'cannot' => array(
  793. 'access' => !empty($row['cannot_access']),
  794. 'post' => !empty($row['cannot_post']),
  795. 'register' => !empty($row['cannot_register']),
  796. 'login' => !empty($row['cannot_login']),
  797. ),
  798. 'is_new' => false,
  799. );
  800. }
  801. if (!empty($row['id_ban']))
  802. {
  803. $context['ban_items'][$row['id_ban']] = array(
  804. 'id' => $row['id_ban'],
  805. 'hits' => $row['hits'],
  806. );
  807. if (!empty($row['ip_high1']))
  808. {
  809. $context['ban_items'][$row['id_ban']]['type'] = 'ip';
  810. $context['ban_items'][$row['id_ban']]['ip'] = range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4'] ,$row['ip_low5'], $row['ip_low6'], $row['ip_low7'], $row['ip_low8']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4'], $row['ip_high5'], $row['ip_high6'], $row['ip_high7'], $row['ip_high8']));
  811. }
  812. elseif (!empty($row['hostname']))
  813. {
  814. $context['ban_items'][$row['id_ban']]['type'] = 'hostname';
  815. $context['ban_items'][$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
  816. }
  817. elseif (!empty($row['email_address']))
  818. {
  819. $context['ban_items'][$row['id_ban']]['type'] = 'email';
  820. $context['ban_items'][$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
  821. }
  822. elseif (!empty($row['id_member']))
  823. {
  824. $context['ban_items'][$row['id_ban']]['type'] = 'user';
  825. $context['ban_items'][$row['id_ban']]['user'] = array(
  826. 'id' => $row['id_member'],
  827. 'name' => $row['real_name'],
  828. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  829. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  830. );
  831. }
  832. // Invalid ban (member probably doesn't exist anymore).
  833. else
  834. {
  835. unset($context['ban_items'][$row['id_ban']]);
  836. $smcFunc['db_query']('', '
  837. DELETE FROM {db_prefix}ban_items
  838. WHERE id_ban = {int:current_ban}',
  839. array(
  840. 'current_ban' => $row['id_ban'],
  841. )
  842. );
  843. }
  844. }
  845. }
  846. $smcFunc['db_free_result']($request);
  847. }
  848. // Not an existing one, then it's probably a new one.
  849. else
  850. {
  851. $context['ban'] = array(
  852. 'id' => 0,
  853. 'name' => '',
  854. 'expiration' => array(
  855. 'status' => 'never',
  856. 'days' => 0
  857. ),
  858. 'reason' => '',
  859. 'notes' => '',
  860. 'ban_days' => 0,
  861. 'cannot' => array(
  862. 'access' => true,
  863. 'post' => false,
  864. 'register' => false,
  865. 'login' => false,
  866. ),
  867. 'is_new' => true,
  868. );
  869. $context['ban_suggestions'] = array(
  870. 'main_ip' => '',
  871. 'hostname' => '',
  872. 'email' => '',
  873. 'member' => array(
  874. 'id' => 0,
  875. ),
  876. );
  877. // Overwrite some of the default form values if a user ID was given.
  878. if (!empty($_REQUEST['u']))
  879. {
  880. $request = $smcFunc['db_query']('', '
  881. SELECT id_member, real_name, member_ip, email_address
  882. FROM {db_prefix}members
  883. WHERE id_member = {int:current_user}
  884. LIMIT 1',
  885. array(
  886. 'current_user' => (int) $_REQUEST['u'],
  887. )
  888. );
  889. if ($smcFunc['db_num_rows']($request) > 0)
  890. list ($context['ban_suggestions']['member']['id'], $context['ban_suggestions']['member']['name'], $context['ban_suggestions']['main_ip'], $context['ban_suggestions']['email']) = $smcFunc['db_fetch_row']($request);
  891. $smcFunc['db_free_result']($request);
  892. if (!empty($context['ban_suggestions']['member']['id']))
  893. {
  894. $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
  895. $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
  896. // Default the ban name to the name of the banned member.
  897. $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
  898. // Would be nice if we could also ban the hostname.
  899. if (preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 && empty($modSettings['disableHostnameLookup']))
  900. $context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
  901. // Find some additional IP's used by this member.
  902. $context['ban_suggestions']['message_ips'] = array();
  903. $request = $smcFunc['db_query']('ban_suggest_message_ips', '
  904. SELECT DISTINCT poster_ip
  905. FROM {db_prefix}messages
  906. WHERE id_member = {int:current_user}
  907. AND poster_ip RLIKE {string:poster_ip_regex}
  908. ORDER BY poster_ip',
  909. array(
  910. 'current_user' => (int) $_REQUEST['u'],
  911. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  912. )
  913. );
  914. while ($row = $smcFunc['db_fetch_assoc']($request))
  915. $context['ban_suggestions']['message_ips'][] = $row['poster_ip'];
  916. $smcFunc['db_free_result']($request);
  917. $context['ban_suggestions']['error_ips'] = array();
  918. $request = $smcFunc['db_query']('ban_suggest_error_ips', '
  919. SELECT DISTINCT ip
  920. FROM {db_prefix}log_errors
  921. WHERE id_member = {int:current_user}
  922. AND ip RLIKE {string:poster_ip_regex}
  923. ORDER BY ip',
  924. array(
  925. 'current_user' => (int) $_REQUEST['u'],
  926. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  927. )
  928. );
  929. while ($row = $smcFunc['db_fetch_assoc']($request))
  930. $context['ban_suggestions']['error_ips'][] = $row['ip'];
  931. $smcFunc['db_free_result']($request);
  932. // Borrowing a few language strings from profile.
  933. loadLanguage('Profile');
  934. }
  935. }
  936. }
  937. // Template needs this to show errors using javascript
  938. loadLanguage('Errors');
  939. $context['sub_template'] = 'ban_edit';
  940. createToken('admin-bet');
  941. }
  942. /**
  943. * This function handles the ins and outs of the screen for adding new ban
  944. * triggers or modifying existing ones.
  945. * Adding new ban triggers:
  946. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
  947. * - uses the ban_edit_trigger sub template of ManageBans.
  948. * Editing existing ban triggers:
  949. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
  950. * - uses the ban_edit_trigger sub template of ManageBans.
  951. */
  952. function action_edittrigger()
  953. {
  954. global $context, $smcFunc;
  955. $context['sub_template'] = 'ban_edit_trigger';
  956. if (empty($_REQUEST['bg']))
  957. fatal_lang_error('ban_not_found', false);
  958. if (empty($_REQUEST['bi']))
  959. {
  960. $context['ban_trigger'] = array(
  961. 'id' => 0,
  962. 'group' => (int) $_REQUEST['bg'],
  963. 'ip' => array(
  964. 'value' => '',
  965. 'selected' => true,
  966. ),
  967. 'hostname' => array(
  968. 'selected' => false,
  969. 'value' => '',
  970. ),
  971. 'email' => array(
  972. 'value' => '',
  973. 'selected' => false,
  974. ),
  975. 'banneduser' => array(
  976. 'value' => '',
  977. 'selected' => false,
  978. ),
  979. 'is_new' => true,
  980. );
  981. }
  982. else
  983. {
  984. $request = $smcFunc['db_query']('', '
  985. SELECT
  986. bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
  987. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  988. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  989. mem.member_name, mem.real_name
  990. FROM {db_prefix}ban_items AS bi
  991. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  992. WHERE bi.id_ban = {int:ban_item}
  993. AND bi.id_ban_group = {int:ban_group}
  994. LIMIT 1',
  995. array(
  996. 'ban_item' => (int) $_REQUEST['bi'],
  997. 'ban_group' => (int) $_REQUEST['bg'],
  998. )
  999. );
  1000. if ($smcFunc['db_num_rows']($request) == 0)
  1001. fatal_lang_error('ban_not_found', false);
  1002. $row = $smcFunc['db_fetch_assoc']($request);
  1003. $smcFunc['db_free_result']($request);
  1004. $context['ban_trigger'] = array(
  1005. 'id' => $row['id_ban'],
  1006. 'group' => $row['id_ban_group'],
  1007. 'ip' => array(
  1008. 'value' => empty($row['ip_low1']) ? '' : range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4'], $row['ip_low5'], $row['ip_low6'], $row['ip_low7'], $row['ip_low8']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4'], $row['ip_high5'], $row['ip_high6'], $row['ip_high7'], $row['ip_high8'])),
  1009. 'selected' => !empty($row['ip_low1']),
  1010. ),
  1011. 'hostname' => array(
  1012. 'value' => str_replace('%', '*', $row['hostname']),
  1013. 'selected' => !empty($row['hostname']),
  1014. ),
  1015. 'email' => array(
  1016. 'value' => str_replace('%', '*', $row['email_address']),
  1017. 'selected' => !empty($row['email_address'])
  1018. ),
  1019. 'banneduser' => array(
  1020. 'value' => $row['member_name'],
  1021. 'selected' => !empty($row['member_name'])
  1022. ),
  1023. 'is_new' => false,
  1024. );
  1025. }
  1026. createToken('admin-bet');
  1027. }
  1028. /**
  1029. * This handles the screen for showing the banned entities
  1030. * It is accessed by ?action=admin;area=ban;sa=browse
  1031. * It uses sub-tabs for browsing by IP, hostname, email or username.
  1032. *
  1033. * @uses ManageBans template, browse_triggers sub template.
  1034. */
  1035. function action_browse()
  1036. {
  1037. global $modSettings, $context, $scripturl, $smcFunc, $txt;
  1038. global $settings;
  1039. if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  1040. {
  1041. checkSession();
  1042. // Clean the integers.
  1043. foreach ($_POST['remove'] as $key => $value)
  1044. $_POST['remove'][$key] = $value;
  1045. $smcFunc['db_query']('', '
  1046. DELETE FROM {db_prefix}ban_items
  1047. WHERE id_ban IN ({array_int:ban_list})',
  1048. array(
  1049. 'ban_list' => $_POST['remove'],
  1050. )
  1051. );
  1052. // Rehabilitate some members.
  1053. if ($_REQUEST['entity'] == 'member')
  1054. updateBanMembers();
  1055. // Make sure the ban cache is refreshed.
  1056. updateSettings(array('banLastUpdated' => time()));
  1057. }
  1058. $context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
  1059. $listOptions = array(
  1060. 'id' => 'ban_trigger_list',
  1061. 'title' => $txt['ban_trigger_browse'],
  1062. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1063. 'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1064. 'default_sort_col' => 'banned_entity',
  1065. 'no_items_label' => $txt['ban_no_triggers'],
  1066. 'get_items' => array(
  1067. 'function' => 'list_getBanTriggers',
  1068. 'params' => array(
  1069. $context['selected_entity'],
  1070. ),
  1071. ),
  1072. 'get_count' => array(
  1073. 'function' => 'list_getNumBanTriggers',
  1074. 'params' => array(
  1075. $context['selected_entity'],
  1076. ),
  1077. ),
  1078. 'columns' => array(
  1079. 'banned_entity' => array(
  1080. 'header' => array(
  1081. 'value' => $txt['ban_banned_entity'],
  1082. ),
  1083. ),
  1084. 'ban_name' => array(
  1085. 'header' => array(
  1086. 'value' => $txt['ban_name'],
  1087. ),
  1088. 'data' => array(
  1089. 'sprintf' => array(
  1090. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
  1091. 'params' => array(
  1092. 'id_ban_group' => false,
  1093. 'name' => false,
  1094. ),
  1095. ),
  1096. ),
  1097. 'sort' => array(
  1098. 'default' => 'bg.name',
  1099. 'reverse' => 'bg.name DESC',
  1100. ),
  1101. ),
  1102. 'hits' => array(
  1103. 'header' => array(
  1104. 'value' => $txt['ban_hits'],
  1105. ),
  1106. 'data' => array(
  1107. 'db' => 'hits',
  1108. ),
  1109. 'sort' => array(
  1110. 'default' => 'bi.hits DESC',
  1111. 'reverse' => 'bi.hits',
  1112. ),
  1113. ),
  1114. 'check' => array(
  1115. 'header' => array(
  1116. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1117. 'class' => 'centercol',
  1118. ),
  1119. 'data' => array(
  1120. 'sprintf' => array(
  1121. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1122. 'params' => array(
  1123. 'id_ban' => false,
  1124. ),
  1125. ),
  1126. 'class' => 'centercol',
  1127. ),
  1128. ),
  1129. ),
  1130. 'form' => array(
  1131. 'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1132. 'include_start' => true,
  1133. 'include_sort' => true,
  1134. ),
  1135. 'additional_rows' => array(
  1136. array(
  1137. 'position' => 'above_column_headers',
  1138. 'value' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=ip">' . ($context['selected_entity'] == 'ip' ? '<img src="' . $settings['images_url'] . '/selected.png" alt="&gt;" /> ' : '') . $txt['ip'] . '</a>&nbsp;|&nbsp;<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=hostname">' . ($context['selected_entity'] == 'hostname' ? '<img src="' . $settings['images_url'] . '/selected.png" alt="&gt;" /> ' : '') . $txt['hostname'] . '</a>&nbsp;|&nbsp;<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=email">' . ($context['selected_entity'] == 'email' ? '<img src="' . $settings['images_url'] . '/selected.png" alt="&gt;" /> ' : '') . $txt['email'] . '</a>&nbsp;|&nbsp;<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=member">' . ($context['selected_entity'] == 'member' ? '<img src="' . $settings['images_url'] . '/selected.png" alt="&gt;" /> ' : '') . $txt['username'] . '</a>',
  1139. ),
  1140. array(
  1141. 'position' => 'bottom_of_list',
  1142. 'value' => '<input type="submit" name="remove_triggers" value="' . $txt['ban_remove_selected_triggers'] . '" onclick="return confirm(\'' . $txt['ban_remove_selected_triggers_confirm'] . '\');" class="button_submit" />',
  1143. ),
  1144. ),
  1145. );
  1146. // Specific data for the first column depending on the selected entity.
  1147. if ($context['selected_entity'] === 'ip')
  1148. {
  1149. $listOptions['columns']['banned_entity']['data'] = array(
  1150. 'function' => create_function('$rowData', '
  1151. return range2ip(array(
  1152. $rowData[\'ip_low1\'],
  1153. $rowData[\'ip_low2\'],
  1154. $rowData[\'ip_low3\'],
  1155. $rowData[\'ip_low4\'],
  1156. $rowData[\'ip_low5\'],
  1157. $rowData[\'ip_low6\'],
  1158. $rowData[\'ip_low7\'],
  1159. $rowData[\'ip_low8\']
  1160. ), array(
  1161. $rowData[\'ip_high1\'],
  1162. $rowData[\'ip_high2\'],
  1163. $rowData[\'ip_high3\'],
  1164. $rowData[\'ip_high4\'],
  1165. $rowData[\'ip_high5\'],
  1166. $rowData[\'ip_high6\'],
  1167. $rowData[\'ip_high7\'],
  1168. $rowData[\'ip_high8\']
  1169. ));
  1170. '),
  1171. );
  1172. $listOptions['columns']['banned_entity']['sort'] = array(
  1173. 'default' => 'bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4, bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8',
  1174. 'reverse' => 'bi.ip_low1 DESC, bi.ip_high1 DESC, bi.ip_low2 DESC, bi.ip_high2 DESC, bi.ip_low3 DESC, bi.ip_high3 DESC, bi.ip_low4 DESC, bi.ip_high4 DESC, bi.ip_low5 DESC, bi.ip_high5 DESC, bi.ip_low6 DESC, bi.ip_high6 DESC, bi.ip_low7 DESC, bi.ip_high7 DESC, bi.ip_low8 DESC, bi.ip_high8 DESC',
  1175. );
  1176. }
  1177. elseif ($context['selected_entity'] === 'hostname')
  1178. {
  1179. $listOptions['columns']['banned_entity']['data'] = array(
  1180. 'function' => create_function('$rowData', '
  1181. global $smcFunc;
  1182. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'hostname\']), array(\'%\' => \'*\'));
  1183. '),
  1184. );
  1185. $listOptions['columns']['banned_entity']['sort'] = array(
  1186. 'default' => 'bi.hostname',
  1187. 'reverse' => 'bi.hostname DESC',
  1188. );
  1189. }
  1190. elseif ($context['selected_entity'] === 'email')
  1191. {
  1192. $listOptions['columns']['banned_entity']['data'] = array(
  1193. 'function' => create_function('$rowData', '
  1194. global $smcFunc;
  1195. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'email_address\']), array(\'%\' => \'*\'));
  1196. '),
  1197. );
  1198. $listOptions['columns']['banned_entity']['sort'] = array(
  1199. 'default' => 'bi.email_address',
  1200. 'reverse' => 'bi.email_address DESC',
  1201. );
  1202. }
  1203. elseif ($context['selected_entity'] === 'member')
  1204. {
  1205. $listOptions['columns']['banned_entity']['data'] = array(
  1206. 'sprintf' => array(
  1207. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1208. 'params' => array(
  1209. 'id_member' => false,
  1210. 'real_name' => false,
  1211. ),
  1212. ),
  1213. );
  1214. $listOptions['columns']['banned_entity']['sort'] = array(
  1215. 'default' => 'mem.real_name',
  1216. 'reverse' => 'mem.real_name DESC',
  1217. );
  1218. }
  1219. // Create the list.
  1220. require_once(SUBSDIR . '/List.subs.php');
  1221. createList($listOptions);
  1222. // The list is the only thing to show, so make it the default sub template.
  1223. $context['sub_template'] = 'show_list';
  1224. $context['default_list'] = 'ban_trigger_list';
  1225. }
  1226. /**
  1227. * Get ban triggers for the given parameters.
  1228. *
  1229. * @param int $start
  1230. * @param int $items_per_page
  1231. * @param string $sort
  1232. * @param string $trigger_type
  1233. * @return array
  1234. */
  1235. function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
  1236. {
  1237. global $smcFunc;
  1238. $where = array(
  1239. 'ip' => 'bi.ip_low1 > 0',
  1240. 'hostname' => 'bi.hostname != {string:blank_string}',
  1241. 'email' => 'bi.email_address != {string:blank_string}',
  1242. );
  1243. $request = $smcFunc['db_query']('', '
  1244. SELECT
  1245. bi.id_ban, bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4, bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8, bi.hostname, bi.email_address, bi.hits,
  1246. bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
  1247. mem.id_member, mem.real_name' : '') . '
  1248. FROM {db_prefix}ban_items AS bi
  1249. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
  1250. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1251. WHERE ' . $where[$trigger_type]) . '
  1252. ORDER BY ' . $sort . '
  1253. LIMIT ' . $start . ', ' . $items_per_page,
  1254. array(
  1255. 'blank_string' => '',
  1256. )
  1257. );
  1258. $ban_triggers = array();
  1259. while ($row = $smcFunc['db_fetch_assoc']($request))
  1260. $ban_triggers[] = $row;
  1261. $smcFunc['db_free_result']($request);
  1262. return $ban_triggers;
  1263. }
  1264. /**
  1265. * This returns the total number of ban triggers of the given type.
  1266. *
  1267. * @param string $trigger_type
  1268. * @return int
  1269. */
  1270. function list_getNumBanTriggers($trigger_type)
  1271. {
  1272. global $smcFunc;
  1273. $where = array(
  1274. 'ip' => 'bi.ip_low1 > 0',
  1275. 'hostname' => 'bi.hostname != {string:blank_string}',
  1276. 'email' => 'bi.email_address != {string:blank_string}',
  1277. );
  1278. $request = $smcFunc['db_query']('', '
  1279. SELECT COUNT(*)
  1280. FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
  1281. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1282. WHERE ' . $where[$trigger_type]),
  1283. array(
  1284. 'blank_string' => '',
  1285. )
  1286. );
  1287. list ($num_triggers) = $smcFunc['db_fetch_row']($request);
  1288. $smcFunc['db_free_result']($request);
  1289. return $num_triggers;
  1290. }
  1291. /**
  1292. * This handles the listing of ban log entries, and allows their deletion.
  1293. * Shows a list of logged access attempts by banned users.
  1294. * It is accessed by ?action=admin;area=ban;sa=log.
  1295. * How it works:
  1296. * - allows sorting of several columns.
  1297. * - also handles deletion of (a selection of) log entries.
  1298. */
  1299. function action_log()
  1300. {
  1301. global $scripturl, $context, $smcFunc, $txt;
  1302. global $context;
  1303. // Delete one or more entries.
  1304. if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
  1305. {
  1306. checkSession();
  1307. validateToken('admin-bl');
  1308. // 'Delete all entries' button was pressed.
  1309. if (!empty($_POST['removeAll']))
  1310. $smcFunc['db_query']('truncate_table', '
  1311. TRUNCATE {db_prefix}log_banned',
  1312. array(
  1313. )
  1314. );
  1315. // 'Delete selection' button was pressed.
  1316. else
  1317. {
  1318. // Make sure every entry is integer.
  1319. foreach ($_POST['remove'] as $index => $log_id)
  1320. $_POST['remove'][$index] = (int) $log_id;
  1321. $smcFunc['db_query']('', '
  1322. DELETE FROM {db_prefix}log_banned
  1323. WHERE id_ban_log IN ({array_int:ban_list})',
  1324. array(
  1325. 'ban_list' => $_POST['remove'],
  1326. )
  1327. );
  1328. }
  1329. }
  1330. $listOptions = array(
  1331. 'id' => 'ban_log',
  1332. 'title' => $txt['ban_log'],
  1333. 'items_per_page' => 30,
  1334. 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1335. 'default_sort_col' => 'date',
  1336. 'get_items' => array(
  1337. 'function' => 'list_getBanLogEntries',
  1338. ),
  1339. 'get_count' => array(
  1340. 'function' => 'list_getNumBanLogEntries',
  1341. ),
  1342. 'no_items_label' => $txt['ban_log_no_entries'],
  1343. 'columns' => array(
  1344. 'ip' => array(
  1345. 'header' => array(
  1346. 'value' => $txt['ban_log_ip'],
  1347. ),
  1348. 'data' => array(
  1349. 'sprintf' => array(
  1350. 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
  1351. 'params' => array(
  1352. 'ip' => false,
  1353. ),
  1354. ),
  1355. ),
  1356. 'sort' => array(
  1357. 'default' => 'lb.ip',
  1358. 'reverse' => 'lb.ip DESC',
  1359. ),
  1360. ),
  1361. 'email' => array(
  1362. 'header' => array(
  1363. 'value' => $txt['ban_log_email'],
  1364. ),
  1365. 'data' => array(
  1366. 'db_htmlsafe' => 'email',
  1367. ),
  1368. 'sort' => array(
  1369. 'default' => 'lb.email = \'\', lb.email',
  1370. 'reverse' => 'lb.email != \'\', lb.email DESC',
  1371. ),
  1372. ),
  1373. 'member' => array(
  1374. 'header' => array(
  1375. 'value' => $txt['ban_log_member'],
  1376. ),
  1377. 'data' => array(
  1378. 'sprintf' => array(
  1379. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1380. 'params' => array(
  1381. 'id_member' => false,
  1382. 'real_name' => false,
  1383. ),
  1384. ),
  1385. ),
  1386. 'sort' => array(
  1387. 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
  1388. 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
  1389. ),
  1390. ),
  1391. 'date' => array(
  1392. 'header' => array(
  1393. 'value' => $txt['ban_log_date'],
  1394. ),
  1395. 'data' => array(
  1396. 'function' => create_function('$rowData', '
  1397. return timeformat($rowData[\'log_time\']);
  1398. '),
  1399. ),
  1400. 'sort' => array(
  1401. 'default' => 'lb.log_time DESC',
  1402. 'reverse' => 'lb.log_time',
  1403. ),
  1404. ),
  1405. 'check' => array(
  1406. 'header' => array(
  1407. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1408. 'class' => 'centercol',
  1409. ),
  1410. 'data' => array(
  1411. 'sprintf' => array(
  1412. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1413. 'params' => array(
  1414. 'id_ban_log' => false,
  1415. ),
  1416. ),
  1417. 'class' => 'centercol',
  1418. ),
  1419. ),
  1420. ),
  1421. 'form' => array(
  1422. 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1423. 'include_start' => true,
  1424. 'include_sort' => true,
  1425. 'token' => 'admin-bl',
  1426. ),
  1427. 'additional_rows' => array(
  1428. array(
  1429. 'position' => 'bottom_of_list',
  1430. 'value' => '
  1431. <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit" />
  1432. <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit" />',
  1433. ),
  1434. ),
  1435. );
  1436. createToken('admin-bl');
  1437. require_once(SUBSDIR . '/List.subs.php');
  1438. createList($listOptions);
  1439. $context['page_title'] = $txt['ban_log'];
  1440. $context['sub_template'] = 'show_list';
  1441. $context['default_list'] = 'ban_log';
  1442. }
  1443. /**
  1444. * Load a list of ban log entries from the database.
  1445. * (no permissions check)
  1446. *
  1447. * @param int $start
  1448. * @param int $items_per_page
  1449. * @param string $sort
  1450. */
  1451. function list_getBanLogEntries($start, $items_per_page, $sort)
  1452. {
  1453. global $smcFunc;
  1454. $request = $smcFunc['db_query']('', '
  1455. SELECT lb.id_ban_log, lb.id_member, IFNULL(lb.ip, {string:dash}) AS ip, IFNULL(lb.email, {string:dash}) AS email, lb.log_time, IFN

Large files files are truncated, but you can click here to view the full file