ManageBans.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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-action.
  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_getBanLogEntries($start, $items_per_page, $sort)
  1457. {
  1458. global $smcFunc;
  1459. $request = $smcFunc['db_query']('', '
  1460. 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, IFNULL(mem.real_name, {string:blank_string}) AS real_name
  1461. FROM {db_prefix}log_banned AS lb
  1462. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
  1463. ORDER BY ' . $sort . '
  1464. LIMIT ' . $start . ', ' . $items_per_page,
  1465. array(
  1466. 'blank_string' => '',
  1467. 'dash' => '-',
  1468. )
  1469. );
  1470. $log_entries = array();
  1471. while ($row = $smcFunc['db_fetch_assoc']($request))
  1472. $log_entries[] = $row;
  1473. $smcFunc['db_free_result']($request);
  1474. return $log_entries;
  1475. }
  1476. /**
  1477. * This returns the total count of ban log entries.
  1478. */
  1479. function list_getNumBanLogEntries()
  1480. {
  1481. global $smcFunc;
  1482. $request = $smcFunc['db_query']('', '
  1483. SELECT COUNT(*)
  1484. FROM {db_prefix}log_banned AS lb',
  1485. array(
  1486. )
  1487. );
  1488. list ($num_entries) = $smcFunc['db_fetch_row']($request);
  1489. $smcFunc['db_free_result']($request);
  1490. return $num_entries;
  1491. }
  1492. /**
  1493. * Convert a range of given IP number into a single string.
  1494. * It's practically the reverse function of ip2range().
  1495. *
  1496. * @example
  1497. * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
  1498. *
  1499. * @param array $low IPv4 format
  1500. * @param array $high IPv4 format
  1501. * @return string
  1502. */
  1503. function range2ip($low, $high)
  1504. {
  1505. // IPv6 check.
  1506. if (!empty($high[4]) || !empty($high[5]) || !empty($high[6]) || !empty($high[7]))
  1507. {
  1508. if (count($low) != 8 || count($high) != 8)
  1509. return '';
  1510. $ip = array();
  1511. for ($i = 0; $i < 8; $i++)
  1512. {
  1513. if ($low[$i] == $high[$i])
  1514. $ip[$i] = dechex($low[$i]);
  1515. elseif ($low[$i] == '0' && $high[$i] == '255')
  1516. $ip[$i] = '*';
  1517. else
  1518. $ip[$i] = dechex($low[$i]) . '-' . dechex($high[$i]);
  1519. }
  1520. return implode(':', $ip);
  1521. }
  1522. // Legacy IPv4 stuff.
  1523. // (count($low) != 4 || count($high) != 4) would not work because $low and $high always contain 8 elements!
  1524. if ((count($low) != 4 || count($high) != 4) && (count($low) != 8 || count($high) != 8))
  1525. return '';
  1526. $ip = array();
  1527. for ($i = 0; $i < 4; $i++)
  1528. {
  1529. if ($low[$i] == $high[$i])
  1530. $ip[$i] = $low[$i];
  1531. elseif ($low[$i] == '0' && $high[$i] == '255')
  1532. $ip[$i] = '*';
  1533. else
  1534. $ip[$i] = $low[$i] . '-' . $high[$i];
  1535. }
  1536. // Pretending is fun... the IP can't be this, so use it for 'unknown'.
  1537. if ($ip == array(255, 255, 255, 255))
  1538. return 'unknown';
  1539. return implode('.', $ip);
  1540. }
  1541. /**
  1542. * Checks whether a given IP range already exists in the trigger list.
  1543. * If yes, it returns an error message. Otherwise, it returns an array
  1544. * optimized for the database.
  1545. *
  1546. * @param array $ip_array
  1547. * @param string $fullip
  1548. * @return boolean
  1549. */
  1550. function checkExistingTriggerIP($ip_array, $fullip = '')
  1551. {
  1552. global $smcFunc, $scripturl;
  1553. if (count($ip_array) == 4 || count($ip_array) == 8)
  1554. $values = array(
  1555. 'ip_low1' => $ip_array[0]['low'],
  1556. 'ip_high1' => $ip_array[0]['high'],
  1557. 'ip_low2' => $ip_array[1]['low'],
  1558. 'ip_high2' => $ip_array[1]['high'],
  1559. 'ip_low3' => $ip_array[2]['low'],
  1560. 'ip_high3' => $ip_array[2]['high'],
  1561. 'ip_low4' => $ip_array[3]['low'],
  1562. 'ip_high4' => $ip_array[3]['high'],
  1563. 'ip_low5' => $ip_array[4]['low'],
  1564. 'ip_high5' => $ip_array[4]['high'],
  1565. 'ip_low6' => $ip_array[5]['low'],
  1566. 'ip_high6' => $ip_array[5]['high'],
  1567. 'ip_low7' => $ip_array[6]['low'],
  1568. 'ip_high7' => $ip_array[6]['high'],
  1569. 'ip_low8' => $ip_array[7]['low'],
  1570. 'ip_high8' => $ip_array[7]['high'],
  1571. );
  1572. else
  1573. return false;
  1574. $request = $smcFunc['db_query']('', '
  1575. SELECT bg.id_ban_group, bg.name
  1576. FROM {db_prefix}ban_groups AS bg
  1577. INNER JOIN {db_prefix}ban_items AS bi ON
  1578. (bi.id_ban_group = bg.id_ban_group)
  1579. AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
  1580. AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
  1581. AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
  1582. AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
  1583. AND ip_low5 = {int:ip_low5} AND ip_high5 = {int:ip_high5}
  1584. AND ip_low6 = {int:ip_low6} AND ip_high6 = {int:ip_high6}
  1585. AND ip_low7 = {int:ip_low7} AND ip_high7 = {int:ip_high7}
  1586. AND ip_low8 = {int:ip_low8} AND ip_high8 = {int:ip_high8}
  1587. LIMIT 1',
  1588. $values
  1589. );
  1590. if ($smcFunc['db_num_rows']($request) != 0)
  1591. {
  1592. list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
  1593. fatal_lang_error('ban_trigger_already_exists', false, array(
  1594. $fullip,
  1595. '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
  1596. ));
  1597. }
  1598. $smcFunc['db_free_result']($request);
  1599. return $values;
  1600. }
  1601. /**
  1602. * As it says... this tries to review the list of banned members, to match new bans.
  1603. * Note: is_activated >= 10: a member is banned.
  1604. */
  1605. function updateBanMembers()
  1606. {
  1607. global $smcFunc;
  1608. $updates = array();
  1609. $allMembers = array();
  1610. $newMembers = array();
  1611. // Start by getting all active bans - it's quicker doing this in parts...
  1612. $request = $smcFunc['db_query']('', '
  1613. SELECT bi.id_member, bi.email_address
  1614. FROM {db_prefix}ban_items AS bi
  1615. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  1616. WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
  1617. AND bg.cannot_access = {int:cannot_access_on}
  1618. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  1619. array(
  1620. 'no_member' => 0,
  1621. 'cannot_access_on' => 1,
  1622. 'current_time' => time(),
  1623. 'blank_string' => '',
  1624. )
  1625. );
  1626. $memberIDs = array();
  1627. $memberEmails = array();
  1628. $memberEmailWild = array();
  1629. while ($row = $smcFunc['db_fetch_assoc']($request))
  1630. {
  1631. if ($row['id_member'])
  1632. $memberIDs[$row['id_member']] = $row['id_member'];
  1633. if ($row['email_address'])
  1634. {
  1635. // Does it have a wildcard - if so we can't do a IN on it.
  1636. if (strpos($row['email_address'], '%') !== false)
  1637. $memberEmailWild[$row['email_address']] = $row['email_address'];
  1638. else
  1639. $memberEmails[$row['email_address']] = $row['email_address'];
  1640. }
  1641. }
  1642. $smcFunc['db_free_result']($request);
  1643. // Build up the query.
  1644. $queryPart = array();
  1645. $queryValues = array();
  1646. if (!empty($memberIDs))
  1647. {
  1648. $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
  1649. $queryValues['member_ids'] = $memberIDs;
  1650. }
  1651. if (!empty($memberEmails))
  1652. {
  1653. $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
  1654. $queryValues['member_emails'] = $memberEmails;
  1655. }
  1656. $count = 0;
  1657. foreach ($memberEmailWild as $email)
  1658. {
  1659. $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
  1660. $queryValues['wild_' . $count++] = $email;
  1661. }
  1662. // Find all banned members.
  1663. if (!empty($queryPart))
  1664. {
  1665. $request = $smcFunc['db_query']('', '
  1666. SELECT mem.id_member, mem.is_activated
  1667. FROM {db_prefix}members AS mem
  1668. WHERE ' . implode( ' OR ', $queryPart),
  1669. $queryValues
  1670. );
  1671. while ($row = $smcFunc['db_fetch_assoc']($request))
  1672. {
  1673. if (!in_array($row['id_member'], $allMembers))
  1674. {
  1675. $allMembers[] = $row['id_member'];
  1676. // Do they need an update?
  1677. if ($row['is_activated'] < 10)
  1678. {
  1679. $updates[($row['is_activated'] + 10)][] = $row['id_member'];
  1680. $newMembers[] = $row['id_member'];
  1681. }
  1682. }
  1683. }
  1684. $smcFunc['db_free_result']($request);
  1685. }
  1686. // We welcome our new members in the realm of the banned.
  1687. if (!empty($newMembers))
  1688. $smcFunc['db_query']('', '
  1689. DELETE FROM {db_prefix}log_online
  1690. WHERE id_member IN ({array_int:new_banned_members})',
  1691. array(
  1692. 'new_banned_members' => $newMembers,
  1693. )
  1694. );
  1695. // Find members that are wrongfully marked as banned.
  1696. $request = $smcFunc['db_query']('', '
  1697. SELECT mem.id_member, mem.is_activated - 10 AS new_value
  1698. FROM {db_prefix}members AS mem
  1699. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
  1700. LEFT JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group AND bg.cannot_access = {int:cannot_access_activated} AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time}))
  1701. WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
  1702. AND mem.is_activated >= {int:ban_flag}',
  1703. array(
  1704. 'cannot_access_activated' => 1,
  1705. 'current_time' => time(),
  1706. 'ban_flag' => 10,
  1707. )
  1708. );
  1709. while ($row = $smcFunc['db_fetch_assoc']($request))
  1710. {
  1711. // Don't do this twice!
  1712. if (!in_array($row['id_member'], $allMembers))
  1713. {
  1714. $updates[$row['new_value']][] = $row['id_member'];
  1715. $allMembers[] = $row['id_member'];
  1716. }
  1717. }
  1718. $smcFunc['db_free_result']($request);
  1719. if (!empty($updates))
  1720. foreach ($updates as $newStatus => $members)
  1721. updateMemberData($members, array('is_activated' => $newStatus));
  1722. // Update the latest member and our total members as banning may change them.
  1723. updateStats('member');
  1724. }
  1725. ?>