Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.98 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Copyright (C) 2008-2012 FluxBB
  5. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  6. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  7. */
  8.  
  9. if (isset($_GET['action']))
  10. define('PUN_QUIET_VISIT', 1);
  11.  
  12. define('PUN_ROOT', dirname(__FILE__).'/');
  13. require PUN_ROOT.'include/common.php';
  14.  
  15.  
  16. // Load the login.php language file
  17. require PUN_ROOT.'lang/'.$pun_user['language'].'/login.php';
  18.  
  19. $action = isset($_GET['action']) ? $_GET['action'] : null;
  20. $errors = array();
  21.  
  22. if (isset($_POST['form_sent']) && $action == 'in')
  23. {
  24. flux_hook('login_before_validation');
  25.  
  26. $form_username = pun_trim($_POST['req_username']);
  27. $form_password = pun_trim($_POST['req_password']);
  28. $save_pass = isset($_POST['save_pass']);
  29.  
  30. $username_sql = ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'mysql_innodb' || $db_type == 'mysqli_innodb') ? 'username=\''.$db->escape($form_username).'\'' : 'LOWER(username)=LOWER(\''.$db->escape($form_username).'\')';
  31.  
  32. $result = $db->query('SELECT * FROM '.$db->prefix.'users WHERE '.$username_sql) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  33. $cur_user = $db->fetch_assoc($result);
  34.  
  35. $authorized = false;
  36.  
  37. if (!empty($cur_user['password']))
  38. {
  39. $form_password_hash = pun_hash($form_password); // Will result in a SHA-1 hash
  40.  
  41. // If there is a salt in the database we have upgraded from 1.3-legacy though haven't yet logged in
  42. if (!empty($cur_user['salt']))
  43. {
  44. $is_salt_authorized = pun_hash_equals(sha1($cur_user['salt'].sha1($form_password)), $cur_user['password']);
  45. if ($is_salt_authorized) // 1.3 used sha1(salt.sha1(pass))
  46. {
  47. $authorized = true;
  48.  
  49. $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());
  50. }
  51. }
  52. // If the length isn't 40 then the password isn't using sha1, so it must be md5 from 1.2
  53. else if (strlen($cur_user['password']) != 40)
  54. {
  55. $is_md5_authorized = pun_hash_equals(md5($form_password), $cur_user['password']);
  56. if ($is_md5_authorized)
  57. {
  58. $authorized = true;
  59.  
  60. $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());
  61. }
  62. }
  63. // Otherwise we should have a normal sha1 password
  64. else
  65. $authorized = pun_hash_equals($cur_user['password'], $form_password_hash);
  66. }
  67.  
  68. if (!$authorized)
  69. $errors[] = $lang_login['Wrong user/pass'];
  70.  
  71. flux_hook('login_after_validation');
  72.  
  73. // Did everything go according to plan?
  74. if (empty($errors))
  75. {
  76. // Update the status if this is the first time the user logged in
  77. if ($cur_user['group_id'] == PUN_UNVERIFIED)
  78. {
  79. $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());
  80.  
  81. // Regenerate the users info cache
  82. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  83. require PUN_ROOT.'include/cache.php';
  84.  
  85. generate_users_info_cache();
  86. }
  87.  
  88. // Remove this user's guest entry from the online list
  89. $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());
  90.  
  91. $expire = ($save_pass == '1') ? time() + 1209600 : time() + $pun_config['o_timeout_visit'];
  92. pun_setcookie($cur_user['id'], $form_password_hash, $expire);
  93.  
  94. // Reset tracked topics
  95. set_tracked_topics(null);
  96.  
  97. // Try to determine if the data in redirect_url is valid (if not, we redirect to index.php after login)
  98. $redirect_url = validate_redirect($_POST['redirect_url'], 'index.php');
  99.  
  100. redirect(pun_htmlspecialchars($redirect_url), $lang_login['Login redirect']);
  101. }
  102. }
  103.  
  104.  
  105. else if ($action == 'out')
  106. {
  107. if ($pun_user['is_guest'] || !isset($_GET['id']) || $_GET['id'] != $pun_user['id'])
  108. {
  109. header('Location: index.php');
  110. exit;
  111. }
  112.  
  113. check_csrf($_GET['csrf_token']);
  114.  
  115. // Remove user from "users online" list
  116. $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());
  117.  
  118. // Update last_visit (make sure there's something to update it with)
  119. if (isset($pun_user['logged']))
  120. $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());
  121.  
  122. pun_setcookie(1, pun_hash(uniqid(rand(), true)), time() + 31536000);
  123.  
  124. redirect('index.php', $lang_login['Logout redirect']);
  125. }
  126.  
  127.  
  128. else if ($action == 'forget' || $action == 'forget_2')
  129. {
  130. if (!$pun_user['is_guest'])
  131. {
  132. header('Location: index.php');
  133. exit;
  134. }
  135.  
  136. if (isset($_POST['form_sent']))
  137. {
  138. flux_hook('forget_password_before_validation');
  139.  
  140. require PUN_ROOT.'include/email.php';
  141.  
  142. // Validate the email address
  143. $email = strtolower(pun_trim($_POST['req_email']));
  144. if (!is_valid_email($email))
  145. $errors[] = $lang_common['Invalid email'];
  146.  
  147. flux_hook('forget_password_after_validation');
  148.  
  149. // Did everything go according to plan?
  150. if (empty($errors))
  151. {
  152. $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());
  153.  
  154. if ($db->num_rows($result))
  155. {
  156. // Load the "activate password" template
  157. $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$pun_user['language'].'/mail_templates/activate_password.tpl'));
  158.  
  159. // The first row contains the subject
  160. $first_crlf = strpos($mail_tpl, "\n");
  161. $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
  162. $mail_message = trim(substr($mail_tpl, $first_crlf));
  163.  
  164. // Do the generic replacements first (they apply to all emails sent out here)
  165. $mail_message = str_replace('<base_url>', get_base_url().'/', $mail_message);
  166. $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'], $mail_message);
  167.  
  168. // Loop through users we found
  169. while ($cur_hit = $db->fetch_assoc($result))
  170. {
  171. if ($cur_hit['last_email_sent'] != '' && (time() - $cur_hit['last_email_sent']) < 3600 && (time() - $cur_hit['last_email_sent']) >= 0)
  172. message(sprintf($lang_login['Email flood'], intval((3600 - (time() - $cur_hit['last_email_sent'])) / 60)), true);
  173.  
  174. // Generate a new password and a new password activation code
  175. $new_password = random_pass(12);
  176. $new_password_key = random_pass(8);
  177.  
  178. $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());
  179.  
  180. // Do the user specific replacements to the template
  181. $cur_mail_message = str_replace('<username>', $cur_hit['username'], $mail_message);
  182. $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);
  183. $cur_mail_message = str_replace('<new_password>', $new_password, $cur_mail_message);
  184.  
  185. pun_mail($email, $mail_subject, $cur_mail_message);
  186. }
  187.  
  188. message($lang_login['Forget mail'].' <a href="mailto:'.pun_htmlspecialchars($pun_config['o_admin_email']).'">'.pun_htmlspecialchars($pun_config['o_admin_email']).'</a>.', true);
  189. }
  190. else
  191. $errors[] = $lang_login['No email match'].' '.pun_htmlspecialchars($email).'.';
  192. }
  193. }
  194.  
  195. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_login['Request pass']);
  196. $required_fields = array('req_email' => $lang_common['Email']);
  197. $focus_element = array('request_pass', 'req_email');
  198.  
  199. flux_hook('forget_password_before_header');
  200.  
  201. define ('PUN_ACTIVE_PAGE', 'login');
  202. require PUN_ROOT.'header.php';
  203.  
  204. // If there are errors, we display them
  205. if (!empty($errors))
  206. {
  207.  
  208. ?>
  209. <div id="posterror" class="block">
  210. <h2><span><?php echo $lang_login['New password errors'] ?></span></h2>
  211. <div class="box">
  212. <div class="inbox error-info">
  213. <p><?php echo $lang_login['New passworderrors info'] ?></p>
  214. <ul class="error-list">
  215. <?php
  216.  
  217. foreach ($errors as $cur_error)
  218. echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
  219. ?>
  220. </ul>
  221. </div>
  222. </div>
  223. </div>
  224.  
  225. <?php
  226.  
  227. }
  228. ?>
  229. <div class="blockform">
  230. <h2><span><?php echo $lang_login['Request pass'] ?></span></h2>
  231. <div class="box">
  232. <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;}">
  233. <div class="inform">
  234. <fieldset>
  235. <legend><?php echo $lang_login['Request pass legend'] ?></legend>
  236. <div class="infldset">
  237. <input type="hidden" name="form_sent" value="1" />
  238. <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" value="<?php if (isset($_POST['req_email'])) echo pun_htmlspecialchars($_POST['req_email']); ?>" size="50" maxlength="80" /><br /></label>
  239. <p><?php echo $lang_login['Request pass info'] ?></p>
  240. </div>
  241. </fieldset>
  242. </div>
  243. <?php flux_hook('forget_password_before_submit') ?>
  244. <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>
  245. </form>
  246. </div>
  247. </div>
  248. <?php
  249.  
  250. require PUN_ROOT.'footer.php';
  251. }
  252.  
  253.  
  254. if (!$pun_user['is_guest'])
  255. {
  256. header('Location: index.php');
  257. exit;
  258. }
  259.  
  260. // Try to determine if the data in HTTP_REFERER is valid (if not, we redirect to index.php after login)
  261. if (!empty($_SERVER['HTTP_REFERER']))
  262. $redirect_url = validate_redirect($_SERVER['HTTP_REFERER'], null);
  263.  
  264. if (!isset($redirect_url))
  265. $redirect_url = get_base_url(true).'/index.php';
  266. else if (preg_match('%viewtopic\.php\?pid=(\d+)$%', $redirect_url, $matches))
  267. $redirect_url .= '#p'.$matches[1];
  268.  
  269. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_common['Login']);
  270. $required_fields = array('req_username' => $lang_common['Username'], 'req_password' => $lang_common['Password']);
  271. $focus_element = array('login', 'req_username');
  272.  
  273. flux_hook('login_before_header');
  274.  
  275. define('PUN_ACTIVE_PAGE', 'login');
  276. require PUN_ROOT.'header.php';
  277.  
  278. // If there are errors, we display them
  279. if (!empty($errors))
  280. {
  281.  
  282. ?>
  283. <div id="posterror" class="block">
  284. <h2><span><?php echo $lang_login['Login errors'] ?></span></h2>
  285. <div class="box">
  286. <div class="inbox error-info">
  287. <p><?php echo $lang_login['Login errors info'] ?></p>
  288. <ul class="error-list">
  289. <?php
  290.  
  291. foreach ($errors as $cur_error)
  292. echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
  293. ?>
  294. </ul>
  295. </div>
  296. </div>
  297. </div>
  298.  
  299. <?php
  300.  
  301. }
  302. ?>
  303. <div class="blockform">
  304. <h2><span><?php echo $lang_common['Login'] ?></span></h2>
  305. <div class="box">
  306. <form id="login" method="post" action="login.php?action=in" onsubmit="return process_form(this)">
  307. <div class="inform">
  308. <fieldset>
  309. <legend><?php echo $lang_login['Login legend'] ?></legend>
  310. <div class="infldset">
  311. <input type="hidden" name="form_sent" value="1" />
  312. <input type="hidden" name="redirect_url" value="<?php echo pun_htmlspecialchars($redirect_url) ?>" />
  313. <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" value="<?php if (isset($_POST['req_username'])) echo pun_htmlspecialchars($_POST['req_username']); ?>" size="25" maxlength="25" tabindex="1" /><br /></label>
  314. <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>
  315.  
  316. <div class="rbox clearb">
  317. <label><input type="checkbox" name="save_pass" value="1"<?php if (isset($_POST['save_pass'])) echo ' checked="checked"'; ?> tabindex="3" /><?php echo $lang_login['Remember me'] ?><br /></label>
  318. </div>
  319.  
  320. <p class="clearb"><?php echo $lang_login['Login info'] ?></p>
  321. <p class="actions"><span><a href="register.php" tabindex="5"><?php echo $lang_login['Not registered'] ?></a></span> <span><a href="login.php?action=forget" tabindex="6"><?php echo $lang_login['Forgotten pass'] ?></a></span></p>
  322. </div>
  323. </fieldset>
  324. </div>
  325. <?php flux_hook('login_before_submit') ?>
  326. <p class="buttons"><input type="submit" name="login" value="<?php echo $lang_common['Login'] ?>" tabindex="4" /></p>
  327. </form>
  328. </div>
  329. </div>
  330. <?php
  331.  
  332. require PUN_ROOT.'footer.php';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement