ManageBans.php 68 KB

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