ManageBans.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  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 2013 Simple Machines and individual contributors
  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('No direct access...');
  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. array_map('intval', $_POST['remove']);
  96. // Unban them all!
  97. removeBanGroups($_POST['remove']);
  98. removeBanTriggers($_POST['remove']);
  99. // No more caching this ban!
  100. updateSettings(array('banLastUpdated' => time()));
  101. // Some members might be unbanned now. Update the members table.
  102. updateBanMembers();
  103. }
  104. // Create a date string so we don't overload them with date info.
  105. if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0]))
  106. $context['ban_time_format'] = $user_info['time_format'];
  107. else
  108. $context['ban_time_format'] = $matches[0];
  109. $listOptions = array(
  110. 'id' => 'ban_list',
  111. 'title' => $txt['ban_title'],
  112. 'items_per_page' => 20,
  113. 'base_href' => $scripturl . '?action=admin;area=ban;sa=list',
  114. 'default_sort_col' => 'added',
  115. 'default_sort_dir' => 'desc',
  116. 'get_items' => array(
  117. 'function' => 'list_getBans',
  118. ),
  119. 'get_count' => array(
  120. 'function' => 'list_getNumBans',
  121. ),
  122. 'no_items_label' => $txt['ban_no_entries'],
  123. 'columns' => array(
  124. 'name' => array(
  125. 'header' => array(
  126. 'value' => $txt['ban_name'],
  127. ),
  128. 'data' => array(
  129. 'db' => 'name',
  130. ),
  131. 'sort' => array(
  132. 'default' => 'bg.name',
  133. 'reverse' => 'bg.name DESC',
  134. ),
  135. ),
  136. 'notes' => array(
  137. 'header' => array(
  138. 'value' => $txt['ban_notes'],
  139. ),
  140. 'data' => array(
  141. 'db' => 'notes',
  142. 'class' => 'smalltext',
  143. ),
  144. 'sort' => array(
  145. 'default' => 'LENGTH(bg.notes) > 0 DESC, bg.notes',
  146. 'reverse' => 'LENGTH(bg.notes) > 0, bg.notes DESC',
  147. ),
  148. ),
  149. 'reason' => array(
  150. 'header' => array(
  151. 'value' => $txt['ban_reason'],
  152. ),
  153. 'data' => array(
  154. 'db' => 'reason',
  155. 'class' => 'smalltext',
  156. ),
  157. 'sort' => array(
  158. 'default' => 'LENGTH(bg.reason) > 0 DESC, bg.reason',
  159. 'reverse' => 'LENGTH(bg.reason) > 0, bg.reason DESC',
  160. ),
  161. ),
  162. 'added' => array(
  163. 'header' => array(
  164. 'value' => $txt['ban_added'],
  165. ),
  166. 'data' => array(
  167. 'function' => create_function('$rowData', '
  168. global $context;
  169. return timeformat($rowData[\'ban_time\'], empty($context[\'ban_time_format\']) ? true : $context[\'ban_time_format\']);
  170. '),
  171. ),
  172. 'sort' => array(
  173. 'default' => 'bg.ban_time',
  174. 'reverse' => 'bg.ban_time DESC',
  175. ),
  176. ),
  177. 'expires' => array(
  178. 'header' => array(
  179. 'value' => $txt['ban_expires'],
  180. ),
  181. 'data' => array(
  182. 'function' => create_function('$rowData', '
  183. global $txt;
  184. // This ban never expires...whahaha.
  185. if ($rowData[\'expire_time\'] === null)
  186. return $txt[\'never\'];
  187. // This ban has already expired.
  188. elseif ($rowData[\'expire_time\'] < time())
  189. return sprintf(\'<span style="color: red">%1$s</span>\', $txt[\'ban_expired\']);
  190. // Still need to wait a few days for this ban to expire.
  191. else
  192. return sprintf(\'%1$d&nbsp;%2$s\', ceil(($rowData[\'expire_time\'] - time()) / (60 * 60 * 24)), $txt[\'ban_days\']);
  193. '),
  194. ),
  195. 'sort' => array(
  196. 'default' => 'IFNULL(bg.expire_time, 1=1) DESC, bg.expire_time DESC',
  197. 'reverse' => 'IFNULL(bg.expire_time, 1=1), bg.expire_time',
  198. ),
  199. ),
  200. 'num_triggers' => array(
  201. 'header' => array(
  202. 'value' => $txt['ban_triggers'],
  203. ),
  204. 'data' => array(
  205. 'db' => 'num_triggers',
  206. ),
  207. 'sort' => array(
  208. 'default' => 'num_triggers DESC',
  209. 'reverse' => 'num_triggers',
  210. ),
  211. ),
  212. 'actions' => array(
  213. 'header' => array(
  214. 'value' => $txt['ban_actions'],
  215. 'class' => 'centercol',
  216. ),
  217. 'data' => array(
  218. 'sprintf' => array(
  219. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">' . $txt['modify'] . '</a>',
  220. 'params' => array(
  221. 'id_ban_group' => false,
  222. ),
  223. ),
  224. 'class' => 'centercol',
  225. ),
  226. ),
  227. 'check' => array(
  228. 'header' => array(
  229. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  230. 'class' => 'centercol',
  231. ),
  232. 'data' => array(
  233. 'sprintf' => array(
  234. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  235. 'params' => array(
  236. 'id_ban_group' => false,
  237. ),
  238. ),
  239. 'class' => 'centercol',
  240. ),
  241. ),
  242. ),
  243. 'form' => array(
  244. 'href' => $scripturl . '?action=admin;area=ban;sa=list',
  245. ),
  246. 'additional_rows' => array(
  247. array(
  248. 'position' => 'bottom_of_list',
  249. 'value' => '<input type="submit" name="removeBans" value="' . $txt['ban_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_remove_selected_confirm'] . '\');" class="button_submit" />',
  250. ),
  251. ),
  252. );
  253. require_once($sourcedir . '/Subs-List.php');
  254. createList($listOptions);
  255. $context['sub_template'] = 'show_list';
  256. $context['default_list'] = 'ban_list';
  257. }
  258. /**
  259. * Get bans, what else? For the given options.
  260. *
  261. * @param int $start
  262. * @param int $items_per_page
  263. * @param string $sort
  264. * @return array
  265. */
  266. function list_getBans($start, $items_per_page, $sort)
  267. {
  268. global $smcFunc;
  269. $request = $smcFunc['db_query']('', '
  270. SELECT bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, COUNT(bi.id_ban) AS num_triggers
  271. FROM {db_prefix}ban_groups AS bg
  272. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  273. GROUP BY bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes
  274. ORDER BY {raw:sort}
  275. LIMIT {int:offset}, {int:limit}',
  276. array(
  277. 'sort' => $sort,
  278. 'offset' => $start,
  279. 'limit' => $items_per_page,
  280. )
  281. );
  282. $bans = array();
  283. while ($row = $smcFunc['db_fetch_assoc']($request))
  284. $bans[] = $row;
  285. $smcFunc['db_free_result']($request);
  286. return $bans;
  287. }
  288. /**
  289. * Get the total number of ban from the ban group table
  290. *
  291. * @return int
  292. */
  293. function list_getNumBans()
  294. {
  295. global $smcFunc;
  296. $request = $smcFunc['db_query']('', '
  297. SELECT COUNT(*) AS num_bans
  298. FROM {db_prefix}ban_groups',
  299. array(
  300. )
  301. );
  302. list ($numBans) = $smcFunc['db_fetch_row']($request);
  303. $smcFunc['db_free_result']($request);
  304. return $numBans;
  305. }
  306. /**
  307. * This function is behind the screen for adding new bans and modifying existing ones.
  308. * Adding new bans:
  309. * - is accesssed by ?action=admin;area=ban;sa=add.
  310. * - uses the ban_edit sub template of the ManageBans template.
  311. * Modifying existing bans:
  312. * - is accesssed by ?action=admin;area=ban;sa=edit;bg=x
  313. * - uses the ban_edit sub template of the ManageBans template.
  314. * - shows a list of ban triggers for the specified ban.
  315. */
  316. function BanEdit()
  317. {
  318. global $txt, $modSettings, $context, $ban_request, $scripturl, $smcFunc, $sourcedir;
  319. if ((isset($_POST['add_ban']) || isset($_POST['modify_ban']) || isset($_POST['remove_selection'])) && empty($context['ban_errors']))
  320. BanEdit2();
  321. $ban_group_id = isset($context['ban']['id']) ? $context['ban']['id'] : (isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0);
  322. // Template needs this to show errors using javascript
  323. loadLanguage('Errors');
  324. createToken('admin-bet');
  325. $context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edit';
  326. if (!empty($context['ban_errors']))
  327. {
  328. foreach ($context['ban_errors'] as $error)
  329. $context['error_messages'][$error] = $txt[$error];
  330. }
  331. else
  332. {
  333. // If we're editing an existing ban, get it from the database.
  334. if (!empty($ban_group_id))
  335. {
  336. $context['ban_group_id'] = $ban_group_id;
  337. // We're going to want this for making our list.
  338. require_once($sourcedir . '/Subs-List.php');
  339. $listOptions = array(
  340. 'id' => 'ban_items',
  341. 'base_href' => $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $ban_group_id,
  342. 'no_items_label' => $txt['ban_no_triggers'],
  343. 'items_per_page' => $modSettings['defaultMaxMessages'],
  344. 'get_items' => array(
  345. 'function' => 'list_getBanItems',
  346. 'params' => array(
  347. 'ban_group_id' => $ban_group_id,
  348. ),
  349. ),
  350. 'get_count' => array(
  351. 'function' => 'list_getNumBanItems',
  352. 'params' => array(
  353. 'ban_group_id' => $ban_group_id,
  354. ),
  355. ),
  356. 'columns' => array(
  357. 'type' => array(
  358. 'header' => array(
  359. 'value' => $txt['ban_banned_entity'],
  360. 'style' => 'width: 60%;text-align: left;',
  361. ),
  362. 'data' => array(
  363. 'function' => create_function('$ban_item', '
  364. global $txt;
  365. if (in_array($ban_item[\'type\'], array(\'ip\', \'hostname\', \'email\')))
  366. return \'<strong>\' . $txt[$ban_item[\'type\']] . \':</strong>&nbsp;\' . $ban_item[$ban_item[\'type\']];
  367. elseif ($ban_item[\'type\'] == \'user\')
  368. return \'<strong>\' . $txt[\'username\'] . \':</strong>&nbsp;\' . $ban_item[\'user\'][\'link\'];
  369. else
  370. return \'<strong>\' . $txt[\'unknown\'] . \':</strong>&nbsp;\' . $ban_item[\'no_bantype_selected\'];
  371. '),
  372. 'style' => 'text-align: left;',
  373. ),
  374. ),
  375. 'hits' => array(
  376. 'header' => array(
  377. 'value' => $txt['ban_hits'],
  378. 'style' => 'width: 15%; text-align: center;',
  379. ),
  380. 'data' => array(
  381. 'db' => 'hits',
  382. 'style' => 'text-align: center;',
  383. ),
  384. ),
  385. 'id' => array(
  386. 'header' => array(
  387. 'value' => $txt['ban_actions'],
  388. 'style' => 'width: 15%; text-align: center;',
  389. ),
  390. 'data' => array(
  391. 'function' => create_function('$ban_item', '
  392. global $txt, $context, $scripturl;
  393. return \'<a href="\' . $scripturl . \'?action=admin;area=ban;sa=edittrigger;bg=\' . $context[\'ban\'][\'id\'] . \';bi=\' . $ban_item[\'id\'] . \'">\' . $txt[\'ban_edit_trigger\'] . \'</a>\';
  394. '),
  395. 'style' => 'text-align: center;',
  396. ),
  397. ),
  398. 'checkboxes' => array(
  399. 'header' => array(
  400. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form, \'ban_items\');" class="input_check" />',
  401. 'style' => 'width: 5%; text-align: center;',
  402. ),
  403. 'data' => array(
  404. 'sprintf' => array(
  405. 'format' => '<input type="checkbox" name="ban_items[]" value="%1$d" class="input_check" />',
  406. 'params' => array(
  407. 'id' => false,
  408. ),
  409. ),
  410. 'style' => 'text-align: center;',
  411. ),
  412. ),
  413. ),
  414. 'form' => array(
  415. 'href' => $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $ban_group_id,
  416. ),
  417. 'additional_rows' => array(
  418. array(
  419. 'position' => 'below_table_data',
  420. 'value' => '
  421. <input type="submit" name="remove_selection" value="' . $txt['ban_remove_selected_triggers'] . '" class="button_submit" /> <a class="button_link" href="' . $scripturl . '?action=admin;area=ban;sa=edittrigger;bg=' . $ban_group_id . '">' . $txt['ban_add_trigger'] . '</a>',
  422. 'style' => 'text-align: right;',
  423. ),
  424. array(
  425. 'position' => 'below_table_data',
  426. 'value' => '
  427. <input type="hidden" name="bg" value="' . $ban_group_id . '" />
  428. <input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" />
  429. <input type="hidden" name="' . $context['admin-bet_token_var'] . '" value="' . $context['admin-bet_token'] . '" />',
  430. ),
  431. ),
  432. );
  433. createList($listOptions);
  434. }
  435. // Not an existing one, then it's probably a new one.
  436. else
  437. {
  438. $context['ban'] = array(
  439. 'id' => 0,
  440. 'name' => '',
  441. 'expiration' => array(
  442. 'status' => 'never',
  443. 'days' => 0
  444. ),
  445. 'reason' => '',
  446. 'notes' => '',
  447. 'ban_days' => 0,
  448. 'cannot' => array(
  449. 'access' => true,
  450. 'post' => false,
  451. 'register' => false,
  452. 'login' => false,
  453. ),
  454. 'is_new' => true,
  455. );
  456. $context['ban_suggestions'] = array(
  457. 'main_ip' => '',
  458. 'hostname' => '',
  459. 'email' => '',
  460. 'member' => array(
  461. 'id' => 0,
  462. ),
  463. );
  464. // Overwrite some of the default form values if a user ID was given.
  465. if (!empty($_REQUEST['u']))
  466. {
  467. $request = $smcFunc['db_query']('', '
  468. SELECT id_member, real_name, member_ip, email_address
  469. FROM {db_prefix}members
  470. WHERE id_member = {int:current_user}
  471. LIMIT 1',
  472. array(
  473. 'current_user' => (int) $_REQUEST['u'],
  474. )
  475. );
  476. if ($smcFunc['db_num_rows']($request) > 0)
  477. 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);
  478. $smcFunc['db_free_result']($request);
  479. if (!empty($context['ban_suggestions']['member']['id']))
  480. {
  481. $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
  482. $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
  483. // Default the ban name to the name of the banned member.
  484. $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
  485. // @todo: there should be a better solution...used to lock the "Ban on Username" input when banning from profile
  486. $context['ban']['from_user'] = true;
  487. // Would be nice if we could also ban the hostname.
  488. if ((preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 || isValidIPv6($context['ban_suggestions']['main_ip'])) && empty($modSettings['disableHostnameLookup']))
  489. $context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
  490. $context['ban_suggestions']['other_ips'] = banLoadAdditionalIPs($context['ban_suggestions']['member']['id']);
  491. }
  492. }
  493. }
  494. }
  495. // If we're in wireless mode remove the admin template layer and use a special template.
  496. if (WIRELESS && WIRELESS_PROTOCOL != 'wap')
  497. {
  498. $context['sub_template'] = WIRELESS_PROTOCOL . '_ban_edit';
  499. foreach ($context['template_layers'] as $k => $v)
  500. if (strpos($v, 'generic_menu') === 0)
  501. unset($context['template_layers'][$k]);
  502. }
  503. else
  504. {
  505. loadJavascriptFile('suggest.js', array('default_theme' => true), 'suggest.js');
  506. $context['sub_template'] = 'ban_edit';
  507. }
  508. }
  509. /**
  510. * Retrieves all the ban items belonging to a certain ban group
  511. *
  512. * @param int $start
  513. * @param int $items_per_page
  514. * @param string $sort
  515. * @param int $ban_group_id
  516. * @return array
  517. */
  518. function list_getBanItems($start = 0, $items_per_page = 0, $sort = 0, $ban_group_id = 0)
  519. {
  520. global $context, $smcFunc, $scripturl;
  521. $ban_items = array();
  522. $request = $smcFunc['db_query']('', '
  523. SELECT
  524. bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
  525. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  526. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  527. 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,
  528. IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
  529. FROM {db_prefix}ban_groups AS bg
  530. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  531. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  532. WHERE bg.id_ban_group = {int:current_ban}
  533. LIMIT {int:start}, {int:items_per_page}',
  534. array(
  535. 'current_ban' => $ban_group_id,
  536. 'start' => $start,
  537. 'items_per_page' => $items_per_page,
  538. )
  539. );
  540. if ($smcFunc['db_num_rows']($request) == 0)
  541. fatal_lang_error('ban_not_found', false);
  542. while ($row = $smcFunc['db_fetch_assoc']($request))
  543. {
  544. if (!isset($context['ban']))
  545. {
  546. $context['ban'] = array(
  547. 'id' => $row['id_ban_group'],
  548. 'name' => $row['name'],
  549. 'expiration' => array(
  550. 'status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'one_day'),
  551. 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0
  552. ),
  553. 'reason' => $row['reason'],
  554. 'notes' => $row['notes'],
  555. 'cannot' => array(
  556. 'access' => !empty($row['cannot_access']),
  557. 'post' => !empty($row['cannot_post']),
  558. 'register' => !empty($row['cannot_register']),
  559. 'login' => !empty($row['cannot_login']),
  560. ),
  561. 'is_new' => false,
  562. 'hostname' => '',
  563. 'email' => '',
  564. );
  565. }
  566. if (!empty($row['id_ban']))
  567. {
  568. $ban_items[$row['id_ban']] = array(
  569. 'id' => $row['id_ban'],
  570. 'hits' => $row['hits'],
  571. );
  572. if (!empty($row['ip_high1']))
  573. {
  574. $ban_items[$row['id_ban']]['type'] = 'ip';
  575. $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']));
  576. }
  577. elseif (!empty($row['hostname']))
  578. {
  579. $ban_items[$row['id_ban']]['type'] = 'hostname';
  580. $ban_items[$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
  581. }
  582. elseif (!empty($row['email_address']))
  583. {
  584. $ban_items[$row['id_ban']]['type'] = 'email';
  585. $ban_items[$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
  586. }
  587. elseif (!empty($row['id_member']))
  588. {
  589. $ban_items[$row['id_ban']]['type'] = 'user';
  590. $ban_items[$row['id_ban']]['user'] = array(
  591. 'id' => $row['id_member'],
  592. 'name' => $row['real_name'],
  593. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  594. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  595. );
  596. }
  597. // Invalid ban (member probably doesn't exist anymore).
  598. else
  599. {
  600. unset($ban_items[$row['id_ban']]);
  601. removeBanTriggers($row['id_ban']);
  602. }
  603. }
  604. }
  605. $smcFunc['db_free_result']($request);
  606. return $ban_items;
  607. }
  608. /**
  609. * Gets the number of ban items belonging to a certain ban group
  610. *
  611. * @return int
  612. */
  613. function list_getNumBanItems()
  614. {
  615. global $smcFunc, $context;
  616. $ban_group_id = isset($context['ban_group_id']) ? $context['ban_group_id'] : 0;
  617. $request = $smcFunc['db_query']('', '
  618. SELECT COUNT(bi.id_ban)
  619. FROM {db_prefix}ban_groups AS bg
  620. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  621. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  622. WHERE bg.id_ban_group = {int:current_ban}',
  623. array(
  624. 'current_ban' => $ban_group_id,
  625. )
  626. );
  627. list($banNumber) = $smcFunc['db_fetch_row']($request);
  628. $smcFunc['db_free_result']($request);
  629. return $banNumber;
  630. }
  631. /**
  632. * Finds additional IPs related to a certain user
  633. *
  634. * @param int $member_id
  635. * @return array
  636. */
  637. function banLoadAdditionalIPs($member_id)
  638. {
  639. // Borrowing a few language strings from profile.
  640. loadLanguage('Profile');
  641. $search_list = array();
  642. call_integration_hook('integrate_load_addtional_ip_ban', array(&$search_list));
  643. $search_list += array('ips_in_messages' => 'banLoadAdditionalIPsMember', 'ips_in_errors' => 'banLoadAdditionalIPsError');
  644. $return = array();
  645. foreach ($search_list as $key => $callable)
  646. if (is_callable($callable))
  647. $return[$key] = call_user_func($callable, $member_id);
  648. return $return;
  649. }
  650. function banLoadAdditionalIPsMember($member_id)
  651. {
  652. global $smcFunc;
  653. // Find some additional IP's used by this member.
  654. $message_ips = array();
  655. $request = $smcFunc['db_query']('ban_suggest_message_ips', '
  656. SELECT DISTINCT poster_ip
  657. FROM {db_prefix}messages
  658. WHERE id_member = {int:current_user}
  659. AND poster_ip RLIKE {string:poster_ip_regex}
  660. ORDER BY poster_ip',
  661. array(
  662. 'current_user' => $member_id,
  663. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  664. )
  665. );
  666. while ($row = $smcFunc['db_fetch_assoc']($request))
  667. $message_ips[] = $row['poster_ip'];
  668. $smcFunc['db_free_result']($request);
  669. return $message_ips;
  670. }
  671. function banLoadAdditionalIPsError($member_id)
  672. {
  673. global $smcFunc;
  674. $error_ips = array();
  675. $request = $smcFunc['db_query']('ban_suggest_error_ips', '
  676. SELECT DISTINCT ip
  677. FROM {db_prefix}log_errors
  678. WHERE id_member = {int:current_user}
  679. AND ip RLIKE {string:poster_ip_regex}
  680. ORDER BY ip',
  681. array(
  682. 'current_user' => $member_id,
  683. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  684. )
  685. );
  686. while ($row = $smcFunc['db_fetch_assoc']($request))
  687. $error_ips[] = $row['ip'];
  688. $smcFunc['db_free_result']($request);
  689. return $error_ips;
  690. }
  691. /**
  692. * This function handles submitted forms that add, modify or remove ban triggers.
  693. */
  694. function banEdit2()
  695. {
  696. global $smcFunc, $context;
  697. checkSession();
  698. validateToken('admin-bet');
  699. $context['ban_errors'] = array();
  700. // Adding or editing a ban group
  701. if (isset($_POST['add_ban']) || isset($_POST['modify_ban']))
  702. {
  703. // Let's collect all the information we need
  704. $ban_info['id'] = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  705. $ban_info['is_new'] = empty($ban_info['id']);
  706. $ban_info['expire_date'] = !empty($_POST['expire_date']) ? (int) $_POST['expire_date'] : 0;
  707. $ban_info['expiration'] = array(
  708. 'status' => isset($_POST['expiration']) && in_array($_POST['expiration'], array('never', 'one_day', 'expired')) ? $_POST['expiration'] : 'never',
  709. 'days' => $ban_info['expire_date'],
  710. );
  711. $ban_info['db_expiration'] = $ban_info['expiration']['status'] == 'never' ? 'NULL' : ($ban_info['expiration']['status'] == 'one_day' ? time() + 24 * 60 * 60 * $ban_info['expire_date'] : 0);
  712. $ban_info['full_ban'] = empty($_POST['full_ban']) ? 0 : 1;
  713. $ban_info['reason'] = !empty($_POST['reason']) ? $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES) : '';
  714. $ban_info['name'] = !empty($_POST['ban_name']) ? $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES) : '';
  715. $ban_info['notes'] = isset($_POST['notes']) ? $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES) : '';
  716. $ban_info['notes'] = str_replace(array("\r", "\n", ' '), array('', '<br />', '&nbsp; '), $ban_info['notes']);
  717. $ban_info['cannot']['access'] = empty($ban_info['full_ban']) ? 0 : 1;
  718. $ban_info['cannot']['post'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_post']) ? 0 : 1;
  719. $ban_info['cannot']['register'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_register']) ? 0 : 1;
  720. $ban_info['cannot']['login'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_login']) ? 0 : 1;
  721. // Adding a new ban group
  722. if (empty($_REQUEST['bg']))
  723. $ban_group_id = insertBanGroup($ban_info);
  724. // Editing an existing ban group
  725. else
  726. $ban_group_id = updateBanGroup($ban_info);
  727. if (is_numeric($ban_group_id))
  728. {
  729. $ban_info['id'] = $ban_group_id;
  730. $ban_info['is_new'] = false;
  731. }
  732. $context['ban'] = $ban_info;
  733. }
  734. if (isset($_POST['ban_suggestions']))
  735. // @TODO: is $_REQUEST['bi'] ever set?
  736. $saved_triggers = saveTriggers($_POST['ban_suggestions'], $ban_info['id'], isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0, isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0);
  737. // Something went wrong somewhere... Oh well, let's go back.
  738. if (!empty($context['ban_errors']))
  739. {
  740. $context['ban_suggestions'] = $saved_triggers;
  741. $context['ban']['from_user'] = true;
  742. $context['ban_suggestions'] = array_merge($context['ban_suggestions'], getMemberData((int) $_REQUEST['u']));
  743. // Not strictly necessary, but it's nice
  744. if (!empty($context['ban_suggestions']['member']['id']))
  745. $context['ban_suggestions']['other_ips'] = banLoadAdditionalIPs($context['ban_suggestions']['member']['id']);
  746. return BanEdit();
  747. }
  748. $context['ban_suggestions']['saved_triggers'] = $saved_triggers;
  749. if (isset($_POST['ban_items']))
  750. {
  751. $items_ids = array();
  752. $ban_group_id = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  753. array_map('intval', $_POST['ban_items']);
  754. removeBanTriggers($_POST['ban_items'], $ban_group_id);
  755. }
  756. // Register the last modified date.
  757. updateSettings(array('banLastUpdated' => time()));
  758. // Update the member table to represent the new ban situation.
  759. updateBanMembers();
  760. redirectexit('action=admin;area=ban;sa=edit;bg=' . $ban_group_id);
  761. }
  762. /**
  763. * Saves one or more ban triggers into a ban item: according to the suggestions
  764. * checks the $_POST variable to verify if the trigger is present
  765. *
  766. * @param array $suggestions
  767. * @param int $ban_group
  768. * @param int $member
  769. * @param int $ban_id
  770. *
  771. * @return mixed array with the saved triggers or false on failure
  772. */
  773. function saveTriggers($suggestions = array(), $ban_group, $member = 0, $ban_id = 0)
  774. {
  775. $triggers = array(
  776. 'main_ip' => '',
  777. 'hostname' => '',
  778. 'email' => '',
  779. 'member' => array(
  780. 'id' => $member,
  781. )
  782. );
  783. $ban_triggers = array();
  784. foreach ($suggestions as $key => $value)
  785. {
  786. if (is_array($value))
  787. $triggers[$key] = $value;
  788. else
  789. $triggers[$value] = !empty($_POST[$value]) ? $_POST[$value] : '';
  790. }
  791. $ban_triggers = validateTriggers($triggers);
  792. // Time to save!
  793. if (!empty($ban_triggers['ban_triggers']) && empty($context['ban_errors']))
  794. {
  795. if (empty($ban_id))
  796. addTriggers($ban_group, $ban_triggers['ban_triggers'], $ban_triggers['log_info']);
  797. else
  798. updateTriggers($ban_id, $ban_group, array_shift($ban_triggers['ban_triggers']), $ban_triggers['log_info']);
  799. }
  800. if (!empty($context['ban_errors']))
  801. return $triggers;
  802. else
  803. return false;
  804. }
  805. /**
  806. * This function removes a bunch of triggers based on ids
  807. * Doesn't clean the inputs
  808. *
  809. * @param array $items_ids
  810. * @return bool
  811. */
  812. function removeBanTriggers($items_ids = array(), $group_id = false)
  813. {
  814. global $smcFunc;
  815. if ($group_id !== false)
  816. $group_id = (int) $group_id;
  817. if (empty($group_id) && empty($items_ids))
  818. return false;
  819. if (!is_array($items_ids))
  820. $items_ids = array($items_ids);
  821. $log_info = array();
  822. $ban_items = array();
  823. // First order of business: Load up the info so we can log this...
  824. $request = $smcFunc['db_query']('', '
  825. SELECT
  826. bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
  827. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  828. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  829. IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
  830. FROM {db_prefix}ban_items AS bi
  831. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  832. WHERE bi.id_ban IN ({array_int:ban_list})',
  833. array(
  834. 'ban_list' => $items_ids,
  835. )
  836. );
  837. // Get all the info for the log
  838. while ($row = $smcFunc['db_fetch_assoc']($request))
  839. {
  840. if (!empty($row['id_ban']))
  841. {
  842. $ban_items[$row['id_ban']] = array(
  843. 'id' => $row['id_ban'],
  844. );
  845. if (!empty($row['ip_high1']))
  846. {
  847. $ban_items[$row['id_ban']]['type'] = 'ip';
  848. $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']));
  849. $is_range = (strpos($ban_items[$row['id_ban']]['ip'], '-') !== false || strpos($ban_items[$row['id_ban']]['ip'], '*') !== false);
  850. $log_info[] = array(
  851. 'bantype' => ($is_range ? 'ip_range' : 'main_ip'),
  852. 'value' => $ban_items[$row['id_ban']]['ip'],
  853. );
  854. }
  855. elseif (!empty($row['hostname']))
  856. {
  857. $ban_items[$row['id_ban']]['type'] = 'hostname';
  858. $ban_items[$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
  859. $log_info[] = array(
  860. 'bantype' => 'hostname',
  861. 'value' => $row['hostname'],
  862. );
  863. }
  864. elseif (!empty($row['email_address']))
  865. {
  866. $ban_items[$row['id_ban']]['type'] = 'email';
  867. $ban_items[$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
  868. $log_info[] = array(
  869. 'bantype' => 'email',
  870. 'value' => $ban_items[$row['id_ban']]['email'],
  871. );
  872. }
  873. elseif (!empty($row['id_member']))
  874. {
  875. $ban_items[$row['id_ban']]['type'] = 'user';
  876. $ban_items[$row['id_ban']]['user'] = array(
  877. 'id' => $row['id_member'],
  878. 'name' => $row['real_name'],
  879. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  880. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  881. );
  882. $log_info[] = array(
  883. 'bantype' => 'user',
  884. 'value' => $row['id_member'],
  885. );
  886. }
  887. }
  888. }
  889. // Log this!
  890. logTriggersUpdates($log_info, false, true);
  891. $smcFunc['db_free_result']($request);
  892. if ($group_id !== false)
  893. {
  894. $smcFunc['db_query']('', '
  895. DELETE FROM {db_prefix}ban_items
  896. WHERE id_ban IN ({array_int:ban_list})
  897. AND id_ban_group = {int:ban_group}',
  898. array(
  899. 'ban_list' => $items_ids,
  900. 'ban_group' => $group_id,
  901. )
  902. );
  903. }
  904. elseif (!empty($items_ids))
  905. {
  906. $smcFunc['db_query']('', '
  907. DELETE FROM {db_prefix}ban_items
  908. WHERE id_ban IN ({array_int:ban_list})',
  909. array(
  910. 'ban_list' => $items_ids,
  911. )
  912. );
  913. }
  914. return true;
  915. }
  916. /**
  917. * This function removes a bunch of ban groups based on ids
  918. * Doesn't clean the inputs
  919. *
  920. * @param array $group_ids
  921. * @return bool
  922. */
  923. function removeBanGroups($group_ids)
  924. {
  925. global $smcFunc;
  926. if (!is_array($group_ids))
  927. $group_ids = array($group_ids);
  928. $group_ids = array_unique($group_ids);
  929. if (empty($group_ids))
  930. return false;
  931. $smcFunc['db_query']('', '
  932. DELETE FROM {db_prefix}ban_groups
  933. WHERE id_ban_group IN ({array_int:ban_list})',
  934. array(
  935. 'ban_list' => $group_ids,
  936. )
  937. );
  938. return true;
  939. }
  940. /**
  941. * Removes logs - by default truncate the table
  942. * Doesn't clean the inputs
  943. *
  944. * @param array (optional) $ids
  945. * @return bool
  946. */
  947. function removeBanLogs($ids = array())
  948. {
  949. global $smcFunc;
  950. if (empty($ids))
  951. $smcFunc['db_query']('truncate_table', '
  952. TRUNCATE {db_prefix}log_banned',
  953. array(
  954. )
  955. );
  956. else
  957. {
  958. if (!is_array($ids))
  959. $ids = array($ids);
  960. $ids = array_unique($ids);
  961. if (empty($ids))
  962. return false;
  963. $smcFunc['db_query']('', '
  964. DELETE FROM {db_prefix}log_banned
  965. WHERE id_ban_log IN ({array_int:ban_list})',
  966. array(
  967. 'ban_list' => $ids,
  968. )
  969. );
  970. }
  971. return true;
  972. }
  973. /**
  974. * This function validates the ban triggers
  975. *
  976. * Errors in $context['ban_errors']
  977. *
  978. * @param array $triggers
  979. * @return array triggers and logs info ready to be used
  980. */
  981. function validateTriggers(&$triggers)
  982. {
  983. global $context, $smcFunc;
  984. if (empty($triggers))
  985. $context['ban_erros'][] = 'ban_empty_triggers';
  986. $ban_triggers = array();
  987. $log_info = array();
  988. foreach ($triggers as $key => $value)
  989. {
  990. if (!empty($value))
  991. {
  992. if ($key == 'member')
  993. continue;
  994. if ($key == 'main_ip')
  995. {
  996. $value = trim($value);
  997. $ip_parts = ip2range($value);
  998. if (!checkExistingTriggerIP($ip_parts, $value))
  999. $context['ban_erros'][] = 'invalid_ip';
  1000. else
  1001. {
  1002. $ban_triggers['main_ip'] = array(
  1003. 'ip_low1' => $ip_parts[0]['low'],
  1004. 'ip_high1' => $ip_parts[0]['high'],
  1005. 'ip_low2' => $ip_parts[1]['low'],
  1006. 'ip_high2' => $ip_parts[1]['high'],
  1007. 'ip_low3' => $ip_parts[2]['low'],
  1008. 'ip_high3' => $ip_parts[2]['high'],
  1009. 'ip_low4' => $ip_parts[3]['low'],
  1010. 'ip_high4' => $ip_parts[3]['high'],
  1011. 'ip_low5' => $ip_parts[4]['low'],
  1012. 'ip_high5' => $ip_parts[4]['high'],
  1013. 'ip_low6' => $ip_parts[5]['low'],
  1014. 'ip_high6' => $ip_parts[5]['high'],
  1015. 'ip_low7' => $ip_parts[6]['low'],
  1016. 'ip_high7' => $ip_parts[6]['high'],
  1017. 'ip_low8' => $ip_parts[7]['low'],
  1018. 'ip_high8' => $ip_parts[7]['high'],
  1019. );
  1020. }
  1021. }
  1022. elseif ($key == 'hostname')
  1023. {
  1024. if (preg_match('/[^\w.\-*]/', $value) == 1)
  1025. $context['ban_erros'][] = 'invalid_hostname';
  1026. else
  1027. {
  1028. // Replace the * wildcard by a MySQL wildcard %.
  1029. $value = substr(str_replace('*', '%', $value), 0, 255);
  1030. $ban_triggers['hostname']['hostname'] = $value;
  1031. }
  1032. }
  1033. elseif ($key == 'email')
  1034. {
  1035. if (preg_match('/[^\w.\-\+*@]/', $value) == 1)
  1036. $context['ban_erros'][] = 'invalid_email';
  1037. // Check the user is not banning an admin.
  1038. $request = $smcFunc['db_query']('', '
  1039. SELECT id_member
  1040. FROM {db_prefix}members
  1041. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  1042. AND email_address LIKE {string:email}
  1043. LIMIT 1',
  1044. array(
  1045. 'admin_group' => 1,
  1046. 'email' => $value,
  1047. )
  1048. );
  1049. if ($smcFunc['db_num_rows']($request) != 0)
  1050. $context['ban_erros'][] = 'no_ban_admin';
  1051. $smcFunc['db_free_result']($request);
  1052. $value = substr(strtolower(str_replace('*', '%', $value)), 0, 255);
  1053. $ban_triggers['email']['email_address'] = $value;
  1054. }
  1055. elseif ($key == 'user')
  1056. {
  1057. $user = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($value, ENT_QUOTES));
  1058. $request = $smcFunc['db_query']('', '
  1059. SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
  1060. FROM {db_prefix}members
  1061. WHERE member_name = {string:username} OR real_name = {string:username}
  1062. LIMIT 1',
  1063. array(
  1064. 'admin_group' => 1,
  1065. 'username' => $user,
  1066. )
  1067. );
  1068. if ($smcFunc['db_num_rows']($request) == 0)
  1069. $context['ban_erros'][] = 'invalid_username';
  1070. list ($value, $isAdmin) = $smcFunc['db_fetch_row']($request);
  1071. $smcFunc['db_free_result']($request);
  1072. if ($isAdmin && strtolower($isAdmin) != 'f')
  1073. {
  1074. unset($value);
  1075. $context['ban_erros'][] = 'no_ban_admin';
  1076. }
  1077. else
  1078. $ban_triggers['user']['id_member'] = $value;
  1079. }
  1080. elseif (in_array($key, array('ips_in_messages', 'ips_in_errors')))
  1081. {
  1082. // Special case, those two are arrays themselves
  1083. $values = array_unique($value);
  1084. // Don't add the main IP again.
  1085. if (isset($triggers['main_ip']))
  1086. $values = array_diff($values, array($triggers['main_ip']));
  1087. unset($value);
  1088. foreach ($values as $val)
  1089. {
  1090. $val = trim($val);
  1091. $ip_parts = ip2range($val);
  1092. if (!checkExistingTriggerIP($ip_parts, $val))
  1093. $context['ban_erros'][] = 'invalid_ip';
  1094. else
  1095. {
  1096. $ban_triggers[$key][] = array(
  1097. 'ip_low1' => $ip_parts[0]['low'],
  1098. 'ip_high1' => $ip_parts[0]['high'],
  1099. 'ip_low2' => $ip_parts[1]['low'],
  1100. 'ip_high2' => $ip_parts[1]['high'],
  1101. 'ip_low3' => $ip_parts[2]['low'],
  1102. 'ip_high3' => $ip_parts[2]['high'],
  1103. 'ip_low4' => $ip_parts[3]['low'],
  1104. 'ip_high4' => $ip_parts[3]['high'],
  1105. 'ip_low5' => $ip_parts[4]['low'],
  1106. 'ip_high5' => $ip_parts[4]['high'],
  1107. 'ip_low6' => $ip_parts[5]['low'],
  1108. 'ip_high6' => $ip_parts[5]['high'],
  1109. 'ip_low7' => $ip_parts[6]['low'],
  1110. 'ip_high7' => $ip_parts[6]['high'],
  1111. 'ip_low8' => $ip_parts[7]['low'],
  1112. 'ip_high8' => $ip_parts[7]['high'],
  1113. );
  1114. $log_info[] = array(
  1115. 'value' => $val,
  1116. 'bantype' => 'ip_range',
  1117. );
  1118. }
  1119. }
  1120. }
  1121. else
  1122. $context['ban_erros'][] = 'no_bantype_selected';
  1123. if (isset($value) && !is_array($value))
  1124. $log_info[] = array(
  1125. 'value' => $value,
  1126. 'bantype' => $key,
  1127. );
  1128. }
  1129. }
  1130. return array('ban_triggers' => $ban_triggers, 'log_info' => $log_info);
  1131. }
  1132. /**
  1133. * This function actually inserts the ban triggers into the database
  1134. *
  1135. * Errors in $context['ban_errors']
  1136. *
  1137. * @param int $group_id
  1138. * @param array $triggers
  1139. * @param array $logs
  1140. * @return nothing
  1141. */
  1142. function addTriggers($group_id = 0, $triggers = array(), $logs = array())
  1143. {
  1144. global $smcFunc;
  1145. if (empty($group_id))
  1146. $context['ban_errors'][] = 'ban_group_id_empty';
  1147. // Preset all values that are required.
  1148. $values = array(
  1149. 'id_ban_group' => $group_id,
  1150. 'hostname' => '',
  1151. 'email_address' => '',
  1152. 'id_member' => 0,
  1153. 'ip_low1' => 0,
  1154. 'ip_high1' => 0,
  1155. 'ip_low2' => 0,
  1156. 'ip_high2' => 0,
  1157. 'ip_low3' => 0,
  1158. 'ip_high3' => 0,
  1159. 'ip_low4' => 0,
  1160. 'ip_high4' => 0,
  1161. 'ip_low5' => 0,
  1162. 'ip_high5' => 0,
  1163. 'ip_low6' => 0,
  1164. 'ip_high6' => 0,
  1165. 'ip_low7' => 0,
  1166. 'ip_high7' => 0,
  1167. 'ip_low8' => 0,
  1168. 'ip_high8' => 0,
  1169. );
  1170. $insertKeys = array(
  1171. 'id_ban_group' => 'int',
  1172. 'hostname' => 'string',
  1173. 'email_address' => 'string',
  1174. 'id_member' => 'int',
  1175. 'ip_low1' => 'int',
  1176. 'ip_high1' => 'int',
  1177. 'ip_low2' => 'int',
  1178. 'ip_high2' => 'int',
  1179. 'ip_low3' => 'int',
  1180. 'ip_high3' => 'int',
  1181. 'ip_low4' => 'int',
  1182. 'ip_high4' => 'int',
  1183. 'ip_low5' => 'int',
  1184. 'ip_high5' => 'int',
  1185. 'ip_low6' => 'int',
  1186. 'ip_high6' => 'int',
  1187. 'ip_low7' => 'int',
  1188. 'ip_high7' => 'int',
  1189. 'ip_low8' => 'int',
  1190. 'ip_high8' => 'int',
  1191. );
  1192. $insertTriggers = array();
  1193. foreach ($triggers as $key => $trigger)
  1194. {
  1195. // Exceptions, exceptions, exceptions...always exceptions... :P
  1196. if (in_array($key, array('ips_in_messages', 'ips_in_errors')))
  1197. foreach ($trigger as $real_trigger)
  1198. $insertTriggers[] = array_merge($values, $real_trigger);
  1199. else
  1200. $insertTriggers[] = array_merge($values, $trigger);
  1201. }
  1202. if (empty($insertTriggers))
  1203. $context['ban_errors'][] = 'ban_no_triggers';
  1204. if (!empty($context['ban_errors']))
  1205. return false;
  1206. $smcFunc['db_insert']('',
  1207. '{db_prefix}ban_items',
  1208. $insertKeys,
  1209. $insertTriggers,
  1210. array('id_ban')
  1211. );
  1212. logTriggersUpdates($logs, true);
  1213. return true;
  1214. }
  1215. /**
  1216. * This function updates an existing ban trigger into the database
  1217. *
  1218. * Errors in $context['ban_errors']
  1219. *
  1220. * @param int $ban_item
  1221. * @param int $group_id
  1222. * @param array $trigger
  1223. * @param array $logs
  1224. * @return nothing
  1225. */
  1226. function updateTriggers($ban_item = 0, $group_id = 0, $trigger = array(), $logs = array())
  1227. {
  1228. global $smcFunc, $context;
  1229. if (empty($ban_item))
  1230. $context['ban_errors'][] = 'ban_ban_item_empty';
  1231. if (empty($group_id))
  1232. $context['ban_errors'][] = 'ban_group_id_empty';
  1233. if (empty($trigger))
  1234. $context['ban_errors'][] = 'ban_no_triggers';
  1235. if (!empty($context['ban_errors']))
  1236. return;
  1237. // Preset all values that are required.
  1238. $values = array(
  1239. 'id_ban_group' => $group_id,
  1240. 'hostname' => '',
  1241. 'email_address' => '',
  1242. 'id_member' => 0,
  1243. 'ip_low1' => 0,
  1244. 'ip_high1' => 0,
  1245. 'ip_low2' => 0,
  1246. 'ip_high2' => 0,
  1247. 'ip_low3' => 0,
  1248. 'ip_high3' => 0,
  1249. 'ip_low4' => 0,
  1250. 'ip_high4' => 0,
  1251. 'ip_low5' => 0,
  1252. 'ip_high5' => 0,
  1253. 'ip_low6' => 0,
  1254. 'ip_high6' => 0,
  1255. 'ip_low7' => 0,
  1256. 'ip_high7' => 0,
  1257. 'ip_low8' => 0,
  1258. 'ip_high8' => 0,
  1259. );
  1260. $trigger = array_merge($values, $trigger);
  1261. $smcFunc['db_query']('', '
  1262. UPDATE {db_prefix}ban_items
  1263. SET
  1264. hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
  1265. ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
  1266. ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
  1267. ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
  1268. ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4},
  1269. ip_low5 = {int:ip_low5}, ip_high5 = {int:ip_high5},
  1270. ip_low6 = {int:ip_low6}, ip_high6 = {int:ip_high6},
  1271. ip_low7 = {int:ip_low7}, ip_high7 = {int:ip_high7},
  1272. ip_low8 = {int:ip_low8}, ip_high8 = {int:ip_high8}
  1273. WHERE id_ban = {int:ban_item}
  1274. AND id_ban_group = {int:id_ban_group}',
  1275. array_merge($trigger, array(
  1276. 'id_ban_group' => $group_id,
  1277. 'ban_item' => $ban_item,
  1278. ))
  1279. );
  1280. logTriggersUpdates($logs, false);
  1281. }
  1282. /**
  1283. * A small function to unify logging of triggers (updates and new)
  1284. *
  1285. * @param array $logs an array of logs, each log contains the following keys:
  1286. * - bantype: a known type of ban (ip_range, hostname, email, user, main_ip)
  1287. * - value: the value of the bantype (e.g. the IP or the email address banned)
  1288. * @param bool $new if the trigger is new or an update of an existing one
  1289. * @param bool $removal if the trigger is being deleted
  1290. */
  1291. function logTriggersUpdates($logs, $new = true, $removal = false)
  1292. {
  1293. if (empty($logs))
  1294. return;
  1295. $log_name_map = array(
  1296. 'main_ip' => 'ip_range',
  1297. 'hostname' => 'hostname',
  1298. 'email' => 'email',
  1299. 'user' => 'member',
  1300. 'ip_range' => 'ip_range',
  1301. );
  1302. // Log the addion of the ban entries into the moderation log.
  1303. foreach ($logs as $log)
  1304. logAction('ban' . ($removal == true ? 'remove' : ''), array(
  1305. $log_name_map[$log['bantype']] => $log['value'],
  1306. 'new' => empty($new) ? 0 : 1,
  1307. 'remove' => empty($removal) ? 0 : 1,
  1308. 'type' => $log['bantype'],
  1309. ));
  1310. }
  1311. /**
  1312. * Updates an existing ban group
  1313. * If the name doesn't exists a new one is created
  1314. *
  1315. * Errors in $context['ban_errors']
  1316. *
  1317. * @param array $ban_info
  1318. * @return nothing
  1319. */
  1320. function updateBanGroup($ban_info = array())
  1321. {
  1322. global $smcFunc, $context;
  1323. if (empty($ban_info['name']))
  1324. $context['ban_errors'][] = 'ban_name_empty';
  1325. if (empty($ban_info['id']))
  1326. $context['ban_errors'][] = 'ban_id_empty';
  1327. if (!empty($context['ban_errors']))
  1328. return;
  1329. $request = $smcFunc['db_query']('', '
  1330. SELECT id_ban_group
  1331. FROM {db_prefix}ban_groups
  1332. WHERE name = {string:new_ban_name}
  1333. AND id_ban_group = {int:ban_group}
  1334. LIMIT 1',
  1335. array(
  1336. 'ban_group' => $ban_info['id'],
  1337. 'new_ban_name' => $ban_info['name'],
  1338. )
  1339. );
  1340. if ($smcFunc['db_num_rows']($request) == 0)
  1341. return insertBanGroup($ban_info);
  1342. $smcFunc['db_free_result']($request);
  1343. $smcFunc['db_query']('', '
  1344. UPDATE {db_prefix}ban_groups
  1345. SET
  1346. name = {string:ban_name},
  1347. reason = {string:reason},
  1348. notes = {string:notes},
  1349. expire_time = {raw:expiration},
  1350. cannot_access = {int:cannot_access},
  1351. cannot_post = {int:cannot_post},
  1352. cannot_register = {int:cannot_register},
  1353. cannot_login = {int:cannot_login}
  1354. WHERE id_ban_group = {int:id_ban_group}',
  1355. array(
  1356. 'expiration' => $ban_info['db_expiration'],
  1357. 'cannot_access' => $ban_info['cannot']['access'],
  1358. 'cannot_post' => $ban_info['cannot']['post'],
  1359. 'cannot_register' => $ban_info['cannot']['register'],
  1360. 'cannot_login' => $ban_info['cannot']['login'],
  1361. 'id_ban_group' => $ban_info['id'],
  1362. 'ban_name' => $ban_info['name'],
  1363. 'reason' => $ban_info['reason'],
  1364. 'notes' => $ban_info['notes'],
  1365. )
  1366. );
  1367. }
  1368. /**
  1369. * Creates a new ban group
  1370. * If a ban group with the same name already exists or the group s sucessfully created the ID is returned
  1371. * On error the error code is returned or false
  1372. *
  1373. * Errors in $context['ban_errors']
  1374. *
  1375. * @param array $ban_info
  1376. * @return int the ban group's ID
  1377. */
  1378. function insertBanGroup($ban_info = array())
  1379. {
  1380. global $smcFunc, $context;
  1381. if (empty($ban_info['name']))
  1382. $context['ban_errors'][] = 'ban_name_empty';
  1383. if (empty($ban_info['cannot']['access']) && empty($ban_info['cannot']['register']) && empty($ban_info['cannot']['post']) && empty($ban_info['cannot']['login']))
  1384. $context['ban_errors'][] = 'ban_unknown_restriction_type';
  1385. if (!empty($context['ban_errors']))
  1386. return;
  1387. // Check whether a ban with this name already exists.
  1388. $request = $smcFunc['db_query']('', '
  1389. SELECT id_ban_group
  1390. FROM {db_prefix}ban_groups
  1391. WHERE name = {string:new_ban_name}' . '
  1392. LIMIT 1',
  1393. array(
  1394. 'new_ban_name' => $ban_info['name'],
  1395. )
  1396. );
  1397. if ($smcFunc['db_num_rows']($request) == 1)
  1398. {
  1399. list($id_ban) = $smcFunc['db_fetch_row']($request);
  1400. $smcFunc['db_free_result']($request);
  1401. return $id_ban;
  1402. }
  1403. $smcFunc['db_free_result']($request);
  1404. // Yes yes, we're ready to add now.
  1405. $smcFunc['db_insert']('',
  1406. '{db_prefix}ban_groups',
  1407. array(
  1408. 'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
  1409. 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
  1410. ),
  1411. array(
  1412. $ban_info['name'], time(), $ban_info['db_expiration'], $ban_info['cannot']['access'], $ban_info['cannot']['register'],
  1413. $ban_info['cannot']['post'], $ban_info['cannot']['login'], $ban_info['reason'], $ban_info['notes'],
  1414. ),
  1415. array('id_ban_group')
  1416. );
  1417. $ban_info['id'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
  1418. if (empty($ban_info['id']))
  1419. $context['ban_errors'][] = 'impossible_insert_new_bangroup';
  1420. return $ban_info['id'];
  1421. }
  1422. /**
  1423. * This function handles the ins and outs of the screen for adding new ban
  1424. * triggers or modifying existing ones.
  1425. * Adding new ban triggers:
  1426. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
  1427. * - uses the ban_edit_trigger sub template of ManageBans.
  1428. * Editing existing ban triggers:
  1429. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
  1430. * - uses the ban_edit_trigger sub template of ManageBans.
  1431. */
  1432. function BanEditTrigger()
  1433. {
  1434. global $context, $smcFunc, $scripturl;
  1435. $context['sub_template'] = 'ban_edit_trigger';
  1436. $context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edittrigger';
  1437. $ban_group = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  1438. $ban_id = isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0;
  1439. if (empty($ban_group))
  1440. fatal_lang_error('ban_not_found', false);
  1441. if (isset($_POST['add_new_trigger']) && !empty($_POST['ban_suggestions']))
  1442. {
  1443. saveTriggers($_POST['ban_suggestions'], $ban_group, 0, $ban_id);
  1444. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1445. }
  1446. elseif (isset($_POST['edit_trigger']) && !empty($_POST['ban_suggestions']))
  1447. {
  1448. // The first replaces the old one, the others are added new (simplification, otherwise it would require another query and some work...)
  1449. saveTriggers(array_shift($_POST['ban_suggestions']), $ban_group, 0, $ban_id);
  1450. if (!empty($_POST['ban_suggestions']))
  1451. saveTriggers($_POST['ban_suggestions'], $ban_group);
  1452. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1453. }
  1454. elseif (isset($_POST['edit_trigger']))
  1455. {
  1456. removeBanTriggers($ban_id);
  1457. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1458. }
  1459. loadJavascriptFile('suggest.js', array('default_theme' => true), 'suggest.js');
  1460. if (empty($ban_id))
  1461. {
  1462. $context['ban_trigger'] = array(
  1463. 'id' => 0,
  1464. 'group' => $ban_group,
  1465. 'ip' => array(
  1466. 'value' => '',
  1467. 'selected' => true,
  1468. ),
  1469. 'hostname' => array(
  1470. 'selected' => false,
  1471. 'value' => '',
  1472. ),
  1473. 'email' => array(
  1474. 'value' => '',
  1475. 'selected' => false,
  1476. ),
  1477. 'banneduser' => array(
  1478. 'value' => '',
  1479. 'selected' => false,
  1480. ),
  1481. 'is_new' => true,
  1482. );
  1483. }
  1484. else
  1485. {
  1486. $request = $smcFunc['db_query']('', '
  1487. SELECT
  1488. bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
  1489. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  1490. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  1491. mem.member_name, mem.real_name
  1492. FROM {db_prefix}ban_items AS bi
  1493. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  1494. WHERE bi.id_ban = {int:ban_item}
  1495. AND bi.id_ban_group = {int:ban_group}
  1496. LIMIT 1',
  1497. array(
  1498. 'ban_item' => $ban_id,
  1499. 'ban_group' => $ban_group,
  1500. )
  1501. );
  1502. if ($smcFunc['db_num_rows']($request) == 0)
  1503. fatal_lang_error('ban_not_found', false);
  1504. $row = $smcFunc['db_fetch_assoc']($request);
  1505. $smcFunc['db_free_result']($request);
  1506. $context['ban_trigger'] = array(
  1507. 'id' => $row['id_ban'],
  1508. 'group' => $row['id_ban_group'],
  1509. 'ip' => array(
  1510. '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'])),
  1511. 'selected' => !empty($row['ip_low1']),
  1512. ),
  1513. 'hostname' => array(
  1514. 'value' => str_replace('%', '*', $row['hostname']),
  1515. 'selected' => !empty($row['hostname']),
  1516. ),
  1517. 'email' => array(
  1518. 'value' => str_replace('%', '*', $row['email_address']),
  1519. 'selected' => !empty($row['email_address'])
  1520. ),
  1521. 'banneduser' => array(
  1522. 'value' => $row['member_name'],
  1523. 'selected' => !empty($row['member_name'])
  1524. ),
  1525. 'is_new' => false,
  1526. );
  1527. }
  1528. createToken('admin-bet');
  1529. }
  1530. /**
  1531. * This handles the screen for showing the banned entities
  1532. * It is accessed by ?action=admin;area=ban;sa=browse
  1533. * It uses sub-tabs for browsing by IP, hostname, email or username.
  1534. *
  1535. * @uses ManageBans template, browse_triggers sub template.
  1536. */
  1537. function BanBrowseTriggers()
  1538. {
  1539. global $modSettings, $context, $scripturl, $smcFunc, $txt;
  1540. global $sourcedir, $settings;
  1541. if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  1542. {
  1543. checkSession();
  1544. removeBanTriggers($_POST['remove']);
  1545. // Rehabilitate some members.
  1546. if ($_REQUEST['entity'] == 'member')
  1547. updateBanMembers();
  1548. // Make sure the ban cache is refreshed.
  1549. updateSettings(array('banLastUpdated' => time()));
  1550. }
  1551. $context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
  1552. $listOptions = array(
  1553. 'id' => 'ban_trigger_list',
  1554. 'title' => $txt['ban_trigger_browse'],
  1555. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1556. 'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1557. 'default_sort_col' => 'banned_entity',
  1558. 'no_items_label' => $txt['ban_no_triggers'],
  1559. 'get_items' => array(
  1560. 'function' => 'list_getBanTriggers',
  1561. 'params' => array(
  1562. $context['selected_entity'],
  1563. ),
  1564. ),
  1565. 'get_count' => array(
  1566. 'function' => 'list_getNumBanTriggers',
  1567. 'params' => array(
  1568. $context['selected_entity'],
  1569. ),
  1570. ),
  1571. 'columns' => array(
  1572. 'banned_entity' => array(
  1573. 'header' => array(
  1574. 'value' => $txt['ban_banned_entity'],
  1575. ),
  1576. ),
  1577. 'ban_name' => array(
  1578. 'header' => array(
  1579. 'value' => $txt['ban_name'],
  1580. ),
  1581. 'data' => array(
  1582. 'sprintf' => array(
  1583. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
  1584. 'params' => array(
  1585. 'id_ban_group' => false,
  1586. 'name' => false,
  1587. ),
  1588. ),
  1589. ),
  1590. 'sort' => array(
  1591. 'default' => 'bg.name',
  1592. 'reverse' => 'bg.name DESC',
  1593. ),
  1594. ),
  1595. 'hits' => array(
  1596. 'header' => array(
  1597. 'value' => $txt['ban_hits'],
  1598. ),
  1599. 'data' => array(
  1600. 'db' => 'hits',
  1601. ),
  1602. 'sort' => array(
  1603. 'default' => 'bi.hits DESC',
  1604. 'reverse' => 'bi.hits',
  1605. ),
  1606. ),
  1607. 'check' => array(
  1608. 'header' => array(
  1609. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1610. 'class' => 'centercol',
  1611. ),
  1612. 'data' => array(
  1613. 'sprintf' => array(
  1614. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1615. 'params' => array(
  1616. 'id_ban' => false,
  1617. ),
  1618. ),
  1619. 'class' => 'centercol',
  1620. ),
  1621. ),
  1622. ),
  1623. 'form' => array(
  1624. 'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1625. 'include_start' => true,
  1626. 'include_sort' => true,
  1627. ),
  1628. 'additional_rows' => array(
  1629. array(
  1630. 'position' => 'above_column_headers',
  1631. '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>',
  1632. ),
  1633. array(
  1634. 'position' => 'bottom_of_list',
  1635. '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" />',
  1636. ),
  1637. ),
  1638. );
  1639. // Specific data for the first column depending on the selected entity.
  1640. if ($context['selected_entity'] === 'ip')
  1641. {
  1642. $listOptions['columns']['banned_entity']['data'] = array(
  1643. 'function' => create_function('$rowData', '
  1644. return range2ip(array(
  1645. $rowData[\'ip_low1\'],
  1646. $rowData[\'ip_low2\'],
  1647. $rowData[\'ip_low3\'],
  1648. $rowData[\'ip_low4\'],
  1649. $rowData[\'ip_low5\'],
  1650. $rowData[\'ip_low6\'],
  1651. $rowData[\'ip_low7\'],
  1652. $rowData[\'ip_low8\']
  1653. ), array(
  1654. $rowData[\'ip_high1\'],
  1655. $rowData[\'ip_high2\'],
  1656. $rowData[\'ip_high3\'],
  1657. $rowData[\'ip_high4\'],
  1658. $rowData[\'ip_high5\'],
  1659. $rowData[\'ip_high6\'],
  1660. $rowData[\'ip_high7\'],
  1661. $rowData[\'ip_high8\']
  1662. ));
  1663. '),
  1664. );
  1665. $listOptions['columns']['banned_entity']['sort'] = array(
  1666. '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',
  1667. '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',
  1668. );
  1669. }
  1670. elseif ($context['selected_entity'] === 'hostname')
  1671. {
  1672. $listOptions['columns']['banned_entity']['data'] = array(
  1673. 'function' => create_function('$rowData', '
  1674. global $smcFunc;
  1675. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'hostname\']), array(\'%\' => \'*\'));
  1676. '),
  1677. );
  1678. $listOptions['columns']['banned_entity']['sort'] = array(
  1679. 'default' => 'bi.hostname',
  1680. 'reverse' => 'bi.hostname DESC',
  1681. );
  1682. }
  1683. elseif ($context['selected_entity'] === 'email')
  1684. {
  1685. $listOptions['columns']['banned_entity']['data'] = array(
  1686. 'function' => create_function('$rowData', '
  1687. global $smcFunc;
  1688. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'email_address\']), array(\'%\' => \'*\'));
  1689. '),
  1690. );
  1691. $listOptions['columns']['banned_entity']['sort'] = array(
  1692. 'default' => 'bi.email_address',
  1693. 'reverse' => 'bi.email_address DESC',
  1694. );
  1695. }
  1696. elseif ($context['selected_entity'] === 'member')
  1697. {
  1698. $listOptions['columns']['banned_entity']['data'] = array(
  1699. 'sprintf' => array(
  1700. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1701. 'params' => array(
  1702. 'id_member' => false,
  1703. 'real_name' => false,
  1704. ),
  1705. ),
  1706. );
  1707. $listOptions['columns']['banned_entity']['sort'] = array(
  1708. 'default' => 'mem.real_name',
  1709. 'reverse' => 'mem.real_name DESC',
  1710. );
  1711. }
  1712. // Create the list.
  1713. require_once($sourcedir . '/Subs-List.php');
  1714. createList($listOptions);
  1715. // The list is the only thing to show, so make it the default sub template.
  1716. $context['sub_template'] = 'show_list';
  1717. $context['default_list'] = 'ban_trigger_list';
  1718. }
  1719. /**
  1720. * Get ban triggers for the given parameters.
  1721. *
  1722. * @param int $start
  1723. * @param int $items_per_page
  1724. * @param string $sort
  1725. * @param string $trigger_type
  1726. * @return array
  1727. */
  1728. function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
  1729. {
  1730. global $smcFunc;
  1731. $where = array(
  1732. 'ip' => 'bi.ip_low1 > 0',
  1733. 'hostname' => 'bi.hostname != {string:blank_string}',
  1734. 'email' => 'bi.email_address != {string:blank_string}',
  1735. );
  1736. $request = $smcFunc['db_query']('', '
  1737. SELECT
  1738. 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,
  1739. bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
  1740. mem.id_member, mem.real_name' : '') . '
  1741. FROM {db_prefix}ban_items AS bi
  1742. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
  1743. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1744. WHERE ' . $where[$trigger_type]) . '
  1745. ORDER BY ' . $sort . '
  1746. LIMIT ' . $start . ', ' . $items_per_page,
  1747. array(
  1748. 'blank_string' => '',
  1749. )
  1750. );
  1751. $ban_triggers = array();
  1752. while ($row = $smcFunc['db_fetch_assoc']($request))
  1753. $ban_triggers[] = $row;
  1754. $smcFunc['db_free_result']($request);
  1755. return $ban_triggers;
  1756. }
  1757. /**
  1758. * This returns the total number of ban triggers of the given type.
  1759. *
  1760. * @param string $trigger_type
  1761. * @return int
  1762. */
  1763. function list_getNumBanTriggers($trigger_type)
  1764. {
  1765. global $smcFunc;
  1766. $where = array(
  1767. 'ip' => 'bi.ip_low1 > 0',
  1768. 'hostname' => 'bi.hostname != {string:blank_string}',
  1769. 'email' => 'bi.email_address != {string:blank_string}',
  1770. );
  1771. $request = $smcFunc['db_query']('', '
  1772. SELECT COUNT(*)
  1773. FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
  1774. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1775. WHERE ' . $where[$trigger_type]),
  1776. array(
  1777. 'blank_string' => '',
  1778. )
  1779. );
  1780. list ($num_triggers) = $smcFunc['db_fetch_row']($request);
  1781. $smcFunc['db_free_result']($request);
  1782. return $num_triggers;
  1783. }
  1784. /**
  1785. * This handles the listing of ban log entries, and allows their deletion.
  1786. * Shows a list of logged access attempts by banned users.
  1787. * It is accessed by ?action=admin;area=ban;sa=log.
  1788. * How it works:
  1789. * - allows sorting of several columns.
  1790. * - also handles deletion of (a selection of) log entries.
  1791. */
  1792. function BanLog()
  1793. {
  1794. global $scripturl, $context, $smcFunc, $sourcedir, $txt;
  1795. global $context;
  1796. // Delete one or more entries.
  1797. if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
  1798. {
  1799. checkSession();
  1800. validateToken('admin-bl');
  1801. // 'Delete all entries' button was pressed.
  1802. if (!empty($_POST['removeAll']))
  1803. removeBanLogs();
  1804. // 'Delete selection' button was pressed.
  1805. else
  1806. {
  1807. array_map('intval', $_POST['remove']);
  1808. removeBanLogs($_POST['remove']);
  1809. }
  1810. }
  1811. $listOptions = array(
  1812. 'id' => 'ban_log',
  1813. 'title' => $txt['ban_log'],
  1814. 'items_per_page' => 30,
  1815. 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1816. 'default_sort_col' => 'date',
  1817. 'get_items' => array(
  1818. 'function' => 'list_getBanLogEntries',
  1819. ),
  1820. 'get_count' => array(
  1821. 'function' => 'list_getNumBanLogEntries',
  1822. ),
  1823. 'no_items_label' => $txt['ban_log_no_entries'],
  1824. 'columns' => array(
  1825. 'ip' => array(
  1826. 'header' => array(
  1827. 'value' => $txt['ban_log_ip'],
  1828. ),
  1829. 'data' => array(
  1830. 'sprintf' => array(
  1831. 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
  1832. 'params' => array(
  1833. 'ip' => false,
  1834. ),
  1835. ),
  1836. ),
  1837. 'sort' => array(
  1838. 'default' => 'lb.ip',
  1839. 'reverse' => 'lb.ip DESC',
  1840. ),
  1841. ),
  1842. 'email' => array(
  1843. 'header' => array(
  1844. 'value' => $txt['ban_log_email'],
  1845. ),
  1846. 'data' => array(
  1847. 'db_htmlsafe' => 'email',
  1848. ),
  1849. 'sort' => array(
  1850. 'default' => 'lb.email = \'\', lb.email',
  1851. 'reverse' => 'lb.email != \'\', lb.email DESC',
  1852. ),
  1853. ),
  1854. 'member' => array(
  1855. 'header' => array(
  1856. 'value' => $txt['ban_log_member'],
  1857. ),
  1858. 'data' => array(
  1859. 'sprintf' => array(
  1860. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1861. 'params' => array(
  1862. 'id_member' => false,
  1863. 'real_name' => false,
  1864. ),
  1865. ),
  1866. ),
  1867. 'sort' => array(
  1868. 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
  1869. 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
  1870. ),
  1871. ),
  1872. 'date' => array(
  1873. 'header' => array(
  1874. 'value' => $txt['ban_log_date'],
  1875. ),
  1876. 'data' => array(
  1877. 'function' => create_function('$rowData', '
  1878. return timeformat($rowData[\'log_time\']);
  1879. '),
  1880. ),
  1881. 'sort' => array(
  1882. 'default' => 'lb.log_time DESC',
  1883. 'reverse' => 'lb.log_time',
  1884. ),
  1885. ),
  1886. 'check' => array(
  1887. 'header' => array(
  1888. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1889. 'class' => 'centercol',
  1890. ),
  1891. 'data' => array(
  1892. 'sprintf' => array(
  1893. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1894. 'params' => array(
  1895. 'id_ban_log' => false,
  1896. ),
  1897. ),
  1898. 'class' => 'centercol',
  1899. ),
  1900. ),
  1901. ),
  1902. 'form' => array(
  1903. 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1904. 'include_start' => true,
  1905. 'include_sort' => true,
  1906. 'token' => 'admin-bl',
  1907. ),
  1908. 'additional_rows' => array(
  1909. array(
  1910. 'position' => 'bottom_of_list',
  1911. 'value' => '
  1912. <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit" />
  1913. <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit" />',
  1914. ),
  1915. ),
  1916. );
  1917. createToken('admin-bl');
  1918. require_once($sourcedir . '/Subs-List.php');
  1919. createList($listOptions);
  1920. $context['page_title'] = $txt['ban_log'];
  1921. $context['sub_template'] = 'show_list';
  1922. $context['default_list'] = 'ban_log';
  1923. }
  1924. /**
  1925. * Load a list of ban log entries from the database.
  1926. * (no permissions check)
  1927. *
  1928. * @param int $start
  1929. * @param int $items_per_page
  1930. * @param string $sort
  1931. */
  1932. function list_getBanLogEntries($start, $items_per_page, $sort)
  1933. {
  1934. global $smcFunc;
  1935. $request = $smcFunc['db_query']('', '
  1936. 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
  1937. FROM {db_prefix}log_banned AS lb
  1938. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
  1939. ORDER BY ' . $sort . '
  1940. LIMIT ' . $start . ', ' . $items_per_page,
  1941. array(
  1942. 'blank_string' => '',
  1943. 'dash' => '-',
  1944. )
  1945. );
  1946. $log_entries = array();
  1947. while ($row = $smcFunc['db_fetch_assoc']($request))
  1948. $log_entries[] = $row;
  1949. $smcFunc['db_free_result']($request);
  1950. return $log_entries;
  1951. }
  1952. /**
  1953. * This returns the total count of ban log entries.
  1954. */
  1955. function list_getNumBanLogEntries()
  1956. {
  1957. global $smcFunc;
  1958. $request = $smcFunc['db_query']('', '
  1959. SELECT COUNT(*)
  1960. FROM {db_prefix}log_banned AS lb',
  1961. array(
  1962. )
  1963. );
  1964. list ($num_entries) = $smcFunc['db_fetch_row']($request);
  1965. $smcFunc['db_free_result']($request);
  1966. return $num_entries;
  1967. }
  1968. /**
  1969. * Convert a range of given IP number into a single string.
  1970. * It's practically the reverse function of ip2range().
  1971. *
  1972. * @example
  1973. * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
  1974. *
  1975. * @param array $low IPv4 format
  1976. * @param array $high IPv4 format
  1977. * @return string
  1978. */
  1979. function range2ip($low, $high)
  1980. {
  1981. // IPv6 check.
  1982. if (!empty($high[4]) || !empty($high[5]) || !empty($high[6]) || !empty($high[7]))
  1983. {
  1984. if (count($low) != 8 || count($high) != 8)
  1985. return '';
  1986. $ip = array();
  1987. for ($i = 0; $i < 8; $i++)
  1988. {
  1989. if ($low[$i] == $high[$i])
  1990. $ip[$i] = dechex($low[$i]);
  1991. elseif ($low[$i] == '0' && $high[$i] == '255')
  1992. $ip[$i] = '*';
  1993. else
  1994. $ip[$i] = dechex($low[$i]) . '-' . dechex($high[$i]);
  1995. }
  1996. return implode(':', $ip);
  1997. }
  1998. // Legacy IPv4 stuff.
  1999. // (count($low) != 4 || count($high) != 4) would not work because $low and $high always contain 8 elements!
  2000. if ((count($low) != 4 || count($high) != 4) && (count($low) != 8 || count($high) != 8))
  2001. return '';
  2002. for ($i = 0; $i < 4; $i++)
  2003. {
  2004. if ($low[$i] == $high[$i])
  2005. $ip[$i] = $low[$i];
  2006. elseif ($low[$i] == '0' && $high[$i] == '255')
  2007. $ip[$i] = '*';
  2008. else
  2009. $ip[$i] = $low[$i] . '-' . $high[$i];
  2010. }
  2011. // Pretending is fun... the IP can't be this, so use it for 'unknown'.
  2012. if ($ip == array(255, 255, 255, 255))
  2013. return 'unknown';
  2014. return implode('.', $ip);
  2015. }
  2016. /**
  2017. * Checks whether a given IP range already exists in the trigger list.
  2018. * If yes, it returns an error message. Otherwise, it returns an array
  2019. * optimized for the database.
  2020. *
  2021. * @param array $ip_array
  2022. * @param string $fullip
  2023. * @return boolean
  2024. */
  2025. function checkExistingTriggerIP($ip_array, $fullip = '')
  2026. {
  2027. global $smcFunc, $scripturl;
  2028. if (count($ip_array) == 4 || count($ip_array) == 8)
  2029. $values = array(
  2030. 'ip_low1' => $ip_array[0]['low'],
  2031. 'ip_high1' => $ip_array[0]['high'],
  2032. 'ip_low2' => $ip_array[1]['low'],
  2033. 'ip_high2' => $ip_array[1]['high'],
  2034. 'ip_low3' => $ip_array[2]['low'],
  2035. 'ip_high3' => $ip_array[2]['high'],
  2036. 'ip_low4' => $ip_array[3]['low'],
  2037. 'ip_high4' => $ip_array[3]['high'],
  2038. 'ip_low5' => $ip_array[4]['low'],
  2039. 'ip_high5' => $ip_array[4]['high'],
  2040. 'ip_low6' => $ip_array[5]['low'],
  2041. 'ip_high6' => $ip_array[5]['high'],
  2042. 'ip_low7' => $ip_array[6]['low'],
  2043. 'ip_high7' => $ip_array[6]['high'],
  2044. 'ip_low8' => $ip_array[7]['low'],
  2045. 'ip_high8' => $ip_array[7]['high'],
  2046. );
  2047. else
  2048. return false;
  2049. $request = $smcFunc['db_query']('', '
  2050. SELECT bg.id_ban_group, bg.name
  2051. FROM {db_prefix}ban_groups AS bg
  2052. INNER JOIN {db_prefix}ban_items AS bi ON
  2053. (bi.id_ban_group = bg.id_ban_group)
  2054. AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
  2055. AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
  2056. AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
  2057. AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
  2058. AND ip_low5 = {int:ip_low5} AND ip_high5 = {int:ip_high5}
  2059. AND ip_low6 = {int:ip_low6} AND ip_high6 = {int:ip_high6}
  2060. AND ip_low7 = {int:ip_low7} AND ip_high7 = {int:ip_high7}
  2061. AND ip_low8 = {int:ip_low8} AND ip_high8 = {int:ip_high8}
  2062. LIMIT 1',
  2063. $values
  2064. );
  2065. if ($smcFunc['db_num_rows']($request) != 0)
  2066. {
  2067. list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
  2068. fatal_lang_error('ban_trigger_already_exists', false, array(
  2069. $fullip,
  2070. '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
  2071. ));
  2072. }
  2073. $smcFunc['db_free_result']($request);
  2074. return $values;
  2075. }
  2076. /**
  2077. * As it says... this tries to review the list of banned members, to match new bans.
  2078. * Note: is_activated >= 10: a member is banned.
  2079. */
  2080. function updateBanMembers()
  2081. {
  2082. global $smcFunc;
  2083. $updates = array();
  2084. $allMembers = array();
  2085. $newMembers = array();
  2086. // Start by getting all active bans - it's quicker doing this in parts...
  2087. $request = $smcFunc['db_query']('', '
  2088. SELECT bi.id_member, bi.email_address
  2089. FROM {db_prefix}ban_items AS bi
  2090. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  2091. WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
  2092. AND bg.cannot_access = {int:cannot_access_on}
  2093. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  2094. array(
  2095. 'no_member' => 0,
  2096. 'cannot_access_on' => 1,
  2097. 'current_time' => time(),
  2098. 'blank_string' => '',
  2099. )
  2100. );
  2101. $memberIDs = array();
  2102. $memberEmails = array();
  2103. $memberEmailWild = array();
  2104. while ($row = $smcFunc['db_fetch_assoc']($request))
  2105. {
  2106. if ($row['id_member'])
  2107. $memberIDs[$row['id_member']] = $row['id_member'];
  2108. if ($row['email_address'])
  2109. {
  2110. // Does it have a wildcard - if so we can't do a IN on it.
  2111. if (strpos($row['email_address'], '%') !== false)
  2112. $memberEmailWild[$row['email_address']] = $row['email_address'];
  2113. else
  2114. $memberEmails[$row['email_address']] = $row['email_address'];
  2115. }
  2116. }
  2117. $smcFunc['db_free_result']($request);
  2118. // Build up the query.
  2119. $queryPart = array();
  2120. $queryValues = array();
  2121. if (!empty($memberIDs))
  2122. {
  2123. $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
  2124. $queryValues['member_ids'] = $memberIDs;
  2125. }
  2126. if (!empty($memberEmails))
  2127. {
  2128. $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
  2129. $queryValues['member_emails'] = $memberEmails;
  2130. }
  2131. $count = 0;
  2132. foreach ($memberEmailWild as $email)
  2133. {
  2134. $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
  2135. $queryValues['wild_' . $count++] = $email;
  2136. }
  2137. // Find all banned members.
  2138. if (!empty($queryPart))
  2139. {
  2140. $request = $smcFunc['db_query']('', '
  2141. SELECT mem.id_member, mem.is_activated
  2142. FROM {db_prefix}members AS mem
  2143. WHERE ' . implode( ' OR ', $queryPart),
  2144. $queryValues
  2145. );
  2146. while ($row = $smcFunc['db_fetch_assoc']($request))
  2147. {
  2148. if (!in_array($row['id_member'], $allMembers))
  2149. {
  2150. $allMembers[] = $row['id_member'];
  2151. // Do they need an update?
  2152. if ($row['is_activated'] < 10)
  2153. {
  2154. $updates[($row['is_activated'] + 10)][] = $row['id_member'];
  2155. $newMembers[] = $row['id_member'];
  2156. }
  2157. }
  2158. }
  2159. $smcFunc['db_free_result']($request);
  2160. }
  2161. // We welcome our new members in the realm of the banned.
  2162. if (!empty($newMembers))
  2163. $smcFunc['db_query']('', '
  2164. DELETE FROM {db_prefix}log_online
  2165. WHERE id_member IN ({array_int:new_banned_members})',
  2166. array(
  2167. 'new_banned_members' => $newMembers,
  2168. )
  2169. );
  2170. // Find members that are wrongfully marked as banned.
  2171. $request = $smcFunc['db_query']('', '
  2172. SELECT mem.id_member, mem.is_activated - 10 AS new_value
  2173. FROM {db_prefix}members AS mem
  2174. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
  2175. 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}))
  2176. WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
  2177. AND mem.is_activated >= {int:ban_flag}',
  2178. array(
  2179. 'cannot_access_activated' => 1,
  2180. 'current_time' => time(),
  2181. 'ban_flag' => 10,
  2182. )
  2183. );
  2184. while ($row = $smcFunc['db_fetch_assoc']($request))
  2185. {
  2186. // Don't do this twice!
  2187. if (!in_array($row['id_member'], $allMembers))
  2188. {
  2189. $updates[$row['new_value']][] = $row['id_member'];
  2190. $allMembers[] = $row['id_member'];
  2191. }
  2192. }
  2193. $smcFunc['db_free_result']($request);
  2194. if (!empty($updates))
  2195. foreach ($updates as $newStatus => $members)
  2196. updateMemberData($members, array('is_activated' => $newStatus));
  2197. // Update the latest member and our total members as banning may change them.
  2198. updateStats('member');
  2199. }
  2200. function getMemberData($id)
  2201. {
  2202. global $smcFunc;
  2203. $suggestions = array();
  2204. $request = $smcFunc['db_query']('', '
  2205. SELECT id_member, real_name, member_ip, email_address
  2206. FROM {db_prefix}members
  2207. WHERE id_member = {int:current_user}
  2208. LIMIT 1',
  2209. array(
  2210. 'current_user' => $id,
  2211. )
  2212. );
  2213. if ($smcFunc['db_num_rows']($request) > 0)
  2214. list ($suggestions['member']['id'], $suggestions['member']['name'], $suggestions['main_ip'], $suggestions['email']) = $smcFunc['db_fetch_row']($request);
  2215. $smcFunc['db_free_result']($request);
  2216. return $suggestions;
  2217. }
  2218. ?>