ManageBans.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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 2014 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, $scripturl;
  89. global $user_info, $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, $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, $scripturl;
  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_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_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), 'smf_suggest');
  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, $sourcedir, $txt;
  1814. // Delete one or more entries.
  1815. if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
  1816. {
  1817. checkSession();
  1818. validateToken('admin-bl');
  1819. // 'Delete all entries' button was pressed.
  1820. if (!empty($_POST['removeAll']))
  1821. removeBanLogs();
  1822. // 'Delete selection' button was pressed.
  1823. else
  1824. {
  1825. array_map('intval', $_POST['remove']);
  1826. removeBanLogs($_POST['remove']);
  1827. }
  1828. }
  1829. $listOptions = array(
  1830. 'id' => 'ban_log',
  1831. 'title' => $txt['ban_log'],
  1832. 'items_per_page' => 30,
  1833. 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1834. 'default_sort_col' => 'date',
  1835. 'get_items' => array(
  1836. 'function' => 'list_getBanLogEntries',
  1837. ),
  1838. 'get_count' => array(
  1839. 'function' => 'list_getNumBanLogEntries',
  1840. ),
  1841. 'no_items_label' => $txt['ban_log_no_entries'],
  1842. 'columns' => array(
  1843. 'ip' => array(
  1844. 'header' => array(
  1845. 'value' => $txt['ban_log_ip'],
  1846. ),
  1847. 'data' => array(
  1848. 'sprintf' => array(
  1849. 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
  1850. 'params' => array(
  1851. 'ip' => false,
  1852. ),
  1853. ),
  1854. ),
  1855. 'sort' => array(
  1856. 'default' => 'lb.ip',
  1857. 'reverse' => 'lb.ip DESC',
  1858. ),
  1859. ),
  1860. 'email' => array(
  1861. 'header' => array(
  1862. 'value' => $txt['ban_log_email'],
  1863. ),
  1864. 'data' => array(
  1865. 'db_htmlsafe' => 'email',
  1866. ),
  1867. 'sort' => array(
  1868. 'default' => 'lb.email = \'\', lb.email',
  1869. 'reverse' => 'lb.email != \'\', lb.email DESC',
  1870. ),
  1871. ),
  1872. 'member' => array(
  1873. 'header' => array(
  1874. 'value' => $txt['ban_log_member'],
  1875. ),
  1876. 'data' => array(
  1877. 'sprintf' => array(
  1878. 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
  1879. 'params' => array(
  1880. 'id_member' => false,
  1881. 'real_name' => false,
  1882. ),
  1883. ),
  1884. ),
  1885. 'sort' => array(
  1886. 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
  1887. 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
  1888. ),
  1889. ),
  1890. 'date' => array(
  1891. 'header' => array(
  1892. 'value' => $txt['ban_log_date'],
  1893. ),
  1894. 'data' => array(
  1895. 'function' => create_function('$rowData', '
  1896. return timeformat($rowData[\'log_time\']);
  1897. '),
  1898. ),
  1899. 'sort' => array(
  1900. 'default' => 'lb.log_time DESC',
  1901. 'reverse' => 'lb.log_time',
  1902. ),
  1903. ),
  1904. 'check' => array(
  1905. 'header' => array(
  1906. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check">',
  1907. 'class' => 'centercol',
  1908. ),
  1909. 'data' => array(
  1910. 'sprintf' => array(
  1911. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check">',
  1912. 'params' => array(
  1913. 'id_ban_log' => false,
  1914. ),
  1915. ),
  1916. 'class' => 'centercol',
  1917. ),
  1918. ),
  1919. ),
  1920. 'form' => array(
  1921. 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
  1922. 'include_start' => true,
  1923. 'include_sort' => true,
  1924. 'token' => 'admin-bl',
  1925. ),
  1926. 'additional_rows' => array(
  1927. array(
  1928. 'position' => 'bottom_of_list',
  1929. 'value' => '
  1930. <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit">
  1931. <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit">',
  1932. ),
  1933. ),
  1934. );
  1935. createToken('admin-bl');
  1936. require_once($sourcedir . '/Subs-List.php');
  1937. createList($listOptions);
  1938. $context['page_title'] = $txt['ban_log'];
  1939. $context['sub_template'] = 'show_list';
  1940. $context['default_list'] = 'ban_log';
  1941. }
  1942. /**
  1943. * Load a list of ban log entries from the database.
  1944. * (no permissions check)
  1945. *
  1946. * @param int $start
  1947. * @param int $items_per_page
  1948. * @param string $sort
  1949. */
  1950. function list_getBanLogEntries($start, $items_per_page, $sort)
  1951. {
  1952. global $smcFunc;
  1953. $request = $smcFunc['db_query']('', '
  1954. 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
  1955. FROM {db_prefix}log_banned AS lb
  1956. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
  1957. ORDER BY ' . $sort . '
  1958. LIMIT ' . $start . ', ' . $items_per_page,
  1959. array(
  1960. 'blank_string' => '',
  1961. 'dash' => '-',
  1962. )
  1963. );
  1964. $log_entries = array();
  1965. while ($row = $smcFunc['db_fetch_assoc']($request))
  1966. $log_entries[] = $row;
  1967. $smcFunc['db_free_result']($request);
  1968. return $log_entries;
  1969. }
  1970. /**
  1971. * This returns the total count of ban log entries.
  1972. */
  1973. function list_getNumBanLogEntries()
  1974. {
  1975. global $smcFunc;
  1976. $request = $smcFunc['db_query']('', '
  1977. SELECT COUNT(*)
  1978. FROM {db_prefix}log_banned AS lb',
  1979. array(
  1980. )
  1981. );
  1982. list ($num_entries) = $smcFunc['db_fetch_row']($request);
  1983. $smcFunc['db_free_result']($request);
  1984. return $num_entries;
  1985. }
  1986. /**
  1987. * Convert a range of given IP number into a single string.
  1988. * It's practically the reverse function of ip2range().
  1989. *
  1990. * @example
  1991. * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
  1992. *
  1993. * @param array $low IPv4 format
  1994. * @param array $high IPv4 format
  1995. * @return string
  1996. */
  1997. function range2ip($low, $high)
  1998. {
  1999. // IPv6 check.
  2000. if (!empty($high[4]) || !empty($high[5]) || !empty($high[6]) || !empty($high[7]))
  2001. {
  2002. if (count($low) != 8 || count($high) != 8)
  2003. return '';
  2004. $ip = array();
  2005. for ($i = 0; $i < 8; $i++)
  2006. {
  2007. if ($low[$i] == $high[$i])
  2008. $ip[$i] = dechex($low[$i]);
  2009. elseif ($low[$i] == '0' && $high[$i] == '255')
  2010. $ip[$i] = '*';
  2011. else
  2012. $ip[$i] = dechex($low[$i]) . '-' . dechex($high[$i]);
  2013. }
  2014. return implode(':', $ip);
  2015. }
  2016. // Legacy IPv4 stuff.
  2017. // (count($low) != 4 || count($high) != 4) would not work because $low and $high always contain 8 elements!
  2018. if ((count($low) != 4 || count($high) != 4) && (count($low) != 8 || count($high) != 8))
  2019. return '';
  2020. for ($i = 0; $i < 4; $i++)
  2021. {
  2022. if ($low[$i] == $high[$i])
  2023. $ip[$i] = $low[$i];
  2024. elseif ($low[$i] == '0' && $high[$i] == '255')
  2025. $ip[$i] = '*';
  2026. else
  2027. $ip[$i] = $low[$i] . '-' . $high[$i];
  2028. }
  2029. // Pretending is fun... the IP can't be this, so use it for 'unknown'.
  2030. if ($ip == array(255, 255, 255, 255))
  2031. return 'unknown';
  2032. return implode('.', $ip);
  2033. }
  2034. /**
  2035. * Checks whether a given IP range already exists in the trigger list.
  2036. * If yes, it returns an error message. Otherwise, it returns an array
  2037. * optimized for the database.
  2038. *
  2039. * @param array $ip_array
  2040. * @param string $fullip
  2041. * @return boolean
  2042. */
  2043. function checkExistingTriggerIP($ip_array, $fullip = '')
  2044. {
  2045. global $smcFunc, $scripturl;
  2046. if (count($ip_array) == 4 || count($ip_array) == 8)
  2047. $values = array(
  2048. 'ip_low1' => $ip_array[0]['low'],
  2049. 'ip_high1' => $ip_array[0]['high'],
  2050. 'ip_low2' => $ip_array[1]['low'],
  2051. 'ip_high2' => $ip_array[1]['high'],
  2052. 'ip_low3' => $ip_array[2]['low'],
  2053. 'ip_high3' => $ip_array[2]['high'],
  2054. 'ip_low4' => $ip_array[3]['low'],
  2055. 'ip_high4' => $ip_array[3]['high'],
  2056. 'ip_low5' => $ip_array[4]['low'],
  2057. 'ip_high5' => $ip_array[4]['high'],
  2058. 'ip_low6' => $ip_array[5]['low'],
  2059. 'ip_high6' => $ip_array[5]['high'],
  2060. 'ip_low7' => $ip_array[6]['low'],
  2061. 'ip_high7' => $ip_array[6]['high'],
  2062. 'ip_low8' => $ip_array[7]['low'],
  2063. 'ip_high8' => $ip_array[7]['high'],
  2064. );
  2065. else
  2066. return false;
  2067. $request = $smcFunc['db_query']('', '
  2068. SELECT bg.id_ban_group, bg.name
  2069. FROM {db_prefix}ban_groups AS bg
  2070. INNER JOIN {db_prefix}ban_items AS bi ON
  2071. (bi.id_ban_group = bg.id_ban_group)
  2072. AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
  2073. AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
  2074. AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
  2075. AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
  2076. AND ip_low5 = {int:ip_low5} AND ip_high5 = {int:ip_high5}
  2077. AND ip_low6 = {int:ip_low6} AND ip_high6 = {int:ip_high6}
  2078. AND ip_low7 = {int:ip_low7} AND ip_high7 = {int:ip_high7}
  2079. AND ip_low8 = {int:ip_low8} AND ip_high8 = {int:ip_high8}
  2080. LIMIT 1',
  2081. $values
  2082. );
  2083. if ($smcFunc['db_num_rows']($request) != 0)
  2084. {
  2085. list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
  2086. fatal_lang_error('ban_trigger_already_exists', false, array(
  2087. $fullip,
  2088. '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
  2089. ));
  2090. }
  2091. $smcFunc['db_free_result']($request);
  2092. return $values;
  2093. }
  2094. /**
  2095. * As it says... this tries to review the list of banned members, to match new bans.
  2096. * Note: is_activated >= 10: a member is banned.
  2097. */
  2098. function updateBanMembers()
  2099. {
  2100. global $smcFunc;
  2101. $updates = array();
  2102. $allMembers = array();
  2103. $newMembers = array();
  2104. // Start by getting all active bans - it's quicker doing this in parts...
  2105. $request = $smcFunc['db_query']('', '
  2106. SELECT bi.id_member, bi.email_address
  2107. FROM {db_prefix}ban_items AS bi
  2108. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  2109. WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
  2110. AND bg.cannot_access = {int:cannot_access_on}
  2111. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
  2112. array(
  2113. 'no_member' => 0,
  2114. 'cannot_access_on' => 1,
  2115. 'current_time' => time(),
  2116. 'blank_string' => '',
  2117. )
  2118. );
  2119. $memberIDs = array();
  2120. $memberEmails = array();
  2121. $memberEmailWild = array();
  2122. while ($row = $smcFunc['db_fetch_assoc']($request))
  2123. {
  2124. if ($row['id_member'])
  2125. $memberIDs[$row['id_member']] = $row['id_member'];
  2126. if ($row['email_address'])
  2127. {
  2128. // Does it have a wildcard - if so we can't do a IN on it.
  2129. if (strpos($row['email_address'], '%') !== false)
  2130. $memberEmailWild[$row['email_address']] = $row['email_address'];
  2131. else
  2132. $memberEmails[$row['email_address']] = $row['email_address'];
  2133. }
  2134. }
  2135. $smcFunc['db_free_result']($request);
  2136. // Build up the query.
  2137. $queryPart = array();
  2138. $queryValues = array();
  2139. if (!empty($memberIDs))
  2140. {
  2141. $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
  2142. $queryValues['member_ids'] = $memberIDs;
  2143. }
  2144. if (!empty($memberEmails))
  2145. {
  2146. $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
  2147. $queryValues['member_emails'] = $memberEmails;
  2148. }
  2149. $count = 0;
  2150. foreach ($memberEmailWild as $email)
  2151. {
  2152. $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
  2153. $queryValues['wild_' . $count++] = $email;
  2154. }
  2155. // Find all banned members.
  2156. if (!empty($queryPart))
  2157. {
  2158. $request = $smcFunc['db_query']('', '
  2159. SELECT mem.id_member, mem.is_activated
  2160. FROM {db_prefix}members AS mem
  2161. WHERE ' . implode( ' OR ', $queryPart),
  2162. $queryValues
  2163. );
  2164. while ($row = $smcFunc['db_fetch_assoc']($request))
  2165. {
  2166. if (!in_array($row['id_member'], $allMembers))
  2167. {
  2168. $allMembers[] = $row['id_member'];
  2169. // Do they need an update?
  2170. if ($row['is_activated'] < 10)
  2171. {
  2172. $updates[($row['is_activated'] + 10)][] = $row['id_member'];
  2173. $newMembers[] = $row['id_member'];
  2174. }
  2175. }
  2176. }
  2177. $smcFunc['db_free_result']($request);
  2178. }
  2179. // We welcome our new members in the realm of the banned.
  2180. if (!empty($newMembers))
  2181. $smcFunc['db_query']('', '
  2182. DELETE FROM {db_prefix}log_online
  2183. WHERE id_member IN ({array_int:new_banned_members})',
  2184. array(
  2185. 'new_banned_members' => $newMembers,
  2186. )
  2187. );
  2188. // Find members that are wrongfully marked as banned.
  2189. $request = $smcFunc['db_query']('', '
  2190. SELECT mem.id_member, mem.is_activated - 10 AS new_value
  2191. FROM {db_prefix}members AS mem
  2192. LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
  2193. 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}))
  2194. WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
  2195. AND mem.is_activated >= {int:ban_flag}',
  2196. array(
  2197. 'cannot_access_activated' => 1,
  2198. 'current_time' => time(),
  2199. 'ban_flag' => 10,
  2200. )
  2201. );
  2202. while ($row = $smcFunc['db_fetch_assoc']($request))
  2203. {
  2204. // Don't do this twice!
  2205. if (!in_array($row['id_member'], $allMembers))
  2206. {
  2207. $updates[$row['new_value']][] = $row['id_member'];
  2208. $allMembers[] = $row['id_member'];
  2209. }
  2210. }
  2211. $smcFunc['db_free_result']($request);
  2212. if (!empty($updates))
  2213. foreach ($updates as $newStatus => $members)
  2214. updateMemberData($members, array('is_activated' => $newStatus));
  2215. // Update the latest member and our total members as banning may change them.
  2216. updateStats('member');
  2217. }
  2218. function getMemberData($id)
  2219. {
  2220. global $smcFunc;
  2221. $suggestions = array();
  2222. $request = $smcFunc['db_query']('', '
  2223. SELECT id_member, real_name, member_ip, email_address
  2224. FROM {db_prefix}members
  2225. WHERE id_member = {int:current_user}
  2226. LIMIT 1',
  2227. array(
  2228. 'current_user' => $id,
  2229. )
  2230. );
  2231. if ($smcFunc['db_num_rows']($request) > 0)
  2232. list ($suggestions['member']['id'], $suggestions['member']['name'], $suggestions['main_ip'], $suggestions['email']) = $smcFunc['db_fetch_row']($request);
  2233. $smcFunc['db_free_result']($request);
  2234. return $suggestions;
  2235. }
  2236. ?>