From e6f314e53a1d1656270b85adc854126539b91b39 Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Sun, 11 Jul 2021 12:44:31 -0400 Subject: [PATCH] Work on user.php; untested --- includes/common.php | 12 +- includes/functions.php | 18 +-- includes/password.php | 59 +-------- profile.php | 162 ++++--------------------- user.php | 264 ++++++++++------------------------------- 5 files changed, 99 insertions(+), 416 deletions(-) diff --git a/includes/common.php b/includes/common.php index 170ab75..04fb377 100644 --- a/includes/common.php +++ b/includes/common.php @@ -7,27 +7,27 @@ require_once('config.php'); require_once('includes/functions.php'); -function getSiteInfo($conn) { +function getSiteInfo(PDO $conn) : array { return $conn->query('SELECT * FROM site_info LIMIT 1')->fetch(); } -function getSiteLangAndTheme($conn) { +function getSiteLangAndTheme(PDO $conn) : array { return $conn->query('SELECT lang, theme FROM interface LIMIT 1')->fetch(); } -function getSitePermissions($conn) { +function getSitePermissions(PDO $conn) : array { return $conn->query('SELECT * FROM site_permissions LIMIT 1')->fetch(); } -function getSiteAds($conn) { +function getSiteAds(PDO $conn) : array | bool { return $conn->query('SELECT text_ads, ads_1, ads_2 FROM ads LIMIT 1')->fetch(); } -function getSiteTotalPastes($conn) { +function getSiteTotalPastes(PDO $conn) : int { return intval($conn->query('SELECT COUNT(*) FROM pastes')->fetch(PDO::FETCH_NUM)[0]); } -function updatePageViews($conn) { +function updatePageViews(PDO $conn) : void { $ip = $_SERVER['REMOTE_ADDR']; $date = date('jS F Y'); $data_ip = file_get_contents('tmp/temp.tdata'); diff --git a/includes/functions.php b/includes/functions.php index 61f2f17..ba84f35 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -307,20 +307,14 @@ function isValidUsername($str) { return !preg_match('/[^A-Za-z0-9._\\-$]/', $str); } -function existingUser( $conn, $username ) { - $query = "SELECT username FROM users WHERE username = '$username'"; - $result = mysqli_query( $conn, $query ); - $num_rows = mysqli_num_rows( $result ); - if ( $num_rows == 0 ) { - // No records. User doesn't exist. - return false; - } else { - return true; - } +function existingUser(PDO $conn, string $username) : bool { + $query = $conn->prepare('SELECT 1 FROM users WHERE username = ?'); + $query->execute([$username]); + + return (bool) $query->fetch(); } -function updateMyView($conn, $paste_id) -{ +function updateMyView($conn, $paste_id) { $query = $conn->prepare("SELECT views, id FROM pastes WHERE id= ?"); $query->execute([$paste_id]); if ($row = $query->fetch()) { diff --git a/includes/password.php b/includes/password.php index c99d3bf..f1be384 100644 --- a/includes/password.php +++ b/includes/password.php @@ -164,63 +164,6 @@ namespace { return $ret; } - /** - * Get information about the password hash. Returns an array of the information - * that was used to generate the password hash. - * - * array( - * 'algo' => 1, - * 'algoName' => 'bcrypt', - * 'options' => array( - * 'cost' => PASSWORD_BCRYPT_DEFAULT_COST, - * ), - * ) - * - * @param string $hash The password hash to extract info from - * - * @return array The array of information about the hash. - */ - function password_get_info($hash) { - $return = array( - 'algo' => 0, - 'algoName' => 'unknown', - 'options' => array(), - ); - if (PasswordCompat\binary\_substr($hash, 0, 4) == '$2y$' && PasswordCompat\binary\_strlen($hash) == 60) { - $return['algo'] = PASSWORD_BCRYPT; - $return['algoName'] = 'bcrypt'; - list($cost) = sscanf($hash, "$2y$%d$"); - $return['options']['cost'] = $cost; - } - return $return; - } - - /** - * Determine if the password hash needs to be rehashed according to the options provided - * - * If the answer is true, after validating the password using password_verify, rehash it. - * - * @param string $hash The hash to test - * @param int $algo The algorithm used for new password hashes - * @param array $options The options array passed to password_hash - * - * @return boolean True if the password needs to be rehashed. - */ - function password_needs_rehash($hash, $algo, array $options = array()) { - $info = password_get_info($hash); - if ($info['algo'] !== (int) $algo) { - return true; - } - switch ($algo) { - case PASSWORD_BCRYPT: - $cost = isset($options['cost']) ? (int) $options['cost'] : PASSWORD_BCRYPT_DEFAULT_COST; - if ($cost !== $info['options']['cost']) { - return true; - } - break; - } - return false; - } /** * Verify a password against a hash using a timing attack resistant approach @@ -230,7 +173,7 @@ namespace { * * @return boolean If the password matches the hash */ - function password_verify($password, $hash) { + function password_verify(string $password, string $hash): bool { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING); return false; diff --git a/profile.php b/profile.php index 70bfa46..1f36d4a 100644 --- a/profile.php +++ b/profile.php @@ -24,179 +24,59 @@ header('Content-Type: text/html; charset=utf-8'); $date = date('jS F Y'); $ip = $_SERVER['REMOTE_ADDR']; -$data_ip = file_get_contents('tmp/temp.tdata'); -$con = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname); -if (mysqli_connect_errno()) { - die("Unable to connect to database"); -} -$query = "SELECT * FROM site_info"; -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $title = Trim($row['title']); - $des = Trim($row['des']); - $baseurl = Trim($row['baseurl']); - $keyword = Trim($row['keyword']); - $site_name = Trim($row['site_name']); - $email = Trim($row['email']); - $twit = Trim($row['twit']); - $face = Trim($row['face']); - $gplus = Trim($row['gplus']); - $ga = Trim($row['ga']); - $additional_scripts = Trim($row['additional_scripts']); -} - -// Set theme and language -$query = "SELECT * FROM interface"; -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $default_lang = Trim($row['lang']); - $default_theme = Trim($row['theme']); -} -require_once("langs/$default_lang"); $p_title = $lang['myprofile']; //"My Profile"; -// Check if IP is banned -if ( is_banned($con, $ip) ) die($lang['banned']); // "You have been banned from ".$site_name; - -// Site permissions -$query = "SELECT * FROM site_permissions where id='1'"; -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $siteprivate = Trim($row['siteprivate']); -} - -if ($_SERVER['REQUEST_METHOD'] == 'POST') { -} else { - if ($siteprivate =="on") { - $privatesite = "on"; - } -} // Check if already logged in if (isset($_SESSION['token'])) { } else { header("Location: ./login.php"); } +$user_username = htmlentities(trim($_SESSION['username'])); -// Logout -if (isset($_GET['logout'])) { - header('Location: ' . $_SERVER['HTTP_REFERER']); - unset($_SESSION['token']); - unset($_SESSION['oauth_uid']); - unset($_SESSION['username']); - session_destroy(); -} +$query = $conn->prepare('SELECT * FROM users WHERE username = ?'); +$query->execute([$user_username]); +$row = $query->fetch(); +$user_oauth_uid = $row['oauth_uid']; +$user_id = $row['id']; +$user_email_id = $row['email_id']; +$user_full_name = $row['full_name']; +$user_platform = Trim($row['platform']); +$user_verified = $row['verified']; +$user_date = $row['date']; +$user_ip = $row['ip']; +$user_password = $row['password']; - -$user_username = htmlentities(Trim($_SESSION['username'])); -$query = "SELECT * FROM users WHERE username='$user_username'"; -$result = mysqli_query($con, $query); -while ($row = mysqli_fetch_array($result)) { - $user_oauth_uid = $row['oauth_uid']; - $user_id = $row['id']; - $user_email_id = $row['email_id']; - $user_full_name = $row['full_name']; - $user_platform = Trim($row['platform']); - $user_verified = $row['verified']; - $user_date = $row['date']; - $user_ip = $row['ip']; - $user_password = $row['password']; -} if ($user_oauth_uid == '0') { $user_oauth_uid = "None"; } if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['cpassword'])) { - $user_new_full = Trim(htmlspecialchars($_POST['full'])); + $user_new_full = trim(htmlspecialchars($_POST['full'])); $user_old_pass = $_POST['old_password']; - $user_new_cpass = password_hash($_POST['password'], PASSWORD_DEFAULT); if (password_verify($user_old_pass, $user_password)) { - $query = "UPDATE users SET full_name='$user_new_full', password='$user_new_cpass' WHERE username='$user_username'"; - $result = mysqli_query($con, $query); - if (mysqli_errno($con)) { - $success = $lang['profileerror']; //" Unable to update the profile information "; - } else { - $success = $lang['profileupdated']; //" Your profile information is updated "; - } + $user_new_cpass = password_hash($_POST['password'], PASSWORD_DEFAULT); + + $conn->prepare('UPDATE users SET full_name = ?, password = ? WHERE username = ?') + ->execute([$user_new_full, $user_new_cpass, $user_username]); + + $success = $lang['profileupdated']; //" Your profile information is updated "; } else { $error = $lang['oldpasswrong']; // " Your old password is wrong."; } - } else { $error = $lang['error']; //"Something went wrong."; } - - } -// Page views -$query = "SELECT @last_id := MAX(id) FROM page_view"; +updatePageViews($conn); -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $last_id = $row['@last_id := MAX(id)']; -} - -$query = "SELECT * FROM page_view WHERE id=" . Trim($last_id); -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $last_date = $row['date']; -} - -if ($last_date == $date) { - if (str_contains($data_ip, $ip)) { - $query = "SELECT * FROM page_view WHERE id=" . Trim($last_id); - $result = mysqli_query($con, $query); - - while ($row = mysqli_fetch_array($result)) { - $last_tpage = Trim($row['tpage']); - } - $last_tpage = $last_tpage + 1; - - // IP already exists, update page views. - $query = "UPDATE page_view SET tpage=$last_tpage WHERE id=" . Trim($last_id); - mysqli_query($con, $query); - } else { - $query = "SELECT * FROM page_view WHERE id=" . Trim($last_id); - $result = mysqli_query($con, $query); - - while ($row = mysqli_fetch_array($result)) { - $last_tpage = Trim($row['tpage']); - $last_tvisit = Trim($row['tvisit']); - } - $last_tpage = $last_tpage + 1; - $last_tvisit = $last_tvisit + 1; - - // Update both tpage and tvisit. - $query = "UPDATE page_view SET tpage=$last_tpage,tvisit=$last_tvisit WHERE id=" . Trim($last_id); - mysqli_query($con, $query); - file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip); - } -} else { - // Delete the file and clear data_ip - unlink("tmp/temp.tdata"); - $data_ip = ""; - - // New date is created - $query = "INSERT INTO page_view (date,tpage,tvisit) VALUES ('$date','1','1')"; - mysqli_query($con, $query); - - // Update the IP - file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip); - -} $total_pastes = getTotalPastes($con, $user_username); // Theme require_once('theme/' . $default_theme . '/header.php'); require_once('theme/' . $default_theme . '/profile.php'); require_once('theme/' . $default_theme . '/footer.php'); -?> \ No newline at end of file diff --git a/user.php b/user.php index 3d3897f..4febdbc 100644 --- a/user.php +++ b/user.php @@ -22,226 +22,93 @@ header('Content-Type: text/html; charset=utf-8'); $date = date('jS F Y'); $ip = $_SERVER['REMOTE_ADDR']; -$data_ip = file_get_contents('tmp/temp.tdata'); -$con = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname); -if (mysqli_connect_errno()) { - die("Unable to connect to database"); -} -$query = "SELECT * FROM site_info"; -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $title = Trim($row['title']); - $des = Trim($row['des']); - $baseurl = Trim($row['baseurl']); - $keyword = Trim($row['keyword']); - $site_name = Trim($row['site_name']); - $email = Trim($row['email']); - $twit = Trim($row['twit']); - $face = Trim($row['face']); - $gplus = Trim($row['gplus']); - $ga = Trim($row['ga']); - $additional_scripts = Trim($row['additional_scripts']); -} - -// Set theme and language -$query = "SELECT * FROM interface"; -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $default_lang = Trim($row['lang']); - $default_theme = Trim($row['theme']); -} -require_once("langs/$default_lang"); - -// Check if IP is banned -if ( is_banned($con, $ip) ) die($lang['banned']); // "You have been banned from ".$site_name; - -// Site permissions -$query = "SELECT * FROM site_permissions where id='1'"; -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $siteprivate = Trim($row['siteprivate']); -} - -if ($_SERVER['REQUEST_METHOD'] == 'POST') { -} else { - if ($siteprivate =="on") { - $privatesite = "on"; - } -} - // If username defined in URL, then check if it's exists in database. If invalid, redirect to main site. -$user_username = Trim($_SESSION['username']); -if ( isset( $_GET['user'] ) ) { - $profile_username = trim( $_GET['user'] ); - if ( !existingUser( $con, $profile_username ) ) { +$user_username = trim($_SESSION['username']); +if (isset($_GET['user'])) { + $profile_username = trim($_GET['user']); + + if (!existingUser( $con, $profile_username ) ) { // Invalid username header("Location: ../error.php"); + die(); } } else { // No access to user.php header("Location: ../error.php"); + die(); } $p_title = $profile_username . $lang['user_public_pastes']; // "Username's Public Pastes" -//Favorite Counts -$query = "SELECT pins.f_time, pins.m_fav, pins.f_paste, pastes.member, pastes.title, pastes.now_time, pastes.tagsys FROM pins, pastes WHERE pins.f_paste = pastes.id AND pastes.member='$profile_username'"; -$result = mysqli_query($con, $query); -$total_pfav = 0; -while ($row = mysqli_fetch_array($result)) { - $total_pfav = $total_pfav + 1; - } - -$query = "SELECT pins.f_time, pins.m_fav, pins.f_paste, pastes.id, pastes.title, pastes.now_time, pastes.tagsys FROM pins, pastes WHERE pins.f_paste = pastes.id AND pins.m_fav='$profile_username'"; -$result = mysqli_query($con, $query); -$total_yfav = 0; -while ($row = mysqli_fetch_array($result)) { - $total_yfav = $total_yfav + 1; -} - -//Badges System -$query = "SELECT * FROM users where username = '$profile_username'"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_badge = $row['badge']; - switch ($profile_badge) { - case 1: - $profile_badge = ''; - break; - case 2: - $profile_badge = ''; - break; - case 3: - $profile_badge = ''; - break; - } -} - -$query = "SELECT count(*) as count FROM pastes where member = '$profile_username'"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_total_pastes = $row['count']; -} -$query = "SELECT count(*) as count FROM pastes where member = '$profile_username' and visible = 0"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_total_public = $row['count']; -} -$query = "SELECT count(*) as count FROM pastes where member = '$profile_username' and visible = 1"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_total_unlisted = $row['count']; -} -$query = "SELECT count(*) as count FROM pastes where member = '$profile_username' and visible = 2"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_total_private = $row['count']; -} -$query = "SELECT sum(views) as total FROM pastes where member = '$profile_username'"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_total_paste_views = $row['total']; -} -$query = "SELECT date FROM users where username = '$profile_username'"; -$result = mysqli_query( $con, $query ); -while ($row = mysqli_fetch_array($result)) { - $profile_join_date = $row['date']; -} +// Favorite Counts +$query = $conn->prepare( + 'SELECT COUNT(*) FROM pins INNER JOIN pastes ON pins.f_paste = pastes.id WHERE pastes.member = ?' +); +$query->execute([$profile_username]); +$total_pfav = intval($query->fetch(PDO::FETCH_NUM)[0]); -// Logout -if (isset($_GET['logout'])) { - unset($_SESSION['token']); - unset($_SESSION['oauth_uid']); - unset($_SESSION['username']); - session_destroy(); - header("location:index.php"); - exit(); -} +$query = $conn->prepare( + 'SELECT COUNT(*) FROM pins INNER JOIN pastes ON pins.f_paste = pastes.id WHERE pins.m_fav = ?' +); +$query->execute([$profile_username]); +$total_yfav = intval($query->fetch(PDO::FETCH_NUM)[0]); -// Page views -$query = "SELECT @last_id := MAX(id) FROM page_view"; +// Badges +$query = $conn->prepare('SELECT badge FROM users WHERE username = ?'); +$query->execute([$profile_username]); -$result = mysqli_query($con, $query); +$profile_badge = match ($query->fetch()['badge']) { + 1 => '', + 2 => '', + 3 => '', + default => '', +}; -while ($row = mysqli_fetch_array($result)) { - $last_id = $row['@last_id := MAX(id)']; -} - -$query = "SELECT * FROM page_view WHERE id=" . Trim($last_id); -$result = mysqli_query($con, $query); - -while ($row = mysqli_fetch_array($result)) { - $last_date = $row['date']; -} - -if ($last_date == $date) { - if (str_contains($data_ip, $ip)) { - $query = "SELECT * FROM page_view WHERE id=" . Trim($last_id); - $result = mysqli_query($con, $query); - - while ($row = mysqli_fetch_array($result)) { - $last_tpage = Trim($row['tpage']); - } - $last_tpage = $last_tpage + 1; - - // IP already exists, update page views - $query = "UPDATE page_view SET tpage=$last_tpage WHERE id=" . Trim($last_id); - mysqli_query($con, $query); - } else { - $query = "SELECT * FROM page_view WHERE id=" . Trim($last_id); - $result = mysqli_query($con, $query); - - while ($row = mysqli_fetch_array($result)) { - $last_tpage = Trim($row['tpage']); - $last_tvisit = Trim($row['tvisit']); - } - $last_tpage = $last_tpage + 1; - $last_tvisit = $last_tvisit + 1; - - // Update both tpage and tvisit. - $query = "UPDATE page_view SET tpage=$last_tpage,tvisit=$last_tvisit WHERE id=" . Trim($last_id); - mysqli_query($con, $query); - file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip); - } -} else { - // Delete the file and clear data_ip - unlink("tmp/temp.tdata"); - $data_ip = ""; - - // New date is created - $query = "INSERT INTO page_view (date,tpage,tvisit) VALUES ('$date','1','1')"; - mysqli_query($con, $query); - - // Update the IP - file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip); - -} +$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE member = ?'); +$query->execute([$profile_username]); +$profile_total_pastes = intval($query->fetch(PDO::FETCH_NUM)[0]); +$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE member = ? AND visible = 0'); +$query->execute([$profile_username]); +$profile_total_public = intval($query->fetch(PDO::FETCH_NUM)[0]); -if ( isset($_GET['del']) ) { - if ( $_SESSION['token'] ) { // Prevent unauthorized deletes - $paste_id = htmlentities( Trim( $_GET['id'] ) ); - // Check if logged in user owns the paste - $query = "SELECT * FROM pastes WHERE id='$paste_id' and member='$user_username'"; - $result = mysqli_query($con, $query); - $num_rows = mysqli_num_rows($result); - if ( $num_rows == 0 ) { +$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE member = ? AND visible = 1'); +$query->execute([$profile_username]); +$profile_total_unlisted = intval($query->fetch(PDO::FETCH_NUM)[0]); + +$query = $conn->prepare('SELECT COUNT(*) FROM pastes WHERE member = ? AND visible = 2'); +$query->execute([$profile_username]); +$profile_total_private = intval($query->fetch(PDO::FETCH_NUM)[0]); + +$query = $conn->prepare('SELECT SUM(views) FROM pastes WHERE member = ?'); +$query->execute([$profile_username]); +$profile_total_paste_views = intval($query->fetch(PDO::FETCH_NUM)[0]); + + +$query = $conn->prepare('SELECT date FROM users WHERE username = ?'); +$query->execute([$profile_username]); +$profile_join_date = $query->fetch()['date']; + + +updatePageViews($conn); + +if (isset($_GET['del'])) { + if ($_SESSION['token']) { // Prevent unauthorized deletes + $paste_id = intval(trim($_GET['id'])); + + $query = $conn->prepare('SELECT member FROM pastes WHERE id = ?'); + $query->execute([$paste_id]); + $result = $query->fetch(); + + if (empty($result) || $result['member'] !== $user_username) { $error = $lang['delete_error_invalid']; // Does not exist or not paste owner } else { - $query = "DELETE FROM pastes WHERE id='$paste_id' and member='$user_username'"; - $result = mysqli_query($con, $query); - - if ( mysqli_errno( $con ) ) { - $error = $lang['error']; // "Something went wrong"; - } else { - $success = $lang['pastedeleted']; // "Paste deleted successfully."; - } + $query = $conn->prepare('DELETE FROM pastes WHERE id = ?'); + $query->execute([$paste_id]); + $success = $lang['pastedeleted']; // "Paste deleted successfully." } } else { $error = $lang['not_logged_in']; // Must be logged in to do that @@ -252,4 +119,3 @@ if ( isset($_GET['del']) ) { require_once('theme/' . $default_theme . '/header.php'); require_once('theme/' . $default_theme . '/user_profile.php'); require_once('theme/' . $default_theme . '/footer.php'); -?> \ No newline at end of file