ManageBans.php 52 KB

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