ManageBans.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  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'][0]);
  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. $logCorrel = array(
  1072. 'main_ip' => 'ip_range',
  1073. 'hostname' => 'email',
  1074. 'email' => 'email',
  1075. 'user' => 'member',
  1076. 'ip_range' => 'ip_range',
  1077. );
  1078. // Preset all values that are required.
  1079. $values = array(
  1080. 'id_ban_group' => $group_id,
  1081. 'hostname' => '',
  1082. 'email_address' => '',
  1083. 'id_member' => 0,
  1084. 'ip_low1' => 0,
  1085. 'ip_high1' => 0,
  1086. 'ip_low2' => 0,
  1087. 'ip_high2' => 0,
  1088. 'ip_low3' => 0,
  1089. 'ip_high3' => 0,
  1090. 'ip_low4' => 0,
  1091. 'ip_high4' => 0,
  1092. 'ip_low5' => 0,
  1093. 'ip_high5' => 0,
  1094. 'ip_low6' => 0,
  1095. 'ip_high6' => 0,
  1096. 'ip_low7' => 0,
  1097. 'ip_high7' => 0,
  1098. 'ip_low8' => 0,
  1099. 'ip_high8' => 0,
  1100. );
  1101. $insertKeys = array(
  1102. 'id_ban_group' => 'int',
  1103. 'hostname' => 'string',
  1104. 'email_address' => 'string',
  1105. 'id_member' => 'int',
  1106. 'ip_low1' => 'int',
  1107. 'ip_high1' => 'int',
  1108. 'ip_low2' => 'int',
  1109. 'ip_high2' => 'int',
  1110. 'ip_low3' => 'int',
  1111. 'ip_high3' => 'int',
  1112. 'ip_low4' => 'int',
  1113. 'ip_high4' => 'int',
  1114. 'ip_low5' => 'int',
  1115. 'ip_high5' => 'int',
  1116. 'ip_low6' => 'int',
  1117. 'ip_high6' => 'int',
  1118. 'ip_low7' => 'int',
  1119. 'ip_high7' => 'int',
  1120. 'ip_low8' => 'int',
  1121. 'ip_high8' => 'int',
  1122. );
  1123. $array_keys = array();
  1124. call_integration_hook('integrate_extend_ban_triggers', array(&$array_keys));
  1125. $array_keys += array('ips_in_messages', 'ips_in_errors');
  1126. $insertTriggers = array();
  1127. foreach ($triggers as $key => $trigger)
  1128. {
  1129. // Exceptions, exceptions, exceptions...always exceptions... :P
  1130. if (in_array($key, $array_keys))
  1131. foreach ($trigger as $real_trigger)
  1132. $insertTriggers[] = array_merge($values, $real_trigger);
  1133. else
  1134. $insertTriggers[] = array_merge($values, $trigger);
  1135. }
  1136. if (empty($insertTriggers))
  1137. $context['ban_errors'][] = 'ban_no_triggers';
  1138. if (!empty($context['ban_errors']))
  1139. return false;
  1140. $smcFunc['db_insert']('',
  1141. '{db_prefix}ban_items',
  1142. $insertKeys,
  1143. $insertTriggers,
  1144. array('id_ban')
  1145. );
  1146. // Log the addion of the ban entries into the moderation log.
  1147. foreach ($logs as $log)
  1148. logAction('ban', array(
  1149. $logCorrel[$log['bantype']] => $log['value'],
  1150. 'new' => 1,
  1151. 'type' => $log['bantype'],
  1152. ));
  1153. return true;
  1154. }
  1155. /**
  1156. * This function updates an existing ban trigger into the database
  1157. *
  1158. * Errors in $context['ban_errors']
  1159. *
  1160. * @param int $ban_item
  1161. * @param int $group_id
  1162. * @param array $trigger
  1163. * @param array $logs
  1164. * @return nothing
  1165. */
  1166. function updateTriggers($ban_item = 0, $group_id = 0, $trigger = array(), $logs = array())
  1167. {
  1168. global $smcFunc, $context;
  1169. if (empty($ban_item))
  1170. $context['ban_errors'][] = 'ban_ban_item_empty';
  1171. if (empty($group_id))
  1172. $context['ban_errors'][] = 'ban_group_id_empty';
  1173. if (empty($trigger))
  1174. $context['ban_errors'][] = 'ban_no_triggers';
  1175. if (!empty($context['ban_errors']))
  1176. return;
  1177. // Preset all values that are required.
  1178. $values = array(
  1179. 'id_ban_group' => $group_id,
  1180. 'hostname' => '',
  1181. 'email_address' => '',
  1182. 'id_member' => 0,
  1183. 'ip_low1' => 0,
  1184. 'ip_high1' => 0,
  1185. 'ip_low2' => 0,
  1186. 'ip_high2' => 0,
  1187. 'ip_low3' => 0,
  1188. 'ip_high3' => 0,
  1189. 'ip_low4' => 0,
  1190. 'ip_high4' => 0,
  1191. 'ip_low5' => 0,
  1192. 'ip_high5' => 0,
  1193. 'ip_low6' => 0,
  1194. 'ip_high6' => 0,
  1195. 'ip_low7' => 0,
  1196. 'ip_high7' => 0,
  1197. 'ip_low8' => 0,
  1198. 'ip_high8' => 0,
  1199. );
  1200. $trigger = array_merge($values, $trigger);
  1201. $smcFunc['db_query']('', '
  1202. UPDATE {db_prefix}ban_items
  1203. SET
  1204. hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
  1205. ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
  1206. ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
  1207. ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
  1208. ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4},
  1209. ip_low5 = {int:ip_low5}, ip_high5 = {int:ip_high5},
  1210. ip_low6 = {int:ip_low6}, ip_high6 = {int:ip_high6},
  1211. ip_low7 = {int:ip_low7}, ip_high7 = {int:ip_high7},
  1212. ip_low8 = {int:ip_low8}, ip_high8 = {int:ip_high8}
  1213. WHERE id_ban = {int:ban_item}
  1214. AND id_ban_group = {int:id_ban_group}',
  1215. array_merge($trigger, array(
  1216. 'id_ban_group' => $group_id,
  1217. 'ban_item' => $ban_item,
  1218. ))
  1219. );
  1220. // Log the addion of the ban entries into the moderation log.
  1221. logAction('ban', array(
  1222. $logCorrel[$log['bantype']] => $log['value'],
  1223. 'new' => 0,
  1224. 'type' => $log['bantype'],
  1225. ));
  1226. }
  1227. /**
  1228. * Updates an existing ban group
  1229. * If the name doesn't exists a new one is created
  1230. *
  1231. * Errors in $context['ban_errors']
  1232. *
  1233. * @param array $ban_info
  1234. * @return nothing
  1235. */
  1236. function updateBanGroup($ban_info = array())
  1237. {
  1238. global $smcFunc, $context;
  1239. if (empty($ban_info['name']))
  1240. $context['ban_errors'][] = 'ban_name_empty';
  1241. if (empty($ban_info['id']))
  1242. $context['ban_errors'][] = 'ban_id_empty';
  1243. if (!empty($context['ban_errors']))
  1244. return;
  1245. $request = $smcFunc['db_query']('', '
  1246. SELECT id_ban_group
  1247. FROM {db_prefix}ban_groups
  1248. WHERE name = {string:new_ban_name}
  1249. AND id_ban_group = {int:ban_group}
  1250. LIMIT 1',
  1251. array(
  1252. 'ban_group' => $ban_info['id'],
  1253. 'new_ban_name' => $ban_info['name'],
  1254. )
  1255. );
  1256. if ($smcFunc['db_num_rows']($request) == 0)
  1257. return insertBanGroup($ban_info);
  1258. $smcFunc['db_free_result']($request);
  1259. $smcFunc['db_query']('', '
  1260. UPDATE {db_prefix}ban_groups
  1261. SET
  1262. name = {string:ban_name},
  1263. reason = {string:reason},
  1264. notes = {string:notes},
  1265. expire_time = {raw:expiration},
  1266. cannot_access = {int:cannot_access},
  1267. cannot_post = {int:cannot_post},
  1268. cannot_register = {int:cannot_register},
  1269. cannot_login = {int:cannot_login}
  1270. WHERE id_ban_group = {int:id_ban_group}',
  1271. array(
  1272. 'expiration' => $ban_info['db_expiration'],
  1273. 'cannot_access' => $ban_info['cannot']['access'],
  1274. 'cannot_post' => $ban_info['cannot']['post'],
  1275. 'cannot_register' => $ban_info['cannot']['register'],
  1276. 'cannot_login' => $ban_info['cannot']['login'],
  1277. 'id_ban_group' => $ban_info['id'],
  1278. 'ban_name' => $ban_info['name'],
  1279. 'reason' => $ban_info['reason'],
  1280. 'notes' => $ban_info['notes'],
  1281. )
  1282. );
  1283. }
  1284. /**
  1285. * Creates a new ban group
  1286. * If a ban group with the same name already exists or the group s sucessfully created the ID is returned
  1287. * On error the error code is returned or false
  1288. *
  1289. * Errors in $context['ban_errors']
  1290. *
  1291. * @param array $ban_info
  1292. * @return int the ban group's ID
  1293. */
  1294. function insertBanGroup($ban_info = array())
  1295. {
  1296. global $smcFunc, $context;
  1297. if (empty($ban_info['name']))
  1298. $context['ban_errors'][] = 'ban_name_empty';
  1299. if (empty($ban_info['cannot']['access']) && empty($ban_info['cannot']['register']) && empty($ban_info['cannot']['post']) && empty($ban_info['cannot']['login']))
  1300. $context['ban_errors'][] = 'ban_unknown_restriction_type';
  1301. if (!empty($context['ban_errors']))
  1302. return;
  1303. // Check whether a ban with this name already exists.
  1304. $request = $smcFunc['db_query']('', '
  1305. SELECT id_ban_group
  1306. FROM {db_prefix}ban_groups
  1307. WHERE name = {string:new_ban_name}' . '
  1308. LIMIT 1',
  1309. array(
  1310. 'new_ban_name' => $ban_info['name'],
  1311. )
  1312. );
  1313. if ($smcFunc['db_num_rows']($request) == 1)
  1314. {
  1315. list($id_ban) = $smcFunc['db_fetch_row']($request);
  1316. $smcFunc['db_free_result']($request);
  1317. return $id_ban;
  1318. }
  1319. $smcFunc['db_free_result']($request);
  1320. // Yes yes, we're ready to add now.
  1321. $smcFunc['db_insert']('',
  1322. '{db_prefix}ban_groups',
  1323. array(
  1324. 'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
  1325. 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
  1326. ),
  1327. array(
  1328. $ban_info['name'], time(), $ban_info['db_expiration'], $ban_info['cannot']['access'], $ban_info['cannot']['register'],
  1329. $ban_info['cannot']['post'], $ban_info['cannot']['login'], $ban_info['reason'], $ban_info['notes'],
  1330. ),
  1331. array('id_ban_group')
  1332. );
  1333. $ban_info['id'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
  1334. if (empty($ban_info['id']))
  1335. $context['ban_errors'][] = 'impossible_insert_new_bangroup';
  1336. return $ban_info['id'];
  1337. }
  1338. /**
  1339. * This function handles the ins and outs of the screen for adding new ban
  1340. * triggers or modifying existing ones.
  1341. * Adding new ban triggers:
  1342. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
  1343. * - uses the ban_edit_trigger sub template of ManageBans.
  1344. * Editing existing ban triggers:
  1345. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
  1346. * - uses the ban_edit_trigger sub template of ManageBans.
  1347. */
  1348. function BanEditTrigger()
  1349. {
  1350. global $context, $smcFunc, $scripturl;
  1351. $context['sub_template'] = 'ban_edit_trigger';
  1352. $context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edittrigger';
  1353. $ban_group = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  1354. $ban_id = isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0;
  1355. if (empty($ban_group))
  1356. fatal_lang_error('ban_not_found', false);
  1357. if (isset($_POST['add_new_trigger']) && !empty($_POST['ban_suggestions']))
  1358. {
  1359. saveTriggers($_POST['ban_suggestions'], $ban_group, 0, $ban_id);
  1360. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1361. }
  1362. elseif (isset($_POST['edit_trigger']) && !empty($_POST['ban_suggestions']))
  1363. {
  1364. // The first replaces the old one, the others are added new (simplification, otherwise it would require another query and some work...)
  1365. saveTriggers(array_shift($_POST['ban_suggestions']), $ban_group, 0, $ban_id);
  1366. if (!empty($_POST['ban_suggestions']))
  1367. saveTriggers($_POST['ban_suggestions'], $ban_group);
  1368. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1369. }
  1370. elseif (isset($_POST['edit_trigger']))
  1371. {
  1372. removeBanTriggers($ban_id);
  1373. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1374. }
  1375. loadJavascriptFile('suggest.js', array('default_theme' => true), 'suggest.js');
  1376. if (empty($ban_id))
  1377. {
  1378. $context['ban_trigger'] = array(
  1379. 'id' => 0,
  1380. 'group' => $ban_group,
  1381. 'ip' => array(
  1382. 'value' => '',
  1383. 'selected' => true,
  1384. ),
  1385. 'hostname' => array(
  1386. 'selected' => false,
  1387. 'value' => '',
  1388. ),
  1389. 'email' => array(
  1390. 'value' => '',
  1391. 'selected' => false,
  1392. ),
  1393. 'banneduser' => array(
  1394. 'value' => '',
  1395. 'selected' => false,
  1396. ),
  1397. 'is_new' => true,
  1398. );
  1399. }
  1400. else
  1401. {
  1402. $request = $smcFunc['db_query']('', '
  1403. SELECT
  1404. bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
  1405. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  1406. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  1407. mem.member_name, mem.real_name
  1408. FROM {db_prefix}ban_items AS bi
  1409. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  1410. WHERE bi.id_ban = {int:ban_item}
  1411. AND bi.id_ban_group = {int:ban_group}
  1412. LIMIT 1',
  1413. array(
  1414. 'ban_item' => $ban_id,
  1415. 'ban_group' => $ban_group,
  1416. )
  1417. );
  1418. if ($smcFunc['db_num_rows']($request) == 0)
  1419. fatal_lang_error('ban_not_found', false);
  1420. $row = $smcFunc['db_fetch_assoc']($request);
  1421. $smcFunc['db_free_result']($request);
  1422. $context['ban_trigger'] = array(
  1423. 'id' => $row['id_ban'],
  1424. 'group' => $row['id_ban_group'],
  1425. 'ip' => array(
  1426. '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'])),
  1427. 'selected' => !empty($row['ip_low1']),
  1428. ),
  1429. 'hostname' => array(
  1430. 'value' => str_replace('%', '*', $row['hostname']),
  1431. 'selected' => !empty($row['hostname']),
  1432. ),
  1433. 'email' => array(
  1434. 'value' => str_replace('%', '*', $row['email_address']),
  1435. 'selected' => !empty($row['email_address'])
  1436. ),
  1437. 'banneduser' => array(
  1438. 'value' => $row['member_name'],
  1439. 'selected' => !empty($row['member_name'])
  1440. ),
  1441. 'is_new' => false,
  1442. );
  1443. }
  1444. createToken('admin-bet');
  1445. }
  1446. /**
  1447. * This handles the screen for showing the banned entities
  1448. * It is accessed by ?action=admin;area=ban;sa=browse
  1449. * It uses sub-tabs for browsing by IP, hostname, email or username.
  1450. *
  1451. * @uses ManageBans template, browse_triggers sub template.
  1452. */
  1453. function BanBrowseTriggers()
  1454. {
  1455. global $modSettings, $context, $scripturl, $smcFunc, $txt;
  1456. global $sourcedir, $settings;
  1457. if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  1458. {
  1459. checkSession();
  1460. removeBanTriggers($_POST['remove']);
  1461. // Rehabilitate some members.
  1462. if ($_REQUEST['entity'] == 'member')
  1463. updateBanMembers();
  1464. // Make sure the ban cache is refreshed.
  1465. updateSettings(array('banLastUpdated' => time()));
  1466. }
  1467. $context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
  1468. $listOptions = array(
  1469. 'id' => 'ban_trigger_list',
  1470. 'title' => $txt['ban_trigger_browse'],
  1471. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1472. 'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1473. 'default_sort_col' => 'banned_entity',
  1474. 'no_items_label' => $txt['ban_no_triggers'],
  1475. 'get_items' => array(
  1476. 'function' => 'list_getBanTriggers',
  1477. 'params' => array(
  1478. $context['selected_entity'],
  1479. ),
  1480. ),
  1481. 'get_count' => array(
  1482. 'function' => 'list_getNumBanTriggers',
  1483. 'params' => array(
  1484. $context['selected_entity'],
  1485. ),
  1486. ),
  1487. 'columns' => array(
  1488. 'banned_entity' => array(
  1489. 'header' => array(
  1490. 'value' => $txt['ban_banned_entity'],
  1491. ),
  1492. ),
  1493. 'ban_name' => array(
  1494. 'header' => array(
  1495. 'value' => $txt['ban_name'],
  1496. ),
  1497. 'data' => array(
  1498. 'sprintf' => array(
  1499. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
  1500. 'params' => array(
  1501. 'id_ban_group' => false,
  1502. 'name' => false,
  1503. ),
  1504. ),
  1505. ),
  1506. 'sort' => array(
  1507. 'default' => 'bg.name',
  1508. 'reverse' => 'bg.name DESC',
  1509. ),
  1510. ),
  1511. 'hits' => array(
  1512. 'header' => array(
  1513. 'value' => $txt['ban_hits'],
  1514. ),
  1515. 'data' => array(
  1516. 'db' => 'hits',
  1517. ),
  1518. 'sort' => array(
  1519. 'default' => 'bi.hits DESC',
  1520. 'reverse' => 'bi.hits',
  1521. ),
  1522. ),
  1523. 'check' => array(
  1524. 'header' => array(
  1525. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1526. 'class' => 'centercol',
  1527. ),
  1528. 'data' => array(
  1529. 'sprintf' => array(
  1530. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1531. 'params' => array(
  1532. 'id_ban' => false,
  1533. ),
  1534. ),
  1535. 'class' => 'centercol',
  1536. ),
  1537. ),
  1538. ),
  1539. 'form' => array(
  1540. 'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1541. 'include_start' => true,
  1542. 'include_sort' => true,
  1543. ),
  1544. 'additional_rows' => array(
  1545. array(
  1546. 'position' => 'above_column_headers',
  1547. '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>',
  1548. ),
  1549. array(
  1550. 'position' => 'bottom_of_list',
  1551. '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" />',
  1552. ),
  1553. ),
  1554. );
  1555. // Specific data for the first column depending on the selected entity.
  1556. if ($context['selected_entity'] === 'ip')
  1557. {
  1558. $listOptions['columns']['banned_entity']['data'] = array(
  1559. 'function' => create_function('$rowData', '
  1560. return range2ip(array(
  1561. $rowData[\'ip_low1\'],
  1562. $rowData[\'ip_low2\'],
  1563. $rowData[\'ip_low3\'],
  1564. $rowData[\'ip_low4\'],
  1565. $rowData[\'ip_low5\'],
  1566. $rowData[\'ip_low6\'],
  1567. $rowData[\'ip_low7\'],
  1568. $rowData[\'ip_low8\']
  1569. ), array(
  1570. $rowData[\'ip_high1\'],
  1571. $rowData[\'ip_high2\'],
  1572. $rowData[\'ip_high3\'],
  1573. $rowData[\'ip_high4\'],
  1574. $rowData[\'ip_high5\'],
  1575. $rowData[\'ip_high6\'],
  1576. $rowData[\'ip_high7\'],
  1577. $rowData[\'ip_high8\']
  1578. ));
  1579. '),
  1580. );
  1581. $listOptions['columns']['banned_entity']['sort'] = array(
  1582. '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',
  1583. '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',
  1584. );
  1585. }
  1586. elseif ($context['selected_entity'] === 'hostname')
  1587. {
  1588. $listOptions['columns']['banned_entity']['data'] = array(
  1589. 'function' => create_function('$rowData', '
  1590. global $smcFunc;
  1591. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'hostname\']), array(\'%\' => \'*\'));
  1592. '),
  1593. );
  1594. $listOptions['columns']['banned_entity']['sort'] = array(
  1595. 'default' => 'bi.hostname',
  1596. 'reverse' => 'bi.hostname DESC',
  1597. );
  1598. }
  1599. elseif ($context['selected_entity'] === 'email')
  1600. {
  1601. $listOptions['columns']['banned_entity']['data'] = array(
  1602. 'function' => create_function('$rowData', '
  1603. global $smcFunc;
  1604. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'email_address\']), array(\'%\' => \'*\'));
  1605. '),
  1606. );
  1607. $listOptions['columns']['banned_entity']['sort'] = array(
  1608. 'default' => 'bi.email_address',
  1609. 'reverse' => 'bi.email_address DESC',
  1610. );
  1611. }
  1612. elseif ($context['selected_entity'] === 'member')
  1613. {
  1614. $listOptions['columns']['banned_entity']['data'] = array(
  1615. 'sprintf' => array(
  1616. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1617. 'params' => array(
  1618. 'id_member' => false,
  1619. 'real_name' => false,
  1620. ),
  1621. ),
  1622. );
  1623. $listOptions['columns']['banned_entity']['sort'] = array(
  1624. 'default' => 'mem.real_name',
  1625. 'reverse' => 'mem.real_name DESC',
  1626. );
  1627. }
  1628. // Create the list.
  1629. require_once($sourcedir . '/Subs-List.php');
  1630. createList($listOptions);
  1631. // The list is the only thing to show, so make it the default sub template.
  1632. $context['sub_template'] = 'show_list';
  1633. $context['default_list'] = 'ban_trigger_list';
  1634. }
  1635. /**
  1636. * Get ban triggers for the given parameters.
  1637. *
  1638. * @param int $start
  1639. * @param int $items_per_page
  1640. * @param string $sort
  1641. * @param string $trigger_type
  1642. * @return array
  1643. */
  1644. function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
  1645. {
  1646. global $smcFunc;
  1647. $where = array(
  1648. 'ip' => 'bi.ip_low1 > 0',
  1649. 'hostname' => 'bi.hostname != {string:blank_string}',
  1650. 'email' => 'bi.email_address != {string:blank_string}',
  1651. );
  1652. $request = $smcFunc['db_query']('', '
  1653. SELECT
  1654. 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,
  1655. bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
  1656. mem.id_member, mem.real_name' : '') . '
  1657. FROM {db_prefix}ban_items AS bi
  1658. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
  1659. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1660. WHERE ' . $where[$trigger_type]) . '
  1661. ORDER BY ' . $sort . '
  1662. LIMIT ' . $start . ', ' . $items_per_page,
  1663. array(
  1664. 'blank_string' => '',
  1665. )
  1666. );
  1667. $ban_triggers = array();
  1668. while ($row = $smcFunc['db_fetch_assoc']($request))
  1669. $ban_triggers[] = $row;
  1670. $smcFunc['db_free_result']($request);
  1671. return $ban_triggers;
  1672. }
  1673. /**
  1674. * This returns the total number of ban triggers of the given type.
  1675. *
  1676. * @param string $trigger_type
  1677. * @return int
  1678. */
  1679. function list_getNumBanTriggers($trigger_type)
  1680. {
  1681. global $smcFunc;
  1682. $where = array(
  1683. 'ip' => 'bi.ip_low1 > 0',
  1684. 'hostname' => 'bi.hostname != {string:blank_string}',
  1685. 'email' => 'bi.email_address != {string:blank_string}',
  1686. );
  1687. $request = $smcFunc['db_query']('', '
  1688. SELECT COUNT(*)
  1689. FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
  1690. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1691. WHERE ' . $where[$trigger_type]),
  1692. array(
  1693. 'blank_string' => '',
  1694. )
  1695. );
  1696. list ($num_triggers) = $smcFunc['db_fetch_row']($request);
  1697. $smcFunc['db_free_result']($request);
  1698. return $num_triggers;
  1699. }
  1700. /**
  1701. * This handles the listing of ban log entries, and allows their deletion.
  1702. * Shows a list of logged access attempts by banned users.
  1703. * It is accessed by ?action=admin;area=ban;sa=log.
  1704. * How it works:
  1705. * - allows sorting of several columns.
  1706. * - also handles deletion of (a selection of) log entries.
  1707. */
  1708. function BanLog()
  1709. {
  1710. global $scripturl, $context, $smcFunc, $sourcedir, $txt;
  1711. global $context;
  1712. // Delete one or more entries.
  1713. if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
  1714. {
  1715. checkSession();
  1716. validateToken('admin-bl');
  1717. // 'Delete all entries' button was pressed.
  1718. if (!empty($_POST['removeAll']))
  1719. removeBanLogs();
  1720. // 'Delete selection' button was pressed.
  1721. else
  1722. {
  1723. array_map('intval', $_POST['remove']);
  1724. removeBanLogs($_POST['remove']);
  1725. }
  1726. }
  1727. $listOptions = array(
  1728. 'id' => 'ban_log',
  1729. 'title' => $txt['ban_log'],
  1730. 'items_per_page' => 30,
  1731. 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1732. 'default_sort_col' => 'date',
  1733. 'get_items' => array(
  1734. 'function' => 'list_getBanLogEntries',
  1735. ),
  1736. 'get_count' => array(
  1737. 'function' => 'list_getNumBanLogEntries',
  1738. ),
  1739. 'no_items_label' => $txt['ban_log_no_entries'],
  1740. 'columns' => array(
  1741. 'ip' => array(
  1742. 'header' => array(
  1743. 'value' => $txt['ban_log_ip'],
  1744. ),
  1745. 'data' => array(
  1746. 'sprintf' => array(
  1747. 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
  1748. 'params' => array(
  1749. 'ip' => false,
  1750. ),
  1751. ),
  1752. ),
  1753. 'sort' => array(
  1754. 'default' => 'lb.ip',
  1755. 'reverse' => 'lb.ip DESC',
  1756. ),
  1757. ),
  1758. 'email' => array(
  1759. 'header' => array(
  1760. 'value' => $txt['ban_log_email'],
  1761. ),
  1762. 'data' => array(
  1763. 'db_htmlsafe' => 'email',
  1764. ),
  1765. 'sort' => array(
  1766. 'default' => 'lb.email = \'\', lb.email',
  1767. 'reverse' => 'lb.email != \'\', lb.email DESC',
  1768. ),
  1769. ),
  1770. 'member' => array(
  1771. 'header' => array(
  1772. 'value' => $txt['ban_log_member'],
  1773. ),
  1774. 'data' => array(
  1775. 'sprintf' => array(
  1776. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1777. 'params' => array(
  1778. 'id_member' => false,
  1779. 'real_name' => false,
  1780. ),
  1781. ),
  1782. ),
  1783. 'sort' => array(
  1784. 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
  1785. 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
  1786. ),
  1787. ),
  1788. 'date' => array(
  1789. 'header' => array(
  1790. 'value' => $txt['ban_log_date'],
  1791. ),
  1792. 'data' => array(
  1793. 'function' => create_function('$rowData', '
  1794. return timeformat($rowData[\'log_time\']);
  1795. '),
  1796. ),
  1797. 'sort' => array(
  1798. 'default' => 'lb.log_time DESC',
  1799. 'reverse' => 'lb.log_time',
  1800. ),
  1801. ),
  1802. 'check' => array(
  1803. 'header' => array(
  1804. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1805. 'class' => 'centercol',
  1806. ),
  1807. 'data' => array(
  1808. 'sprintf' => array(
  1809. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1810. 'params' => array(
  1811. 'id_ban_log' => false,
  1812. ),
  1813. ),
  1814. 'class' => 'centercol',
  1815. ),
  1816. ),
  1817. ),
  1818. 'form' => array(
  1819. 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1820. 'include_start' => true,
  1821. 'include_sort' => true,
  1822. 'token' => 'admin-bl',
  1823. ),
  1824. 'additional_rows' => array(
  1825. array(
  1826. 'position' => 'bottom_of_list',
  1827. 'value' => '
  1828. <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit" />
  1829. <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit" />',
  1830. ),
  1831. ),
  1832. );
  1833. createToken('admin-bl');
  1834. require_once($sourcedir . '/Subs-List.php');
  1835. createList($listOptions);
  1836. $context['page_title'] = $txt['ban_log'];
  1837. $context['sub_template'] = 'show_list';
  1838. $context['default_list'] = 'ban_log';
  1839. }
  1840. /**
  1841. * Load a list of ban log entries from the database.
  1842. * (no permissions check)
  1843. *
  1844. * @param int $start
  1845. * @param int $items_per_page
  1846. * @param string $sort
  1847. */
  1848. function list_getBanLogEntries($start, $items_per_page, $sort)
  1849. {
  1850. global $smcFunc;
  1851. $request = $smcFunc['db_query']('', '
  1852. 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
  1853. FROM {db_prefix}log_banned AS lb
  1854. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
  1855. ORDER BY ' . $sort . '
  1856. LIMIT ' . $start . ', ' . $items_per_page,
  1857. array(
  1858. 'blank_string' => '',
  1859. 'dash' => '-',
  1860. )
  1861. );
  1862. $log_entries = array();
  1863. while ($row = $smcFunc['db_fetch_assoc']($request))
  1864. $log_entries[] = $row;
  1865. $smcFunc['db_free_result']($request);
  1866. return $log_entries;
  1867. }
  1868. /**
  1869. * This returns the total count of ban log entries.
  1870. */
  1871. function list_getNumBanLogEntries()
  1872. {
  1873. global $smcFunc;
  1874. $request = $smcFunc['db_query']('', '
  1875. SELECT COUNT(*)
  1876. FROM {db_prefix}log_banned AS lb',
  1877. array(
  1878. )
  1879. );
  1880. list ($num_entries) = $smcFunc['db_fetch_row']($request);
  1881. $smcFunc['db_free_result']($request);
  1882. return $num_entries;
  1883. }
  1884. /**
  1885. * Convert a range of given IP number into a single string.
  1886. * It's practically the reverse function of ip2range().
  1887. *
  1888. * @example
  1889. * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
  1890. *
  1891. * @param array $low IPv4 format
  1892. * @param array $high IPv4 format
  1893. * @return string
  1894. */
  1895. function range2ip($low, $high)
  1896. {
  1897. // IPv6 check.
  1898. if (!empty($high[4]) || !empty($high[5]) || !empty($high[6]) || !empty($high[7]))
  1899. {
  1900. if (count($low) != 8 || count($high) != 8)
  1901. return '';
  1902. $ip = array();
  1903. for ($i = 0; $i < 8; $i++)
  1904. {
  1905. if ($low[$i] == $high[$i])
  1906. $ip[$i] = dechex($low[$i]);
  1907. elseif ($low[$i] == '0' && $high[$i] == '255')
  1908. $ip[$i] = '*';
  1909. else
  1910. $ip[$i] = dechex($low[$i]) . '-' . dechex($high[$i]);
  1911. }
  1912. return implode(':', $ip);
  1913. }
  1914. // Legacy IPv4 stuff.
  1915. // (count($low) != 4 || count($high) != 4) would not work because $low and $high always contain 8 elements!
  1916. if ((count($low) != 4 || count($high) != 4) && (count($low) != 8 || count($high) != 8))
  1917. return '';
  1918. for ($i = 0; $i < 4; $i++)
  1919. {
  1920. if ($low[$i] == $high[$i])
  1921. $ip[$i] = $low[$i];
  1922. elseif ($low[$i] == '0' && $high[$i] == '255')
  1923. $ip[$i] = '*';
  1924. else
  1925. $ip[$i] = $low[$i] . '-' . $high[$i];
  1926. }
  1927. // Pretending is fun... the IP can't be this, so use it for 'unknown'.
  1928. if ($ip == array(255, 255, 255, 255))
  1929. return 'unknown';
  1930. return implode('.', $ip);
  1931. }
  1932. /**
  1933. * Checks whether a given IP range already exists in the trigger list.
  1934. * If yes, it returns an error message. Otherwise, it returns an array
  1935. * optimized for the database.
  1936. *
  1937. * @param array $ip_array
  1938. * @param string $fullip
  1939. * @return boolean
  1940. */
  1941. function checkExistingTriggerIP($ip_array, $fullip = '')
  1942. {
  1943. global $smcFunc, $scripturl;
  1944. if (count($ip_array) == 4 || count($ip_array) == 8)
  1945. $values = array(
  1946. 'ip_low1' => $ip_array[0]['low'],
  1947. 'ip_high1' => $ip_array[0]['high'],
  1948. 'ip_low2' => $ip_array[1]['low'],
  1949. 'ip_high2' => $ip_array[1]['high'],
  1950. 'ip_low3' => $ip_array[2]['low'],
  1951. 'ip_high3' => $ip_array[2]['high'],
  1952. 'ip_low4' => $ip_array[3]['low'],
  1953. 'ip_high4' => $ip_array[3]['high'],
  1954. 'ip_low5' => $ip_array[4]['low'],
  1955. 'ip_high5' => $ip_array[4]['high'],
  1956. 'ip_low6' => $ip_array[5]['low'],
  1957. 'ip_high6' => $ip_array[5]['high'],
  1958. 'ip_low7' => $ip_array[6]['low'],
  1959. 'ip_high7' => $ip_array[6]['high'],
  1960. 'ip_low8' => $ip_array[7]['low'],
  1961. 'ip_high8' => $ip_array[7]['high'],
  1962. );
  1963. else
  1964. return false;
  1965. $request = $smcFunc['db_query']('', '
  1966. SELECT bg.id_ban_group, bg.name
  1967. FROM {db_prefix}ban_groups AS bg
  1968. INNER JOIN {db_prefix}ban_items AS bi ON
  1969. (bi.id_ban_group = bg.id_ban_group)
  1970. AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
  1971. AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
  1972. AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
  1973. AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
  1974. AND ip_low5 = {int:ip_low5} AND ip_high5 = {int:ip_high5}
  1975. AND ip_low6 = {int:ip_low6} AND ip_high6 = {int:ip_high6}
  1976. AND ip_low7 = {int:ip_low7} AND ip_high7 = {int:ip_high7}
  1977. AND ip_low8 = {int:ip_low8} AND ip_high8 = {int:ip_high8}
  1978. LIMIT 1',
  1979. $values
  1980. );
  1981. if ($smcFunc['db_num_rows']($request) != 0)
  1982. {
  1983. list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
  1984. fatal_lang_error('ban_trigger_already_exists', false, array(
  1985. $fullip,
  1986. '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
  1987. ));
  1988. }
  1989. $smcFunc['db_free_result']($request);
  1990. return $values;
  1991. }
  1992. /**
  1993. * As it says... this tries to review the list of banned members, to match new bans.
  1994. * Note: is_activated >= 10: a member is banned.
  1995. */
  1996. function updateBanMembers()
  1997. {
  1998. global $smcFunc;
  1999. $updates = array();
  2000. $allMembers = array();
  2001. $newMembers = array();
  2002. // Start by getting all active bans - it's quicker doing this in parts...
  2003. $request = $smcFunc['db_query']('', '
  2004. SELECT bi.id_member, bi.email_address
  2005. FROM {db_prefix}ban_items AS bi
  2006. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  2007. WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
  2008. AND bg.cannot_access = {int:cannot_access_on}
  2009. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  2010. array(
  2011. 'no_member' => 0,
  2012. 'cannot_access_on' => 1,
  2013. 'current_time' => time(),
  2014. 'blank_string' => '',
  2015. )
  2016. );
  2017. $memberIDs = array();
  2018. $memberEmails = array();
  2019. $memberEmailWild = array();
  2020. while ($row = $smcFunc['db_fetch_assoc']($request))
  2021. {
  2022. if ($row['id_member'])
  2023. $memberIDs[$row['id_member']] = $row['id_member'];
  2024. if ($row['email_address'])
  2025. {
  2026. // Does it have a wildcard - if so we can't do a IN on it.
  2027. if (strpos($row['email_address'], '%') !== false)
  2028. $memberEmailWild[$row['email_address']] = $row['email_address'];
  2029. else
  2030. $memberEmails[$row['email_address']] = $row['email_address'];
  2031. }
  2032. }
  2033. $smcFunc['db_free_result']($request);
  2034. // Build up the query.
  2035. $queryPart = array();
  2036. $queryValues = array();
  2037. if (!empty($memberIDs))
  2038. {
  2039. $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
  2040. $queryValues['member_ids'] = $memberIDs;
  2041. }
  2042. if (!empty($memberEmails))
  2043. {
  2044. $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
  2045. $queryValues['member_emails'] = $memberEmails;
  2046. }
  2047. $count = 0;
  2048. foreach ($memberEmailWild as $email)
  2049. {
  2050. $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
  2051. $queryValues['wild_' . $count++] = $email;
  2052. }
  2053. // Find all banned members.
  2054. if (!empty($queryPart))
  2055. {
  2056. $request = $smcFunc['db_query']('', '
  2057. SELECT mem.id_member, mem.is_activated
  2058. FROM {db_prefix}members AS mem
  2059. WHERE ' . implode( ' OR ', $queryPart),
  2060. $queryValues
  2061. );
  2062. while ($row = $smcFunc['db_fetch_assoc']($request))
  2063. {
  2064. if (!in_array($row['id_member'], $allMembers))
  2065. {
  2066. $allMembers[] = $row['id_member'];
  2067. // Do they need an update?
  2068. if ($row['is_activated'] < 10)
  2069. {
  2070. $updates[($row['is_activated'] + 10)][] = $row['id_member'];
  2071. $newMembers[] = $row['id_member'];
  2072. }
  2073. }
  2074. }
  2075. $smcFunc['db_free_result']($request);
  2076. }
  2077. // We welcome our new members in the realm of the banned.
  2078. if (!empty($newMembers))
  2079. $smcFunc['db_query']('', '
  2080. DELETE FROM {db_prefix}log_online
  2081. WHERE id_member IN ({array_int:new_banned_members})',
  2082. array(
  2083. 'new_banned_members' => $newMembers,
  2084. )
  2085. );
  2086. // Find members that are wrongfully marked as banned.
  2087. $request = $smcFunc['db_query']('', '
  2088. SELECT mem.id_member, mem.is_activated - 10 AS new_value
  2089. FROM {db_prefix}members AS mem
  2090. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
  2091. 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}))
  2092. WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
  2093. AND mem.is_activated >= {int:ban_flag}',
  2094. array(
  2095. 'cannot_access_activated' => 1,
  2096. 'current_time' => time(),
  2097. 'ban_flag' => 10,
  2098. )
  2099. );
  2100. while ($row = $smcFunc['db_fetch_assoc']($request))
  2101. {
  2102. // Don't do this twice!
  2103. if (!in_array($row['id_member'], $allMembers))
  2104. {
  2105. $updates[$row['new_value']][] = $row['id_member'];
  2106. $allMembers[] = $row['id_member'];
  2107. }
  2108. }
  2109. $smcFunc['db_free_result']($request);
  2110. if (!empty($updates))
  2111. foreach ($updates as $newStatus => $members)
  2112. updateMemberData($members, array('is_activated' => $newStatus));
  2113. // Update the latest member and our total members as banning may change them.
  2114. updateStats('member');
  2115. }
  2116. ?>