ManageBans.php 57 KB

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