PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/login.php

https://github.com/Dratone/EveBB
PHP | 351 lines | 237 code | 75 blank | 39 comment | 74 complexity | 6aba937ac3b01d2d364f4b86ae65a756 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2010 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. if (isset($_GET['action']))
  8. define('PUN_QUIET_VISIT', 1);
  9. define('PUN_ROOT', dirname(__FILE__).'/');
  10. require PUN_ROOT.'include/common.php';
  11. // Load the login.php language file
  12. require PUN_ROOT.'lang/'.$pun_user['language'].'/login.php';
  13. $action = isset($_GET['action']) ? $_GET['action'] : null;
  14. if (isset($_POST['form_sent']) && $action == 'in')
  15. {
  16. $form_username = pun_trim($_POST['req_username']);
  17. $form_password = pun_trim($_POST['req_password']);
  18. $save_pass = isset($_POST['save_pass']);
  19. $username_sql = ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'mysql_innodb' || $db_type == 'mysqli_innodb') ? 'u.username=\''.$db->escape($form_username).'\'' : 'LOWER(u.username)=LOWER(\''.$db->escape($form_username).'\')';
  20. $result = $db->query('SELECT * FROM '.$db->prefix.'users AS u LEFT JOIN '.$db->prefix.'session AS s ON s.user_id=u.id WHERE '.$username_sql) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  21. $cur_user = $db->fetch_assoc($result);
  22. $authorized = false;
  23. if (!empty($cur_user['password']))
  24. {
  25. $form_password_hash = pun_hash($form_password); // Will result in a SHA-1 hash
  26. // If there is a salt in the database we have upgraded from 1.3-legacy though havent yet logged in
  27. if (!empty($cur_user['salt']))
  28. {
  29. if (sha1($cur_user['salt'].sha1($form_password)) == $cur_user['password']) // 1.3 used sha1(salt.sha1(pass))
  30. {
  31. $authorized = true;
  32. $db->query('UPDATE '.$db->prefix.'users SET password=\''.$form_password_hash.'\', salt=NULL WHERE id='.$cur_user['id']) or error('Unable to update user password', __FILE__, __LINE__, $db->error());
  33. }
  34. }
  35. // If the length isn't 40 then the password isn't using sha1, so it must be md5 from 1.2
  36. else if (strlen($cur_user['password']) != 40)
  37. {
  38. if (md5($form_password) == $cur_user['password'])
  39. {
  40. $authorized = true;
  41. $db->query('UPDATE '.$db->prefix.'users SET password=\''.$form_password_hash.'\' WHERE id='.$cur_user['id']) or error('Unable to update user password', __FILE__, __LINE__, $db->error());
  42. }
  43. }
  44. // Otherwise we should have a normal sha1 password
  45. else {
  46. $authorized = ($cur_user['password'] == $form_password_hash);
  47. } //End if - else.
  48. }
  49. if (!$authorized) {
  50. sleep(3);
  51. foreach ($_HOOKS['users'] as $hook) {
  52. $hook->login_failed($cur_user['id']);
  53. } //End foreach().
  54. message($lang_login['Wrong user/pass'].' <a href="login.php?action=forget">'.$lang_login['Forgotten pass'].'</a>');
  55. } //End if.
  56. foreach ($_HOOKS['users'] as $hook) {
  57. $hook->login($cur_user['id']);
  58. } //End foreach().
  59. // Update the status if this is the first time the user logged in
  60. if ($cur_user['group_id'] == PUN_UNVERIFIED) {
  61. $db->query('UPDATE '.$db->prefix.'users SET group_id='.$pun_config['o_default_user_group'].' WHERE id='.$cur_user['id']) or error('Unable to update user status', __FILE__, __LINE__, $db->error());
  62. $log = '';
  63. apply_rules($log);
  64. // Regenerate the users info cache
  65. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  66. require PUN_ROOT.'include/cache.php';
  67. generate_users_info_cache();
  68. } //End if.
  69. // Remove this users guest entry from the online list
  70. $db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape(get_remote_address()).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());
  71. $now = gmmktime();
  72. $new_token = gen_token();
  73. //Should we prevent them from getting a new token?
  74. if ($pun_config['o_regen_token'] != '1' && $save_pass == '1') {
  75. //Try and set the token if it exists, otherwise use the new token stil.
  76. $new_token = (strlen($cur_user['token']) == 32) ? $cur_user['token'] : $new_token;
  77. } //End if.
  78. //Lets update all our tracking bits.
  79. $db->insert_or_update(
  80. array(
  81. 'user_id' => $cur_user['id'],
  82. 'token' => $new_token,
  83. 'stamp' => $now,
  84. 'length' => (($save_pass == '1') ? 2629743 : $pun_config['session_length']),
  85. 'ip' => $_SERVER['REMOTE_ADDR'],
  86. ),
  87. 'user_id',
  88. $db->prefix.'session'
  89. ) or error('Unable to update session data.', __FILE__, __LINE__, $db->error());
  90. //Have they never logged logged out before?
  91. if ($cur_user['last_visit'] == 0) {
  92. $db->query("UPDATE ".$db->prefix."users SET last_visit=".$now." WHERE id=".$cur_user['id']);
  93. } //End if.
  94. // Send a new, updated cookie with a new expiration timestamp
  95. if ($save_pass == '1') {
  96. forum_setcookie(str_replace('=', '', base64_encode($_SERVER['SERVER_NAME'])), $cur_user['id'].':'.$new_token.':'.md5($cur_user['id'].$cookie_seed.$new_token), $now+2629743); //Expire the cookie in 1 month.
  97. } //End if - else.
  98. //Update the PHP Session.
  99. $_SESSION[str_replace('=', '', base64_encode($_SERVER['SERVER_NAME']))] = $cur_user['id'].':'.$new_token.':'.md5($cur_user['id'].$cookie_seed.$new_token);
  100. /*$now = $offset > 0 ? $now - $offset : $now + offset;
  101. $expire = ($save_pass == '1') ? $now + 1209600 : $now +$pun_config['o_timeout_visit'];
  102. pun_setcookie($cur_user['id'], $form_password_hash, $expire);*/
  103. // Reset tracked topics
  104. set_tracked_topics(null);
  105. redirect(htmlspecialchars($_POST['redirect_url']), $lang_login['Login redirect']);
  106. }
  107. else if ($action == 'out')
  108. {
  109. if ($pun_user['is_guest'] || !isset($_GET['id']) || $_GET['id'] != $pun_user['id'] || !isset($_GET['csrf_token']) || $_GET['csrf_token'] != pun_hash($pun_user['id'].pun_hash(get_remote_address())))
  110. {
  111. header('Location: index.php');
  112. exit;
  113. } //End if.
  114. $db->query("DELETE FROM ".$db->prefix."session WHERE user_id=".$pun_user['user_id']); //Get rid of the old session.
  115. unset($_SESSION[$cookie_name]);
  116. // Remove user from "users online" list
  117. $db->query('DELETE FROM '.$db->prefix.'online WHERE user_id='.$pun_user['id']) or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());
  118. $db->query("DELETE FROM ".$db->prefix."session WHERE user_id=".$pun_user['id']) or error('Unable to delete from session list', __FILE__, __LINE__, $db->error()); //Get rid of the old session.
  119. // Update last_visit (make sure there's something to update it with)
  120. if (isset($pun_user['logged']))
  121. $db->query('UPDATE '.$db->prefix.'users SET last_visit='.$pun_user['logged'].' WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error());
  122. forum_setcookie(str_replace('=', '', base64_encode($_SERVER['SERVER_NAME'])), pun_hash(uniqid(rand(), true)), time() + 31536000);
  123. redirect('index.php', $lang_login['Logout redirect']);
  124. }
  125. else if ($action == 'forget' || $action == 'forget_2')
  126. {
  127. if (!$pun_user['is_guest'])
  128. header('Location: index.php');
  129. if (isset($_POST['form_sent']))
  130. {
  131. // Start with a clean slate
  132. $errors = array();
  133. require PUN_ROOT.'include/email.php';
  134. // Validate the email address
  135. $email = strtolower(trim($_POST['req_email']));
  136. if (!is_valid_email($email))
  137. $errors[] = $lang_common['Invalid email'];
  138. // Did everything go according to plan?
  139. if (empty($errors))
  140. {
  141. $result = $db->query('SELECT id, username, last_email_sent FROM '.$db->prefix.'users WHERE email=\''.$db->escape($email).'\'') or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  142. if ($db->num_rows($result))
  143. {
  144. // Load the "activate password" template
  145. $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$pun_user['language'].'/mail_templates/activate_password.tpl'));
  146. // The first row contains the subject
  147. $first_crlf = strpos($mail_tpl, "\n");
  148. $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
  149. $mail_message = trim(substr($mail_tpl, $first_crlf));
  150. // Do the generic replacements first (they apply to all emails sent out here)
  151. $mail_message = str_replace('<base_url>', get_base_url().'/', $mail_message);
  152. $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'].' '.$lang_common['Mailer'], $mail_message);
  153. // Loop through users we found
  154. while ($cur_hit = $db->fetch_assoc($result))
  155. {
  156. if ($cur_hit['last_email_sent'] != '' && (time() - $cur_hit['last_email_sent']) < 3600 && (time() - $cur_hit['last_email_sent']) >= 0)
  157. message($lang_login['Email flood'], true);
  158. // Generate a new password and a new password activation code
  159. $new_password = random_pass(8);
  160. $new_password_key = random_pass(8);
  161. $db->query('UPDATE '.$db->prefix.'users SET activate_string=\''.pun_hash($new_password).'\', activate_key=\''.$new_password_key.'\', last_email_sent = '.time().' WHERE id='.$cur_hit['id']) or error('Unable to update activation data', __FILE__, __LINE__, $db->error());
  162. // Do the user specific replacements to the template
  163. $cur_mail_message = str_replace('<username>', $cur_hit['username'], $mail_message);
  164. $cur_mail_message = str_replace('<activation_url>', get_base_url().'/profile.php?id='.$cur_hit['id'].'&action=change_pass&key='.$new_password_key, $cur_mail_message);
  165. $cur_mail_message = str_replace('<new_password>', $new_password, $cur_mail_message);
  166. pun_mail($email, $mail_subject, $cur_mail_message);
  167. }
  168. message($lang_login['Forget mail'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.', true);
  169. }
  170. else
  171. $errors[] = $lang_login['No email match'].' '.htmlspecialchars($email).'.';
  172. }
  173. }
  174. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_login['Request pass']);
  175. $required_fields = array('req_email' => $lang_common['Email']);
  176. $focus_element = array('request_pass', 'req_email');
  177. define ('PUN_ACTIVE_PAGE', 'login');
  178. require PUN_ROOT.'header.php';
  179. // If there are errors, we display them
  180. if (!empty($errors))
  181. {
  182. ?>
  183. <div id="posterror" class="block">
  184. <h2><span><?php echo $lang_login['New password errors'] ?></span></h2>
  185. <div class="box">
  186. <div class="inbox error-info">
  187. <p><?php echo $lang_login['New passworderrors info'] ?></p>
  188. <ul class="error-list">
  189. <?php
  190. foreach ($errors as $cur_error)
  191. echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
  192. ?>
  193. </ul>
  194. </div>
  195. </div>
  196. </div>
  197. <?php
  198. }
  199. ?>
  200. <div class="blockform">
  201. <h2><span><?php echo $lang_login['Request pass'] ?></span></h2>
  202. <div class="box">
  203. <form id="request_pass" method="post" action="login.php?action=forget_2" onsubmit="this.request_pass.disabled=true;if(process_form(this)){return true;}else{this.request_pass.disabled=false;return false;}">
  204. <div class="inform">
  205. <fieldset>
  206. <legend><?php echo $lang_login['Request pass legend'] ?></legend>
  207. <div class="infldset">
  208. <input type="hidden" name="form_sent" value="1" />
  209. <label class="required"><strong><?php echo $lang_common['Email'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br /><input id="req_email" type="text" name="req_email" size="50" maxlength="80" /><br /></label>
  210. <p><?php echo $lang_login['Request pass info'] ?></p>
  211. </div>
  212. </fieldset>
  213. </div>
  214. <p class="buttons"><input type="submit" name="request_pass" value="<?php echo $lang_common['Submit'] ?>" /><?php if (empty($errors)): ?> <a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a><?php endif; ?></p>
  215. </form>
  216. </div>
  217. </div>
  218. <?php
  219. require PUN_ROOT.'footer.php';
  220. }
  221. if (!$pun_user['is_guest'])
  222. header('Location: index.php');
  223. // Try to determine if the data in HTTP_REFERER is valid (if not, we redirect to index.php after login)
  224. if (!empty($_SERVER['HTTP_REFERER']))
  225. {
  226. $referrer = parse_url($_SERVER['HTTP_REFERER']);
  227. // Remove www subdomain if it exists
  228. if (strpos($referrer['host'], 'www.') === 0)
  229. $referrer['host'] = substr($referrer['host'], 4);
  230. // Make sure the path component exists
  231. if (!isset($referrer['path']))
  232. $referrer['path'] = '';
  233. $valid = parse_url(get_base_url());
  234. // Remove www subdomain if it exists
  235. if (strpos($valid['host'], 'www.') === 0)
  236. $valid['host'] = substr($valid['host'], 4);
  237. // Make sure the path component exists
  238. if (!isset($valid['path']))
  239. $valid['path'] = '';
  240. if ($referrer['host'] == $valid['host'] && preg_match('#^'.preg_quote($valid['path']).'/(.*?)\.php#i', $referrer['path']))
  241. $redirect_url = $_SERVER['HTTP_REFERER'];
  242. }
  243. if (!isset($redirect_url))
  244. $redirect_url = 'index.php';
  245. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_common['Login']);
  246. $required_fields = array('req_username' => $lang_common['Username'], 'req_password' => $lang_common['Password']);
  247. $focus_element = array('login', 'req_username');
  248. define('PUN_ACTIVE_PAGE', 'login');
  249. require PUN_ROOT.'header.php';
  250. ?>
  251. <div class="blockform">
  252. <h2><span><?php echo $lang_common['Login'] ?></span></h2>
  253. <div class="box">
  254. <form id="login" method="post" action="login.php?action=in" onsubmit="return process_form(this)">
  255. <div class="inform">
  256. <fieldset>
  257. <legend><?php echo $lang_login['Login legend'] ?></legend>
  258. <div class="infldset">
  259. <input type="hidden" name="form_sent" value="1" />
  260. <input type="hidden" name="redirect_url" value="<?php echo pun_htmlspecialchars($redirect_url) ?>" />
  261. <label class="conl required"><strong><?php echo $lang_common['Username'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br /><input type="text" name="req_username" size="25" maxlength="25" tabindex="1" /><br /></label>
  262. <label class="conl required"><strong><?php echo $lang_common['Password'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br /><input type="password" name="req_password" size="25" tabindex="2" /><br /></label>
  263. <div class="rbox clearb">
  264. <label><input type="checkbox" name="save_pass" value="1" tabindex="3" /><?php echo $lang_login['Remember me'] ?><br /></label>
  265. </div>
  266. <p class="clearb"><?php echo $lang_login['Login info'] ?></p>
  267. <p class="actions"><span><a href="register.php" tabindex="4"><?php echo $lang_login['Not registered'] ?></a></span> <span><a href="login.php?action=forget" tabindex="5"><?php echo $lang_login['Forgotten pass'] ?></a></span></p>
  268. </div>
  269. </fieldset>
  270. </div>
  271. <p class="buttons"><input type="submit" name="login" value="<?php echo $lang_common['Login'] ?>" tabindex="3" /></p>
  272. </form>
  273. </div>
  274. </div>
  275. <?php
  276. require PUN_ROOT.'footer.php';