PageRenderTime 25ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/punbb/login.php

https://github.com/radicalsuz/amp
PHP | 226 lines | 143 code | 48 blank | 35 comment | 43 complexity | b2fc9a6f66cc334341a0efaeeb67d7cb MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-2.0
  1. <?php
  2. /***********************************************************************
  3. Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org)
  4. This file is part of PunBB.
  5. PunBB is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published
  7. by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. PunBB is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  16. MA 02111-1307 USA
  17. ************************************************************************/
  18. if (isset($_GET['action']))
  19. define('PUN_QUIET_VISIT', 1);
  20. define('PUN_ROOT', './');
  21. require PUN_ROOT.'include/common.php';
  22. // Load the login.php language file
  23. require PUN_ROOT.'lang/'.$pun_user['language'].'/login.php';
  24. $action = isset($_GET['action']) ? $_GET['action'] : null;
  25. if (isset($_POST['form_sent']) && $action == 'in')
  26. {
  27. $form_username = trim($_POST['req_username']);
  28. $form_password = trim($_POST['req_password']);
  29. $username_sql = ($db_type == 'mysql' || $db_type == 'mysqli') ? 'username=\''.$db->escape($form_username).'\'' : 'LOWER(username)=LOWER(\''.$db->escape($form_username).'\')';
  30. $result = $db->query('SELECT id, group_id, password, save_pass FROM '.$db->prefix.'users WHERE '.$username_sql) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  31. list($user_id, $group_id, $db_password_hash, $save_pass) = $db->fetch_row($result);
  32. $authorized = false;
  33. if (!empty($db_password_hash))
  34. {
  35. $sha1_in_db = (strlen($db_password_hash) == 40) ? true : false;
  36. $sha1_available = (function_exists('sha1') || function_exists('mhash')) ? true : false;
  37. $form_password_hash = pun_hash($form_password); // This could result in either an SHA-1 or an MD5 hash (depends on $sha1_available)
  38. if ($sha1_in_db && $sha1_available && $db_password_hash == $form_password_hash)
  39. $authorized = true;
  40. else if (!$sha1_in_db && $db_password_hash == md5($form_password))
  41. {
  42. $authorized = true;
  43. if ($sha1_available) // There's an MD5 hash in the database, but SHA1 hashing is available, so we update the DB
  44. $db->query('UPDATE '.$db->prefix.'users SET password=\''.$form_password_hash.'\' WHERE id='.$user_id) or error('Unable to update user password', __FILE__, __LINE__, $db->error());
  45. }
  46. }
  47. if (!$authorized)
  48. message($lang_login['Wrong user/pass'].' <a href="login.php?action=forget">'.$lang_login['Forgotten pass'].'</a>');
  49. // Update the status if this is the first time the user logged in
  50. if ($group_id == PUN_UNVERIFIED)
  51. $db->query('UPDATE '.$db->prefix.'users SET group_id='.$pun_config['o_default_user_group'].' WHERE id='.$user_id) or error('Unable to update user status', __FILE__, __LINE__, $db->error());
  52. // Remove this users guest entry from the online list
  53. $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());
  54. $expire = ($save_pass == '1') ? time() + 31536000 : 0;
  55. pun_setcookie($user_id, $form_password_hash, $expire);
  56. redirect($_POST['redirect_url'], $lang_login['Login redirect']);
  57. }
  58. else if ($action == 'out')
  59. {
  60. if ($pun_user['is_guest'] || !isset($_GET['id']) || $_GET['id'] != $pun_user['id'])
  61. {
  62. header('Location: index.php');
  63. exit;
  64. }
  65. // Remove user from "users online" list.
  66. $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());
  67. // Update last_visit (make sure there's something to update it with)
  68. if (isset($pun_user['logged']))
  69. $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());
  70. pun_setcookie(1, random_pass(8), time() + 31536000);
  71. redirect('index.php', $lang_login['Logout redirect']);
  72. }
  73. else if ($action == 'forget' || $action == 'forget_2')
  74. {
  75. if (!$pun_user['is_guest'])
  76. header('Location: index.php');
  77. if (isset($_POST['form_sent']))
  78. {
  79. require PUN_ROOT.'include/email.php';
  80. // Validate the email-address
  81. $email = strtolower(trim($_POST['req_email']));
  82. if (!is_valid_email($email))
  83. message($lang_common['Invalid e-mail']);
  84. $result = $db->query('SELECT id, username FROM '.$db->prefix.'users WHERE email=\''.$db->escape($email).'\'') or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  85. if ($db->num_rows($result))
  86. {
  87. // Load the "activate password" template
  88. $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$pun_user['language'].'/mail_templates/activate_password.tpl'));
  89. // The first row contains the subject
  90. $first_crlf = strpos($mail_tpl, "\n");
  91. $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
  92. $mail_message = trim(substr($mail_tpl, $first_crlf));
  93. // Do the generic replacements first (they apply to all e-mails sent out here)
  94. $mail_message = str_replace('<base_url>', $pun_config['o_base_url'].'/', $mail_message);
  95. $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'].' '.$lang_common['Mailer'], $mail_message);
  96. // Loop through users we found
  97. while ($cur_hit = $db->fetch_assoc($result))
  98. {
  99. // Generate a new password and a new password activation code
  100. $new_password = random_pass(8);
  101. $new_password_key = random_pass(8);
  102. $db->query('UPDATE '.$db->prefix.'users SET activate_string=\''.pun_hash($new_password).'\', activate_key=\''.$new_password_key.'\' WHERE id='.$cur_hit['id']) or error('Unable to update activation data', __FILE__, __LINE__, $db->error());
  103. // Do the user specific replacements to the template
  104. $cur_mail_message = str_replace('<username>', $cur_hit['username'], $mail_message);
  105. $cur_mail_message = str_replace('<activation_url>', $pun_config['o_base_url'].'/profile.php?id='.$cur_hit['id'].'&action=change_pass&key='.$new_password_key, $cur_mail_message);
  106. $cur_mail_message = str_replace('<new_password>', $new_password, $cur_mail_message);
  107. pun_mail($email, $mail_subject, $cur_mail_message);
  108. }
  109. message($lang_login['Forget mail'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.');
  110. }
  111. else
  112. message($lang_login['No e-mail match'].' '.htmlspecialchars($email).'.');
  113. }
  114. $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_login['Request pass'];
  115. $required_fields = array('req_email' => $lang_common['E-mail']);
  116. $focus_element = array('request_pass', 'req_email');
  117. require PUN_ROOT.'header.php';
  118. ?>
  119. <div class="blockform">
  120. <h2><span><?php echo $lang_login['Request pass'] ?></span></h2>
  121. <div class="box">
  122. <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;}">
  123. <div class="inform">
  124. <fieldset>
  125. <legend><?php echo $lang_login['Request pass legend'] ?></legend>
  126. <div class="infldset">
  127. <input type="hidden" name="form_sent" value="1" />
  128. <input id="req_email" type="text" name="req_email" size="50" maxlength="50" />
  129. <p><?php echo $lang_login['Request pass info'] ?></p>
  130. </div>
  131. </fieldset>
  132. </div>
  133. <p><input type="submit" name="request_pass" value="<?php echo $lang_common['Submit'] ?>" /><a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>
  134. </form>
  135. </div>
  136. </div>
  137. <?php
  138. require PUN_ROOT.'footer.php';
  139. }
  140. if (!$pun_user['is_guest'])
  141. header('Location: index.php');
  142. // Try to determine if the data in HTTP_REFERER is valid (if not, we redirect to index.php after login)
  143. $redirect_url = (isset($_SERVER['HTTP_REFERER']) && preg_match('#^'.preg_quote($pun_config['o_base_url']).'/(.*?)\.php#i', $_SERVER['HTTP_REFERER'])) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : 'index.php';
  144. $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Login'];
  145. $required_fields = array('req_username' => $lang_common['Username'], 'req_password' => $lang_common['Password']);
  146. $focus_element = array('login', 'req_username');
  147. require PUN_ROOT.'header.php';
  148. ?>
  149. <div class="blockform">
  150. <h2><span><?php echo $lang_common['Login'] ?></span></h2>
  151. <div class="box">
  152. <form id="login" method="post" action="login.php?action=in" onsubmit="return process_form(this)">
  153. <div class="inform">
  154. <fieldset>
  155. <legend><?php echo $lang_login['Login legend'] ?></legend>
  156. <div class="infldset">
  157. <input type="hidden" name="form_sent" value="1" />
  158. <input type="hidden" name="redirect_url" value="<?php echo $redirect_url ?>" />
  159. <label class="conl"><strong><?php echo $lang_common['Username'] ?></strong><br /><input type="text" name="req_username" size="25" maxlength="25" tabindex="1" /><br /></label>
  160. <label class="conl"><strong><?php echo $lang_common['Password'] ?></strong><br /><input type="password" name="req_password" size="16" maxlength="16" tabindex="2" /><br /></label>
  161. <p class="clearb"><?php echo $lang_login['Login info'] ?></p>
  162. <p><a href="register.php" tabindex="4"><?php echo $lang_login['Not registered'] ?></a>&nbsp;&nbsp;
  163. <a href="login.php?action=forget" tabindex="5"><?php echo $lang_login['Forgotten pass'] ?></a></p>
  164. </div>
  165. </fieldset>
  166. </div>
  167. <p><input type="submit" name="login" value="<?php echo $lang_common['Login'] ?>" tabindex="3" /></p>
  168. </form>
  169. </div>
  170. </div>
  171. <?php
  172. require PUN_ROOT.'footer.php';