ManageBans.php 54 KB

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