2021-07-10 19:18:17 +01:00
< ? php
/*
* Paste < https :// github . com / jordansamuel / PASTE >
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 3
* of the License , or ( at your option ) any later version .
2021-07-11 12:50:24 -04:00
*
2021-07-10 19:18:17 +01:00
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License in GPL . txt for more details .
*/
// Required functions
2021-07-12 10:44:39 -04:00
define ( 'IN_PONEPASTE' , 1 );
2021-07-10 18:21:03 -04:00
require_once ( 'includes/common.php' );
2021-07-10 19:18:17 +01:00
require_once ( 'includes/functions.php' );
2021-07-17 12:33:08 -04:00
require_once ( 'includes/passwords.php' );
2021-07-15 12:40:12 -04:00
2021-07-10 19:18:17 +01:00
// Current Date & User IP
2021-07-11 12:50:24 -04:00
$date = date ( 'jS F Y' );
$ip = $_SERVER [ 'REMOTE_ADDR' ];
2021-07-10 19:18:17 +01:00
2021-07-15 12:40:12 -04:00
2021-07-10 19:18:17 +01:00
// Check if already logged in
2021-07-17 18:17:29 -04:00
if ( $current_user !== null ) {
2021-07-11 12:50:24 -04:00
header ( " Location: ./ " );
2021-07-17 18:17:29 -04:00
die ();
2021-07-10 19:18:17 +01:00
}
// Page title
2021-07-10 18:21:03 -04:00
$p_title = $lang [ 'login/register' ]; // "Login/Register";
2021-07-10 19:18:17 +01:00
2021-07-10 18:21:03 -04:00
updatePageViews ( $conn );
2021-07-10 19:18:17 +01:00
2021-07-16 10:32:25 -04:00
if ( isset ( $_POST [ 'forgot' ])) {
if ( ! empty ( $_POST [ 'username' ]) && ! empty ( $_POST [ 'recovery_code' ])) {
$username = trim ( $_POST [ 'username' ]);
$recovery_code = trim ( $_POST [ 'recovery_code' ]);
2021-07-24 14:45:46 -04:00
$query = $conn -> query ( " SELECT id, recovery_code_hash FROM users WHERE username = ? " , [ $username ]);
2021-07-16 10:32:25 -04:00
$row = $query -> fetch ();
2021-07-24 14:45:46 -04:00
2021-07-17 12:33:08 -04:00
if ( $row && pp_password_verify ( $_POST [ 'recovery_code' ], $row [ 'recovery_code_hash' ])) {
2021-07-24 14:45:46 -04:00
$new_password = pp_random_password ();
2021-07-17 12:33:08 -04:00
$new_password_hash = pp_password_hash ( $new_password );
2021-07-16 10:32:25 -04:00
2021-07-24 14:45:46 -04:00
$recovery_code = pp_random_token ();
2021-07-17 12:33:08 -04:00
$new_recovery_code_hash = pp_password_hash ( $recovery_code );
2021-07-16 10:32:25 -04:00
$conn -> prepare ( 'UPDATE users SET password = ?, recovery_code_hash = ? WHERE id = ?' )
-> execute ([ $new_password_hash , $new_recovery_code_hash , $row [ 'id' ]]);
$success = 'Your password has been changed. A new recovery code has also been generated. Please note the recovery code and then sign in with the new password.' ;
} else {
$error = $lang [ 'incorrect' ];
}
} else {
$error = $lang [ 'missingfields' ]; // "All fields must be filled out";
2021-07-15 12:40:12 -04:00
}
2021-07-26 17:42:43 -04:00
} elseif ( isset ( $_POST [ 'signin' ])) { // Login process
2021-07-16 10:32:25 -04:00
if ( ! empty ( $_POST [ 'username' ]) && ! empty ( $_POST [ 'password' ])) {
2021-07-26 17:42:43 -04:00
$remember_me = ( bool ) $_POST [ 'remember_me' ];
2021-07-16 10:32:25 -04:00
$username = trim ( $_POST [ 'username' ]);
2021-07-24 14:45:46 -04:00
$row = $conn -> query ( " SELECT id, password, banned FROM users WHERE username = ? " , [ $username ])
2021-07-26 17:42:43 -04:00
-> fetch ();
2021-07-24 14:45:46 -04:00
2021-07-17 12:33:08 -04:00
$needs_rehash = false ;
2021-07-24 14:45:46 -04:00
/* This is designed to be a constant time lookup , hence the warning suppression operator so that
* we always call pp_password_verify , even if row is null .
*/
if ( pp_password_verify ( $_POST [ 'password' ], @ $row [ 'password' ], $needs_rehash )) {
$user_id = $row [ 'id' ];
2021-07-11 12:50:24 -04:00
2021-07-17 12:33:08 -04:00
if ( $needs_rehash ) {
$new_password_hash = pp_password_hash ( $_POST [ 'password' ]);
2021-07-24 14:45:46 -04:00
$conn -> query ( 'UPDATE users SET password = ? WHERE id = ?' ,
2021-07-26 17:42:43 -04:00
[ $new_password_hash , $user_id ]);
2021-07-17 12:33:08 -04:00
}
2021-07-16 10:32:25 -04:00
if ( $row [ 'banned' ]) {
// User is banned
$error = $lang [ 'banned' ];
2021-07-17 12:36:21 -04:00
} else {
2021-07-16 10:32:25 -04:00
// Login successful
2021-07-26 17:42:43 -04:00
$_SESSION [ 'user_id' ] = ( string ) $user_id ;
2021-07-24 14:45:46 -04:00
if ( $remember_me ) {
$remember_token = pp_random_token ();
2021-07-24 15:12:19 -04:00
$expire_at = ( new DateTime ()) -> add ( new DateInterval ( 'P1Y' ));
2021-07-24 14:45:46 -04:00
2021-07-24 15:12:19 -04:00
$conn -> query ( 'INSERT INTO user_sessions (user_id, token, expire_at) VALUES (?, ?, FROM_UNIXTIME(?))' , [ $user_id , $remember_token , $expire_at -> format ( 'U' )]);
2021-07-24 14:45:46 -04:00
setcookie ( User :: REMEMBER_TOKEN_COOKIE , $remember_token , [
2021-07-26 17:42:43 -04:00
'expires' => ( int ) $expire_at -> format ( 'U' ),
2021-07-24 14:45:46 -04:00
'secure' => ! empty ( $_SERVER [ 'HTTPS' ]), /* Local dev environment is non-HTTPS */
'httponly' => true ,
'samesite' => 'Lax'
]);
}
2021-07-16 10:32:25 -04:00
header ( 'Location: ' . $_SERVER [ 'HTTP_REFERER' ]);
exit ();
2021-07-10 19:18:17 +01:00
}
2021-07-15 12:40:12 -04:00
} else {
2021-07-16 10:32:25 -04:00
// Username not found or password incorrect.
$error = $lang [ 'incorrect' ];
2021-07-10 19:18:17 +01:00
}
2021-07-16 10:32:25 -04:00
} else {
$error = $lang [ 'missingfields' ]; // "All fields must be filled out.";
2021-07-10 19:18:17 +01:00
}
2021-07-26 17:42:43 -04:00
} elseif ( isset ( $_POST [ 'signup' ])) { // Registration process
2021-07-11 12:50:24 -04:00
$username = htmlentities ( trim ( $_POST [ 'username' ], ENT_QUOTES ));
2021-07-17 12:33:08 -04:00
$password = pp_password_hash ( $_POST [ 'password' ]);
2021-07-11 12:50:24 -04:00
$chara_max = 25 ; //characters for max input
2021-07-16 10:08:21 -04:00
if ( empty ( $_POST [ 'password' ]) || empty ( $_POST [ 'username' ])) {
2021-07-15 12:40:12 -04:00
$error = $lang [ 'missingfields' ]; // "All fields must be filled out";
} elseif ( strlen ( $username ) > $chara_max ) {
$error = $lang [ 'maxnamelimit' ]; // "Username already taken.";
} elseif ( ! isValidUsername ( $username )) {
$error = $lang [ 'usrinvalid' ]; // "Username not valid. Usernames can't contain special characters.";
2021-07-11 12:50:24 -04:00
} else {
2021-07-24 14:45:46 -04:00
$query = $conn -> query ( 'SELECT 1 FROM users WHERE username = ?' , [ $username ]);
2021-07-16 10:08:21 -04:00
2021-07-15 12:40:12 -04:00
if ( $query -> fetch ()) {
$error = $lang [ 'userexists' ]; // "Username already taken.";
2021-07-11 12:50:24 -04:00
} else {
2021-07-24 14:45:46 -04:00
$recovery_code = pp_random_token ();
2021-07-17 12:33:08 -04:00
$recovery_code_hash = pp_password_hash ( $recovery_code );
2021-07-16 10:08:21 -04:00
$query = $conn -> prepare (
2021-07-16 10:32:25 -04:00
" INSERT INTO users (username, password, recovery_code_hash, picture, date, ip, badge) VALUES (?, ?, ?, 'NONE', ?, ?, '0') "
2021-07-16 10:08:21 -04:00
);
2021-07-16 10:32:25 -04:00
$query -> execute ([ $username , $password , $recovery_code_hash , $date , $ip ]);
2021-07-15 12:40:12 -04:00
2021-07-16 10:08:21 -04:00
$success = $lang [ 'registered' ]; // "Your account was successfully registered.";
2021-07-10 19:18:17 +01:00
}
}
}
// Theme
require_once ( 'theme/' . $default_theme . '/header.php' );
require_once ( 'theme/' . $default_theme . '/login.php' );
require_once ( 'theme/' . $default_theme . '/footer.php' );