Advertisement
Guest User

Untitled

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