ManageBans.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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 2013 Simple Machines and individual contributors
  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('No direct access...');
  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-action.
  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. array_map('intval', $_POST['remove']);
  96. // Unban them all!
  97. removeBanGroups($_POST['remove']);
  98. removeBanTriggers($_POST['remove']);
  99. // No more caching this ban!
  100. updateSettings(array('banLastUpdated' => time()));
  101. // Some members might be unbanned now. Update the members table.
  102. updateBanMembers();
  103. }
  104. // Create a date string so we don't overload them with date info.
  105. if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0]))
  106. $context['ban_time_format'] = $user_info['time_format'];
  107. else
  108. $context['ban_time_format'] = $matches[0];
  109. $listOptions = array(
  110. 'id' => 'ban_list',
  111. 'title' => $txt['ban_title'],
  112. 'items_per_page' => 20,
  113. 'base_href' => $scripturl . '?action=admin;area=ban;sa=list',
  114. 'default_sort_col' => 'added',
  115. 'default_sort_dir' => 'desc',
  116. 'get_items' => array(
  117. 'function' => 'list_getBans',
  118. ),
  119. 'get_count' => array(
  120. 'function' => 'list_getNumBans',
  121. ),
  122. 'no_items_label' => $txt['ban_no_entries'],
  123. 'columns' => array(
  124. 'name' => array(
  125. 'header' => array(
  126. 'value' => $txt['ban_name'],
  127. ),
  128. 'data' => array(
  129. 'db' => 'name',
  130. ),
  131. 'sort' => array(
  132. 'default' => 'bg.name',
  133. 'reverse' => 'bg.name DESC',
  134. ),
  135. ),
  136. 'notes' => array(
  137. 'header' => array(
  138. 'value' => $txt['ban_notes'],
  139. ),
  140. 'data' => array(
  141. 'db' => 'notes',
  142. 'class' => 'smalltext',
  143. ),
  144. 'sort' => array(
  145. 'default' => 'LENGTH(bg.notes) > 0 DESC, bg.notes',
  146. 'reverse' => 'LENGTH(bg.notes) > 0, bg.notes DESC',
  147. ),
  148. ),
  149. 'reason' => array(
  150. 'header' => array(
  151. 'value' => $txt['ban_reason'],
  152. ),
  153. 'data' => array(
  154. 'db' => 'reason',
  155. 'class' => 'smalltext',
  156. ),
  157. 'sort' => array(
  158. 'default' => 'LENGTH(bg.reason) > 0 DESC, bg.reason',
  159. 'reverse' => 'LENGTH(bg.reason) > 0, bg.reason DESC',
  160. ),
  161. ),
  162. 'added' => array(
  163. 'header' => array(
  164. 'value' => $txt['ban_added'],
  165. ),
  166. 'data' => array(
  167. 'function' => create_function('$rowData', '
  168. global $context;
  169. return timeformat($rowData[\'ban_time\'], empty($context[\'ban_time_format\']) ? true : $context[\'ban_time_format\']);
  170. '),
  171. ),
  172. 'sort' => array(
  173. 'default' => 'bg.ban_time',
  174. 'reverse' => 'bg.ban_time DESC',
  175. ),
  176. ),
  177. 'expires' => array(
  178. 'header' => array(
  179. 'value' => $txt['ban_expires'],
  180. ),
  181. 'data' => array(
  182. 'function' => create_function('$rowData', '
  183. global $txt;
  184. // This ban never expires...whahaha.
  185. if ($rowData[\'expire_time\'] === null)
  186. return $txt[\'never\'];
  187. // This ban has already expired.
  188. elseif ($rowData[\'expire_time\'] < time())
  189. return sprintf(\'<span style="color: red">%1$s</span>\', $txt[\'ban_expired\']);
  190. // Still need to wait a few days for this ban to expire.
  191. else
  192. return sprintf(\'%1$d&nbsp;%2$s\', ceil(($rowData[\'expire_time\'] - time()) / (60 * 60 * 24)), $txt[\'ban_days\']);
  193. '),
  194. ),
  195. 'sort' => array(
  196. 'default' => 'IFNULL(bg.expire_time, 1=1) DESC, bg.expire_time DESC',
  197. 'reverse' => 'IFNULL(bg.expire_time, 1=1), bg.expire_time',
  198. ),
  199. ),
  200. 'num_triggers' => array(
  201. 'header' => array(
  202. 'value' => $txt['ban_triggers'],
  203. ),
  204. 'data' => array(
  205. 'db' => 'num_triggers',
  206. ),
  207. 'sort' => array(
  208. 'default' => 'num_triggers DESC',
  209. 'reverse' => 'num_triggers',
  210. ),
  211. ),
  212. 'actions' => array(
  213. 'header' => array(
  214. 'value' => $txt['ban_actions'],
  215. 'class' => 'centercol',
  216. ),
  217. 'data' => array(
  218. 'sprintf' => array(
  219. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">' . $txt['modify'] . '</a>',
  220. 'params' => array(
  221. 'id_ban_group' => false,
  222. ),
  223. ),
  224. 'class' => 'centercol',
  225. ),
  226. ),
  227. 'check' => array(
  228. 'header' => array(
  229. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  230. 'class' => 'centercol',
  231. ),
  232. 'data' => array(
  233. 'sprintf' => array(
  234. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  235. 'params' => array(
  236. 'id_ban_group' => false,
  237. ),
  238. ),
  239. 'class' => 'centercol',
  240. ),
  241. ),
  242. ),
  243. 'form' => array(
  244. 'href' => $scripturl . '?action=admin;area=ban;sa=list',
  245. ),
  246. 'additional_rows' => array(
  247. array(
  248. 'position' => 'bottom_of_list',
  249. 'value' => '<input type="submit" name="removeBans" value="' . $txt['ban_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_remove_selected_confirm'] . '\');" class="button_submit" />',
  250. ),
  251. ),
  252. );
  253. require_once($sourcedir . '/Subs-List.php');
  254. createList($listOptions);
  255. $context['sub_template'] = 'show_list';
  256. $context['default_list'] = 'ban_list';
  257. }
  258. /**
  259. * Get bans, what else? For the given options.
  260. *
  261. * @param int $start
  262. * @param int $items_per_page
  263. * @param string $sort
  264. * @return array
  265. */
  266. function list_getBans($start, $items_per_page, $sort)
  267. {
  268. global $smcFunc;
  269. $request = $smcFunc['db_query']('', '
  270. SELECT bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, COUNT(bi.id_ban) AS num_triggers
  271. FROM {db_prefix}ban_groups AS bg
  272. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  273. GROUP BY bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes
  274. ORDER BY {raw:sort}
  275. LIMIT {int:offset}, {int:limit}',
  276. array(
  277. 'sort' => $sort,
  278. 'offset' => $start,
  279. 'limit' => $items_per_page,
  280. )
  281. );
  282. $bans = array();
  283. while ($row = $smcFunc['db_fetch_assoc']($request))
  284. $bans[] = $row;
  285. $smcFunc['db_free_result']($request);
  286. return $bans;
  287. }
  288. /**
  289. * Get the total number of ban from the ban group table
  290. *
  291. * @return int
  292. */
  293. function list_getNumBans()
  294. {
  295. global $smcFunc;
  296. $request = $smcFunc['db_query']('', '
  297. SELECT COUNT(*) AS num_bans
  298. FROM {db_prefix}ban_groups',
  299. array(
  300. )
  301. );
  302. list ($numBans) = $smcFunc['db_fetch_row']($request);
  303. $smcFunc['db_free_result']($request);
  304. return $numBans;
  305. }
  306. /**
  307. * This function is behind the screen for adding new bans and modifying existing ones.
  308. * Adding new bans:
  309. * - is accesssed by ?action=admin;area=ban;sa=add.
  310. * - uses the ban_edit sub template of the ManageBans template.
  311. * Modifying existing bans:
  312. * - is accesssed by ?action=admin;area=ban;sa=edit;bg=x
  313. * - uses the ban_edit sub template of the ManageBans template.
  314. * - shows a list of ban triggers for the specified ban.
  315. */
  316. function BanEdit()
  317. {
  318. global $txt, $modSettings, $context, $ban_request, $scripturl, $smcFunc, $sourcedir;
  319. if ((isset($_POST['add_ban']) || isset($_POST['modify_ban']) || isset($_POST['remove_selection'])) && empty($context['ban_errors']))
  320. BanEdit2();
  321. $ban_group_id = isset($context['ban']['id']) ? $context['ban']['id'] : (isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0);
  322. // Template needs this to show errors using javascript
  323. loadLanguage('Errors');
  324. createToken('admin-bet');
  325. $context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edit';
  326. if (!empty($context['ban_errors']))
  327. {
  328. foreach ($context['ban_errors'] as $error)
  329. $context['error_messages'][$error] = $txt[$error];
  330. }
  331. else
  332. {
  333. // If we're editing an existing ban, get it from the database.
  334. if (!empty($ban_group_id))
  335. {
  336. $context['ban_group_id'] = $ban_group_id;
  337. // We're going to want this for making our list.
  338. require_once($sourcedir . '/Subs-List.php');
  339. $listOptions = array(
  340. 'id' => 'ban_items',
  341. 'base_href' => $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $ban_group_id,
  342. 'no_items_label' => $txt['ban_no_triggers'],
  343. 'items_per_page' => $modSettings['defaultMaxMessages'],
  344. 'get_items' => array(
  345. 'function' => 'list_getBanItems',
  346. 'params' => array(
  347. 'ban_group_id' => $ban_group_id,
  348. ),
  349. ),
  350. 'get_count' => array(
  351. 'function' => 'list_getNumBanItems',
  352. 'params' => array(
  353. 'ban_group_id' => $ban_group_id,
  354. ),
  355. ),
  356. 'columns' => array(
  357. 'type' => array(
  358. 'header' => array(
  359. 'value' => $txt['ban_banned_entity'],
  360. 'style' => 'width: 60%;text-align: left;',
  361. ),
  362. 'data' => array(
  363. 'function' => create_function('$ban_item', '
  364. global $txt;
  365. if (in_array($ban_item[\'type\'], array(\'ip\', \'hostname\', \'email\')))
  366. return \'<strong>\' . $txt[$ban_item[\'type\']] . \':</strong>&nbsp;\' . $ban_item[$ban_item[\'type\']];
  367. elseif ($ban_item[\'type\'] == \'user\')
  368. return \'<strong>\' . $txt[\'username\'] . \':</strong>&nbsp;\' . $ban_item[\'user\'][\'link\'];
  369. else
  370. return \'<strong>\' . $txt[\'unknown\'] . \':</strong>&nbsp;\' . $ban_item[\'no_bantype_selected\'];
  371. '),
  372. 'style' => 'text-align: left;',
  373. ),
  374. ),
  375. 'hits' => array(
  376. 'header' => array(
  377. 'value' => $txt['ban_hits'],
  378. 'style' => 'width: 15%; text-align: center;',
  379. ),
  380. 'data' => array(
  381. 'db' => 'hits',
  382. 'style' => 'text-align: center;',
  383. ),
  384. ),
  385. 'id' => array(
  386. 'header' => array(
  387. 'value' => $txt['ban_actions'],
  388. 'style' => 'width: 15%; text-align: center;',
  389. ),
  390. 'data' => array(
  391. 'function' => create_function('$ban_item', '
  392. global $txt, $context, $scripturl;
  393. return \'<a href="\' . $scripturl . \'?action=admin;area=ban;sa=edittrigger;bg=\' . $context[\'ban\'][\'id\'] . \';bi=\' . $ban_item[\'id\'] . \'">\' . $txt[\'ban_edit_trigger\'] . \'</a>\';
  394. '),
  395. 'style' => 'text-align: center;',
  396. ),
  397. ),
  398. 'checkboxes' => array(
  399. 'header' => array(
  400. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form, \'ban_items\');" class="input_check" />',
  401. 'style' => 'width: 5%; text-align: center;',
  402. ),
  403. 'data' => array(
  404. 'sprintf' => array(
  405. 'format' => '<input type="checkbox" name="ban_items[]" value="%1$d" class="input_check" />',
  406. 'params' => array(
  407. 'id' => false,
  408. ),
  409. ),
  410. 'style' => 'text-align: center;',
  411. ),
  412. ),
  413. ),
  414. 'form' => array(
  415. 'href' => $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $ban_group_id,
  416. ),
  417. 'additional_rows' => array(
  418. array(
  419. 'position' => 'below_table_data',
  420. 'value' => '
  421. <input type="submit" name="remove_selection" value="' . $txt['ban_remove_selected_triggers'] . '" class="button_submit" /> <a class="button_link" href="' . $scripturl . '?action=admin;area=ban;sa=edittrigger;bg=' . $ban_group_id . '">' . $txt['ban_add_trigger'] . '</a>',
  422. 'style' => 'text-align: right;',
  423. ),
  424. array(
  425. 'position' => 'below_table_data',
  426. 'value' => '
  427. <input type="hidden" name="bg" value="' . $ban_group_id . '" />
  428. <input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" />
  429. <input type="hidden" name="' . $context['admin-bet_token_var'] . '" value="' . $context['admin-bet_token'] . '" />',
  430. ),
  431. ),
  432. );
  433. createList($listOptions);
  434. }
  435. // Not an existing one, then it's probably a new one.
  436. else
  437. {
  438. $context['ban'] = array(
  439. 'id' => 0,
  440. 'name' => '',
  441. 'expiration' => array(
  442. 'status' => 'never',
  443. 'days' => 0
  444. ),
  445. 'reason' => '',
  446. 'notes' => '',
  447. 'ban_days' => 0,
  448. 'cannot' => array(
  449. 'access' => true,
  450. 'post' => false,
  451. 'register' => false,
  452. 'login' => false,
  453. ),
  454. 'is_new' => true,
  455. );
  456. $context['ban_suggestions'] = array(
  457. 'main_ip' => '',
  458. 'hostname' => '',
  459. 'email' => '',
  460. 'member' => array(
  461. 'id' => 0,
  462. ),
  463. );
  464. // Overwrite some of the default form values if a user ID was given.
  465. if (!empty($_REQUEST['u']))
  466. {
  467. $request = $smcFunc['db_query']('', '
  468. SELECT id_member, real_name, member_ip, email_address
  469. FROM {db_prefix}members
  470. WHERE id_member = {int:current_user}
  471. LIMIT 1',
  472. array(
  473. 'current_user' => (int) $_REQUEST['u'],
  474. )
  475. );
  476. if ($smcFunc['db_num_rows']($request) > 0)
  477. 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);
  478. $smcFunc['db_free_result']($request);
  479. if (!empty($context['ban_suggestions']['member']['id']))
  480. {
  481. $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
  482. $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
  483. // Default the ban name to the name of the banned member.
  484. $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
  485. // @todo: there should be a better solution...used to lock the "Ban on Username" input when banning from profile
  486. $context['ban']['from_user'] = true;
  487. // Would be nice if we could also ban the hostname.
  488. if ((preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 || isValidIPv6($context['ban_suggestions']['main_ip'])) && empty($modSettings['disableHostnameLookup']))
  489. $context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
  490. $context['ban_suggestions']['other_ips'] = banLoadAdditionalIPs($context['ban_suggestions']['member']['id']);
  491. }
  492. }
  493. // We come from the mod center.
  494. elseif(isset($_GET['msg']) && !empty($_GET['msg']))
  495. {
  496. $request = $smcFunc['db_query']('', '
  497. SELECT poster_name, poster_ip, poster_email
  498. FROM {db_prefix}messages
  499. WHERE id_msg = {int:message}
  500. LIMIT 1',
  501. array(
  502. 'message' => (int) $_REQUEST['msg'],
  503. )
  504. );
  505. if ($smcFunc['db_num_rows']($request) > 0)
  506. list ($context['ban_suggestions']['member']['name'], $context['ban_suggestions']['main_ip'], $context['ban_suggestions']['email']) = $smcFunc['db_fetch_row']($request);
  507. $smcFunc['db_free_result']($request);
  508. // Can't hurt to ban base on the guest name...
  509. $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
  510. $context['ban']['from_user'] = true;
  511. }
  512. }
  513. }
  514. // If we're in wireless mode remove the admin template layer and use a special template.
  515. if (WIRELESS && WIRELESS_PROTOCOL != 'wap')
  516. {
  517. $context['sub_template'] = WIRELESS_PROTOCOL . '_ban_edit';
  518. foreach ($context['template_layers'] as $k => $v)
  519. if (strpos($v, 'generic_menu') === 0)
  520. unset($context['template_layers'][$k]);
  521. }
  522. else
  523. {
  524. loadJavascriptFile('suggest.js', array('default_theme' => true), 'suggest.js');
  525. $context['sub_template'] = 'ban_edit';
  526. }
  527. }
  528. /**
  529. * Retrieves all the ban items belonging to a certain ban group
  530. *
  531. * @param int $start
  532. * @param int $items_per_page
  533. * @param string $sort
  534. * @param int $ban_group_id
  535. * @return array
  536. */
  537. function list_getBanItems($start = 0, $items_per_page = 0, $sort = 0, $ban_group_id = 0)
  538. {
  539. global $context, $smcFunc, $scripturl;
  540. $ban_items = array();
  541. $request = $smcFunc['db_query']('', '
  542. SELECT
  543. bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
  544. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  545. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  546. 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,
  547. IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
  548. FROM {db_prefix}ban_groups AS bg
  549. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  550. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  551. WHERE bg.id_ban_group = {int:current_ban}
  552. LIMIT {int:start}, {int:items_per_page}',
  553. array(
  554. 'current_ban' => $ban_group_id,
  555. 'start' => $start,
  556. 'items_per_page' => $items_per_page,
  557. )
  558. );
  559. if ($smcFunc['db_num_rows']($request) == 0)
  560. fatal_lang_error('ban_not_found', false);
  561. while ($row = $smcFunc['db_fetch_assoc']($request))
  562. {
  563. if (!isset($context['ban']))
  564. {
  565. $context['ban'] = array(
  566. 'id' => $row['id_ban_group'],
  567. 'name' => $row['name'],
  568. 'expiration' => array(
  569. 'status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'one_day'),
  570. 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0
  571. ),
  572. 'reason' => $row['reason'],
  573. 'notes' => $row['notes'],
  574. 'cannot' => array(
  575. 'access' => !empty($row['cannot_access']),
  576. 'post' => !empty($row['cannot_post']),
  577. 'register' => !empty($row['cannot_register']),
  578. 'login' => !empty($row['cannot_login']),
  579. ),
  580. 'is_new' => false,
  581. 'hostname' => '',
  582. 'email' => '',
  583. );
  584. }
  585. if (!empty($row['id_ban']))
  586. {
  587. $ban_items[$row['id_ban']] = array(
  588. 'id' => $row['id_ban'],
  589. 'hits' => $row['hits'],
  590. );
  591. if (!empty($row['ip_high1']))
  592. {
  593. $ban_items[$row['id_ban']]['type'] = 'ip';
  594. $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']));
  595. }
  596. elseif (!empty($row['hostname']))
  597. {
  598. $ban_items[$row['id_ban']]['type'] = 'hostname';
  599. $ban_items[$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
  600. }
  601. elseif (!empty($row['email_address']))
  602. {
  603. $ban_items[$row['id_ban']]['type'] = 'email';
  604. $ban_items[$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
  605. }
  606. elseif (!empty($row['id_member']))
  607. {
  608. $ban_items[$row['id_ban']]['type'] = 'user';
  609. $ban_items[$row['id_ban']]['user'] = array(
  610. 'id' => $row['id_member'],
  611. 'name' => $row['real_name'],
  612. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  613. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  614. );
  615. }
  616. // Invalid ban (member probably doesn't exist anymore).
  617. else
  618. {
  619. unset($ban_items[$row['id_ban']]);
  620. removeBanTriggers($row['id_ban']);
  621. }
  622. }
  623. }
  624. $smcFunc['db_free_result']($request);
  625. return $ban_items;
  626. }
  627. /**
  628. * Gets the number of ban items belonging to a certain ban group
  629. *
  630. * @return int
  631. */
  632. function list_getNumBanItems()
  633. {
  634. global $smcFunc, $context;
  635. $ban_group_id = isset($context['ban_group_id']) ? $context['ban_group_id'] : 0;
  636. $request = $smcFunc['db_query']('', '
  637. SELECT COUNT(bi.id_ban)
  638. FROM {db_prefix}ban_groups AS bg
  639. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
  640. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  641. WHERE bg.id_ban_group = {int:current_ban}',
  642. array(
  643. 'current_ban' => $ban_group_id,
  644. )
  645. );
  646. list($banNumber) = $smcFunc['db_fetch_row']($request);
  647. $smcFunc['db_free_result']($request);
  648. return $banNumber;
  649. }
  650. /**
  651. * Finds additional IPs related to a certain user
  652. *
  653. * @param int $member_id
  654. * @return array
  655. */
  656. function banLoadAdditionalIPs($member_id)
  657. {
  658. // Borrowing a few language strings from profile.
  659. loadLanguage('Profile');
  660. $search_list = array();
  661. call_integration_hook('integrate_load_addtional_ip_ban', array(&$search_list));
  662. $search_list += array('ips_in_messages' => 'banLoadAdditionalIPsMember', 'ips_in_errors' => 'banLoadAdditionalIPsError');
  663. $return = array();
  664. foreach ($search_list as $key => $callable)
  665. if (is_callable($callable))
  666. $return[$key] = call_user_func($callable, $member_id);
  667. return $return;
  668. }
  669. function banLoadAdditionalIPsMember($member_id)
  670. {
  671. global $smcFunc;
  672. // Find some additional IP's used by this member.
  673. $message_ips = array();
  674. $request = $smcFunc['db_query']('ban_suggest_message_ips', '
  675. SELECT DISTINCT poster_ip
  676. FROM {db_prefix}messages
  677. WHERE id_member = {int:current_user}
  678. AND poster_ip RLIKE {string:poster_ip_regex}
  679. ORDER BY poster_ip',
  680. array(
  681. 'current_user' => $member_id,
  682. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  683. )
  684. );
  685. while ($row = $smcFunc['db_fetch_assoc']($request))
  686. $message_ips[] = $row['poster_ip'];
  687. $smcFunc['db_free_result']($request);
  688. return $message_ips;
  689. }
  690. function banLoadAdditionalIPsError($member_id)
  691. {
  692. global $smcFunc;
  693. $error_ips = array();
  694. $request = $smcFunc['db_query']('ban_suggest_error_ips', '
  695. SELECT DISTINCT ip
  696. FROM {db_prefix}log_errors
  697. WHERE id_member = {int:current_user}
  698. AND ip RLIKE {string:poster_ip_regex}
  699. ORDER BY ip',
  700. array(
  701. 'current_user' => $member_id,
  702. 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
  703. )
  704. );
  705. while ($row = $smcFunc['db_fetch_assoc']($request))
  706. $error_ips[] = $row['ip'];
  707. $smcFunc['db_free_result']($request);
  708. return $error_ips;
  709. }
  710. /**
  711. * This function handles submitted forms that add, modify or remove ban triggers.
  712. */
  713. function banEdit2()
  714. {
  715. global $smcFunc, $context;
  716. checkSession();
  717. validateToken('admin-bet');
  718. $context['ban_errors'] = array();
  719. // Adding or editing a ban group
  720. if (isset($_POST['add_ban']) || isset($_POST['modify_ban']))
  721. {
  722. // Let's collect all the information we need
  723. $ban_info['id'] = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  724. $ban_info['is_new'] = empty($ban_info['id']);
  725. $ban_info['expire_date'] = !empty($_POST['expire_date']) ? (int) $_POST['expire_date'] : 0;
  726. $ban_info['expiration'] = array(
  727. 'status' => isset($_POST['expiration']) && in_array($_POST['expiration'], array('never', 'one_day', 'expired')) ? $_POST['expiration'] : 'never',
  728. 'days' => $ban_info['expire_date'],
  729. );
  730. $ban_info['db_expiration'] = $ban_info['expiration']['status'] == 'never' ? 'NULL' : ($ban_info['expiration']['status'] == 'one_day' ? time() + 24 * 60 * 60 * $ban_info['expire_date'] : 0);
  731. $ban_info['full_ban'] = empty($_POST['full_ban']) ? 0 : 1;
  732. $ban_info['reason'] = !empty($_POST['reason']) ? $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES) : '';
  733. $ban_info['name'] = !empty($_POST['ban_name']) ? $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES) : '';
  734. $ban_info['notes'] = isset($_POST['notes']) ? $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES) : '';
  735. $ban_info['notes'] = str_replace(array("\r", "\n", ' '), array('', '<br />', '&nbsp; '), $ban_info['notes']);
  736. $ban_info['cannot']['access'] = empty($ban_info['full_ban']) ? 0 : 1;
  737. $ban_info['cannot']['post'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_post']) ? 0 : 1;
  738. $ban_info['cannot']['register'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_register']) ? 0 : 1;
  739. $ban_info['cannot']['login'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_login']) ? 0 : 1;
  740. // Adding a new ban group
  741. if (empty($_REQUEST['bg']))
  742. $ban_group_id = insertBanGroup($ban_info);
  743. // Editing an existing ban group
  744. else
  745. $ban_group_id = updateBanGroup($ban_info);
  746. if (is_numeric($ban_group_id))
  747. {
  748. $ban_info['id'] = $ban_group_id;
  749. $ban_info['is_new'] = false;
  750. }
  751. $context['ban'] = $ban_info;
  752. }
  753. if (isset($_POST['ban_suggestions']))
  754. // @TODO: is $_REQUEST['bi'] ever set?
  755. $saved_triggers = saveTriggers($_POST['ban_suggestions'], $ban_info['id'], isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0, isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0);
  756. // Something went wrong somewhere... Oh well, let's go back.
  757. if (!empty($context['ban_errors']))
  758. {
  759. $context['ban_suggestions'] = $saved_triggers;
  760. $context['ban']['from_user'] = true;
  761. $context['ban_suggestions'] = array_merge($context['ban_suggestions'], getMemberData((int) $_REQUEST['u']));
  762. // Not strictly necessary, but it's nice
  763. if (!empty($context['ban_suggestions']['member']['id']))
  764. $context['ban_suggestions']['other_ips'] = banLoadAdditionalIPs($context['ban_suggestions']['member']['id']);
  765. return BanEdit();
  766. }
  767. $context['ban_suggestions']['saved_triggers'] = $saved_triggers;
  768. if (isset($_POST['ban_items']))
  769. {
  770. $items_ids = array();
  771. $ban_group_id = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  772. array_map('intval', $_POST['ban_items']);
  773. removeBanTriggers($_POST['ban_items'], $ban_group_id);
  774. }
  775. // Register the last modified date.
  776. updateSettings(array('banLastUpdated' => time()));
  777. // Update the member table to represent the new ban situation.
  778. updateBanMembers();
  779. redirectexit('action=admin;area=ban;sa=edit;bg=' . $ban_group_id);
  780. }
  781. /**
  782. * Saves one or more ban triggers into a ban item: according to the suggestions
  783. * checks the $_POST variable to verify if the trigger is present
  784. *
  785. * @param array $suggestions
  786. * @param int $ban_group
  787. * @param int $member
  788. * @param int $ban_id
  789. *
  790. * @return mixed array with the saved triggers or false on failure
  791. */
  792. function saveTriggers($suggestions = array(), $ban_group, $member = 0, $ban_id = 0)
  793. {
  794. $triggers = array(
  795. 'main_ip' => '',
  796. 'hostname' => '',
  797. 'email' => '',
  798. 'member' => array(
  799. 'id' => $member,
  800. )
  801. );
  802. $ban_triggers = array();
  803. foreach ($suggestions as $key => $value)
  804. {
  805. if (is_array($value))
  806. $triggers[$key] = $value;
  807. else
  808. $triggers[$value] = !empty($_POST[$value]) ? $_POST[$value] : '';
  809. }
  810. $ban_triggers = validateTriggers($triggers);
  811. // Time to save!
  812. if (!empty($ban_triggers['ban_triggers']) && empty($context['ban_errors']))
  813. {
  814. if (empty($ban_id))
  815. addTriggers($ban_group, $ban_triggers['ban_triggers'], $ban_triggers['log_info']);
  816. else
  817. updateTriggers($ban_id, $ban_group, array_shift($ban_triggers['ban_triggers']), $ban_triggers['log_info']);
  818. }
  819. if (!empty($context['ban_errors']))
  820. return $triggers;
  821. else
  822. return false;
  823. }
  824. /**
  825. * This function removes a bunch of triggers based on ids
  826. * Doesn't clean the inputs
  827. *
  828. * @param array $items_ids
  829. * @return bool
  830. */
  831. function removeBanTriggers($items_ids = array(), $group_id = false)
  832. {
  833. global $smcFunc;
  834. if ($group_id !== false)
  835. $group_id = (int) $group_id;
  836. if (empty($group_id) && empty($items_ids))
  837. return false;
  838. if (!is_array($items_ids))
  839. $items_ids = array($items_ids);
  840. $log_info = array();
  841. $ban_items = array();
  842. // First order of business: Load up the info so we can log this...
  843. $request = $smcFunc['db_query']('', '
  844. SELECT
  845. bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
  846. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  847. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  848. IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
  849. FROM {db_prefix}ban_items AS bi
  850. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  851. WHERE bi.id_ban IN ({array_int:ban_list})',
  852. array(
  853. 'ban_list' => $items_ids,
  854. )
  855. );
  856. // Get all the info for the log
  857. while ($row = $smcFunc['db_fetch_assoc']($request))
  858. {
  859. if (!empty($row['id_ban']))
  860. {
  861. $ban_items[$row['id_ban']] = array(
  862. 'id' => $row['id_ban'],
  863. );
  864. if (!empty($row['ip_high1']))
  865. {
  866. $ban_items[$row['id_ban']]['type'] = 'ip';
  867. $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']));
  868. $is_range = (strpos($ban_items[$row['id_ban']]['ip'], '-') !== false || strpos($ban_items[$row['id_ban']]['ip'], '*') !== false);
  869. $log_info[] = array(
  870. 'bantype' => ($is_range ? 'ip_range' : 'main_ip'),
  871. 'value' => $ban_items[$row['id_ban']]['ip'],
  872. );
  873. }
  874. elseif (!empty($row['hostname']))
  875. {
  876. $ban_items[$row['id_ban']]['type'] = 'hostname';
  877. $ban_items[$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
  878. $log_info[] = array(
  879. 'bantype' => 'hostname',
  880. 'value' => $row['hostname'],
  881. );
  882. }
  883. elseif (!empty($row['email_address']))
  884. {
  885. $ban_items[$row['id_ban']]['type'] = 'email';
  886. $ban_items[$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
  887. $log_info[] = array(
  888. 'bantype' => 'email',
  889. 'value' => $ban_items[$row['id_ban']]['email'],
  890. );
  891. }
  892. elseif (!empty($row['id_member']))
  893. {
  894. $ban_items[$row['id_ban']]['type'] = 'user';
  895. $ban_items[$row['id_ban']]['user'] = array(
  896. 'id' => $row['id_member'],
  897. 'name' => $row['real_name'],
  898. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  899. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
  900. );
  901. $log_info[] = array(
  902. 'bantype' => 'user',
  903. 'value' => $row['id_member'],
  904. );
  905. }
  906. }
  907. }
  908. // Log this!
  909. logTriggersUpdates($log_info, false, true);
  910. $smcFunc['db_free_result']($request);
  911. if ($group_id !== false)
  912. {
  913. $smcFunc['db_query']('', '
  914. DELETE FROM {db_prefix}ban_items
  915. WHERE id_ban IN ({array_int:ban_list})
  916. AND id_ban_group = {int:ban_group}',
  917. array(
  918. 'ban_list' => $items_ids,
  919. 'ban_group' => $group_id,
  920. )
  921. );
  922. }
  923. elseif (!empty($items_ids))
  924. {
  925. $smcFunc['db_query']('', '
  926. DELETE FROM {db_prefix}ban_items
  927. WHERE id_ban IN ({array_int:ban_list})',
  928. array(
  929. 'ban_list' => $items_ids,
  930. )
  931. );
  932. }
  933. return true;
  934. }
  935. /**
  936. * This function removes a bunch of ban groups based on ids
  937. * Doesn't clean the inputs
  938. *
  939. * @param array $group_ids
  940. * @return bool
  941. */
  942. function removeBanGroups($group_ids)
  943. {
  944. global $smcFunc;
  945. if (!is_array($group_ids))
  946. $group_ids = array($group_ids);
  947. $group_ids = array_unique($group_ids);
  948. if (empty($group_ids))
  949. return false;
  950. $smcFunc['db_query']('', '
  951. DELETE FROM {db_prefix}ban_groups
  952. WHERE id_ban_group IN ({array_int:ban_list})',
  953. array(
  954. 'ban_list' => $group_ids,
  955. )
  956. );
  957. return true;
  958. }
  959. /**
  960. * Removes logs - by default truncate the table
  961. * Doesn't clean the inputs
  962. *
  963. * @param array (optional) $ids
  964. * @return bool
  965. */
  966. function removeBanLogs($ids = array())
  967. {
  968. global $smcFunc;
  969. if (empty($ids))
  970. $smcFunc['db_query']('truncate_table', '
  971. TRUNCATE {db_prefix}log_banned',
  972. array(
  973. )
  974. );
  975. else
  976. {
  977. if (!is_array($ids))
  978. $ids = array($ids);
  979. $ids = array_unique($ids);
  980. if (empty($ids))
  981. return false;
  982. $smcFunc['db_query']('', '
  983. DELETE FROM {db_prefix}log_banned
  984. WHERE id_ban_log IN ({array_int:ban_list})',
  985. array(
  986. 'ban_list' => $ids,
  987. )
  988. );
  989. }
  990. return true;
  991. }
  992. /**
  993. * This function validates the ban triggers
  994. *
  995. * Errors in $context['ban_errors']
  996. *
  997. * @param array $triggers
  998. * @return array triggers and logs info ready to be used
  999. */
  1000. function validateTriggers(&$triggers)
  1001. {
  1002. global $context, $smcFunc;
  1003. if (empty($triggers))
  1004. $context['ban_erros'][] = 'ban_empty_triggers';
  1005. $ban_triggers = array();
  1006. $log_info = array();
  1007. foreach ($triggers as $key => $value)
  1008. {
  1009. if (!empty($value))
  1010. {
  1011. if ($key == 'member')
  1012. continue;
  1013. if ($key == 'main_ip')
  1014. {
  1015. $value = trim($value);
  1016. $ip_parts = ip2range($value);
  1017. if (!checkExistingTriggerIP($ip_parts, $value))
  1018. $context['ban_erros'][] = 'invalid_ip';
  1019. else
  1020. {
  1021. $ban_triggers['main_ip'] = array(
  1022. 'ip_low1' => $ip_parts[0]['low'],
  1023. 'ip_high1' => $ip_parts[0]['high'],
  1024. 'ip_low2' => $ip_parts[1]['low'],
  1025. 'ip_high2' => $ip_parts[1]['high'],
  1026. 'ip_low3' => $ip_parts[2]['low'],
  1027. 'ip_high3' => $ip_parts[2]['high'],
  1028. 'ip_low4' => $ip_parts[3]['low'],
  1029. 'ip_high4' => $ip_parts[3]['high'],
  1030. 'ip_low5' => $ip_parts[4]['low'],
  1031. 'ip_high5' => $ip_parts[4]['high'],
  1032. 'ip_low6' => $ip_parts[5]['low'],
  1033. 'ip_high6' => $ip_parts[5]['high'],
  1034. 'ip_low7' => $ip_parts[6]['low'],
  1035. 'ip_high7' => $ip_parts[6]['high'],
  1036. 'ip_low8' => $ip_parts[7]['low'],
  1037. 'ip_high8' => $ip_parts[7]['high'],
  1038. );
  1039. }
  1040. }
  1041. elseif ($key == 'hostname')
  1042. {
  1043. if (preg_match('/[^\w.\-*]/', $value) == 1)
  1044. $context['ban_erros'][] = 'invalid_hostname';
  1045. else
  1046. {
  1047. // Replace the * wildcard by a MySQL wildcard %.
  1048. $value = substr(str_replace('*', '%', $value), 0, 255);
  1049. $ban_triggers['hostname']['hostname'] = $value;
  1050. }
  1051. }
  1052. elseif ($key == 'email')
  1053. {
  1054. if (preg_match('/[^\w.\-\+*@]/', $value) == 1)
  1055. $context['ban_erros'][] = 'invalid_email';
  1056. // Check the user is not banning an admin.
  1057. $request = $smcFunc['db_query']('', '
  1058. SELECT id_member
  1059. FROM {db_prefix}members
  1060. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  1061. AND email_address LIKE {string:email}
  1062. LIMIT 1',
  1063. array(
  1064. 'admin_group' => 1,
  1065. 'email' => $value,
  1066. )
  1067. );
  1068. if ($smcFunc['db_num_rows']($request) != 0)
  1069. $context['ban_erros'][] = 'no_ban_admin';
  1070. $smcFunc['db_free_result']($request);
  1071. $value = substr(strtolower(str_replace('*', '%', $value)), 0, 255);
  1072. $ban_triggers['email']['email_address'] = $value;
  1073. }
  1074. elseif ($key == 'user')
  1075. {
  1076. $user = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($value, ENT_QUOTES));
  1077. $request = $smcFunc['db_query']('', '
  1078. SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
  1079. FROM {db_prefix}members
  1080. WHERE member_name = {string:username} OR real_name = {string:username}
  1081. LIMIT 1',
  1082. array(
  1083. 'admin_group' => 1,
  1084. 'username' => $user,
  1085. )
  1086. );
  1087. if ($smcFunc['db_num_rows']($request) == 0)
  1088. $context['ban_erros'][] = 'invalid_username';
  1089. list ($value, $isAdmin) = $smcFunc['db_fetch_row']($request);
  1090. $smcFunc['db_free_result']($request);
  1091. if ($isAdmin && strtolower($isAdmin) != 'f')
  1092. {
  1093. unset($value);
  1094. $context['ban_erros'][] = 'no_ban_admin';
  1095. }
  1096. else
  1097. $ban_triggers['user']['id_member'] = $value;
  1098. }
  1099. elseif (in_array($key, array('ips_in_messages', 'ips_in_errors')))
  1100. {
  1101. // Special case, those two are arrays themselves
  1102. $values = array_unique($value);
  1103. // Don't add the main IP again.
  1104. if (isset($triggers['main_ip']))
  1105. $values = array_diff($values, array($triggers['main_ip']));
  1106. unset($value);
  1107. foreach ($values as $val)
  1108. {
  1109. $val = trim($val);
  1110. $ip_parts = ip2range($val);
  1111. if (!checkExistingTriggerIP($ip_parts, $val))
  1112. $context['ban_erros'][] = 'invalid_ip';
  1113. else
  1114. {
  1115. $ban_triggers[$key][] = array(
  1116. 'ip_low1' => $ip_parts[0]['low'],
  1117. 'ip_high1' => $ip_parts[0]['high'],
  1118. 'ip_low2' => $ip_parts[1]['low'],
  1119. 'ip_high2' => $ip_parts[1]['high'],
  1120. 'ip_low3' => $ip_parts[2]['low'],
  1121. 'ip_high3' => $ip_parts[2]['high'],
  1122. 'ip_low4' => $ip_parts[3]['low'],
  1123. 'ip_high4' => $ip_parts[3]['high'],
  1124. 'ip_low5' => $ip_parts[4]['low'],
  1125. 'ip_high5' => $ip_parts[4]['high'],
  1126. 'ip_low6' => $ip_parts[5]['low'],
  1127. 'ip_high6' => $ip_parts[5]['high'],
  1128. 'ip_low7' => $ip_parts[6]['low'],
  1129. 'ip_high7' => $ip_parts[6]['high'],
  1130. 'ip_low8' => $ip_parts[7]['low'],
  1131. 'ip_high8' => $ip_parts[7]['high'],
  1132. );
  1133. $log_info[] = array(
  1134. 'value' => $val,
  1135. 'bantype' => 'ip_range',
  1136. );
  1137. }
  1138. }
  1139. }
  1140. else
  1141. $context['ban_erros'][] = 'no_bantype_selected';
  1142. if (isset($value) && !is_array($value))
  1143. $log_info[] = array(
  1144. 'value' => $value,
  1145. 'bantype' => $key,
  1146. );
  1147. }
  1148. }
  1149. return array('ban_triggers' => $ban_triggers, 'log_info' => $log_info);
  1150. }
  1151. /**
  1152. * This function actually inserts the ban triggers into the database
  1153. *
  1154. * Errors in $context['ban_errors']
  1155. *
  1156. * @param int $group_id
  1157. * @param array $triggers
  1158. * @param array $logs
  1159. * @return nothing
  1160. */
  1161. function addTriggers($group_id = 0, $triggers = array(), $logs = array())
  1162. {
  1163. global $smcFunc;
  1164. if (empty($group_id))
  1165. $context['ban_errors'][] = 'ban_group_id_empty';
  1166. // Preset all values that are required.
  1167. $values = array(
  1168. 'id_ban_group' => $group_id,
  1169. 'hostname' => '',
  1170. 'email_address' => '',
  1171. 'id_member' => 0,
  1172. 'ip_low1' => 0,
  1173. 'ip_high1' => 0,
  1174. 'ip_low2' => 0,
  1175. 'ip_high2' => 0,
  1176. 'ip_low3' => 0,
  1177. 'ip_high3' => 0,
  1178. 'ip_low4' => 0,
  1179. 'ip_high4' => 0,
  1180. 'ip_low5' => 0,
  1181. 'ip_high5' => 0,
  1182. 'ip_low6' => 0,
  1183. 'ip_high6' => 0,
  1184. 'ip_low7' => 0,
  1185. 'ip_high7' => 0,
  1186. 'ip_low8' => 0,
  1187. 'ip_high8' => 0,
  1188. );
  1189. $insertKeys = array(
  1190. 'id_ban_group' => 'int',
  1191. 'hostname' => 'string',
  1192. 'email_address' => 'string',
  1193. 'id_member' => 'int',
  1194. 'ip_low1' => 'int',
  1195. 'ip_high1' => 'int',
  1196. 'ip_low2' => 'int',
  1197. 'ip_high2' => 'int',
  1198. 'ip_low3' => 'int',
  1199. 'ip_high3' => 'int',
  1200. 'ip_low4' => 'int',
  1201. 'ip_high4' => 'int',
  1202. 'ip_low5' => 'int',
  1203. 'ip_high5' => 'int',
  1204. 'ip_low6' => 'int',
  1205. 'ip_high6' => 'int',
  1206. 'ip_low7' => 'int',
  1207. 'ip_high7' => 'int',
  1208. 'ip_low8' => 'int',
  1209. 'ip_high8' => 'int',
  1210. );
  1211. $insertTriggers = array();
  1212. foreach ($triggers as $key => $trigger)
  1213. {
  1214. // Exceptions, exceptions, exceptions...always exceptions... :P
  1215. if (in_array($key, array('ips_in_messages', 'ips_in_errors')))
  1216. foreach ($trigger as $real_trigger)
  1217. $insertTriggers[] = array_merge($values, $real_trigger);
  1218. else
  1219. $insertTriggers[] = array_merge($values, $trigger);
  1220. }
  1221. if (empty($insertTriggers))
  1222. $context['ban_errors'][] = 'ban_no_triggers';
  1223. if (!empty($context['ban_errors']))
  1224. return false;
  1225. $smcFunc['db_insert']('',
  1226. '{db_prefix}ban_items',
  1227. $insertKeys,
  1228. $insertTriggers,
  1229. array('id_ban')
  1230. );
  1231. logTriggersUpdates($logs, true);
  1232. return true;
  1233. }
  1234. /**
  1235. * This function updates an existing ban trigger into the database
  1236. *
  1237. * Errors in $context['ban_errors']
  1238. *
  1239. * @param int $ban_item
  1240. * @param int $group_id
  1241. * @param array $trigger
  1242. * @param array $logs
  1243. * @return nothing
  1244. */
  1245. function updateTriggers($ban_item = 0, $group_id = 0, $trigger = array(), $logs = array())
  1246. {
  1247. global $smcFunc, $context;
  1248. if (empty($ban_item))
  1249. $context['ban_errors'][] = 'ban_ban_item_empty';
  1250. if (empty($group_id))
  1251. $context['ban_errors'][] = 'ban_group_id_empty';
  1252. if (empty($trigger))
  1253. $context['ban_errors'][] = 'ban_no_triggers';
  1254. if (!empty($context['ban_errors']))
  1255. return;
  1256. // Preset all values that are required.
  1257. $values = array(
  1258. 'id_ban_group' => $group_id,
  1259. 'hostname' => '',
  1260. 'email_address' => '',
  1261. 'id_member' => 0,
  1262. 'ip_low1' => 0,
  1263. 'ip_high1' => 0,
  1264. 'ip_low2' => 0,
  1265. 'ip_high2' => 0,
  1266. 'ip_low3' => 0,
  1267. 'ip_high3' => 0,
  1268. 'ip_low4' => 0,
  1269. 'ip_high4' => 0,
  1270. 'ip_low5' => 0,
  1271. 'ip_high5' => 0,
  1272. 'ip_low6' => 0,
  1273. 'ip_high6' => 0,
  1274. 'ip_low7' => 0,
  1275. 'ip_high7' => 0,
  1276. 'ip_low8' => 0,
  1277. 'ip_high8' => 0,
  1278. );
  1279. $trigger = array_merge($values, $trigger);
  1280. $smcFunc['db_query']('', '
  1281. UPDATE {db_prefix}ban_items
  1282. SET
  1283. hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
  1284. ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
  1285. ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
  1286. ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
  1287. ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4},
  1288. ip_low5 = {int:ip_low5}, ip_high5 = {int:ip_high5},
  1289. ip_low6 = {int:ip_low6}, ip_high6 = {int:ip_high6},
  1290. ip_low7 = {int:ip_low7}, ip_high7 = {int:ip_high7},
  1291. ip_low8 = {int:ip_low8}, ip_high8 = {int:ip_high8}
  1292. WHERE id_ban = {int:ban_item}
  1293. AND id_ban_group = {int:id_ban_group}',
  1294. array_merge($trigger, array(
  1295. 'id_ban_group' => $group_id,
  1296. 'ban_item' => $ban_item,
  1297. ))
  1298. );
  1299. logTriggersUpdates($logs, false);
  1300. }
  1301. /**
  1302. * A small function to unify logging of triggers (updates and new)
  1303. *
  1304. * @param array $logs an array of logs, each log contains the following keys:
  1305. * - bantype: a known type of ban (ip_range, hostname, email, user, main_ip)
  1306. * - value: the value of the bantype (e.g. the IP or the email address banned)
  1307. * @param bool $new if the trigger is new or an update of an existing one
  1308. * @param bool $removal if the trigger is being deleted
  1309. */
  1310. function logTriggersUpdates($logs, $new = true, $removal = false)
  1311. {
  1312. if (empty($logs))
  1313. return;
  1314. $log_name_map = array(
  1315. 'main_ip' => 'ip_range',
  1316. 'hostname' => 'hostname',
  1317. 'email' => 'email',
  1318. 'user' => 'member',
  1319. 'ip_range' => 'ip_range',
  1320. );
  1321. // Log the addion of the ban entries into the moderation log.
  1322. foreach ($logs as $log)
  1323. logAction('ban' . ($removal == true ? 'remove' : ''), array(
  1324. $log_name_map[$log['bantype']] => $log['value'],
  1325. 'new' => empty($new) ? 0 : 1,
  1326. 'remove' => empty($removal) ? 0 : 1,
  1327. 'type' => $log['bantype'],
  1328. ));
  1329. }
  1330. /**
  1331. * Updates an existing ban group
  1332. * If the name doesn't exists a new one is created
  1333. *
  1334. * Errors in $context['ban_errors']
  1335. *
  1336. * @param array $ban_info
  1337. * @return nothing
  1338. */
  1339. function updateBanGroup($ban_info = array())
  1340. {
  1341. global $smcFunc, $context;
  1342. if (empty($ban_info['name']))
  1343. $context['ban_errors'][] = 'ban_name_empty';
  1344. if (empty($ban_info['id']))
  1345. $context['ban_errors'][] = 'ban_id_empty';
  1346. if (!empty($context['ban_errors']))
  1347. return;
  1348. $request = $smcFunc['db_query']('', '
  1349. SELECT id_ban_group
  1350. FROM {db_prefix}ban_groups
  1351. WHERE name = {string:new_ban_name}
  1352. AND id_ban_group = {int:ban_group}
  1353. LIMIT 1',
  1354. array(
  1355. 'ban_group' => $ban_info['id'],
  1356. 'new_ban_name' => $ban_info['name'],
  1357. )
  1358. );
  1359. if ($smcFunc['db_num_rows']($request) == 0)
  1360. return insertBanGroup($ban_info);
  1361. $smcFunc['db_free_result']($request);
  1362. $smcFunc['db_query']('', '
  1363. UPDATE {db_prefix}ban_groups
  1364. SET
  1365. name = {string:ban_name},
  1366. reason = {string:reason},
  1367. notes = {string:notes},
  1368. expire_time = {raw:expiration},
  1369. cannot_access = {int:cannot_access},
  1370. cannot_post = {int:cannot_post},
  1371. cannot_register = {int:cannot_register},
  1372. cannot_login = {int:cannot_login}
  1373. WHERE id_ban_group = {int:id_ban_group}',
  1374. array(
  1375. 'expiration' => $ban_info['db_expiration'],
  1376. 'cannot_access' => $ban_info['cannot']['access'],
  1377. 'cannot_post' => $ban_info['cannot']['post'],
  1378. 'cannot_register' => $ban_info['cannot']['register'],
  1379. 'cannot_login' => $ban_info['cannot']['login'],
  1380. 'id_ban_group' => $ban_info['id'],
  1381. 'ban_name' => $ban_info['name'],
  1382. 'reason' => $ban_info['reason'],
  1383. 'notes' => $ban_info['notes'],
  1384. )
  1385. );
  1386. }
  1387. /**
  1388. * Creates a new ban group
  1389. * If a ban group with the same name already exists or the group s sucessfully created the ID is returned
  1390. * On error the error code is returned or false
  1391. *
  1392. * Errors in $context['ban_errors']
  1393. *
  1394. * @param array $ban_info
  1395. * @return int the ban group's ID
  1396. */
  1397. function insertBanGroup($ban_info = array())
  1398. {
  1399. global $smcFunc, $context;
  1400. if (empty($ban_info['name']))
  1401. $context['ban_errors'][] = 'ban_name_empty';
  1402. if (empty($ban_info['cannot']['access']) && empty($ban_info['cannot']['register']) && empty($ban_info['cannot']['post']) && empty($ban_info['cannot']['login']))
  1403. $context['ban_errors'][] = 'ban_unknown_restriction_type';
  1404. if (!empty($context['ban_errors']))
  1405. return;
  1406. // Check whether a ban with this name already exists.
  1407. $request = $smcFunc['db_query']('', '
  1408. SELECT id_ban_group
  1409. FROM {db_prefix}ban_groups
  1410. WHERE name = {string:new_ban_name}' . '
  1411. LIMIT 1',
  1412. array(
  1413. 'new_ban_name' => $ban_info['name'],
  1414. )
  1415. );
  1416. if ($smcFunc['db_num_rows']($request) == 1)
  1417. {
  1418. list($id_ban) = $smcFunc['db_fetch_row']($request);
  1419. $smcFunc['db_free_result']($request);
  1420. return $id_ban;
  1421. }
  1422. $smcFunc['db_free_result']($request);
  1423. // Yes yes, we're ready to add now.
  1424. $smcFunc['db_insert']('',
  1425. '{db_prefix}ban_groups',
  1426. array(
  1427. 'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
  1428. 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
  1429. ),
  1430. array(
  1431. $ban_info['name'], time(), $ban_info['db_expiration'], $ban_info['cannot']['access'], $ban_info['cannot']['register'],
  1432. $ban_info['cannot']['post'], $ban_info['cannot']['login'], $ban_info['reason'], $ban_info['notes'],
  1433. ),
  1434. array('id_ban_group')
  1435. );
  1436. $ban_info['id'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
  1437. if (empty($ban_info['id']))
  1438. $context['ban_errors'][] = 'impossible_insert_new_bangroup';
  1439. return $ban_info['id'];
  1440. }
  1441. /**
  1442. * This function handles the ins and outs of the screen for adding new ban
  1443. * triggers or modifying existing ones.
  1444. * Adding new ban triggers:
  1445. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
  1446. * - uses the ban_edit_trigger sub template of ManageBans.
  1447. * Editing existing ban triggers:
  1448. * - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
  1449. * - uses the ban_edit_trigger sub template of ManageBans.
  1450. */
  1451. function BanEditTrigger()
  1452. {
  1453. global $context, $smcFunc, $scripturl;
  1454. $context['sub_template'] = 'ban_edit_trigger';
  1455. $context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edittrigger';
  1456. $ban_group = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
  1457. $ban_id = isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0;
  1458. if (empty($ban_group))
  1459. fatal_lang_error('ban_not_found', false);
  1460. if (isset($_POST['add_new_trigger']) && !empty($_POST['ban_suggestions']))
  1461. {
  1462. saveTriggers($_POST['ban_suggestions'], $ban_group, 0, $ban_id);
  1463. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1464. }
  1465. elseif (isset($_POST['edit_trigger']) && !empty($_POST['ban_suggestions']))
  1466. {
  1467. // The first replaces the old one, the others are added new (simplification, otherwise it would require another query and some work...)
  1468. saveTriggers(array_shift($_POST['ban_suggestions']), $ban_group, 0, $ban_id);
  1469. if (!empty($_POST['ban_suggestions']))
  1470. saveTriggers($_POST['ban_suggestions'], $ban_group);
  1471. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1472. }
  1473. elseif (isset($_POST['edit_trigger']))
  1474. {
  1475. removeBanTriggers($ban_id);
  1476. redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
  1477. }
  1478. loadJavascriptFile('suggest.js', array('default_theme' => true), 'suggest.js');
  1479. if (empty($ban_id))
  1480. {
  1481. $context['ban_trigger'] = array(
  1482. 'id' => 0,
  1483. 'group' => $ban_group,
  1484. 'ip' => array(
  1485. 'value' => '',
  1486. 'selected' => true,
  1487. ),
  1488. 'hostname' => array(
  1489. 'selected' => false,
  1490. 'value' => '',
  1491. ),
  1492. 'email' => array(
  1493. 'value' => '',
  1494. 'selected' => false,
  1495. ),
  1496. 'banneduser' => array(
  1497. 'value' => '',
  1498. 'selected' => false,
  1499. ),
  1500. 'is_new' => true,
  1501. );
  1502. }
  1503. else
  1504. {
  1505. $request = $smcFunc['db_query']('', '
  1506. SELECT
  1507. bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
  1508. bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
  1509. bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8,
  1510. mem.member_name, mem.real_name
  1511. FROM {db_prefix}ban_items AS bi
  1512. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
  1513. WHERE bi.id_ban = {int:ban_item}
  1514. AND bi.id_ban_group = {int:ban_group}
  1515. LIMIT 1',
  1516. array(
  1517. 'ban_item' => $ban_id,
  1518. 'ban_group' => $ban_group,
  1519. )
  1520. );
  1521. if ($smcFunc['db_num_rows']($request) == 0)
  1522. fatal_lang_error('ban_not_found', false);
  1523. $row = $smcFunc['db_fetch_assoc']($request);
  1524. $smcFunc['db_free_result']($request);
  1525. $context['ban_trigger'] = array(
  1526. 'id' => $row['id_ban'],
  1527. 'group' => $row['id_ban_group'],
  1528. 'ip' => array(
  1529. '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'])),
  1530. 'selected' => !empty($row['ip_low1']),
  1531. ),
  1532. 'hostname' => array(
  1533. 'value' => str_replace('%', '*', $row['hostname']),
  1534. 'selected' => !empty($row['hostname']),
  1535. ),
  1536. 'email' => array(
  1537. 'value' => str_replace('%', '*', $row['email_address']),
  1538. 'selected' => !empty($row['email_address'])
  1539. ),
  1540. 'banneduser' => array(
  1541. 'value' => $row['member_name'],
  1542. 'selected' => !empty($row['member_name'])
  1543. ),
  1544. 'is_new' => false,
  1545. );
  1546. }
  1547. createToken('admin-bet');
  1548. }
  1549. /**
  1550. * This handles the screen for showing the banned entities
  1551. * It is accessed by ?action=admin;area=ban;sa=browse
  1552. * It uses sub-tabs for browsing by IP, hostname, email or username.
  1553. *
  1554. * @uses ManageBans template, browse_triggers sub template.
  1555. */
  1556. function BanBrowseTriggers()
  1557. {
  1558. global $modSettings, $context, $scripturl, $smcFunc, $txt;
  1559. global $sourcedir, $settings;
  1560. if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  1561. {
  1562. checkSession();
  1563. removeBanTriggers($_POST['remove']);
  1564. // Rehabilitate some members.
  1565. if ($_REQUEST['entity'] == 'member')
  1566. updateBanMembers();
  1567. // Make sure the ban cache is refreshed.
  1568. updateSettings(array('banLastUpdated' => time()));
  1569. }
  1570. $context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
  1571. $listOptions = array(
  1572. 'id' => 'ban_trigger_list',
  1573. 'title' => $txt['ban_trigger_browse'],
  1574. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1575. 'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1576. 'default_sort_col' => 'banned_entity',
  1577. 'no_items_label' => $txt['ban_no_triggers'],
  1578. 'get_items' => array(
  1579. 'function' => 'list_getBanTriggers',
  1580. 'params' => array(
  1581. $context['selected_entity'],
  1582. ),
  1583. ),
  1584. 'get_count' => array(
  1585. 'function' => 'list_getNumBanTriggers',
  1586. 'params' => array(
  1587. $context['selected_entity'],
  1588. ),
  1589. ),
  1590. 'columns' => array(
  1591. 'banned_entity' => array(
  1592. 'header' => array(
  1593. 'value' => $txt['ban_banned_entity'],
  1594. ),
  1595. ),
  1596. 'ban_name' => array(
  1597. 'header' => array(
  1598. 'value' => $txt['ban_name'],
  1599. ),
  1600. 'data' => array(
  1601. 'sprintf' => array(
  1602. 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
  1603. 'params' => array(
  1604. 'id_ban_group' => false,
  1605. 'name' => false,
  1606. ),
  1607. ),
  1608. ),
  1609. 'sort' => array(
  1610. 'default' => 'bg.name',
  1611. 'reverse' => 'bg.name DESC',
  1612. ),
  1613. ),
  1614. 'hits' => array(
  1615. 'header' => array(
  1616. 'value' => $txt['ban_hits'],
  1617. ),
  1618. 'data' => array(
  1619. 'db' => 'hits',
  1620. ),
  1621. 'sort' => array(
  1622. 'default' => 'bi.hits DESC',
  1623. 'reverse' => 'bi.hits',
  1624. ),
  1625. ),
  1626. 'check' => array(
  1627. 'header' => array(
  1628. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1629. 'class' => 'centercol',
  1630. ),
  1631. 'data' => array(
  1632. 'sprintf' => array(
  1633. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1634. 'params' => array(
  1635. 'id_ban' => false,
  1636. ),
  1637. ),
  1638. 'class' => 'centercol',
  1639. ),
  1640. ),
  1641. ),
  1642. 'form' => array(
  1643. 'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
  1644. 'include_start' => true,
  1645. 'include_sort' => true,
  1646. ),
  1647. 'additional_rows' => array(
  1648. array(
  1649. 'position' => 'above_column_headers',
  1650. '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>',
  1651. ),
  1652. array(
  1653. 'position' => 'bottom_of_list',
  1654. '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" />',
  1655. ),
  1656. ),
  1657. );
  1658. // Specific data for the first column depending on the selected entity.
  1659. if ($context['selected_entity'] === 'ip')
  1660. {
  1661. $listOptions['columns']['banned_entity']['data'] = array(
  1662. 'function' => create_function('$rowData', '
  1663. return range2ip(array(
  1664. $rowData[\'ip_low1\'],
  1665. $rowData[\'ip_low2\'],
  1666. $rowData[\'ip_low3\'],
  1667. $rowData[\'ip_low4\'],
  1668. $rowData[\'ip_low5\'],
  1669. $rowData[\'ip_low6\'],
  1670. $rowData[\'ip_low7\'],
  1671. $rowData[\'ip_low8\']
  1672. ), array(
  1673. $rowData[\'ip_high1\'],
  1674. $rowData[\'ip_high2\'],
  1675. $rowData[\'ip_high3\'],
  1676. $rowData[\'ip_high4\'],
  1677. $rowData[\'ip_high5\'],
  1678. $rowData[\'ip_high6\'],
  1679. $rowData[\'ip_high7\'],
  1680. $rowData[\'ip_high8\']
  1681. ));
  1682. '),
  1683. );
  1684. $listOptions['columns']['banned_entity']['sort'] = array(
  1685. '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',
  1686. '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',
  1687. );
  1688. }
  1689. elseif ($context['selected_entity'] === 'hostname')
  1690. {
  1691. $listOptions['columns']['banned_entity']['data'] = array(
  1692. 'function' => create_function('$rowData', '
  1693. global $smcFunc;
  1694. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'hostname\']), array(\'%\' => \'*\'));
  1695. '),
  1696. );
  1697. $listOptions['columns']['banned_entity']['sort'] = array(
  1698. 'default' => 'bi.hostname',
  1699. 'reverse' => 'bi.hostname DESC',
  1700. );
  1701. }
  1702. elseif ($context['selected_entity'] === 'email')
  1703. {
  1704. $listOptions['columns']['banned_entity']['data'] = array(
  1705. 'function' => create_function('$rowData', '
  1706. global $smcFunc;
  1707. return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'email_address\']), array(\'%\' => \'*\'));
  1708. '),
  1709. );
  1710. $listOptions['columns']['banned_entity']['sort'] = array(
  1711. 'default' => 'bi.email_address',
  1712. 'reverse' => 'bi.email_address DESC',
  1713. );
  1714. }
  1715. elseif ($context['selected_entity'] === 'member')
  1716. {
  1717. $listOptions['columns']['banned_entity']['data'] = array(
  1718. 'sprintf' => array(
  1719. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1720. 'params' => array(
  1721. 'id_member' => false,
  1722. 'real_name' => false,
  1723. ),
  1724. ),
  1725. );
  1726. $listOptions['columns']['banned_entity']['sort'] = array(
  1727. 'default' => 'mem.real_name',
  1728. 'reverse' => 'mem.real_name DESC',
  1729. );
  1730. }
  1731. // Create the list.
  1732. require_once($sourcedir . '/Subs-List.php');
  1733. createList($listOptions);
  1734. // The list is the only thing to show, so make it the default sub template.
  1735. $context['sub_template'] = 'show_list';
  1736. $context['default_list'] = 'ban_trigger_list';
  1737. }
  1738. /**
  1739. * Get ban triggers for the given parameters.
  1740. *
  1741. * @param int $start
  1742. * @param int $items_per_page
  1743. * @param string $sort
  1744. * @param string $trigger_type
  1745. * @return array
  1746. */
  1747. function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
  1748. {
  1749. global $smcFunc;
  1750. $where = array(
  1751. 'ip' => 'bi.ip_low1 > 0',
  1752. 'hostname' => 'bi.hostname != {string:blank_string}',
  1753. 'email' => 'bi.email_address != {string:blank_string}',
  1754. );
  1755. $request = $smcFunc['db_query']('', '
  1756. SELECT
  1757. 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,
  1758. bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
  1759. mem.id_member, mem.real_name' : '') . '
  1760. FROM {db_prefix}ban_items AS bi
  1761. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
  1762. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1763. WHERE ' . $where[$trigger_type]) . '
  1764. ORDER BY ' . $sort . '
  1765. LIMIT ' . $start . ', ' . $items_per_page,
  1766. array(
  1767. 'blank_string' => '',
  1768. )
  1769. );
  1770. $ban_triggers = array();
  1771. while ($row = $smcFunc['db_fetch_assoc']($request))
  1772. $ban_triggers[] = $row;
  1773. $smcFunc['db_free_result']($request);
  1774. return $ban_triggers;
  1775. }
  1776. /**
  1777. * This returns the total number of ban triggers of the given type.
  1778. *
  1779. * @param string $trigger_type
  1780. * @return int
  1781. */
  1782. function list_getNumBanTriggers($trigger_type)
  1783. {
  1784. global $smcFunc;
  1785. $where = array(
  1786. 'ip' => 'bi.ip_low1 > 0',
  1787. 'hostname' => 'bi.hostname != {string:blank_string}',
  1788. 'email' => 'bi.email_address != {string:blank_string}',
  1789. );
  1790. $request = $smcFunc['db_query']('', '
  1791. SELECT COUNT(*)
  1792. FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
  1793. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
  1794. WHERE ' . $where[$trigger_type]),
  1795. array(
  1796. 'blank_string' => '',
  1797. )
  1798. );
  1799. list ($num_triggers) = $smcFunc['db_fetch_row']($request);
  1800. $smcFunc['db_free_result']($request);
  1801. return $num_triggers;
  1802. }
  1803. /**
  1804. * This handles the listing of ban log entries, and allows their deletion.
  1805. * Shows a list of logged access attempts by banned users.
  1806. * It is accessed by ?action=admin;area=ban;sa=log.
  1807. * How it works:
  1808. * - allows sorting of several columns.
  1809. * - also handles deletion of (a selection of) log entries.
  1810. */
  1811. function BanLog()
  1812. {
  1813. global $scripturl, $context, $smcFunc, $sourcedir, $txt;
  1814. global $context;
  1815. // Delete one or more entries.
  1816. if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
  1817. {
  1818. checkSession();
  1819. validateToken('admin-bl');
  1820. // 'Delete all entries' button was pressed.
  1821. if (!empty($_POST['removeAll']))
  1822. removeBanLogs();
  1823. // 'Delete selection' button was pressed.
  1824. else
  1825. {
  1826. array_map('intval', $_POST['remove']);
  1827. removeBanLogs($_POST['remove']);
  1828. }
  1829. }
  1830. $listOptions = array(
  1831. 'id' => 'ban_log',
  1832. 'title' => $txt['ban_log'],
  1833. 'items_per_page' => 30,
  1834. 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1835. 'default_sort_col' => 'date',
  1836. 'get_items' => array(
  1837. 'function' => 'list_getBanLogEntries',
  1838. ),
  1839. 'get_count' => array(
  1840. 'function' => 'list_getNumBanLogEntries',
  1841. ),
  1842. 'no_items_label' => $txt['ban_log_no_entries'],
  1843. 'columns' => array(
  1844. 'ip' => array(
  1845. 'header' => array(
  1846. 'value' => $txt['ban_log_ip'],
  1847. ),
  1848. 'data' => array(
  1849. 'sprintf' => array(
  1850. 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
  1851. 'params' => array(
  1852. 'ip' => false,
  1853. ),
  1854. ),
  1855. ),
  1856. 'sort' => array(
  1857. 'default' => 'lb.ip',
  1858. 'reverse' => 'lb.ip DESC',
  1859. ),
  1860. ),
  1861. 'email' => array(
  1862. 'header' => array(
  1863. 'value' => $txt['ban_log_email'],
  1864. ),
  1865. 'data' => array(
  1866. 'db_htmlsafe' => 'email',
  1867. ),
  1868. 'sort' => array(
  1869. 'default' => 'lb.email = \'\', lb.email',
  1870. 'reverse' => 'lb.email != \'\', lb.email DESC',
  1871. ),
  1872. ),
  1873. 'member' => array(
  1874. 'header' => array(
  1875. 'value' => $txt['ban_log_member'],
  1876. ),
  1877. 'data' => array(
  1878. 'sprintf' => array(
  1879. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1880. 'params' => array(
  1881. 'id_member' => false,
  1882. 'real_name' => false,
  1883. ),
  1884. ),
  1885. ),
  1886. 'sort' => array(
  1887. 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
  1888. 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
  1889. ),
  1890. ),
  1891. 'date' => array(
  1892. 'header' => array(
  1893. 'value' => $txt['ban_log_date'],
  1894. ),
  1895. 'data' => array(
  1896. 'function' => create_function('$rowData', '
  1897. return timeformat($rowData[\'log_time\']);
  1898. '),
  1899. ),
  1900. 'sort' => array(
  1901. 'default' => 'lb.log_time DESC',
  1902. 'reverse' => 'lb.log_time',
  1903. ),
  1904. ),
  1905. 'check' => array(
  1906. 'header' => array(
  1907. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1908. 'class' => 'centercol',
  1909. ),
  1910. 'data' => array(
  1911. 'sprintf' => array(
  1912. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  1913. 'params' => array(
  1914. 'id_ban_log' => false,
  1915. ),
  1916. ),
  1917. 'class' => 'centercol',
  1918. ),
  1919. ),
  1920. ),
  1921. 'form' => array(
  1922. 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1923. 'include_start' => true,
  1924. 'include_sort' => true,
  1925. 'token' => 'admin-bl',
  1926. ),
  1927. 'additional_rows' => array(
  1928. array(
  1929. 'position' => 'bottom_of_list',
  1930. 'value' => '
  1931. <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit" />
  1932. <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit" />',
  1933. ),
  1934. ),
  1935. );
  1936. createToken('admin-bl');
  1937. require_once($sourcedir . '/Subs-List.php');
  1938. createList($listOptions);
  1939. $context['page_title'] = $txt['ban_log'];
  1940. $context['sub_template'] = 'show_list';
  1941. $context['default_list'] = 'ban_log';
  1942. }
  1943. /**
  1944. * Load a list of ban log entries from the database.
  1945. * (no permissions check)
  1946. *
  1947. * @param int $start
  1948. * @param int $items_per_page
  1949. * @param string $sort
  1950. */
  1951. function list_getBanLogEntries($start, $items_per_page, $sort)
  1952. {
  1953. global $smcFunc;
  1954. $request = $smcFunc['db_query']('', '
  1955. 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
  1956. FROM {db_prefix}log_banned AS lb
  1957. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
  1958. ORDER BY ' . $sort . '
  1959. LIMIT ' . $start . ', ' . $items_per_page,
  1960. array(
  1961. 'blank_string' => '',
  1962. 'dash' => '-',
  1963. )
  1964. );
  1965. $log_entries = array();
  1966. while ($row = $smcFunc['db_fetch_assoc']($request))
  1967. $log_entries[] = $row;
  1968. $smcFunc['db_free_result']($request);
  1969. return $log_entries;
  1970. }
  1971. /**
  1972. * This returns the total count of ban log entries.
  1973. */
  1974. function list_getNumBanLogEntries()
  1975. {
  1976. global $smcFunc;
  1977. $request = $smcFunc['db_query']('', '
  1978. SELECT COUNT(*)
  1979. FROM {db_prefix}log_banned AS lb',
  1980. array(
  1981. )
  1982. );
  1983. list ($num_entries) = $smcFunc['db_fetch_row']($request);
  1984. $smcFunc['db_free_result']($request);
  1985. return $num_entries;
  1986. }
  1987. /**
  1988. * Convert a range of given IP number into a single string.
  1989. * It's practically the reverse function of ip2range().
  1990. *
  1991. * @example
  1992. * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
  1993. *
  1994. * @param array $low IPv4 format
  1995. * @param array $high IPv4 format
  1996. * @return string
  1997. */
  1998. function range2ip($low, $high)
  1999. {
  2000. // IPv6 check.
  2001. if (!empty($high[4]) || !empty($high[5]) || !empty($high[6]) || !empty($high[7]))
  2002. {
  2003. if (count($low) != 8 || count($high) != 8)
  2004. return '';
  2005. $ip = array();
  2006. for ($i = 0; $i < 8; $i++)
  2007. {
  2008. if ($low[$i] == $high[$i])
  2009. $ip[$i] = dechex($low[$i]);
  2010. elseif ($low[$i] == '0' && $high[$i] == '255')
  2011. $ip[$i] = '*';
  2012. else
  2013. $ip[$i] = dechex($low[$i]) . '-' . dechex($high[$i]);
  2014. }
  2015. return implode(':', $ip);
  2016. }
  2017. // Legacy IPv4 stuff.
  2018. // (count($low) != 4 || count($high) != 4) would not work because $low and $high always contain 8 elements!
  2019. if ((count($low) != 4 || count($high) != 4) && (count($low) != 8 || count($high) != 8))
  2020. return '';
  2021. for ($i = 0; $i < 4; $i++)
  2022. {
  2023. if ($low[$i] == $high[$i])
  2024. $ip[$i] = $low[$i];
  2025. elseif ($low[$i] == '0' && $high[$i] == '255')
  2026. $ip[$i] = '*';
  2027. else
  2028. $ip[$i] = $low[$i] . '-' . $high[$i];
  2029. }
  2030. // Pretending is fun... the IP can't be this, so use it for 'unknown'.
  2031. if ($ip == array(255, 255, 255, 255))
  2032. return 'unknown';
  2033. return implode('.', $ip);
  2034. }
  2035. /**
  2036. * Checks whether a given IP range already exists in the trigger list.
  2037. * If yes, it returns an error message. Otherwise, it returns an array
  2038. * optimized for the database.
  2039. *
  2040. * @param array $ip_array
  2041. * @param string $fullip
  2042. * @return boolean
  2043. */
  2044. function checkExistingTriggerIP($ip_array, $fullip = '')
  2045. {
  2046. global $smcFunc, $scripturl;
  2047. if (count($ip_array) == 4 || count($ip_array) == 8)
  2048. $values = array(
  2049. 'ip_low1' => $ip_array[0]['low'],
  2050. 'ip_high1' => $ip_array[0]['high'],
  2051. 'ip_low2' => $ip_array[1]['low'],
  2052. 'ip_high2' => $ip_array[1]['high'],
  2053. 'ip_low3' => $ip_array[2]['low'],
  2054. 'ip_high3' => $ip_array[2]['high'],
  2055. 'ip_low4' => $ip_array[3]['low'],
  2056. 'ip_high4' => $ip_array[3]['high'],
  2057. 'ip_low5' => $ip_array[4]['low'],
  2058. 'ip_high5' => $ip_array[4]['high'],
  2059. 'ip_low6' => $ip_array[5]['low'],
  2060. 'ip_high6' => $ip_array[5]['high'],
  2061. 'ip_low7' => $ip_array[6]['low'],
  2062. 'ip_high7' => $ip_array[6]['high'],
  2063. 'ip_low8' => $ip_array[7]['low'],
  2064. 'ip_high8' => $ip_array[7]['high'],
  2065. );
  2066. else
  2067. return false;
  2068. $request = $smcFunc['db_query']('', '
  2069. SELECT bg.id_ban_group, bg.name
  2070. FROM {db_prefix}ban_groups AS bg
  2071. INNER JOIN {db_prefix}ban_items AS bi ON
  2072. (bi.id_ban_group = bg.id_ban_group)
  2073. AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
  2074. AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
  2075. AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
  2076. AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
  2077. AND ip_low5 = {int:ip_low5} AND ip_high5 = {int:ip_high5}
  2078. AND ip_low6 = {int:ip_low6} AND ip_high6 = {int:ip_high6}
  2079. AND ip_low7 = {int:ip_low7} AND ip_high7 = {int:ip_high7}
  2080. AND ip_low8 = {int:ip_low8} AND ip_high8 = {int:ip_high8}
  2081. LIMIT 1',
  2082. $values
  2083. );
  2084. if ($smcFunc['db_num_rows']($request) != 0)
  2085. {
  2086. list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
  2087. fatal_lang_error('ban_trigger_already_exists', false, array(
  2088. $fullip,
  2089. '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
  2090. ));
  2091. }
  2092. $smcFunc['db_free_result']($request);
  2093. return $values;
  2094. }
  2095. /**
  2096. * As it says... this tries to review the list of banned members, to match new bans.
  2097. * Note: is_activated >= 10: a member is banned.
  2098. */
  2099. function updateBanMembers()
  2100. {
  2101. global $smcFunc;
  2102. $updates = array();
  2103. $allMembers = array();
  2104. $newMembers = array();
  2105. // Start by getting all active bans - it's quicker doing this in parts...
  2106. $request = $smcFunc['db_query']('', '
  2107. SELECT bi.id_member, bi.email_address
  2108. FROM {db_prefix}ban_items AS bi
  2109. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  2110. WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
  2111. AND bg.cannot_access = {int:cannot_access_on}
  2112. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  2113. array(
  2114. 'no_member' => 0,
  2115. 'cannot_access_on' => 1,
  2116. 'current_time' => time(),
  2117. 'blank_string' => '',
  2118. )
  2119. );
  2120. $memberIDs = array();
  2121. $memberEmails = array();
  2122. $memberEmailWild = array();
  2123. while ($row = $smcFunc['db_fetch_assoc']($request))
  2124. {
  2125. if ($row['id_member'])
  2126. $memberIDs[$row['id_member']] = $row['id_member'];
  2127. if ($row['email_address'])
  2128. {
  2129. // Does it have a wildcard - if so we can't do a IN on it.
  2130. if (strpos($row['email_address'], '%') !== false)
  2131. $memberEmailWild[$row['email_address']] = $row['email_address'];
  2132. else
  2133. $memberEmails[$row['email_address']] = $row['email_address'];
  2134. }
  2135. }
  2136. $smcFunc['db_free_result']($request);
  2137. // Build up the query.
  2138. $queryPart = array();
  2139. $queryValues = array();
  2140. if (!empty($memberIDs))
  2141. {
  2142. $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
  2143. $queryValues['member_ids'] = $memberIDs;
  2144. }
  2145. if (!empty($memberEmails))
  2146. {
  2147. $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
  2148. $queryValues['member_emails'] = $memberEmails;
  2149. }
  2150. $count = 0;
  2151. foreach ($memberEmailWild as $email)
  2152. {
  2153. $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
  2154. $queryValues['wild_' . $count++] = $email;
  2155. }
  2156. // Find all banned members.
  2157. if (!empty($queryPart))
  2158. {
  2159. $request = $smcFunc['db_query']('', '
  2160. SELECT mem.id_member, mem.is_activated
  2161. FROM {db_prefix}members AS mem
  2162. WHERE ' . implode( ' OR ', $queryPart),
  2163. $queryValues
  2164. );
  2165. while ($row = $smcFunc['db_fetch_assoc']($request))
  2166. {
  2167. if (!in_array($row['id_member'], $allMembers))
  2168. {
  2169. $allMembers[] = $row['id_member'];
  2170. // Do they need an update?
  2171. if ($row['is_activated'] < 10)
  2172. {
  2173. $updates[($row['is_activated'] + 10)][] = $row['id_member'];
  2174. $newMembers[] = $row['id_member'];
  2175. }
  2176. }
  2177. }
  2178. $smcFunc['db_free_result']($request);
  2179. }
  2180. // We welcome our new members in the realm of the banned.
  2181. if (!empty($newMembers))
  2182. $smcFunc['db_query']('', '
  2183. DELETE FROM {db_prefix}log_online
  2184. WHERE id_member IN ({array_int:new_banned_members})',
  2185. array(
  2186. 'new_banned_members' => $newMembers,
  2187. )
  2188. );
  2189. // Find members that are wrongfully marked as banned.
  2190. $request = $smcFunc['db_query']('', '
  2191. SELECT mem.id_member, mem.is_activated - 10 AS new_value
  2192. FROM {db_prefix}members AS mem
  2193. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
  2194. 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}))
  2195. WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
  2196. AND mem.is_activated >= {int:ban_flag}',
  2197. array(
  2198. 'cannot_access_activated' => 1,
  2199. 'current_time' => time(),
  2200. 'ban_flag' => 10,
  2201. )
  2202. );
  2203. while ($row = $smcFunc['db_fetch_assoc']($request))
  2204. {
  2205. // Don't do this twice!
  2206. if (!in_array($row['id_member'], $allMembers))
  2207. {
  2208. $updates[$row['new_value']][] = $row['id_member'];
  2209. $allMembers[] = $row['id_member'];
  2210. }
  2211. }
  2212. $smcFunc['db_free_result']($request);
  2213. if (!empty($updates))
  2214. foreach ($updates as $newStatus => $members)
  2215. updateMemberData($members, array('is_activated' => $newStatus));
  2216. // Update the latest member and our total members as banning may change them.
  2217. updateStats('member');
  2218. }
  2219. function getMemberData($id)
  2220. {
  2221. global $smcFunc;
  2222. $suggestions = array();
  2223. $request = $smcFunc['db_query']('', '
  2224. SELECT id_member, real_name, member_ip, email_address
  2225. FROM {db_prefix}members
  2226. WHERE id_member = {int:current_user}
  2227. LIMIT 1',
  2228. array(
  2229. 'current_user' => $id,
  2230. )
  2231. );
  2232. if ($smcFunc['db_num_rows']($request) > 0)
  2233. list ($suggestions['member']['id'], $suggestions['member']['name'], $suggestions['main_ip'], $suggestions['email']) = $smcFunc['db_fetch_row']($request);
  2234. $smcFunc['db_free_result']($request);
  2235. return $suggestions;
  2236. }
  2237. ?>