Does the site even work anymore? Probably not.

This commit is contained in:
Floorb 2021-08-27 19:24:48 -04:00
parent d036647181
commit 82c956d9e1
10 changed files with 55 additions and 84 deletions

View file

@ -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 {

View file

@ -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';

View file

@ -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]);
}
}

View file

@ -1,4 +1,6 @@
<?php
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
require_once(__DIR__ . '/Tag.php');
@ -13,4 +15,40 @@ class Paste extends Model {
public function tags() {
return $this->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();
}
}

View file

@ -16,7 +16,5 @@ class User extends Model {
/*public function pastes() {
return $this->hasMany(Paste::class);
}*/
}

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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']);

View file

@ -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');

View file

@ -1,7 +1,6 @@
<?php
define('IN_PONEPASTE', 1);
require_once('../includes/common.php');
require_once('../includes/Tag.class.php');
function upgrade_tagsys(DatabaseHandle $conn) {
$result = $conn->query('SELECT id, tagsys FROM pastes')