From 82c956d9e18a2cfb7d299b1c82ff374707bccfdc Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Fri, 27 Aug 2021 19:24:48 -0400 Subject: [PATCH] Does the site even work anymore? Probably not. --- api/tags_autocomplete.php | 1 - discover.php | 73 +++---------------------------- includes/SessionManager.class.php | 13 ++++-- includes/models/Paste.php | 38 ++++++++++++++++ includes/models/User.php | 2 - includes/models/UserSession.php | 5 --- index.php | 1 - logout.php | 4 +- paste.php | 1 - scripts/convert_tags.php | 1 - 10 files changed, 55 insertions(+), 84 deletions(-) diff --git a/api/tags_autocomplete.php b/api/tags_autocomplete.php index d25097b..00c5047 100644 --- a/api/tags_autocomplete.php +++ b/api/tags_autocomplete.php @@ -2,7 +2,6 @@ define('IN_PONEPASTE', 1); require_once(__DIR__ . '/../includes/common.php'); -require_once(__DIR__ . '/../includes/Tag.class.php'); /* get rid of unintended wildcards in a parameter to LIKE queries; not a security issue, just unexpected behaviour. */ function escapeLikeQuery(string $query) : string { diff --git a/discover.php b/discover.php index d1e6108..2932be9 100644 --- a/discover.php +++ b/discover.php @@ -3,66 +3,7 @@ define('IN_PONEPASTE', 1); require_once('includes/common.php'); require_once('includes/functions.php'); -function getMonthPopularPastes(DatabaseHandle $conn, int $count) : array { - $query = $conn->prepare( - "SELECT pastes.id AS id, title, created_at, updated_at, users.username AS member - FROM pastes - INNER JOIN users ON users.id = pastes.user_id - WHERE MONTH(created_at) = MONTH(NOW()) AND visible = '0' ORDER BY views DESC LIMIT ?"); - $query->execute([$count]); - return $query->fetchAll(); -} - -function getRecentUpdatesPastes(DatabaseHandle $conn, int $count) : array { - $query = $conn->prepare( - "SELECT pastes.id AS id, title, created_at, updated_at, users.username AS member - FROM pastes - INNER JOIN users ON users.id = pastes.user_id - WHERE visible = '0' ORDER BY updated_at DESC - LIMIT ?"); - $query->execute([$count]); - return $query->fetchAll(); -} - -function getRecentCreatedPastes(DatabaseHandle $conn, int $count) : array { - $query = $conn->prepare(" - SELECT pastes.id, title, created_at, updated_at, users.username AS member - FROM pastes - INNER JOIN users ON pastes.user_id = users.id - WHERE visible = '0' - ORDER BY created_at DESC - LIMIT ?"); - $query->execute([$count]); - return $query->fetchAll(); -} - -function getMostViewedPastes(DatabaseHandle $conn, int $count) : array { - $query = $conn->prepare(" - SELECT pastes.id AS id, title, created_at, updated_at, views, users.username AS member - FROM pastes INNER JOIN users ON users.id = pastes.user_id - WHERE visible = '0' - ORDER BY views DESC - LIMIT ? - "); - $query->execute([$count]); - return $query->fetchAll(); -} - -function getRandomPastes(DatabaseHandle $conn, int $count) : array { - $query = $conn->prepare(" - SELECT pastes.id, title, created_at, updated_at, views, users.username AS member - FROM pastes - INNER JOIN users ON users.id = pastes.user_id - WHERE visible = '0' - ORDER BY RAND() - LIMIT ?"); - $query->execute([$count]); - return $query->fetchAll(); -} - -function transformPasteRow(array $row) : array { - global $conn; - +function transformPasteRow(Paste $row) : array { return [ 'id' => $row['id'], 'title' => $row['title'], @@ -71,15 +12,15 @@ function transformPasteRow(array $row) : array { 'time_update' => $row['updated_at'], 'friendly_update_time' => friendlyDateDifference(new DateTime($row['updated_at']), new DateTime()), 'friendly_time' => friendlyDateDifference(new DateTime($row['created_at']), new DateTime()), - 'tags' => getPasteTags($conn, $row['id']) + 'tags' => $row->tags ]; } -$popular_pastes = array_map('transformPasteRow', getMostViewedPastes($conn, 10)); -$monthly_popular_pastes = array_map('transformPasteRow', getMonthPopularPastes($conn, 10)); -$recent_pastes = array_map('transformPasteRow', getRecentCreatedPastes($conn, 10)); -$updated_pastes = array_map('transformPasteRow', getRecentUpdatesPastes($conn, 10)); -$random_pastes = array_map('transformPasteRow', getRandomPastes($conn, 10)); +$popular_pastes = Paste::getMostViewed()->map('transformPasteRow'); +$monthly_popular_pastes = Paste::getMonthPopular()->map('transformPasteRow'); +$recent_pastes = Paste::getRecent()->map('transformPasteRow'); +$updated_pastes = Paste::getRecentlyUpdated()->map('transformPasteRow'); +$random_pastes = Paste::getRandom()->map('transformPasteRow'); // Theme $page_template = 'discover'; diff --git a/includes/SessionManager.class.php b/includes/SessionManager.class.php index c0ba641..2fe61bb 100644 --- a/includes/SessionManager.class.php +++ b/includes/SessionManager.class.php @@ -24,6 +24,15 @@ class SessionHelper { return null; } + public static function destroySession() { + $token = $_COOKIE[SessionHelper::REMEMBER_TOKEN_COOKIE]; + + \UserSession::where('token', $token)->delete(); + + unset($_COOKIE[SessionHelper::REMEMBER_TOKEN_COOKIE]); + setcookie(SessionHelper::REMEMBER_TOKEN_COOKIE, null, time() - 3600); + } + private static function currentUserFromRememberToken(string $remember_token) { $session = \UserSession ::with('user') @@ -54,8 +63,4 @@ class SessionHelper { return \User::find(intval($_SESSION['user_id'])); } - - public static function destroySession(DatabaseHandle $conn, string $token) { - $conn->query('DELETE FROM user_sessions WHERE user_id = ? AND token = ?', [$this->user_id, $token]); - } } \ No newline at end of file diff --git a/includes/models/Paste.php b/includes/models/Paste.php index 69d744f..1ed3fcc 100644 --- a/includes/models/Paste.php +++ b/includes/models/Paste.php @@ -1,4 +1,6 @@ belongsToMany(Tag::class, 'paste_taggings'); } + + public static function getRecent(int $count = 10) : Collection { + return Paste::with('user') + ->orderBy('created_at', 'DESC') + ->where('visible', 0) + ->limit($count)->get(); + } + + public static function getRecentlyUpdated(int $count = 10) : Collection { + return Paste::with('user') + ->orderBy('updated_at', 'DESC') + ->where('visible', 0) + ->limit($count)->get(); + } + + public static function getMostViewed(int $count = 10) : Collection { + return Paste::with('user') + ->orderBy('views') + ->where('visible', 0) + ->limit($count)->get(); + } + + public static function getMonthPopular(int $count = 10) : Collection { + return Paste::with('user') + ->whereRaw('MONTH(created_at) = MONTH(NOW())') + ->where('visible', 0) + ->orderBy('views') + ->limit($count)->get(); + } + + public static function getRandom(int $count = 10) : Collection { + return Paste::with('user') + ->orderByRaw('RAND()') + ->where('visible', 0) + ->limit($count)->get(); + } } diff --git a/includes/models/User.php b/includes/models/User.php index f25f820..c46ae5e 100644 --- a/includes/models/User.php +++ b/includes/models/User.php @@ -16,7 +16,5 @@ class User extends Model { /*public function pastes() { return $this->hasMany(Paste::class); }*/ - - } diff --git a/includes/models/UserSession.php b/includes/models/UserSession.php index 745040b..a5e250b 100644 --- a/includes/models/UserSession.php +++ b/includes/models/UserSession.php @@ -2,7 +2,6 @@ use Illuminate\Database\Eloquent\Model; class UserSession extends Model { - protected $table = 'user_sessions'; protected $casts = [ @@ -12,8 +11,4 @@ class UserSession extends Model { public function user() { return $this->belongsTo(User::class); } - - - - } diff --git a/index.php b/index.php index 3645342..b41024e 100644 --- a/index.php +++ b/index.php @@ -3,7 +3,6 @@ define('IN_PONEPASTE', 1); require_once('includes/common.php'); require_once('includes/captcha.php'); require_once('includes/functions.php'); -require_once('includes/Tag.class.php'); function verifyCaptcha() : string|bool { global $captcha_config; diff --git a/logout.php b/logout.php index 3019e94..fff1d3d 100644 --- a/logout.php +++ b/logout.php @@ -9,9 +9,7 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST' || $current_user === null) { } /* Destroy remember token */ -$current_user->destroySession($conn, $_COOKIE[User::REMEMBER_TOKEN_COOKIE]); -unset($_COOKIE[User::REMEMBER_TOKEN_COOKIE]); -setcookie(User::REMEMBER_TOKEN_COOKIE, null, time() - 3600); +\PonePaste\Helpers\SessionHelper::destroySession(); /* Destroy PHP session */ unset($_SESSION['user_id']); diff --git a/paste.php b/paste.php index c27f04e..0bcf0cf 100644 --- a/paste.php +++ b/paste.php @@ -2,7 +2,6 @@ define('IN_PONEPASTE', 1); require_once('includes/common.php'); require_once('includes/functions.php'); -require_once('includes/Tag.class.php'); require_once('includes/passwords.php'); require_once('includes/models/Paste.php'); diff --git a/scripts/convert_tags.php b/scripts/convert_tags.php index f1bab2d..9473f6e 100644 --- a/scripts/convert_tags.php +++ b/scripts/convert_tags.php @@ -1,7 +1,6 @@ query('SELECT id, tagsys FROM pastes')