ManageBans.php 57 KB

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