ManageBans.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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.1 Alpha 1
  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. 'ip_low5' => 0,
  343. 'ip_high5' => 0,
  344. 'ip_low6' => 0,
  345. 'ip_high6' => 0,
  346. 'ip_low7' => 0,
  347. 'ip_high7' => 0,
  348. 'ip_low8' => 0,
  349. 'ip_high8' => 0,
  350. );
  351. // Preset all values that are required.
  352. if ($newBan)
  353. {
  354. $insertKeys = array(
  355. 'id_ban_group' => 'int',
  356. 'hostname' => 'string',
  357. 'email_address' => 'string',
  358. 'id_member' => 'int',
  359. 'ip_low1' => 'int',
  360. 'ip_high1' => 'int',
  361. 'ip_low2' => 'int',
  362. 'ip_high2' => 'int',
  363. 'ip_low3' => 'int',
  364. 'ip_high3' => 'int',
  365. 'ip_low4' => 'int',
  366. 'ip_high4' => 'int',
  367. 'ip_low5' => 'int',
  368. 'ip_high5' => 'int',
  369. 'ip_low6' => 'int',
  370. 'ip_high6' => 'int',
  371. 'ip_low7' => 'int',
  372. 'ip_high7' => 'int',
  373. 'ip_low8' => 'int',
  374. 'ip_high8' => 'int',
  375. );
  376. }
  377. else
  378. $updateString = '
  379. hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
  380. ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
  381. ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
  382. ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
  383. ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4},
  384. ip_low5 = {int:ip_low5}, ip_high5 = {int:ip_high5},
  385. ip_low6 = {int:ip_low6}, ip_high6 = {int:ip_high6},
  386. ip_low7 = {int:ip_low7}, ip_high7 = {int:ip_high7},
  387. ip_low8 = {int:ip_low8}, ip_high8 = {int:ip_high8}';
  388. if ($_POST['bantype'] == 'ip_ban')
  389. {
  390. $ip = trim($_POST['ip']);
  391. $ip_parts = ip2range($ip);
  392. $ip_check = checkExistingTriggerIP($ip_parts, $ip);
  393. if (!$ip_check)
  394. fatal_lang_error('invalid_ip', false);
  395. $values = array_merge($values, $ip_check);
  396. $modlogInfo['ip_range'] = $_POST['ip'];
  397. }
  398. elseif ($_POST['bantype'] == 'hostname_ban')
  399. {
  400. if (preg_match('/[^\w.\-*]/', $_POST['hostname']) == 1)
  401. fatal_lang_error('invalid_hostname', false);
  402. // Replace the * wildcard by a MySQL compatible wildcard %.
  403. $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
  404. $values['hostname'] = $_POST['hostname'];
  405. $modlogInfo['hostname'] = $_POST['hostname'];
  406. }
  407. elseif ($_POST['bantype'] == 'email_ban')
  408. {
  409. if (preg_match('/[^\w.\-\+*@]/', $_POST['email']) == 1)
  410. fatal_lang_error('invalid_email', false);
  411. $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
  412. // Check the user is not banning an admin.
  413. $request = $smcFunc['db_query']('', '
  414. SELECT id_member
  415. FROM {db_prefix}members
  416. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  417. AND email_address LIKE {string:email}
  418. LIMIT 1',
  419. array(
  420. 'admin_group' => 1,
  421. 'email' => $_POST['email'],
  422. )
  423. );
  424. if ($smcFunc['db_num_rows']($request) != 0)
  425. fatal_lang_error('no_ban_admin', 'critical');
  426. $smcFunc['db_free_result']($request);
  427. $values['email_address'] = $_POST['email'];
  428. $modlogInfo['email'] = $_POST['email'];
  429. }
  430. elseif ($_POST['bantype'] == 'user_ban')
  431. {
  432. $_POST['user'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
  433. $request = $smcFunc['db_query']('', '
  434. SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
  435. FROM {db_prefix}members
  436. WHERE member_name = {string:user_name} OR real_name = {string:user_name}
  437. LIMIT 1',
  438. array(
  439. 'admin_group' => 1,
  440. 'user_name' => $_POST['user'],
  441. )
  442. );
  443. if ($smcFunc['db_num_rows']($request) == 0)
  444. fatal_lang_error('invalid_username', false);
  445. list ($memberid, $isAdmin) = $smcFunc['db_fetch_row']($request);
  446. $smcFunc['db_free_result']($request);
  447. if ($isAdmin && $isAdmin != 'f')
  448. fatal_lang_error('no_ban_admin', 'critical');
  449. $values['id_member'] = $memberid;
  450. $modlogInfo['member'] = $memberid;
  451. }
  452. else
  453. fatal_lang_error('no_bantype_selected', false);
  454. if ($newBan)
  455. $smcFunc['db_insert']('',
  456. '{db_prefix}ban_items',
  457. $insertKeys,
  458. $values,
  459. array('id_ban')
  460. );
  461. else
  462. $smcFunc['db_query']('', '
  463. UPDATE {db_prefix}ban_items
  464. SET ' . $updateString . '
  465. WHERE id_ban = {int:ban_item}
  466. AND id_ban_group = {int:id_ban_group}',
  467. array_merge($values, array(
  468. 'ban_item' => (int) $_REQUEST['bi'],
  469. ))
  470. );
  471. // Log the addion of the ban entry into the moderation log.
  472. logAction('ban', $modlogInfo + array(
  473. 'new' => $newBan,
  474. 'type' => $_POST['bantype'],
  475. ));
  476. // Register the last modified date.
  477. updateSettings(array('banLastUpdated' => time()));
  478. // Update the member table to represent the new ban situation.
  479. updateBanMembers();
  480. }
  481. // The user pressed 'Remove selected ban entries'.
  482. elseif (!empty($_POST['remove_selection']) && !empty($_POST['ban_items']) && is_array($_POST['ban_items']))
  483. {
  484. checkSession();
  485. validateToken('admin-bet');
  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. validateToken('admin-bet');
  507. $addBan = !empty($_POST['add_ban']);
  508. if (empty($_POST['ban_name']))
  509. fatal_lang_error('ban_name_empty', false);
  510. // Let's not allow HTML in ban names, it's more evil than beneficial.
  511. $_POST['ban_name'] = $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES);
  512. // Check whether a ban with this name already exists.
  513. $request = $smcFunc['db_query']('', '
  514. SELECT id_ban_group
  515. FROM {db_prefix}ban_groups
  516. WHERE name = {string:new_ban_name}' . ($addBan ? '' : '
  517. AND id_ban_group != {int:ban_group}') . '
  518. LIMIT 1',
  519. array(
  520. 'ban_group' => $_REQUEST['bg'],
  521. 'new_ban_name' => $_POST['ban_name'],
  522. )
  523. );
  524. if ($smcFunc['db_num_rows']($request) == 1)
  525. fatal_lang_error('ban_name_exists', false, array($_POST['ban_name']));
  526. $smcFunc['db_free_result']($request);
  527. $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
  528. $_POST['notes'] = $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES);
  529. $_POST['notes'] = str_replace(array("\r", "\n", ' '), array('', '<br />', '&nbsp; '), $_POST['notes']);
  530. $_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'));
  531. $_POST['full_ban'] = empty($_POST['full_ban']) ? '0' : '1';
  532. $_POST['cannot_post'] = !empty($_POST['full_ban']) || empty($_POST['cannot_post']) ? '0' : '1';
  533. $_POST['cannot_register'] = !empty($_POST['full_ban']) || empty($_POST['cannot_register']) ? '0' : '1';
  534. $_POST['cannot_login'] = !empty($_POST['full_ban']) || empty($_POST['cannot_login']) ? '0' : '1';
  535. if ($addBan)
  536. {
  537. // Adding some ban triggers?
  538. if ($addBan && !empty($_POST['ban_suggestion']) && is_array($_POST['ban_suggestion']))
  539. {
  540. $ban_triggers = array();
  541. $ban_logs = array();
  542. if (in_array('main_ip', $_POST['ban_suggestion']) && !empty($_POST['main_ip']))
  543. {
  544. $ip = trim($_POST['main_ip']);
  545. $ip_parts = ip2range($ip);
  546. if (!checkExistingTriggerIP($ip_parts, $ip))
  547. fatal_lang_error('invalid_ip', false);
  548. $ban_triggers[] = array(
  549. $ip_parts[0]['low'],
  550. $ip_parts[0]['high'],
  551. $ip_parts[1]['low'],
  552. $ip_parts[1]['high'],
  553. $ip_parts[2]['low'],
  554. $ip_parts[2]['high'],
  555. $ip_parts[3]['low'],
  556. $ip_parts[3]['high'],
  557. $ip_parts[4]['low'],
  558. $ip_parts[4]['high'],
  559. $ip_parts[5]['low'],
  560. $ip_parts[5]['high'],
  561. $ip_parts[6]['low'],
  562. $ip_parts[6]['high'],
  563. $ip_parts[7]['low'],
  564. $ip_parts[7]['high'],
  565. '',
  566. '',
  567. 0,
  568. );
  569. $ban_logs[] = array(
  570. 'ip_range' => $_POST['main_ip'],
  571. );
  572. }
  573. if (in_array('hostname', $_POST['ban_suggestion']) && !empty($_POST['hostname']))
  574. {
  575. if (preg_match('/[^\w.\-*]/', $_POST['hostname']) == 1)
  576. fatal_lang_error('invalid_hostname', false);
  577. // Replace the * wildcard by a MySQL wildcard %.
  578. $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
  579. $ban_triggers[] = array(
  580. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  581. substr($_POST['hostname'], 0, 255),
  582. '',
  583. 0,
  584. );
  585. $ban_logs[] = array(
  586. 'hostname' => $_POST['hostname'],
  587. );
  588. }
  589. if (in_array('email', $_POST['ban_suggestion']) && !empty($_POST['email']))
  590. {
  591. if (preg_match('/[^\w.\-\+*@]/', $_POST['email']) == 1)
  592. fatal_lang_error('invalid_email', false);
  593. $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
  594. $ban_triggers[] = array(
  595. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  596. '',
  597. substr($_POST['email'], 0, 255),
  598. 0,
  599. );
  600. $ban_logs[] = array(
  601. 'email' => $_POST['email'],
  602. );
  603. }
  604. if (in_array('user', $_POST['ban_suggestion']) && (!empty($_POST['bannedUser']) || !empty($_POST['user'])))
  605. {
  606. // We got a username, let's find its ID.
  607. if (empty($_POST['bannedUser']))
  608. {
  609. $_POST['user'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
  610. $request = $smcFunc['db_query']('', '
  611. SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
  612. FROM {db_prefix}members
  613. WHERE member_name = {string:username} OR real_name = {string:username}
  614. LIMIT 1',
  615. array(
  616. 'admin_group' => 1,
  617. 'username' => $_POST['user'],
  618. )
  619. );
  620. if ($smcFunc['db_num_rows']($request) == 0)
  621. fatal_lang_error('invalid_username', false);
  622. list ($_POST['bannedUser'], $isAdmin) = $smcFunc['db_fetch_row']($request);
  623. $smcFunc['db_free_result']($request);
  624. if ($isAdmin && $isAdmin != 'f')
  625. fatal_lang_error('no_ban_admin', 'critical');
  626. }
  627. $ban_triggers[] = array(
  628. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  629. '',
  630. '',
  631. (int) $_POST['bannedUser'],
  632. );
  633. $ban_logs[] = array(
  634. 'member' => $_POST['bannedUser'],
  635. );
  636. }
  637. if (!empty($_POST['ban_suggestion']['ips']) && is_array($_POST['ban_suggestion']['ips']))
  638. {
  639. $_POST['ban_suggestion']['ips'] = array_unique($_POST['ban_suggestion']['ips']);
  640. // Don't add the main IP again.
  641. if (in_array('main_ip', $_POST['ban_suggestion']))
  642. $_POST['ban_suggestion']['ips'] = array_diff($_POST['ban_suggestion']['ips'], array($_POST['main_ip']));
  643. foreach ($_POST['ban_suggestion']['ips'] as $ip)
  644. {
  645. $ip_parts = ip2range($ip);
  646. // They should be alright, but just to be sure...
  647. if (count($ip_parts) != 4 || count($ip_parts) != 8)
  648. fatal_lang_error('invalid_ip', false);
  649. $ban_triggers[] = array(
  650. $ip_parts[0]['low'],
  651. $ip_parts[0]['high'],
  652. $ip_parts[1]['low'],
  653. $ip_parts[1]['high'],
  654. $ip_parts[2]['low'],
  655. $ip_parts[2]['high'],
  656. $ip_parts[3]['low'],
  657. $ip_parts[3]['high'],
  658. $ip_parts[4]['low'],
  659. $ip_parts[4]['high'],
  660. $ip_parts[5]['low'],
  661. $ip_parts[5]['high'],
  662. $ip_parts[6]['low'],
  663. $ip_parts[6]['high'],
  664. $ip_parts[7]['low'],
  665. $ip_parts[7]['high'],
  666. '',
  667. '',
  668. 0,
  669. );
  670. $ban_logs[] = array(
  671. 'ip_range' => $ip,
  672. );
  673. }
  674. }
  675. }
  676. // Yes yes, we're ready to add now.
  677. $smcFunc['db_insert']('',
  678. '{db_prefix}ban_groups',
  679. array(
  680. 'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
  681. 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
  682. ),
  683. array(
  684. $_POST['ban_name'], time(), $_POST['expiration'], $_POST['full_ban'], $_POST['cannot_register'],
  685. $_POST['cannot_post'], $_POST['cannot_login'], $_POST['reason'], $_POST['notes'],
  686. ),
  687. array('id_ban_group')
  688. );
  689. $_REQUEST['bg'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
  690. // Now that the ban group is added, add some triggers as well.
  691. if (!empty($ban_triggers) && !empty($_REQUEST['bg']))
  692. {
  693. // Put in the ban group ID.
  694. foreach ($ban_triggers as $k => $trigger)
  695. array_unshift($ban_triggers[$k], $_REQUEST['bg']);
  696. // Log what we are doing!
  697. foreach ($ban_logs as $log_details)
  698. logAction('ban', $log_details + array('new' => 1));
  699. $smcFunc['db_insert']('',
  700. '{db_prefix}ban_items',
  701. array(
  702. 'id_ban_group' => 'int', 'ip_low1' => 'int', 'ip_high1' => 'int', 'ip_low2' => 'int', 'ip_high2' => 'int',
  703. 'ip_low3' => 'int', 'ip_high3' => 'int', 'ip_low4' => 'int', 'ip_high4' => 'int', 'ip_low5' => 'int',
  704. 'ip_high5' => 'int', 'ip_low6' => 'int', 'ip_high6' => 'int', 'ip_low7' => 'int', 'ip_high7' => 'int',
  705. 'ip_low8' => 'int', 'ip_high8' => 'int', 'hostname' => 'string-255', 'email_address' => 'string-255', 'id_member' => 'int',
  706. ),
  707. $ban_triggers,
  708. array('id_ban')
  709. );
  710. }
  711. }
  712. else
  713. $smcFunc['db_query']('', '
  714. UPDATE {db_prefix}ban_groups
  715. SET
  716. name = {string:ban_name},
  717. reason = {string:reason},
  718. notes = {string:notes},
  719. expire_time = {raw:expiration},
  720. cannot_access = {int:cannot_access},
  721. cannot_post = {int:cannot_post},
  722. cannot_register = {int:cannot_register},
  723. cannot_login = {int:cannot_login}
  724. WHERE id_ban_group = {int:id_ban_group}',
  725. array(
  726. 'expiration' => $_POST['expiration'],
  727. 'cannot_access' => $_POST['full_ban'],
  728. 'cannot_post' => $_POST['cannot_post'],
  729. 'cannot_register' => $_POST['cannot_register'],
  730. 'cannot_login' => $_POST['cannot_login'],
  731. 'id_ban_group' => $_REQUEST['bg'],
  732. 'ban_name' => $_POST['ban_name'],
  733. 'reason' => $_POST['reason'],
  734. 'notes' => $_POST['notes'],
  735. )
  736. );
  737. // No more caching, we have something new here.
  738. updateSettings(array('banLastUpdated' => time()));
  739. updateBanMembers();
  740. }
  741. // If we're editing an existing ban, get it from the database.
  742. if (!empty($_REQUEST['bg']))
  743. {
  744. $context['ban_items'] = array();
  745. $request = $smcFunc['db_query']('', '
  746. SELECT
  747. bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
  748. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  749. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  750. 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,
  751. IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
  752. FROM {db_prefix}ban_groups AS bg
  753. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  754. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  755. WHERE bg.id_ban_group = {int:current_ban}',
  756. array(
  757. 'current_ban' => $_REQUEST['bg'],
  758. )
  759. );
  760. if ($smcFunc['db_num_rows']($request) == 0)
  761. fatal_lang_error('ban_not_found', false);
  762. while ($row = $smcFunc['db_fetch_assoc']($request))
  763. {
  764. if (!isset($context['ban']))
  765. {
  766. $context['ban'] = array(
  767. 'id' => $row['id_ban_group'],
  768. 'name' => $row['name'],
  769. 'expiration' => array(
  770. 'status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'still_active_but_we_re_counting_the_days'),
  771. 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0
  772. ),
  773. 'reason' => $row['reason'],
  774. 'notes' => $row['notes'],
  775. 'cannot' => array(
  776. 'access' => !empty($row['cannot_access']),
  777. 'post' => !empty($row['cannot_post']),
  778. 'register' => !empty($row['cannot_register']),
  779. 'login' => !empty($row['cannot_login']),
  780. ),
  781. 'is_new' => false,
  782. );
  783. }
  784. if (!empty($row['id_ban']))
  785. {
  786. $context['ban_items'][$row['id_ban']] = array(
  787. 'id' => $row['id_ban'],
  788. 'hits' => $row['hits'],
  789. );
  790. if (!empty($row['ip_high1']))
  791. {
  792. $context['ban_items'][$row['id_ban']]['type'] = 'ip';
  793. $context['ban_items'][$row['id_ban']]['ip'] = range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4'] ,$row['ip_low5'], $row['ip_low6'], $row['ip_low7'], $row['ip_low8']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4'], $row['ip_high5'], $row['ip_high6'], $row['ip_high7'], $row['ip_high8']));
  794. }
  795. elseif (!empty($row['hostname']))
  796. {
  797. $context['ban_items'][$row['id_ban']]['type'] = 'hostname';
  798. $context['ban_items'][$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
  799. }
  800. elseif (!empty($row['email_address']))
  801. {
  802. $context['ban_items'][$row['id_ban']]['type'] = 'email';
  803. $context['ban_items'][$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
  804. }
  805. elseif (!empty($row['id_member']))
  806. {
  807. $context['ban_items'][$row['id_ban']]['type'] = 'user';
  808. $context['ban_items'][$row['id_ban']]['user'] = array(
  809. 'id' => $row['id_member'],
  810. 'name' => $row['real_name'],
  811. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  812. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  813. );
  814. }
  815. // Invalid ban (member probably doesn't exist anymore).
  816. else
  817. {
  818. unset($context['ban_items'][$row['id_ban']]);
  819. $smcFunc['db_query']('', '
  820. DELETE FROM {db_prefix}ban_items
  821. WHERE id_ban = {int:current_ban}',
  822. array(
  823. 'current_ban' => $row['id_ban'],
  824. )
  825. );
  826. }
  827. }
  828. }
  829. $smcFunc['db_free_result']($request);
  830. }
  831. // Not an existing one, then it's probably a new one.
  832. else
  833. {
  834. $context['ban'] = array(
  835. 'id' => 0,
  836. 'name' => '',
  837. 'expiration' => array(
  838. 'status' => 'never',
  839. 'days' => 0
  840. ),
  841. 'reason' => '',
  842. 'notes' => '',
  843. 'ban_days' => 0,
  844. 'cannot' => array(
  845. 'access' => true,
  846. 'post' => false,
  847. 'register' => false,
  848. 'login' => false,
  849. ),
  850. 'is_new' => true,
  851. );
  852. $context['ban_suggestions'] = array(
  853. 'main_ip' => '',
  854. 'hostname' => '',
  855. 'email' => '',
  856. 'member' => array(
  857. 'id' => 0,
  858. ),
  859. );
  860. // Overwrite some of the default form values if a user ID was given.
  861. if (!empty($_REQUEST['u']))
  862. {
  863. $request = $smcFunc['db_query']('', '
  864. SELECT id_member, real_name, member_ip, email_address
  865. FROM {db_prefix}members
  866. WHERE id_member = {int:current_user}
  867. LIMIT 1',
  868. array(
  869. 'current_user' => (int) $_REQUEST['u'],
  870. )
  871. );
  872. if ($smcFunc['db_num_rows']($request) > 0)
  873. 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);
  874. $smcFunc['db_free_result']($request);
  875. if (!empty($context['ban_suggestions']['member']['id']))
  876. {
  877. $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
  878. $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
  879. // Default the ban name to the name of the banned member.
  880. $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
  881. // Would be nice if we could also ban the hostname.
  882. if (preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 && empty($modSettings['disableHostnameLookup']))
  883. $context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
  884. // Find some additional IP's used by this member.
  885. $context['ban_suggestions']['message_ips'] = array();
  886. $request = $smcFunc['db_query']('ban_suggest_message_ips', '
  887. SELECT DISTINCT poster_ip
  888. FROM {db_prefix}messages
  889. WHERE id_member = {int:current_user}
  890. AND poster_ip RLIKE {string:poster_ip_regex}
  891. ORDER BY poster_ip',
  892. array(
  893. 'current_user' => (int) $_REQUEST['u'],
  894. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  895. )
  896. );
  897. while ($row = $smcFunc['db_fetch_assoc']($request))
  898. $context['ban_suggestions']['message_ips'][] = $row['poster_ip'];
  899. $smcFunc['db_free_result']($request);
  900. $context['ban_suggestions']['error_ips'] = array();
  901. $request = $smcFunc['db_query']('ban_suggest_error_ips', '
  902. SELECT DISTINCT ip
  903. FROM {db_prefix}log_errors
  904. WHERE id_member = {int:current_user}
  905. AND ip RLIKE {string:poster_ip_regex}
  906. ORDER BY ip',
  907. array(
  908. 'current_user' => (int) $_REQUEST['u'],
  909. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  910. )
  911. );
  912. while ($row = $smcFunc['db_fetch_assoc']($request))
  913. $context['ban_suggestions']['error_ips'][] = $row['ip'];
  914. $smcFunc['db_free_result']($request);
  915. // Borrowing a few language strings from profile.
  916. loadLanguage('Profile');
  917. }
  918. }
  919. }
  920. // Template needs this to show errors using javascript
  921. loadLanguage('Errors');
  922. // If we're in wireless mode remove the admin template layer and use a special template.
  923. if (WIRELESS && WIRELESS_PROTOCOL != 'wap')
  924. {
  925. $context['sub_template'] = WIRELESS_PROTOCOL . '_ban_edit';
  926. foreach ($context['template_layers'] as $k => $v)
  927. if (strpos($v, 'generic_menu') === 0)
  928. unset($context['template_layers'][$k]);
  929. }
  930. else
  931. $context['sub_template'] = 'ban_edit';
  932. createToken('admin-bet');
  933. }
  934. /**
  935. * This function handles the ins and outs of the screen for adding new ban
  936. * triggers or modifying existing ones.
  937. * Adding new ban triggers:
  938. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
  939. * - uses the ban_edit_trigger sub template of ManageBans.
  940. * Editing existing ban triggers:
  941. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
  942. * - uses the ban_edit_trigger sub template of ManageBans.
  943. */
  944. function BanEditTrigger()
  945. {
  946. global $context, $smcFunc;
  947. $context['sub_template'] = 'ban_edit_trigger';
  948. if (empty($_REQUEST['bg']))
  949. fatal_lang_error('ban_not_found', false);
  950. if (empty($_REQUEST['bi']))
  951. {
  952. $context['ban_trigger'] = array(
  953. 'id' => 0,
  954. 'group' => (int) $_REQUEST['bg'],
  955. 'ip' => array(
  956. 'value' => '',
  957. 'selected' => true,
  958. ),
  959. 'hostname' => array(
  960. 'selected' => false,
  961. 'value' => '',
  962. ),
  963. 'email' => array(
  964. 'value' => '',
  965. 'selected' => false,
  966. ),
  967. 'banneduser' => array(
  968. 'value' => '',
  969. 'selected' => false,
  970. ),
  971. 'is_new' => true,
  972. );
  973. }
  974. else
  975. {
  976. $request = $smcFunc['db_query']('', '
  977. SELECT
  978. bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
  979. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  980. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8
  981. mem.member_name, mem.real_name
  982. FROM {db_prefix}ban_items AS bi
  983. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  984. WHERE bi.id_ban = {int:ban_item}
  985. AND bi.id_ban_group = {int:ban_group}
  986. LIMIT 1',
  987. array(
  988. 'ban_item' => (int) $_REQUEST['bi'],
  989. 'ban_group' => (int) $_REQUEST['bg'],
  990. )
  991. );
  992. if ($smcFunc['db_num_rows']($request) == 0)
  993. fatal_lang_error('ban_not_found', false);
  994. $row = $smcFunc['db_fetch_assoc']($request);
  995. $smcFunc['db_free_result']($request);
  996. $context['ban_trigger'] = array(
  997. 'id' => $row['id_ban'],
  998. 'group' => $row['id_ban_group'],
  999. 'ip' => array(
  1000. 'value' => empty($row['ip_low1']) ? '' : range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4'], $row['ip_low5'], $row['ip_low6'], $row['ip_low7'], $row['ip_low8']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4'], $row['ip_high5'], $row['ip_high6'], $row['ip_high7'], $row['ip_high8'])),
  1001. 'selected' => !empty($row['ip_low1']),
  1002. ),
  1003. 'hostname' => array(
  1004. 'value' => str_replace('%', '*', $row['hostname']),
  1005. 'selected' => !empty($row['hostname']),
  1006. ),
  1007. 'email' => array(
  1008. 'value' => str_replace('%', '*', $row['email_address']),
  1009. 'selected' => !empty($row['email_address'])
  1010. ),
  1011. 'banneduser' => array(
  1012. 'value' => $row['member_name'],
  1013. 'selected' => !empty($row['member_name'])
  1014. ),
  1015. 'is_new' => false,
  1016. );
  1017. }
  1018. createToken('admin-bet');
  1019. }
  1020. /**
  1021. * This handles the screen for showing the banned entities
  1022. * It is accessed by ?action=admin;area=ban;sa=browse
  1023. * It uses sub-tabs for browsing by IP, hostname, email or username.
  1024. *
  1025. * @uses ManageBans template, browse_triggers sub template.
  1026. */
  1027. function BanBrowseTriggers()
  1028. {
  1029. global $modSettings, $context, $scripturl, $smcFunc, $txt;
  1030. global $sourcedir, $settings;
  1031. if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  1032. {
  1033. checkSession();
  1034. // Clean the integers.
  1035. foreach ($_POST['remove'] as $key => $value)
  1036. $_POST['remove'][$key] = $value;
  1037. $smcFunc['db_query']('', '
  1038. DELETE FROM {db_prefix}ban_items
  1039. WHERE id_ban IN ({array_int:ban_list})',
  1040. array(
  1041. 'ban_list' => $_POST['remove'],
  1042. )
  1043. );
  1044. // Rehabilitate some members.
  1045. if ($_REQUEST['entity'] == 'member')
  1046. updateBanMembers();
  1047. // Make sure the ban cache is refreshed.
  1048. updateSettings(array('banLastUpdated' => time()));
  1049. }
  1050. $context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
  1051. $listOptions = array(
  1052. 'id' => 'ban_trigger_list',
  1053. 'title' => $txt['ban_trigger_browse'],
  1054. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1055. 'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1056. 'default_sort_col' => 'banned_entity',
  1057. 'no_items_label' => $txt['ban_no_triggers'],
  1058. 'get_items' => array(
  1059. 'function' => 'list_getBanTriggers',
  1060. 'params' => array(
  1061. $context['selected_entity'],
  1062. ),
  1063. ),
  1064. 'get_count' => array(
  1065. 'function' => 'list_getNumBanTriggers',
  1066. 'params' => array(
  1067. $context['selected_entity'],
  1068. ),
  1069. ),
  1070. 'columns' => array(
  1071. 'banned_entity' => array(
  1072. 'header' => array(
  1073. 'value' => $txt['ban_banned_entity'],
  1074. ),
  1075. ),
  1076. 'ban_name' => array(
  1077. 'header' => array(
  1078. 'value' => $txt['ban_name'],
  1079. ),
  1080. 'data' => array(
  1081. 'sprintf' => array(
  1082. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
  1083. 'params' => array(
  1084. 'id_ban_group' => false,
  1085. 'name' => false,
  1086. ),
  1087. ),
  1088. ),
  1089. 'sort' => array(
  1090. 'default' => 'bg.name',
  1091. 'reverse' => 'bg.name DESC',
  1092. ),
  1093. ),
  1094. 'hits' => array(
  1095. 'header' => array(
  1096. 'value' => $txt['ban_hits'],
  1097. ),
  1098. 'data' => array(
  1099. 'db' => 'hits',
  1100. 'style' => 'text-align: center;',
  1101. ),
  1102. 'sort' => array(
  1103. 'default' => 'bi.hits DESC',
  1104. 'reverse' => 'bi.hits',
  1105. ),
  1106. ),
  1107. 'check' => array(
  1108. 'header' => array(
  1109. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1110. ),
  1111. 'data' => array(
  1112. 'sprintf' => array(
  1113. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1114. 'params' => array(
  1115. 'id_ban' => false,
  1116. ),
  1117. ),
  1118. 'style' => 'text-align: center',
  1119. ),
  1120. ),
  1121. ),
  1122. 'form' => array(
  1123. 'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1124. 'include_start' => true,
  1125. 'include_sort' => true,
  1126. ),
  1127. 'additional_rows' => array(
  1128. array(
  1129. 'position' => 'above_column_headers',
  1130. '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>',
  1131. ),
  1132. array(
  1133. 'position' => 'below_table_data',
  1134. '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" />',
  1135. 'style' => 'text-align: right;',
  1136. ),
  1137. ),
  1138. );
  1139. // Specific data for the first column depending on the selected entity.
  1140. if ($context['selected_entity'] === 'ip')
  1141. {
  1142. $listOptions['columns']['banned_entity']['data'] = array(
  1143. 'function' => create_function('$rowData', '
  1144. return range2ip(array(
  1145. $rowData[\'ip_low1\'],
  1146. $rowData[\'ip_low2\'],
  1147. $rowData[\'ip_low3\'],
  1148. $rowData[\'ip_low4\']
  1149. ), array(
  1150. $rowData[\'ip_high1\'],
  1151. $rowData[\'ip_high2\'],
  1152. $rowData[\'ip_high3\'],
  1153. $rowData[\'ip_high4\']
  1154. ));
  1155. '),
  1156. );
  1157. $listOptions['columns']['banned_entity']['sort'] = array(
  1158. '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',
  1159. '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',
  1160. );
  1161. }
  1162. elseif ($context['selected_entity'] === 'hostname')
  1163. {
  1164. $listOptions['columns']['banned_entity']['data'] = array(
  1165. 'function' => create_function('$rowData', '
  1166. global $smcFunc;
  1167. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'hostname\']), array(\'%\' => \'*\'));
  1168. '),
  1169. );
  1170. $listOptions['columns']['banned_entity']['sort'] = array(
  1171. 'default' => 'bi.hostname',
  1172. 'reverse' => 'bi.hostname DESC',
  1173. );
  1174. }
  1175. elseif ($context['selected_entity'] === 'email')
  1176. {
  1177. $listOptions['columns']['banned_entity']['data'] = array(
  1178. 'function' => create_function('$rowData', '
  1179. global $smcFunc;
  1180. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'email_address\']), array(\'%\' => \'*\'));
  1181. '),
  1182. );
  1183. $listOptions['columns']['banned_entity']['sort'] = array(
  1184. 'default' => 'bi.email_address',
  1185. 'reverse' => 'bi.email_address DESC',
  1186. );
  1187. }
  1188. elseif ($context['selected_entity'] === 'member')
  1189. {
  1190. $listOptions['columns']['banned_entity']['data'] = array(
  1191. 'sprintf' => array(
  1192. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1193. 'params' => array(
  1194. 'id_member' => false,
  1195. 'real_name' => false,
  1196. ),
  1197. ),
  1198. );
  1199. $listOptions['columns']['banned_entity']['sort'] = array(
  1200. 'default' => 'mem.real_name',
  1201. 'reverse' => 'mem.real_name DESC',
  1202. );
  1203. }
  1204. // Create the list.
  1205. require_once($sourcedir . '/Subs-List.php');
  1206. createList($listOptions);
  1207. // The list is the only thing to show, so make it the default sub template.
  1208. $context['sub_template'] = 'show_list';
  1209. $context['default_list'] = 'ban_trigger_list';
  1210. }
  1211. /**
  1212. * Get ban triggers for the given parameters.
  1213. *
  1214. * @param int $start
  1215. * @param int $items_per_page
  1216. * @param string $sort
  1217. * @param string $trigger_type
  1218. * @return array
  1219. */
  1220. function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
  1221. {
  1222. global $smcFunc;
  1223. $where = array(
  1224. 'ip' => 'bi.ip_low1 > 0',
  1225. 'hostname' => 'bi.hostname != {string:blank_string}',
  1226. 'email' => 'bi.email_address != {string:blank_string}',
  1227. );
  1228. $request = $smcFunc['db_query']('', '
  1229. SELECT
  1230. 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,
  1231. bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
  1232. mem.id_member, mem.real_name' : '') . '
  1233. FROM {db_prefix}ban_items AS bi
  1234. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
  1235. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1236. WHERE ' . $where[$trigger_type]) . '
  1237. ORDER BY ' . $sort . '
  1238. LIMIT ' . $start . ', ' . $items_per_page,
  1239. array(
  1240. 'blank_string' => '',
  1241. )
  1242. );
  1243. $ban_triggers = array();
  1244. while ($row = $smcFunc['db_fetch_assoc']($request))
  1245. $ban_triggers[] = $row;
  1246. $smcFunc['db_free_result']($request);
  1247. return $ban_triggers;
  1248. }
  1249. /**
  1250. * This returns the total number of ban triggers of the given type.
  1251. *
  1252. * @param string $trigger_type
  1253. * @return int
  1254. */
  1255. function list_getNumBanTriggers($trigger_type)
  1256. {
  1257. global $smcFunc;
  1258. $where = array(
  1259. 'ip' => 'bi.ip_low1 > 0',
  1260. 'hostname' => 'bi.hostname != {string:blank_string}',
  1261. 'email' => 'bi.email_address != {string:blank_string}',
  1262. );
  1263. $request = $smcFunc['db_query']('', '
  1264. SELECT COUNT(*)
  1265. FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
  1266. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1267. WHERE ' . $where[$trigger_type]),
  1268. array(
  1269. 'blank_string' => '',
  1270. )
  1271. );
  1272. list ($num_triggers) = $smcFunc['db_fetch_row']($request);
  1273. $smcFunc['db_free_result']($request);
  1274. return $num_triggers;
  1275. }
  1276. /**
  1277. * This handles the listing of ban log entries, and allows their deletion.
  1278. * Shows a list of logged access attempts by banned users.
  1279. * It is accessed by ?action=admin;area=ban;sa=log.
  1280. * How it works:
  1281. * - allows sorting of several columns.
  1282. * - also handles deletion of (a selection of) log entries.
  1283. */
  1284. function BanLog()
  1285. {
  1286. global $scripturl, $context, $smcFunc, $sourcedir, $txt;
  1287. global $context;
  1288. // Delete one or more entries.
  1289. if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
  1290. {
  1291. checkSession();
  1292. validateToken('admin-bl');
  1293. // 'Delete all entries' button was pressed.
  1294. if (!empty($_POST['removeAll']))
  1295. $smcFunc['db_query']('truncate_table', '
  1296. TRUNCATE {db_prefix}log_banned',
  1297. array(
  1298. )
  1299. );
  1300. // 'Delete selection' button was pressed.
  1301. else
  1302. {
  1303. // Make sure every entry is integer.
  1304. foreach ($_POST['remove'] as $index => $log_id)
  1305. $_POST['remove'][$index] = (int) $log_id;
  1306. $smcFunc['db_query']('', '
  1307. DELETE FROM {db_prefix}log_banned
  1308. WHERE id_ban_log IN ({array_int:ban_list})',
  1309. array(
  1310. 'ban_list' => $_POST['remove'],
  1311. )
  1312. );
  1313. }
  1314. }
  1315. $listOptions = array(
  1316. 'id' => 'ban_log',
  1317. 'items_per_page' => 30,
  1318. 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1319. 'default_sort_col' => 'date',
  1320. 'get_items' => array(
  1321. 'function' => 'list_getBanLogEntries',
  1322. ),
  1323. 'get_count' => array(
  1324. 'function' => 'list_getNumBanLogEntries',
  1325. ),
  1326. 'no_items_label' => $txt['ban_log_no_entries'],
  1327. 'columns' => array(
  1328. 'ip' => array(
  1329. 'header' => array(
  1330. 'value' => $txt['ban_log_ip'],
  1331. ),
  1332. 'data' => array(
  1333. 'sprintf' => array(
  1334. 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
  1335. 'params' => array(
  1336. 'ip' => false,
  1337. ),
  1338. ),
  1339. ),
  1340. 'sort' => array(
  1341. 'default' => 'lb.ip',
  1342. 'reverse' => 'lb.ip DESC',
  1343. ),
  1344. ),
  1345. 'email' => array(
  1346. 'header' => array(
  1347. 'value' => $txt['ban_log_email'],
  1348. ),
  1349. 'data' => array(
  1350. 'db_htmlsafe' => 'email',
  1351. ),
  1352. 'sort' => array(
  1353. 'default' => 'lb.email = \'\', lb.email',
  1354. 'reverse' => 'lb.email != \'\', lb.email DESC',
  1355. ),
  1356. ),
  1357. 'member' => array(
  1358. 'header' => array(
  1359. 'value' => $txt['ban_log_member'],
  1360. ),
  1361. 'data' => array(
  1362. 'sprintf' => array(
  1363. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1364. 'params' => array(
  1365. 'id_member' => false,
  1366. 'real_name' => false,
  1367. ),
  1368. ),
  1369. ),
  1370. 'sort' => array(
  1371. 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
  1372. 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
  1373. ),
  1374. ),
  1375. 'date' => array(
  1376. 'header' => array(
  1377. 'value' => $txt['ban_log_date'],
  1378. ),
  1379. 'data' => array(
  1380. 'function' => create_function('$rowData', '
  1381. return timeformat($rowData[\'log_time\']);
  1382. '),
  1383. ),
  1384. 'sort' => array(
  1385. 'default' => 'lb.log_time DESC',
  1386. 'reverse' => 'lb.log_time',
  1387. ),
  1388. ),
  1389. 'check' => array(
  1390. 'header' => array(
  1391. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1392. ),
  1393. 'data' => array(
  1394. 'sprintf' => array(
  1395. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1396. 'params' => array(
  1397. 'id_ban_log' => false,
  1398. ),
  1399. ),
  1400. 'style' => 'text-align: center',
  1401. ),
  1402. ),
  1403. ),
  1404. 'form' => array(
  1405. 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1406. 'include_start' => true,
  1407. 'include_sort' => true,
  1408. 'token' => 'admin-bl',
  1409. ),
  1410. 'additional_rows' => array(
  1411. array(
  1412. 'position' => 'below_table_data',
  1413. 'value' => '
  1414. <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit" />
  1415. <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit" />',
  1416. 'style' => 'text-align: right;',
  1417. ),
  1418. ),
  1419. );
  1420. createToken('admin-bl');
  1421. require_once($sourcedir . '/Subs-List.php');
  1422. createList($listOptions);
  1423. $context['page_title'] = $txt['ban_log'];
  1424. $context['sub_template'] = 'show_list';
  1425. $context['default_list'] = 'ban_log';
  1426. }
  1427. /**
  1428. * Load a list of ban log entries from the database.
  1429. * (no permissions check)
  1430. *
  1431. * @param int $start
  1432. * @param int $items_per_page
  1433. * @param string $sort
  1434. */
  1435. function list_getBanLogEntries($start, $items_per_page, $sort)
  1436. {
  1437. global $smcFunc;
  1438. $request = $smcFunc['db_query']('', '
  1439. 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
  1440. FROM {db_prefix}log_banned AS lb
  1441. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
  1442. ORDER BY ' . $sort . '
  1443. LIMIT ' . $start . ', ' . $items_per_page,
  1444. array(
  1445. 'blank_string' => '',
  1446. 'dash' => '-',
  1447. )
  1448. );
  1449. $log_entries = array();
  1450. while ($row = $smcFunc['db_fetch_assoc']($request))
  1451. $log_entries[] = $row;
  1452. $smcFunc['db_free_result']($request);
  1453. return $log_entries;
  1454. }
  1455. /**
  1456. * This returns the total count of ban log entries.
  1457. */
  1458. function list_getNumBanLogEntries()
  1459. {
  1460. global $smcFunc;
  1461. $request = $smcFunc['db_query']('', '
  1462. SELECT COUNT(*)
  1463. FROM {db_prefix}log_banned AS lb',
  1464. array(
  1465. )
  1466. );
  1467. list ($num_entries) = $smcFunc['db_fetch_row']($request);
  1468. $smcFunc['db_free_result']($request);
  1469. return $num_entries;
  1470. }
  1471. /**
  1472. * Convert a range of given IP number into a single string.
  1473. * It's practically the reverse function of ip2range().
  1474. *
  1475. * @example
  1476. * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
  1477. *
  1478. * @param array $low, IPv4 format
  1479. * @param array $high, IPv4 format
  1480. * @return string
  1481. */
  1482. function range2ip($low, $high)
  1483. {
  1484. // IPv6 check.
  1485. if (!empty($low[5]))
  1486. {
  1487. if (count($low) != 8 || count($high) != 8)
  1488. return '';
  1489. $ip = array();
  1490. for ($i = 0; $i < 8; $i++)
  1491. {
  1492. if ($low[$i] == $high[$i])
  1493. $ip[$i] = dechex($low[$i]);
  1494. elseif ($low[$i] == '0' && $high[$i] == '255')
  1495. $ip[$i] = '*';
  1496. else
  1497. $ip[$i] = dechex($low[$i]) . '-' . dechex($high[$i]);
  1498. }
  1499. return implode(':', $ip);
  1500. }
  1501. // Legacy IPv4 stuff.
  1502. if (count($low) != 4 || count($high) != 4)
  1503. return '';
  1504. $ip = array();
  1505. for ($i = 0; $i < 4; $i++)
  1506. {
  1507. if ($low[$i] == $high[$i])
  1508. $ip[$i] = $low[$i];
  1509. elseif ($low[$i] == '0' && $high[$i] == '255')
  1510. $ip[$i] = '*';
  1511. else
  1512. $ip[$i] = $low[$i] . '-' . $high[$i];
  1513. }
  1514. // Pretending is fun... the IP can't be this, so use it for 'unknown'.
  1515. if ($ip == array(255, 255, 255, 255))
  1516. return 'unknown';
  1517. return implode('.', $ip);
  1518. }
  1519. /**
  1520. * Checks whether a given IP range already exists in the trigger list.
  1521. * If yes, it returns an error message. Otherwise, it returns an array
  1522. * optimized for the database.
  1523. *
  1524. * @param array $ip_array
  1525. * @param string $fullip
  1526. * @return bool
  1527. */
  1528. function checkExistingTriggerIP($ip_array, $fullip = '')
  1529. {
  1530. global $smcFunc, $scripturl;
  1531. if (count($ip_array) == 4 || count($ip_array) == 8)
  1532. $values = array(
  1533. 'ip_low1' => $ip_array[0]['low'],
  1534. 'ip_high1' => $ip_array[0]['high'],
  1535. 'ip_low2' => $ip_array[1]['low'],
  1536. 'ip_high2' => $ip_array[1]['high'],
  1537. 'ip_low3' => $ip_array[2]['low'],
  1538. 'ip_high3' => $ip_array[2]['high'],
  1539. 'ip_low4' => $ip_array[3]['low'],
  1540. 'ip_high4' => $ip_array[3]['high'],
  1541. 'ip_low5' => $ip_array[4]['low'],
  1542. 'ip_high5' => $ip_array[4]['high'],
  1543. 'ip_low6' => $ip_array[5]['low'],
  1544. 'ip_high6' => $ip_array[5]['high'],
  1545. 'ip_low7' => $ip_array[6]['low'],
  1546. 'ip_high7' => $ip_array[6]['high'],
  1547. 'ip_low8' => $ip_array[7]['low'],
  1548. 'ip_high8' => $ip_array[7]['high'],
  1549. );
  1550. else
  1551. return false;
  1552. $request = $smcFunc['db_query']('', '
  1553. SELECT bg.id_ban_group, bg.name
  1554. FROM {db_prefix}ban_groups AS bg
  1555. INNER JOIN {db_prefix}ban_items AS bi ON
  1556. (bi.id_ban_group = bg.id_ban_group)
  1557. AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
  1558. AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
  1559. AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
  1560. AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
  1561. AND ip_low5 = {int:ip_low5} AND ip_high5 = {int:ip_high5}
  1562. AND ip_low6 = {int:ip_low6} AND ip_high6 = {int:ip_high6}
  1563. AND ip_low7 = {int:ip_low7} AND ip_high7 = {int:ip_high7}
  1564. AND ip_low8 = {int:ip_low8} AND ip_high8 = {int:ip_high8}
  1565. LIMIT 1',
  1566. $values
  1567. );
  1568. if ($smcFunc['db_num_rows']($request) != 0)
  1569. {
  1570. list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
  1571. fatal_lang_error('ban_trigger_already_exists', false, array(
  1572. $fullip,
  1573. '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
  1574. ));
  1575. }
  1576. $smcFunc['db_free_result']($request);
  1577. return $values;
  1578. }
  1579. /**
  1580. * As it says... this tries to review the list of banned members, to match new bans.
  1581. * Note: is_activated >= 10: a member is banned.
  1582. */
  1583. function updateBanMembers()
  1584. {
  1585. global $smcFunc;
  1586. $updates = array();
  1587. $allMembers = array();
  1588. $newMembers = array();
  1589. // Start by getting all active bans - it's quicker doing this in parts...
  1590. $request = $smcFunc['db_query']('', '
  1591. SELECT bi.id_member, bi.email_address
  1592. FROM {db_prefix}ban_items AS bi
  1593. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  1594. WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
  1595. AND bg.cannot_access = {int:cannot_access_on}
  1596. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  1597. array(
  1598. 'no_member' => 0,
  1599. 'cannot_access_on' => 1,
  1600. 'current_time' => time(),
  1601. 'blank_string' => '',
  1602. )
  1603. );
  1604. $memberIDs = array();
  1605. $memberEmails = array();
  1606. $memberEmailWild = array();
  1607. while ($row = $smcFunc['db_fetch_assoc']($request))
  1608. {
  1609. if ($row['id_member'])
  1610. $memberIDs[$row['id_member']] = $row['id_member'];
  1611. if ($row['email_address'])
  1612. {
  1613. // Does it have a wildcard - if so we can't do a IN on it.
  1614. if (strpos($row['email_address'], '%') !== false)
  1615. $memberEmailWild[$row['email_address']] = $row['email_address'];
  1616. else
  1617. $memberEmails[$row['email_address']] = $row['email_address'];
  1618. }
  1619. }
  1620. $smcFunc['db_free_result']($request);
  1621. // Build up the query.
  1622. $queryPart = array();
  1623. $queryValues = array();
  1624. if (!empty($memberIDs))
  1625. {
  1626. $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
  1627. $queryValues['member_ids'] = $memberIDs;
  1628. }
  1629. if (!empty($memberEmails))
  1630. {
  1631. $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
  1632. $queryValues['member_emails'] = $memberEmails;
  1633. }
  1634. $count = 0;
  1635. foreach ($memberEmailWild as $email)
  1636. {
  1637. $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
  1638. $queryValues['wild_' . $count++] = $email;
  1639. }
  1640. // Find all banned members.
  1641. if (!empty($queryPart))
  1642. {
  1643. $request = $smcFunc['db_query']('', '
  1644. SELECT mem.id_member, mem.is_activated
  1645. FROM {db_prefix}members AS mem
  1646. WHERE ' . implode( ' OR ', $queryPart),
  1647. $queryValues
  1648. );
  1649. while ($row = $smcFunc['db_fetch_assoc']($request))
  1650. {
  1651. if (!in_array($row['id_member'], $allMembers))
  1652. {
  1653. $allMembers[] = $row['id_member'];
  1654. // Do they need an update?
  1655. if ($row['is_activated'] < 10)
  1656. {
  1657. $updates[($row['is_activated'] + 10)][] = $row['id_member'];
  1658. $newMembers[] = $row['id_member'];
  1659. }
  1660. }
  1661. }
  1662. $smcFunc['db_free_result']($request);
  1663. }
  1664. // We welcome our new members in the realm of the banned.
  1665. if (!empty($newMembers))
  1666. $smcFunc['db_query']('', '
  1667. DELETE FROM {db_prefix}log_online
  1668. WHERE id_member IN ({array_int:new_banned_members})',
  1669. array(
  1670. 'new_banned_members' => $newMembers,
  1671. )
  1672. );
  1673. // Find members that are wrongfully marked as banned.
  1674. $request = $smcFunc['db_query']('', '
  1675. SELECT mem.id_member, mem.is_activated - 10 AS new_value
  1676. FROM {db_prefix}members AS mem
  1677. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
  1678. 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}))
  1679. WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
  1680. AND mem.is_activated >= {int:ban_flag}',
  1681. array(
  1682. 'cannot_access_activated' => 1,
  1683. 'current_time' => time(),
  1684. 'ban_flag' => 10,
  1685. )
  1686. );
  1687. while ($row = $smcFunc['db_fetch_assoc']($request))
  1688. {
  1689. // Don't do this twice!
  1690. if (!in_array($row['id_member'], $allMembers))
  1691. {
  1692. $updates[$row['new_value']][] = $row['id_member'];
  1693. $allMembers[] = $row['id_member'];
  1694. }
  1695. }
  1696. $smcFunc['db_free_result']($request);
  1697. if (!empty($updates))
  1698. foreach ($updates as $newStatus => $members)
  1699. updateMemberData($members, array('is_activated' => $newStatus));
  1700. // Update the latest member and our total members as banning may change them.
  1701. updateStats('member');
  1702. }
  1703. ?>