ManageBans.php 69 KB

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