ManageBans.php 54 KB

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