PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/Security.php

https://github.com/Arantor/Elkarte
PHP | 1437 lines | 920 code | 173 blank | 344 comment | 245 complexity | edbe7f6e17fe9a09fcea69bfb8e6270d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file has the very important job of ensuring forum security.
  16. * This task includes banning and permissions, namely.
  17. *
  18. */
  19. if (!defined('ELKARTE'))
  20. die('No access...');
  21. /**
  22. * Check if the user is who he/she says he is
  23. * Makes sure the user is who they claim to be by requiring a password to be typed in every hour.
  24. * Is turned on and off by the securityDisable setting.
  25. * Uses the adminLogin() function of subs/Auth.subs.php if they need to login, which saves all request (post and get) data.
  26. *
  27. * @param string $type = admin
  28. */
  29. function validateSession($type = 'admin')
  30. {
  31. global $modSettings, $user_info, $sc, $user_settings;
  32. // We don't care if the option is off, because Guests should NEVER get past here.
  33. is_not_guest();
  34. // Validate what type of session check this is.
  35. $types = array();
  36. call_integration_hook('integrate_validateSession', array($types));
  37. $type = in_array($type, $types) || $type == 'moderate' ? $type : 'admin';
  38. // If we're using XML give an additional ten minutes grace as an admin can't log on in XML mode.
  39. $refreshTime = isset($_GET['xml']) ? 4200 : 3600;
  40. // Is the security option off?
  41. if (!empty($modSettings['securityDisable' . ($type != 'admin' ? '_' . $type : '')]))
  42. return;
  43. // Or are they already logged in?, Moderator or admin sesssion is need for this area
  44. if ((!empty($_SESSION[$type . '_time']) && $_SESSION[$type . '_time'] + $refreshTime >= time()) || (!empty($_SESSION['admin_time']) && $_SESSION['admin_time'] + $refreshTime >= time()))
  45. return;
  46. require_once(SUBSDIR . '/Auth.subs.php');
  47. // Hashed password, ahoy!
  48. if (isset($_POST[$type . '_hash_pass']) && strlen($_POST[$type . '_hash_pass']) == 40)
  49. {
  50. checkSession();
  51. $good_password = in_array(true, call_integration_hook('integrate_verify_password', array($user_info['username'], $_POST[$type . '_hash_pass'], true)), true);
  52. if ($good_password || $_POST[$type . '_hash_pass'] == sha1($user_info['passwd'] . $sc))
  53. {
  54. $_SESSION[$type . '_time'] = time();
  55. unset($_SESSION['request_referer']);
  56. return;
  57. }
  58. }
  59. // Posting the password... check it.
  60. if (isset($_POST[$type. '_pass']))
  61. {
  62. checkSession();
  63. $good_password = in_array(true, call_integration_hook('integrate_verify_password', array($user_info['username'], $_POST[$type . '_pass'], false)), true);
  64. // Password correct?
  65. if ($good_password || sha1(strtolower($user_info['username']) . $_POST[$type . '_pass']) == $user_info['passwd'])
  66. {
  67. $_SESSION[$type . '_time'] = time();
  68. unset($_SESSION['request_referer']);
  69. return;
  70. }
  71. }
  72. // OpenID?
  73. if (!empty($user_settings['openid_uri']))
  74. {
  75. require_once(SUBSDIR . '/OpenID.subs.php');
  76. openID_revalidate();
  77. $_SESSION[$type . '_time'] = time();
  78. unset($_SESSION['request_referer']);
  79. return;
  80. }
  81. // Better be sure to remember the real referer
  82. if (empty($_SESSION['request_referer']))
  83. $_SESSION['request_referer'] = isset($_SERVER['HTTP_REFERER']) ? @parse_url($_SERVER['HTTP_REFERER']) : array();
  84. elseif (empty($_POST))
  85. unset($_SESSION['request_referer']);
  86. // Need to type in a password for that, man.
  87. if (!isset($_GET['xml']))
  88. adminLogin($type);
  89. else
  90. return 'session_verify_fail';
  91. }
  92. /**
  93. * Require a user who is logged in. (not a guest.)
  94. * Checks if the user is currently a guest, and if so asks them to login with a message telling them why.
  95. * Message is what to tell them when asking them to login.
  96. *
  97. * @param string $message = ''
  98. */
  99. function is_not_guest($message = '')
  100. {
  101. global $user_info, $txt, $context, $scripturl;
  102. // Luckily, this person isn't a guest.
  103. if (isset($user_info['is_guest']) && !$user_info['is_guest'])
  104. return;
  105. // People always worry when they see people doing things they aren't actually doing...
  106. $_GET['action'] = '';
  107. $_GET['board'] = '';
  108. $_GET['topic'] = '';
  109. writeLog(true);
  110. // Just die.
  111. if (isset($_REQUEST['xml']))
  112. obExit(false);
  113. // Attempt to detect if they came from dlattach.
  114. if (ELKARTE != 'SSI' && empty($context['theme_loaded']))
  115. loadTheme();
  116. // Never redirect to an attachment
  117. if (strpos($_SERVER['REQUEST_URL'], 'dlattach') === false)
  118. $_SESSION['login_url'] = $_SERVER['REQUEST_URL'];
  119. // Load the Login template and language file.
  120. loadLanguage('Login');
  121. // Apparently we're not in a position to handle this now. Let's go to a safer location for now.
  122. if (empty($context['template_layers']))
  123. {
  124. $_SESSION['login_url'] = $scripturl . '?' . $_SERVER['QUERY_STRING'];
  125. redirectexit('action=login');
  126. }
  127. else
  128. {
  129. loadTemplate('Login');
  130. $context['sub_template'] = 'kick_guest';
  131. $context['robot_no_index'] = true;
  132. }
  133. // Use the kick_guest sub template...
  134. $context['kick_message'] = $message;
  135. $context['page_title'] = $txt['login'];
  136. obExit();
  137. // We should never get to this point, but if we did we wouldn't know the user isn't a guest.
  138. trigger_error('Hacking attempt...', E_USER_ERROR);
  139. }
  140. /**
  141. * Do banning related stuff. (ie. disallow access....)
  142. * Checks if the user is banned, and if so dies with an error.
  143. * Caches this information for optimization purposes.
  144. * Forces a recheck if force_check is true.
  145. *
  146. * @param bool $forceCheck = false
  147. */
  148. function is_not_banned($forceCheck = false)
  149. {
  150. global $txt, $modSettings, $context, $user_info;
  151. global $cookiename, $user_settings, $smcFunc;
  152. // You cannot be banned if you are an admin - doesn't help if you log out.
  153. if ($user_info['is_admin'])
  154. return;
  155. // Only check the ban every so often. (to reduce load.)
  156. if ($forceCheck || !isset($_SESSION['ban']) || empty($modSettings['banLastUpdated']) || ($_SESSION['ban']['last_checked'] < $modSettings['banLastUpdated']) || $_SESSION['ban']['id_member'] != $user_info['id'] || $_SESSION['ban']['ip'] != $user_info['ip'] || $_SESSION['ban']['ip2'] != $user_info['ip2'] || (isset($user_info['email'], $_SESSION['ban']['email']) && $_SESSION['ban']['email'] != $user_info['email']))
  157. {
  158. // Innocent until proven guilty. (but we know you are! :P)
  159. $_SESSION['ban'] = array(
  160. 'last_checked' => time(),
  161. 'id_member' => $user_info['id'],
  162. 'ip' => $user_info['ip'],
  163. 'ip2' => $user_info['ip2'],
  164. 'email' => $user_info['email'],
  165. );
  166. $ban_query = array();
  167. $ban_query_vars = array('current_time' => time());
  168. $flag_is_activated = false;
  169. // Check both IP addresses.
  170. foreach (array('ip', 'ip2') as $ip_number)
  171. {
  172. if ($ip_number == 'ip2' && $user_info['ip2'] == $user_info['ip'])
  173. continue;
  174. $ban_query[] = constructBanQueryIP($user_info[$ip_number]);
  175. // IP was valid, maybe there's also a hostname...
  176. if (empty($modSettings['disableHostnameLookup']) && $user_info[$ip_number] != 'unknown')
  177. {
  178. $hostname = host_from_ip($user_info[$ip_number]);
  179. if (strlen($hostname) > 0)
  180. {
  181. $ban_query[] = '({string:hostname} LIKE bi.hostname)';
  182. $ban_query_vars['hostname'] = $hostname;
  183. }
  184. }
  185. }
  186. // Is their email address banned?
  187. if (strlen($user_info['email']) != 0)
  188. {
  189. $ban_query[] = '({string:email} LIKE bi.email_address)';
  190. $ban_query_vars['email'] = $user_info['email'];
  191. }
  192. // How about this user?
  193. if (!$user_info['is_guest'] && !empty($user_info['id']))
  194. {
  195. $ban_query[] = 'bi.id_member = {int:id_member}';
  196. $ban_query_vars['id_member'] = $user_info['id'];
  197. }
  198. // Check the ban, if there's information.
  199. if (!empty($ban_query))
  200. {
  201. $restrictions = array(
  202. 'cannot_access',
  203. 'cannot_login',
  204. 'cannot_post',
  205. 'cannot_register',
  206. );
  207. $request = $smcFunc['db_query']('', '
  208. SELECT bi.id_ban, bi.email_address, bi.id_member, bg.cannot_access, bg.cannot_register,
  209. bg.cannot_post, bg.cannot_login, bg.reason, IFNULL(bg.expire_time, 0) AS expire_time
  210. FROM {db_prefix}ban_items AS bi
  211. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time}))
  212. WHERE
  213. (' . implode(' OR ', $ban_query) . ')',
  214. $ban_query_vars
  215. );
  216. // Store every type of ban that applies to you in your session.
  217. while ($row = $smcFunc['db_fetch_assoc']($request))
  218. {
  219. foreach ($restrictions as $restriction)
  220. if (!empty($row[$restriction]))
  221. {
  222. $_SESSION['ban'][$restriction]['reason'] = $row['reason'];
  223. $_SESSION['ban'][$restriction]['ids'][] = $row['id_ban'];
  224. if (!isset($_SESSION['ban']['expire_time']) || ($_SESSION['ban']['expire_time'] != 0 && ($row['expire_time'] == 0 || $row['expire_time'] > $_SESSION['ban']['expire_time'])))
  225. $_SESSION['ban']['expire_time'] = $row['expire_time'];
  226. if (!$user_info['is_guest'] && $restriction == 'cannot_access' && ($row['id_member'] == $user_info['id'] || $row['email_address'] == $user_info['email']))
  227. $flag_is_activated = true;
  228. }
  229. }
  230. $smcFunc['db_free_result']($request);
  231. }
  232. // Mark the cannot_access and cannot_post bans as being 'hit'.
  233. if (isset($_SESSION['ban']['cannot_access']) || isset($_SESSION['ban']['cannot_post']) || isset($_SESSION['ban']['cannot_login']))
  234. log_ban(array_merge(isset($_SESSION['ban']['cannot_access']) ? $_SESSION['ban']['cannot_access']['ids'] : array(), isset($_SESSION['ban']['cannot_post']) ? $_SESSION['ban']['cannot_post']['ids'] : array(), isset($_SESSION['ban']['cannot_login']) ? $_SESSION['ban']['cannot_login']['ids'] : array()));
  235. // If for whatever reason the is_activated flag seems wrong, do a little work to clear it up.
  236. if ($user_info['id'] && (($user_settings['is_activated'] >= 10 && !$flag_is_activated)
  237. || ($user_settings['is_activated'] < 10 && $flag_is_activated)))
  238. {
  239. require_once(ADMINDIR . '/ManageBans.php');
  240. updateBanMembers();
  241. }
  242. }
  243. // Hey, I know you! You're ehm...
  244. if (!isset($_SESSION['ban']['cannot_access']) && !empty($_COOKIE[$cookiename . '_']))
  245. {
  246. $bans = explode(',', $_COOKIE[$cookiename . '_']);
  247. foreach ($bans as $key => $value)
  248. $bans[$key] = (int) $value;
  249. $request = $smcFunc['db_query']('', '
  250. SELECT bi.id_ban, bg.reason
  251. FROM {db_prefix}ban_items AS bi
  252. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  253. WHERE bi.id_ban IN ({array_int:ban_list})
  254. AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})
  255. AND bg.cannot_access = {int:cannot_access}
  256. LIMIT ' . count($bans),
  257. array(
  258. 'cannot_access' => 1,
  259. 'ban_list' => $bans,
  260. 'current_time' => time(),
  261. )
  262. );
  263. while ($row = $smcFunc['db_fetch_assoc']($request))
  264. {
  265. $_SESSION['ban']['cannot_access']['ids'][] = $row['id_ban'];
  266. $_SESSION['ban']['cannot_access']['reason'] = $row['reason'];
  267. }
  268. $smcFunc['db_free_result']($request);
  269. // My mistake. Next time better.
  270. if (!isset($_SESSION['ban']['cannot_access']))
  271. {
  272. require_once(SUBSDIR . '/Auth.subs.php');
  273. $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
  274. elk_setcookie($cookiename . '_', '', time() - 3600, $cookie_url[1], $cookie_url[0], false, false);
  275. }
  276. }
  277. // If you're fully banned, it's end of the story for you.
  278. if (isset($_SESSION['ban']['cannot_access']))
  279. {
  280. // We don't wanna see you!
  281. if (!$user_info['is_guest'])
  282. $smcFunc['db_query']('', '
  283. DELETE FROM {db_prefix}log_online
  284. WHERE id_member = {int:current_member}',
  285. array(
  286. 'current_member' => $user_info['id'],
  287. )
  288. );
  289. // 'Log' the user out. Can't have any funny business... (save the name!)
  290. $old_name = isset($user_info['name']) && $user_info['name'] != '' ? $user_info['name'] : $txt['guest_title'];
  291. $user_info['name'] = '';
  292. $user_info['username'] = '';
  293. $user_info['is_guest'] = true;
  294. $user_info['is_admin'] = false;
  295. $user_info['permissions'] = array();
  296. $user_info['id'] = 0;
  297. $context['user'] = array(
  298. 'id' => 0,
  299. 'username' => '',
  300. 'name' => $txt['guest_title'],
  301. 'is_guest' => true,
  302. 'is_logged' => false,
  303. 'is_admin' => false,
  304. 'is_mod' => false,
  305. 'can_mod' => false,
  306. 'language' => $user_info['language'],
  307. );
  308. // A goodbye present.
  309. require_once(SUBSDIR . '/Auth.subs.php');
  310. $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
  311. elk_setcookie($cookiename . '_', implode(',', $_SESSION['ban']['cannot_access']['ids']), time() + 3153600, $cookie_url[1], $cookie_url[0], false, false);
  312. // Don't scare anyone, now.
  313. $_GET['action'] = '';
  314. $_GET['board'] = '';
  315. $_GET['topic'] = '';
  316. writeLog(true);
  317. // You banned, sucka!
  318. fatal_error(sprintf($txt['your_ban'], $old_name) . (empty($_SESSION['ban']['cannot_access']['reason']) ? '' : '<br />' . $_SESSION['ban']['cannot_access']['reason']) . '<br />' . (!empty($_SESSION['ban']['expire_time']) ? sprintf($txt['your_ban_expires'], timeformat($_SESSION['ban']['expire_time'], false)) : $txt['your_ban_expires_never']), 'user');
  319. // If we get here, something's gone wrong.... but let's try anyway.
  320. trigger_error('Hacking attempt...', E_USER_ERROR);
  321. }
  322. // You're not allowed to log in but yet you are. Let's fix that.
  323. elseif (isset($_SESSION['ban']['cannot_login']) && !$user_info['is_guest'])
  324. {
  325. // We don't wanna see you!
  326. $smcFunc['db_query']('', '
  327. DELETE FROM {db_prefix}log_online
  328. WHERE id_member = {int:current_member}',
  329. array(
  330. 'current_member' => $user_info['id'],
  331. )
  332. );
  333. // 'Log' the user out. Can't have any funny business... (save the name!)
  334. $old_name = isset($user_info['name']) && $user_info['name'] != '' ? $user_info['name'] : $txt['guest_title'];
  335. $user_info['name'] = '';
  336. $user_info['username'] = '';
  337. $user_info['is_guest'] = true;
  338. $user_info['is_admin'] = false;
  339. $user_info['permissions'] = array();
  340. $user_info['id'] = 0;
  341. $context['user'] = array(
  342. 'id' => 0,
  343. 'username' => '',
  344. 'name' => $txt['guest_title'],
  345. 'is_guest' => true,
  346. 'is_logged' => false,
  347. 'is_admin' => false,
  348. 'is_mod' => false,
  349. 'can_mod' => false,
  350. 'language' => $user_info['language'],
  351. );
  352. // Wipe 'n Clean(r) erases all traces.
  353. $_GET['action'] = '';
  354. $_GET['board'] = '';
  355. $_GET['topic'] = '';
  356. writeLog(true);
  357. require_once(CONTROLLERDIR . '/LogInOut.controller.php');
  358. Logout(true, false);
  359. fatal_error(sprintf($txt['your_ban'], $old_name) . (empty($_SESSION['ban']['cannot_login']['reason']) ? '' : '<br />' . $_SESSION['ban']['cannot_login']['reason']) . '<br />' . (!empty($_SESSION['ban']['expire_time']) ? sprintf($txt['your_ban_expires'], timeformat($_SESSION['ban']['expire_time'], false)) : $txt['your_ban_expires_never']) . '<br />' . $txt['ban_continue_browse'], 'user');
  360. }
  361. // Fix up the banning permissions.
  362. if (isset($user_info['permissions']))
  363. banPermissions();
  364. }
  365. /**
  366. * Fix permissions according to ban status.
  367. * Applies any states of banning by removing permissions the user cannot have.
  368. */
  369. function banPermissions()
  370. {
  371. global $user_info, $modSettings, $context;
  372. // Somehow they got here, at least take away all permissions...
  373. if (isset($_SESSION['ban']['cannot_access']))
  374. $user_info['permissions'] = array();
  375. // Okay, well, you can watch, but don't touch a thing.
  376. elseif (isset($_SESSION['ban']['cannot_post']) || (!empty($modSettings['warning_mute']) && $modSettings['warning_mute'] <= $user_info['warning']))
  377. {
  378. $denied_permissions = array(
  379. 'pm_send',
  380. 'calendar_post', 'calendar_edit_own', 'calendar_edit_any',
  381. 'poll_post',
  382. 'poll_add_own', 'poll_add_any',
  383. 'poll_edit_own', 'poll_edit_any',
  384. 'poll_lock_own', 'poll_lock_any',
  385. 'poll_remove_own', 'poll_remove_any',
  386. 'manage_attachments', 'manage_smileys', 'manage_boards', 'admin_forum', 'manage_permissions',
  387. 'moderate_forum', 'manage_membergroups', 'manage_bans', 'send_mail', 'edit_news',
  388. 'profile_identity_any', 'profile_extra_any', 'profile_title_any',
  389. 'post_new', 'post_reply_own', 'post_reply_any',
  390. 'delete_own', 'delete_any', 'delete_replies',
  391. 'make_sticky',
  392. 'merge_any', 'split_any',
  393. 'modify_own', 'modify_any', 'modify_replies',
  394. 'move_any',
  395. 'send_topic',
  396. 'lock_own', 'lock_any',
  397. 'remove_own', 'remove_any',
  398. 'post_unapproved_topics', 'post_unapproved_replies_own', 'post_unapproved_replies_any',
  399. );
  400. call_integration_hook('integrate_post_ban_permissions', array($denied_permissions));
  401. $user_info['permissions'] = array_diff($user_info['permissions'], $denied_permissions);
  402. }
  403. // Are they absolutely under moderation?
  404. elseif (!empty($modSettings['warning_moderate']) && $modSettings['warning_moderate'] <= $user_info['warning'])
  405. {
  406. // Work out what permissions should change...
  407. $permission_change = array(
  408. 'post_new' => 'post_unapproved_topics',
  409. 'post_reply_own' => 'post_unapproved_replies_own',
  410. 'post_reply_any' => 'post_unapproved_replies_any',
  411. 'post_attachment' => 'post_unapproved_attachments',
  412. );
  413. call_integration_hook('integrate_warn_permissions', array($permission_change));
  414. foreach ($permission_change as $old => $new)
  415. {
  416. if (!in_array($old, $user_info['permissions']))
  417. unset($permission_change[$old]);
  418. else
  419. $user_info['permissions'][] = $new;
  420. }
  421. $user_info['permissions'] = array_diff($user_info['permissions'], array_keys($permission_change));
  422. }
  423. // @todo Find a better place to call this? Needs to be after permissions loaded!
  424. // Finally, some bits we cache in the session because it saves queries.
  425. if (isset($_SESSION['mc']) && $_SESSION['mc']['time'] > $modSettings['settings_updated'] && $_SESSION['mc']['id'] == $user_info['id'])
  426. $user_info['mod_cache'] = $_SESSION['mc'];
  427. else
  428. {
  429. require_once(SUBSDIR . '/Auth.subs.php');
  430. rebuildModCache();
  431. }
  432. // Now that we have the mod cache taken care of lets setup a cache for the number of mod reports still open
  433. if (isset($_SESSION['rc']) && $_SESSION['rc']['time'] > $modSettings['last_mod_report_action'] && $_SESSION['rc']['id'] == $user_info['id'])
  434. $context['open_mod_reports'] = $_SESSION['rc']['reports'];
  435. elseif ($_SESSION['mc']['bq'] != '0=1')
  436. {
  437. require_once(SUBSDIR . '/Moderation.subs.php');
  438. recountOpenReports();
  439. }
  440. else
  441. $context['open_mod_reports'] = 0;
  442. }
  443. /**
  444. * Log a ban in the database.
  445. * Log the current user in the ban logs.
  446. * Increment the hit counters for the specified ban ID's (if any.)
  447. *
  448. * @param array $ban_ids = array()
  449. * @param string $email = null
  450. */
  451. function log_ban($ban_ids = array(), $email = null)
  452. {
  453. global $user_info, $smcFunc;
  454. // Don't log web accelerators, it's very confusing...
  455. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  456. return;
  457. $smcFunc['db_insert']('',
  458. '{db_prefix}log_banned',
  459. array('id_member' => 'int', 'ip' => 'string-16', 'email' => 'string', 'log_time' => 'int'),
  460. array($user_info['id'], $user_info['ip'], ($email === null ? ($user_info['is_guest'] ? '' : $user_info['email']) : $email), time()),
  461. array('id_ban_log')
  462. );
  463. // One extra point for these bans.
  464. if (!empty($ban_ids))
  465. $smcFunc['db_query']('', '
  466. UPDATE {db_prefix}ban_items
  467. SET hits = hits + 1
  468. WHERE id_ban IN ({array_int:ban_ids})',
  469. array(
  470. 'ban_ids' => $ban_ids,
  471. )
  472. );
  473. }
  474. /**
  475. * Checks if a given email address might be banned.
  476. * Check if a given email is banned.
  477. * Performs an immediate ban if the turns turns out positive.
  478. *
  479. * @param string $email
  480. * @param string $restriction
  481. * @param string $error
  482. */
  483. function isBannedEmail($email, $restriction, $error)
  484. {
  485. global $txt, $smcFunc;
  486. // Can't ban an empty email
  487. if (empty($email) || trim($email) == '')
  488. return;
  489. // Let's start with the bans based on your IP/hostname/memberID...
  490. $ban_ids = isset($_SESSION['ban'][$restriction]) ? $_SESSION['ban'][$restriction]['ids'] : array();
  491. $ban_reason = isset($_SESSION['ban'][$restriction]) ? $_SESSION['ban'][$restriction]['reason'] : '';
  492. // ...and add to that the email address you're trying to register.
  493. $request = $smcFunc['db_query']('', '
  494. SELECT bi.id_ban, bg.' . $restriction . ', bg.cannot_access, bg.reason
  495. FROM {db_prefix}ban_items AS bi
  496. INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
  497. WHERE {string:email} LIKE bi.email_address
  498. AND (bg.' . $restriction . ' = {int:cannot_access} OR bg.cannot_access = {int:cannot_access})
  499. AND (bg.expire_time IS NULL OR bg.expire_time >= {int:now})',
  500. array(
  501. 'email' => $email,
  502. 'cannot_access' => 1,
  503. 'now' => time(),
  504. )
  505. );
  506. while ($row = $smcFunc['db_fetch_assoc']($request))
  507. {
  508. if (!empty($row['cannot_access']))
  509. {
  510. $_SESSION['ban']['cannot_access']['ids'][] = $row['id_ban'];
  511. $_SESSION['ban']['cannot_access']['reason'] = $row['reason'];
  512. }
  513. if (!empty($row[$restriction]))
  514. {
  515. $ban_ids[] = $row['id_ban'];
  516. $ban_reason = $row['reason'];
  517. }
  518. }
  519. $smcFunc['db_free_result']($request);
  520. // You're in biiig trouble. Banned for the rest of this session!
  521. if (isset($_SESSION['ban']['cannot_access']))
  522. {
  523. log_ban($_SESSION['ban']['cannot_access']['ids']);
  524. $_SESSION['ban']['last_checked'] = time();
  525. fatal_error(sprintf($txt['your_ban'], $txt['guest_title']) . $_SESSION['ban']['cannot_access']['reason'], false);
  526. }
  527. if (!empty($ban_ids))
  528. {
  529. // Log this ban for future reference.
  530. log_ban($ban_ids, $email);
  531. fatal_error($error . $ban_reason, false);
  532. }
  533. }
  534. /**
  535. * Make sure the user's correct session was passed, and they came from here.
  536. * Checks the current session, verifying that the person is who he or she should be.
  537. * Also checks the referrer to make sure they didn't get sent here.
  538. * Depends on the disableCheckUA setting, which is usually missing.
  539. * Will check GET, POST, or REQUEST depending on the passed type.
  540. * Also optionally checks the referring action if passed. (note that the referring action must be by GET.)
  541. *
  542. * @param string $type = 'post' (post, get, request)
  543. * @param string $from_action = ''
  544. * @param bool $is_fatal = true
  545. * @return string the error message if is_fatal is false.
  546. */
  547. function checkSession($type = 'post', $from_action = '', $is_fatal = true)
  548. {
  549. global $sc, $modSettings, $boardurl;
  550. // Is it in as $_POST['sc']?
  551. if ($type == 'post')
  552. {
  553. $check = isset($_POST[$_SESSION['session_var']]) ? $_POST[$_SESSION['session_var']] : (empty($modSettings['strictSessionCheck']) && isset($_POST['sc']) ? $_POST['sc'] : null);
  554. if ($check !== $sc)
  555. $error = 'session_timeout';
  556. }
  557. // How about $_GET['sesc']?
  558. elseif ($type === 'get')
  559. {
  560. $check = isset($_GET[$_SESSION['session_var']]) ? $_GET[$_SESSION['session_var']] : (empty($modSettings['strictSessionCheck']) && isset($_GET['sesc']) ? $_GET['sesc'] : null);
  561. if ($check !== $sc)
  562. $error = 'session_verify_fail';
  563. }
  564. // Or can it be in either?
  565. elseif ($type == 'request')
  566. {
  567. $check = isset($_GET[$_SESSION['session_var']]) ? $_GET[$_SESSION['session_var']] : (empty($modSettings['strictSessionCheck']) && isset($_GET['sesc']) ? $_GET['sesc'] : (isset($_POST[$_SESSION['session_var']]) ? $_POST[$_SESSION['session_var']] : (empty($modSettings['strictSessionCheck']) && isset($_POST['sc']) ? $_POST['sc'] : null)));
  568. if ($check !== $sc)
  569. $error = 'session_verify_fail';
  570. }
  571. // Verify that they aren't changing user agents on us - that could be bad.
  572. if ((!isset($_SESSION['USER_AGENT']) || $_SESSION['USER_AGENT'] != $_SERVER['HTTP_USER_AGENT']) && empty($modSettings['disableCheckUA']))
  573. $error = 'session_verify_fail';
  574. // Make sure a page with session check requirement is not being prefetched.
  575. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  576. {
  577. ob_end_clean();
  578. header('HTTP/1.1 403 Forbidden');
  579. die;
  580. }
  581. // Check the referring site - it should be the same server at least!
  582. if (isset($_SESSION['request_referer']))
  583. $referrer = $_SESSION['request_referer'];
  584. else
  585. $referrer = isset($_SERVER['HTTP_REFERER']) ? @parse_url($_SERVER['HTTP_REFERER']) : array();
  586. if (!empty($referrer['host']))
  587. {
  588. if (strpos($_SERVER['HTTP_HOST'], ':') !== false)
  589. $real_host = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], ':'));
  590. else
  591. $real_host = $_SERVER['HTTP_HOST'];
  592. $parsed_url = parse_url($boardurl);
  593. // Are global cookies on? If so, let's check them ;).
  594. if (!empty($modSettings['globalCookies']))
  595. {
  596. if (preg_match('~(?:[^\.]+\.)?([^\.]{3,}\..+)\z~i', $parsed_url['host'], $parts) == 1)
  597. $parsed_url['host'] = $parts[1];
  598. if (preg_match('~(?:[^\.]+\.)?([^\.]{3,}\..+)\z~i', $referrer['host'], $parts) == 1)
  599. $referrer['host'] = $parts[1];
  600. if (preg_match('~(?:[^\.]+\.)?([^\.]{3,}\..+)\z~i', $real_host, $parts) == 1)
  601. $real_host = $parts[1];
  602. }
  603. // Okay: referrer must either match parsed_url or real_host.
  604. if (isset($parsed_url['host']) && strtolower($referrer['host']) != strtolower($parsed_url['host']) && strtolower($referrer['host']) != strtolower($real_host))
  605. {
  606. $error = 'verify_url_fail';
  607. $log_error = true;
  608. }
  609. }
  610. // Well, first of all, if a from_action is specified you'd better have an old_url.
  611. if (!empty($from_action) && (!isset($_SESSION['old_url']) || preg_match('~[?;&]action=' . $from_action . '([;&]|$)~', $_SESSION['old_url']) == 0))
  612. {
  613. $error = 'verify_url_fail';
  614. $log_error = true;
  615. }
  616. if (strtolower($_SERVER['HTTP_USER_AGENT']) == 'hacker')
  617. fatal_error('Sound the alarm! It\'s a hacker! Close the castle gates!!', false);
  618. // Everything is ok, return an empty string.
  619. if (!isset($error))
  620. return '';
  621. // A session error occurred, show the error.
  622. elseif ($is_fatal)
  623. {
  624. if (isset($_GET['xml']))
  625. {
  626. ob_end_clean();
  627. header('HTTP/1.1 403 Forbidden - Session timeout');
  628. die;
  629. }
  630. else
  631. fatal_lang_error($error, isset($log_error) ? 'user' : false);
  632. }
  633. // A session error occurred, return the error to the calling function.
  634. else
  635. return $error;
  636. // We really should never fall through here, for very important reasons. Let's make sure.
  637. trigger_error('Hacking attempt...', E_USER_ERROR);
  638. }
  639. /**
  640. * Check if a specific confirm parameter was given.
  641. *
  642. * @param string $action
  643. */
  644. function checkConfirm($action)
  645. {
  646. global $modSettings;
  647. if (isset($_GET['confirm']) && isset($_SESSION['confirm_' . $action]) && md5($_GET['confirm'] . $_SERVER['HTTP_USER_AGENT']) === $_SESSION['confirm_' . $action])
  648. return true;
  649. else
  650. {
  651. $token = md5(mt_rand() . session_id() . (string) microtime() . $modSettings['rand_seed']);
  652. $_SESSION['confirm_' . $action] = md5($token . $_SERVER['HTTP_USER_AGENT']);
  653. return $token;
  654. }
  655. }
  656. /**
  657. * Lets give you a token of our appreciation.
  658. *
  659. * @param string $action
  660. * @param string $type = 'post'
  661. * @return array
  662. */
  663. function createToken($action, $type = 'post')
  664. {
  665. global $modSettings, $context;
  666. $token = md5(mt_rand() . session_id() . (string) microtime() . $modSettings['rand_seed'] . $type);
  667. $token_var = substr(preg_replace('~^\d+~', '', md5(mt_rand() . (string) microtime() . mt_rand())), 0, rand(7, 12));
  668. $_SESSION['token'][$type . '-' . $action] = array($token_var, md5($token . $_SERVER['HTTP_USER_AGENT']), time(), $token);
  669. $context[$action . '_token'] = $token;
  670. $context[$action . '_token_var'] = $token_var;
  671. return array($action . '_token_var' => $token_var, $action . '_token' => $token);
  672. }
  673. /**
  674. * Only patrons with valid tokens can ride this ride.
  675. *
  676. * @param string $action
  677. * @param string $type = 'post' (get, request, or post)
  678. * @param bool $reset = true
  679. * @return boolean
  680. */
  681. function validateToken($action, $type = 'post', $reset = true)
  682. {
  683. global $modSettings;
  684. $type = $type == 'get' || $type == 'request' ? $type : 'post';
  685. // Logins are special: the token is used to has the password with javascript before POST it
  686. if ($action == 'login')
  687. {
  688. if (isset($_SESSION['token'][$type . '-' . $action]))
  689. {
  690. $return = $_SESSION['token'][$type . '-' . $action][3];
  691. unset($_SESSION['token'][$type . '-' . $action]);
  692. return $return;
  693. }
  694. else
  695. return '';
  696. }
  697. // This nasty piece of code validates a token.
  698. /*
  699. 1. The token exists in session.
  700. 2. The {$type} variable should exist.
  701. 3. We concat the variable we received with the user agent
  702. 4. Match that result against what is in the session.
  703. 5. If it matchs, success, otherwise we fallout.
  704. */
  705. if (isset($_SESSION['token'][$type . '-' . $action], $GLOBALS['_' . strtoupper($type)][$_SESSION['token'][$type . '-' . $action][0]]) && md5($GLOBALS['_' . strtoupper($type)][$_SESSION['token'][$type . '-' . $action][0]] . $_SERVER['HTTP_USER_AGENT']) === $_SESSION['token'][$type . '-' . $action][1])
  706. {
  707. // Invalidate this token now.
  708. unset($_SESSION['token'][$type . '-' . $action]);
  709. return true;
  710. }
  711. // Patrons with invalid tokens get the boot.
  712. if ($reset)
  713. {
  714. // Might as well do some cleanup on this.
  715. cleanTokens();
  716. // I'm back baby.
  717. createToken($action, $type);
  718. fatal_lang_error('token_verify_fail', false);
  719. }
  720. // Remove this token as its useless
  721. else
  722. unset($_SESSION['token'][$type . '-' . $action]);
  723. // Randomly check if we should remove some older tokens.
  724. if (mt_rand(0, 138) == 23)
  725. cleanTokens();
  726. return false;
  727. }
  728. /**
  729. * Removes old unused tokens from session
  730. * defaults to 3 hours before a token is considered expired
  731. * if $complete = true will remove all tokens
  732. *
  733. * @param bool $complete = false
  734. */
  735. function cleanTokens($complete = false)
  736. {
  737. // We appreciate cleaning up after yourselves.
  738. if (!isset($_SESSION['token']))
  739. return;
  740. // Clean up tokens, trying to give enough time still.
  741. foreach ($_SESSION['token'] as $key => $data)
  742. if ($data[2] + 10800 < time() || $complete)
  743. unset($_SESSION['token'][$key]);
  744. }
  745. /**
  746. * Check whether a form has been submitted twice.
  747. * Registers a sequence number for a form.
  748. * Checks whether a submitted sequence number is registered in the current session.
  749. * Depending on the value of is_fatal shows an error or returns true or false.
  750. * Frees a sequence number from the stack after it's been checked.
  751. * Frees a sequence number without checking if action == 'free'.
  752. *
  753. * @param string $action
  754. * @param bool $is_fatal = true
  755. * @return boolean
  756. */
  757. function checkSubmitOnce($action, $is_fatal = true)
  758. {
  759. global $context;
  760. if (!isset($_SESSION['forms']))
  761. $_SESSION['forms'] = array();
  762. // Register a form number and store it in the session stack. (use this on the page that has the form.)
  763. if ($action == 'register')
  764. {
  765. $context['form_sequence_number'] = 0;
  766. while (empty($context['form_sequence_number']) || in_array($context['form_sequence_number'], $_SESSION['forms']))
  767. $context['form_sequence_number'] = mt_rand(1, 16000000);
  768. }
  769. // Check whether the submitted number can be found in the session.
  770. elseif ($action == 'check')
  771. {
  772. if (!isset($_REQUEST['seqnum']))
  773. return true;
  774. elseif (!in_array($_REQUEST['seqnum'], $_SESSION['forms']))
  775. {
  776. $_SESSION['forms'][] = (int) $_REQUEST['seqnum'];
  777. return true;
  778. }
  779. elseif ($is_fatal)
  780. fatal_lang_error('error_form_already_submitted', false);
  781. else
  782. return false;
  783. }
  784. // Don't check, just free the stack number.
  785. elseif ($action == 'free' && isset($_REQUEST['seqnum']) && in_array($_REQUEST['seqnum'], $_SESSION['forms']))
  786. $_SESSION['forms'] = array_diff($_SESSION['forms'], array($_REQUEST['seqnum']));
  787. elseif ($action != 'free')
  788. trigger_error('checkSubmitOnce(): Invalid action \'' . $action . '\'', E_USER_WARNING);
  789. }
  790. /**
  791. * Check the user's permissions.
  792. * checks whether the user is allowed to do permission. (ie. post_new.)
  793. * If boards is specified, checks those boards instead of the current one.
  794. * Always returns true if the user is an administrator.
  795. *
  796. * @param string $permission
  797. * @param array $boards = null
  798. * @return boolean if the user can do the permission
  799. */
  800. function allowedTo($permission, $boards = null)
  801. {
  802. global $user_info, $modSettings, $smcFunc;
  803. // You're always allowed to do nothing. (unless you're a working man, MR. LAZY :P!)
  804. if (empty($permission))
  805. return true;
  806. // You're never allowed to do something if your data hasn't been loaded yet!
  807. if (empty($user_info))
  808. return false;
  809. // Administrators are supermen :P.
  810. if ($user_info['is_admin'])
  811. return true;
  812. // Are we checking the _current_ board, or some other boards?
  813. if ($boards === null)
  814. {
  815. // Check if they can do it.
  816. if (!is_array($permission) && in_array($permission, $user_info['permissions']))
  817. return true;
  818. // Search for any of a list of permissions.
  819. elseif (is_array($permission) && count(array_intersect($permission, $user_info['permissions'])) != 0)
  820. return true;
  821. // You aren't allowed, by default.
  822. else
  823. return false;
  824. }
  825. elseif (!is_array($boards))
  826. $boards = array($boards);
  827. $request = $smcFunc['db_query']('', '
  828. SELECT MIN(bp.add_deny) AS add_deny
  829. FROM {db_prefix}boards AS b
  830. INNER JOIN {db_prefix}board_permissions AS bp ON (bp.id_profile = b.id_profile)
  831. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board AND mods.id_member = {int:current_member})
  832. WHERE b.id_board IN ({array_int:board_list})
  833. AND bp.id_group IN ({array_int:group_list}, {int:moderator_group})
  834. AND bp.permission {raw:permission_list}
  835. AND (mods.id_member IS NOT NULL OR bp.id_group != {int:moderator_group})
  836. GROUP BY b.id_board',
  837. array(
  838. 'current_member' => $user_info['id'],
  839. 'board_list' => $boards,
  840. 'group_list' => $user_info['groups'],
  841. 'moderator_group' => 3,
  842. 'permission_list' => (is_array($permission) ? 'IN (\'' . implode('\', \'', $permission) . '\')' : ' = \'' . $permission . '\''),
  843. )
  844. );
  845. // Make sure they can do it on all of the boards.
  846. if ($smcFunc['db_num_rows']($request) != count($boards))
  847. return false;
  848. $result = true;
  849. while ($row = $smcFunc['db_fetch_assoc']($request))
  850. $result &= !empty($row['add_deny']);
  851. $smcFunc['db_free_result']($request);
  852. // If the query returned 1, they can do it... otherwise, they can't.
  853. return $result;
  854. }
  855. /**
  856. * Fatal error if they cannot.
  857. * Uses allowedTo() to check if the user is allowed to do permission.
  858. * Checks the passed boards or current board for the permission.
  859. * If they are not, it loads the Errors language file and shows an error using $txt['cannot_' . $permission].
  860. * If they are a guest and cannot do it, this calls is_not_guest().
  861. *
  862. * @param string $permission
  863. * @param array $boards = null
  864. */
  865. function isAllowedTo($permission, $boards = null)
  866. {
  867. global $user_info, $txt;
  868. static $heavy_permissions = array(
  869. 'admin_forum',
  870. 'manage_attachments',
  871. 'manage_smileys',
  872. 'manage_boards',
  873. 'edit_news',
  874. 'moderate_forum',
  875. 'manage_bans',
  876. 'manage_membergroups',
  877. 'manage_permissions',
  878. );
  879. // Make it an array, even if a string was passed.
  880. $permission = is_array($permission) ? $permission : array($permission);
  881. // Check the permission and return an error...
  882. if (!allowedTo($permission, $boards))
  883. {
  884. // Pick the last array entry as the permission shown as the error.
  885. $error_permission = array_shift($permission);
  886. // If they are a guest, show a login. (because the error might be gone if they do!)
  887. if ($user_info['is_guest'])
  888. {
  889. loadLanguage('Errors');
  890. is_not_guest($txt['cannot_' . $error_permission]);
  891. }
  892. // Clear the action because they aren't really doing that!
  893. $_GET['action'] = '';
  894. $_GET['board'] = '';
  895. $_GET['topic'] = '';
  896. writeLog(true);
  897. fatal_lang_error('cannot_' . $error_permission, false);
  898. // Getting this far is a really big problem, but let's try our best to prevent any cases...
  899. trigger_error('Hacking attempt...', E_USER_ERROR);
  900. }
  901. // If you're doing something on behalf of some "heavy" permissions, validate your session.
  902. // (take out the heavy permissions, and if you can't do anything but those, you need a validated session.)
  903. if (!allowedTo(array_diff($permission, $heavy_permissions), $boards))
  904. validateSession();
  905. }
  906. /**
  907. * Return the boards a user has a certain (board) permission on. (array(0) if all.)
  908. * - returns a list of boards on which the user is allowed to do the specified permission.
  909. * - returns an array with only a 0 in it if the user has permission to do this on every board.
  910. * - returns an empty array if he or she cannot do this on any board.
  911. * If check_access is true will also make sure the group has proper access to that board.
  912. *
  913. * @param array $permissions
  914. * @param bool $check_access = true
  915. * @param bool $simple = true
  916. */
  917. function boardsAllowedTo($permissions, $check_access = true, $simple = true)
  918. {
  919. global $user_info, $modSettings, $smcFunc;
  920. // Arrays are nice, most of the time.
  921. if (!is_array($permissions))
  922. $permissions = array($permissions);
  923. /*
  924. * Set $simple to true to use this function in compatability mode
  925. * Otherwise, the resultant array becomes split into the multiple
  926. * permissions that were passed. Other than that, it's just the normal
  927. * state of play that you're used to.
  928. */
  929. // Administrators are all powerful, sorry.
  930. if ($user_info['is_admin'])
  931. {
  932. if ($simple)
  933. return array(0);
  934. else
  935. {
  936. $boards = array();
  937. foreach ($permissions as $permission)
  938. $boards[$permission] = array(0);
  939. return $boards;
  940. }
  941. }
  942. // All groups the user is in except 'moderator'.
  943. $groups = array_diff($user_info['groups'], array(3));
  944. $request = $smcFunc['db_query']('', '
  945. SELECT b.id_board, bp.add_deny' . ($simple ? '' : ', bp.permission') . '
  946. FROM {db_prefix}board_permissions AS bp
  947. INNER JOIN {db_prefix}boards AS b ON (b.id_profile = bp.id_profile)
  948. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board AND mods.id_member = {int:current_member})
  949. WHERE bp.id_group IN ({array_int:group_list}, {int:moderator_group})
  950. AND bp.permission IN ({array_string:permissions})
  951. AND (mods.id_member IS NOT NULL OR bp.id_group != {int:moderator_group})' .
  952. ($check_access ? ' AND {query_see_board}' : ''),
  953. array(
  954. 'current_member' => $user_info['id'],
  955. 'group_list' => $groups,
  956. 'moderator_group' => 3,
  957. 'permissions' => $permissions,
  958. )
  959. );
  960. $boards = array();
  961. $deny_boards = array();
  962. while ($row = $smcFunc['db_fetch_assoc']($request))
  963. {
  964. if ($simple)
  965. {
  966. if (empty($row['add_deny']))
  967. $deny_boards[] = $row['id_board'];
  968. else
  969. $boards[] = $row['id_board'];
  970. }
  971. else
  972. {
  973. if (empty($row['add_deny']))
  974. $deny_boards[$row['permission']][] = $row['id_board'];
  975. else
  976. $boards[$row['permission']][] = $row['id_board'];
  977. }
  978. }
  979. $smcFunc['db_free_result']($request);
  980. if ($simple)
  981. $boards = array_unique(array_values(array_diff($boards, $deny_boards)));
  982. else
  983. {
  984. foreach ($permissions as $permission)
  985. {
  986. // never had it to start with
  987. if (empty($boards[$permission]))
  988. $boards[$permission] = array();
  989. else
  990. {
  991. // Or it may have been removed
  992. $deny_boards[$permission] = isset($deny_boards[$permission]) ? $deny_boards[$permission] : array();
  993. $boards[$permission] = array_unique(array_values(array_diff($boards[$permission], $deny_boards[$permission])));
  994. }
  995. }
  996. }
  997. return $boards;
  998. }
  999. /**
  1000. * Returns whether an email address should be shown and how.
  1001. * Possible outcomes are
  1002. * 'yes': show the full email address
  1003. * 'yes_permission_override': show the full email address, either you
  1004. * are a moderator or it's your own email address.
  1005. * 'no_through_forum': don't show the email address, but do allow
  1006. * things to be mailed using the built-in forum mailer.
  1007. * 'no': keep the email address hidden.
  1008. *
  1009. * @param bool $userProfile_hideEmail
  1010. * @param int $userProfile_id
  1011. * @return string (yes, yes_permission_override, no_through_forum, no)
  1012. */
  1013. function showEmailAddress($userProfile_hideEmail, $userProfile_id)
  1014. {
  1015. global $modSettings, $user_info;
  1016. // Should this user's email address be shown?
  1017. // If you're guest and the forum is set to hide email for guests: no.
  1018. // If the user is post-banned: no.
  1019. // If it's your own profile and you've set your address hidden: yes_permission_override.
  1020. // If you're a moderator with sufficient permissions: yes_permission_override.
  1021. // If the user has set their email address to be hidden: no.
  1022. // If the forum is set to show full email addresses: yes.
  1023. // Otherwise: no_through_forum.
  1024. if ((!empty($modSettings['guest_hideContacts']) && $user_info['is_guest']) || isset($_SESSION['ban']['cannot_post']))
  1025. return 'no';
  1026. elseif ((!$user_info['is_guest'] && $user_info['id'] == $userProfile_id && !$userProfile_hideEmail) || allowedTo('moderate_forum'))
  1027. return 'yes_permission_override';
  1028. elseif ($userProfile_hideEmail)
  1029. return 'no';
  1030. elseif (!empty($modSettings['make_email_viewable']) )
  1031. return 'yes';
  1032. else
  1033. return 'no_through_forum';
  1034. }
  1035. /**
  1036. * This function attempts to protect from spammed messages and the like.
  1037. * The time taken depends on error_type - generally uses the modSetting.
  1038. *
  1039. * @param string $error_type used also as a $txt index. (not an actual string.)
  1040. * @return boolean
  1041. */
  1042. function spamProtection($error_type)
  1043. {
  1044. global $modSettings, $txt, $user_info, $smcFunc;
  1045. // Certain types take less/more time.
  1046. $timeOverrides = array(
  1047. 'login' => 2,
  1048. 'register' => 2,
  1049. 'remind' => 30,
  1050. 'sendtopic' => $modSettings['spamWaitTime'] * 4,
  1051. 'sendmail' => $modSettings['spamWaitTime'] * 5,
  1052. 'reporttm' => $modSettings['spamWaitTime'] * 4,
  1053. 'search' => !empty($modSettings['search_floodcontrol_time']) ? $modSettings['search_floodcontrol_time'] : 1,
  1054. );
  1055. call_integration_hook('integrate_spam_protection', array($timeOverrides));
  1056. // Moderators are free...
  1057. if (!allowedTo('moderate_board'))
  1058. $timeLimit = isset($timeOverrides[$error_type]) ? $timeOverrides[$error_type] : $modSettings['spamWaitTime'];
  1059. else
  1060. $timeLimit = 2;
  1061. // Delete old entries...
  1062. $smcFunc['db_query']('', '
  1063. DELETE FROM {db_prefix}log_floodcontrol
  1064. WHERE log_time < {int:log_time}
  1065. AND log_type = {string:log_type}',
  1066. array(
  1067. 'log_time' => time() - $timeLimit,
  1068. 'log_type' => $error_type,
  1069. )
  1070. );
  1071. // Add a new entry, deleting the old if necessary.
  1072. $smcFunc['db_insert']('replace',
  1073. '{db_prefix}log_floodcontrol',
  1074. array('ip' => 'string-16', 'log_time' => 'int', 'log_type' => 'string'),
  1075. array($user_info['ip'], time(), $error_type),
  1076. array('ip', 'log_type')
  1077. );
  1078. // If affected is 0 or 2, it was there already.
  1079. if ($smcFunc['db_affected_rows']() != 1)
  1080. {
  1081. // Spammer! You only have to wait a *few* seconds!
  1082. fatal_lang_error($error_type . '_WaitTime_broken', false, array($timeLimit));
  1083. return true;
  1084. }
  1085. // They haven't posted within the limit.
  1086. return false;
  1087. }
  1088. /**
  1089. * A generic function to create a pair of index.php and .htaccess files in a directory
  1090. *
  1091. * @param string $path the (absolute) directory path
  1092. * @param boolean $attachments if the directory is an attachments directory or not
  1093. * @return true on success error string if anything fails
  1094. */
  1095. function secureDirectory($path, $attachments = false)
  1096. {
  1097. if (empty($path))
  1098. return 'empty_path';
  1099. if (!is_writable($path))
  1100. return 'path_not_writable';
  1101. $directoryname = basename($path);
  1102. $errors = array();
  1103. $close = empty($attachments) ? '
  1104. </Files>' : '
  1105. Allow from localhost
  1106. </Files>
  1107. RemoveHandler .php .php3 .phtml .cgi .fcgi .pl .fpl .shtml';
  1108. if (file_exists($path . '/.htaccess'))
  1109. $errors[] = 'htaccess_exists';
  1110. else
  1111. {
  1112. $fh = @fopen($path . '/.htaccess', 'w');
  1113. if ($fh) {
  1114. fwrite($fh, '<Files *>
  1115. Order Deny,Allow
  1116. Deny from all' . $close);
  1117. fclose($fh);
  1118. }
  1119. $errors[] = 'htaccess_cannot_create_file';
  1120. }
  1121. if (file_exists($path . '/index.php'))
  1122. $errors[] = 'index-php_exists';
  1123. else
  1124. {
  1125. $fh = @fopen($path . '/index.php', 'w');
  1126. if ($fh) {
  1127. fwrite($fh, '<?php
  1128. /**
  1129. * This file is here solely to protect your ' . $directoryname . ' directory.
  1130. */
  1131. // Look for Settings.php....
  1132. if (file_exists(dirname(dirname(__FILE__)) . \'/Settings.php\'))
  1133. {
  1134. // Found it!
  1135. require(dirname(dirname(__FILE__)) . \'/Settings.php\');
  1136. header(\'Location: \' . $boardurl);
  1137. }
  1138. // Can\'t find it... just forget it.
  1139. else
  1140. exit;
  1141. ');
  1142. fclose($fh);
  1143. }
  1144. $errors[] = 'index-php_cannot_create_file';
  1145. }
  1146. if (!empty($errors))
  1147. return $errors;
  1148. else
  1149. return true;
  1150. }
  1151. /**
  1152. * Helper function that puts together a ban query for a given ip
  1153. * builds the query for ipv6, ipv4 or 255.255.255.255 depending on whats supplied
  1154. *
  1155. * @param string $fullip An IP address either IPv6 or not
  1156. * @return string A SQL condition
  1157. */
  1158. function constructBanQueryIP($fullip)
  1159. {
  1160. // First attempt a IPv6 address.
  1161. if (isValidIPv6($fullip))
  1162. {
  1163. $ip_parts = convertIPv6toInts($fullip);
  1164. $ban_query = '((' . $ip_parts[0] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
  1165. AND (' . $ip_parts[1] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
  1166. AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
  1167. AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low4 AND bi.ip_high4)
  1168. AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low5 AND bi.ip_high5)
  1169. AND (' . $ip_parts[5] . ' BETWEEN bi.ip_low6 AND bi.ip_high6)
  1170. AND (' . $ip_parts[6] . ' BETWEEN bi.ip_low7 AND bi.ip_high7)
  1171. AND (' . $ip_parts[7] . ' BETWEEN bi.ip_low8 AND bi.ip_high8))';
  1172. }
  1173. // Check if we have a valid IPv4 address.
  1174. elseif (preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $fullip, $ip_parts) == 1)
  1175. $ban_query = '((' . $ip_parts[1] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
  1176. AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
  1177. AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
  1178. AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low4 AND bi.ip_high4))';
  1179. // We use '255.255.255.255' for 'unknown' since it's not valid anyway.
  1180. else
  1181. $ban_query = '(bi.ip_low1 = 255 AND bi.ip_high1 = 255
  1182. AND bi.ip_low2 = 255 AND bi.ip_high2 = 255
  1183. AND bi.ip_low3 = 255 AND bi.ip_high3 = 255
  1184. AND bi.ip_low4 = 255 AND bi.ip_high4 = 255)';
  1185. return $ban_query;
  1186. }
  1187. /**
  1188. * Decide if we are going to enable bad behavior scanning for this user
  1189. * - Admins and Moderators get a free pass
  1190. * - Optionally existing users with post counts over a limit are bypassed
  1191. * - Others get a humane frisking
  1192. */
  1193. function loadBadBehavior()
  1194. {
  1195. global $modSettings, $user_info, $context, $bb2_results;
  1196. $bb_run = false;
  1197. // Bad Behavior Enabled?
  1198. if (!empty($modSettings['badbehavior_enabled']))
  1199. {
  1200. require_once(EXTDIR . '/bad-behavior/badbehavior-plugin.php');
  1201. $bb_run = true;
  1202. // We may want to give some folks a hallway pass
  1203. if (!$user_info['is_guest'])
  1204. {
  1205. if (!empty($user_info['is_mod']) || !empty($user_info['is_admin']))
  1206. $bb_run = false;
  1207. elseif (!empty($modSettings['badbehavior_postcount_wl']) && $modSettings['badbehavior_postcount_wl'] < 0)
  1208. $bb_run = false;
  1209. elseif (!empty($modSettings['badbehavior_postcount_wl']) && $modSettings['badbehavior_postcount_wl'] > 0 && ($user_info['posts'] > $modSettings['badbehavior_postcount_wl']))
  1210. $bb_run = false;
  1211. }
  1212. // Put on the sanitary gloves, its time for a patdown !
  1213. if ($bb_run === true)
  1214. {
  1215. $bb2_results = bb2_start(bb2_read_settings());
  1216. addInlineJavascript(bb2_insert_head());
  1217. }
  1218. }
  1219. }
  1220. /**
  1221. * This protects against brute force attacks on a member's password.
  1222. * Importantly, even if the password was right we DON'T TELL THEM!
  1223. *
  1224. * @param $id_member
  1225. * @param $password_flood_value = false
  1226. * @param $was_correct = false
  1227. */
  1228. function validatePasswordFlood($id_member, $password_flood_value = false, $was_correct = false)
  1229. {
  1230. global $smcFunc, $cookiename;
  1231. // As this is only brute protection, we allow 5 attempts every 10 seconds.
  1232. // Destroy any session or cookie data about this member, as they validated wrong.
  1233. require_once(SUBSDIR . '/Auth.subs.php');
  1234. setLoginCookie(-3600, 0);
  1235. if (isset($_SESSION['login_' . $cookiename]))
  1236. unset($_SESSION['login_' . $cookiename]);
  1237. // We need a member!
  1238. if (!$id_member)
  1239. {
  1240. // Redirect back!
  1241. redirectexit();
  1242. // Probably not needed, but still make sure...
  1243. fatal_lang_error('no_access', false);
  1244. }
  1245. // Right, have we got a flood value?
  1246. if ($password_flood_value !== false)
  1247. @list ($time_stamp, $number_tries) = explode('|', $password_flood_value);
  1248. // Timestamp invalid or non-existent?
  1249. if (empty($number_tries) || $time_stamp < (time() - 10))
  1250. {
  1251. // If it wasn't *that* long ago, don't give them another five goes.
  1252. $number_tries = !empty($number_tries) && $time_stamp < (time() - 20) ? 2 : 0;
  1253. $time_stamp = time();
  1254. }
  1255. $number_tries++;
  1256. // Broken the law?
  1257. if ($number_tries > 5)
  1258. fatal_lang_error('login_threshold_brute_fail', 'critical');
  1259. // Otherwise set the members data. If they correct on their first attempt then we actually clear it, otherwise we set it!
  1260. updateMemberData($id_member, array('passwd_flood' => $was_correct && $number_tries == 1 ? '' : $time_stamp . '|' . $number_tries));
  1261. }