PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/ManageBans.php

https://github.com/smf-portal/SMF2.1
PHP | 1879 lines | 1531 code | 150 blank | 198 comment | 175 complexity | 56d88e4c2779873a1fb21becd9a1fd16 MD5 | raw file

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

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

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