From f6cddef9aa3fb0e81b746f2d05072c6a12c0dff4 Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Tue, 2 Nov 2021 08:46:40 -0400 Subject: [PATCH] More Eloquent moves --- admin/admin.php | 29 ++----- admin/ads.php | 127 ------------------------------ admin/common.php | 31 ++++---- admin/configuration.php | 8 +- admin/dashboard.php | 21 ++--- admin/index.php | 31 ++++---- admin/pastes.php | 2 - admin/users.php | 39 +++------ api/tags_autocomplete.php | 7 +- fav.php | 23 ------ includes/DatabaseHandle.class.php | 6 -- includes/Models/AdminLog.php | 12 +++ includes/Models/Paste.php | 4 + includes/common.php | 1 + includes/functions.php | 44 +---------- index.php | 20 +++-- pages.php | 1 + paste.php | 37 +++++---- profile.php | 7 +- theme/bulma/common.php | 2 +- theme/bulma/main.php | 16 +--- theme/bulma/view.php | 33 +++----- user.php | 14 +--- 23 files changed, 139 insertions(+), 376 deletions(-) delete mode 100644 admin/ads.php delete mode 100644 fav.php create mode 100644 includes/Models/AdminLog.php diff --git a/admin/admin.php b/admin/admin.php index 3648b17..700e07a 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -1,10 +1,6 @@ query('SELECT user FROM admin LIMIT 1'); -$adminid = $query->fetch()['user']; - ?> @@ -48,23 +44,13 @@ $adminid = $query->fetch()['user']; - ' . mysqli_error($con) . ' - '; - - } else { - $msg = '
+ $current_user->admin_password_hash = $password; + $current_user->save(); + $msg = '
Account details updated.
'; - } } ?> @@ -90,14 +76,9 @@ $adminid = $query->fetch()['user'];
-
- - -
+ class="form-control" placeholder="Admin Password">
diff --git a/admin/ads.php b/admin/ads.php deleted file mode 100644 index c77093f..0000000 --- a/admin/ads.php +++ /dev/null @@ -1,127 +0,0 @@ -query('SELECT text_ads, ads_1, ads_2 FROM ads LIMIT 1')->fetch(); - -if ($row) { - $text_ads = trim($row['text_ads']); - $ads_1 = trim($row['ads_1']); - $ads_2 = trim($row['ads_2']); -} else { - $text_ads = ''; - $ads_1 = ''; - $ads_2 = ''; -} - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $text_ads = trim($_POST['text_ads']); - $ads_1 = trim($_POST['ads_1']); - $ads_2 = trim($_POST['ads_2']); - - $conn->prepare('UPDATE ads SET text_ads = ?, ads_1 = ?, ads_2 = ? WHERE id = 1')->execute([$text_ads, $ads_1, $ads_2]); - $msg = '
- Ads saved -
'; -} -?> - - - - - - - Paste - Ads - - - - - -
- - - - - - - -
- - -
- -
- - - - -
-
-
-
-
Manage Ads
- - -
- -
- -
-
-
- -
- -
-
- -
- -
- -
-
- - -
-
-
-
- -
- - - - - -
- - - - - - \ No newline at end of file diff --git a/admin/common.php b/admin/common.php index 7076f49..55d7f54 100644 --- a/admin/common.php +++ b/admin/common.php @@ -2,27 +2,28 @@ if (!defined('IN_PONEPASTE')) { die('This file may not be accessed directly.'); } + require_once('../includes/common.php'); -function updateAdminHistory($conn) { - $last_date = null; - $last_ip = null; - $ip = $_SERVER['REMOTE_ADDR']; - $date = date('jS F Y'); +use PonePaste\Models\AdminLog; +use PonePaste\Models\User; - $query = $conn->query('SELECT ip, last_date FROM admin_history ORDER BY ID DESC LIMIT 1'); +function updateAdminHistory(User $admin, int $action) { + $log = new AdminLog([ + 'user_id' => $admin->user_id, + 'action' => $action, + 'ip' => $_SERVER['REMOTE_ADDR'] + ]); - if ($row = $query->fetch()) { - $last_date = $row['last_date']; - $last_ip = $row['ip']; - } - - if ($last_ip !== $ip || $last_date !== $date) { - $conn->prepare('INSERT INTO admin_history (ip, last_date) VALUES (?, ?)')->execute([$date, $ip]); - } + $log->save(); } -if (!isset($_SESSION['login'])) { +if ($current_user === null || !$current_user->admin) { + header('Location: ..'); + die(); +} + +if (!isset($_SESSION['admin_login'])) { header('Location: .'); exit(); } diff --git a/admin/configuration.php b/admin/configuration.php index f6480d6..5ac8beb 100644 --- a/admin/configuration.php +++ b/admin/configuration.php @@ -1,12 +1,12 @@ '; } + + updateAdminHistory($current_user, AdminLog::ACTION_EDIT_CONFIG); } ?> diff --git a/admin/dashboard.php b/admin/dashboard.php index d96a8c6..10af938 100644 --- a/admin/dashboard.php +++ b/admin/dashboard.php @@ -8,8 +8,6 @@ $today_pastes_count = 0; require_once('../includes/common.php'); require_once('../includes/functions.php'); -updateAdminHistory($conn); - $query = $conn->query("SELECT @last_id := MAX(id) FROM page_view"); $row = $query->fetch(PDO::FETCH_NUM); $page_last_id = intval($row[0]); @@ -58,6 +56,12 @@ for ($loop = 0; $loop <= 6; $loop++) { $tvisit[$loop] = $row['tvisit']; } } + +function getRecentadmin($count = 5) { + return Paste::with('user') + ->orderBy('id') + ->limit($count)->get(); +} ?> @@ -200,18 +204,15 @@ for ($loop = 0; $loop <= 6; $loop++) { query('SELECT id, username, date, ip FROM users ORDER BY id DESC LIMIT 7')->fetchAll(); - $last_id = intval( - $conn->query('SELECT MAX(id) FROM users')->fetch(PDO::FETCH_NUM)[0] - ); + $most_recent_users = User::select('id', 'username', 'date', 'ip')->orderBy('id', 'desc')->limit(7); foreach ($most_recent_users as $user) { echo " - ${user['id']} - ${user['username']} - ${user['date']} - ${user['ip']} + $user->id + " . pp_html_escape($user->username) . " + $user->date + $user->ip "; } diff --git a/admin/index.php b/admin/index.php index 7f6da00..3171699 100644 --- a/admin/index.php +++ b/admin/index.php @@ -2,18 +2,23 @@ define('IN_PONEPASTE', 1); require_once(__DIR__ . '/../includes/common.php'); -$row = $conn->querySelectOne('SELECT user, pass FROM admin LIMIT 1'); -$adminid = $row['user']; -$password = $row['pass']; +use PonePaste\Models\AdminLog; + +if ($current_user === null || !$current_user->admin) { + header('Location: ..'); + die(); +} if ($_SERVER['REQUEST_METHOD'] == 'POST') { - if ($adminid === trim($_POST['username']) && password_verify($_POST['password'], $password)) { - $_SESSION['login'] = true; + if (password_verify($_POST['password'], $current_user->admin_password_hash)) { + updateAdminHistory($current_user, AdminLog::ACTION_LOGIN); + $_SESSION['admin_login'] = true; header("Location: dashboard.php"); exit(); } else { + updateAdminHistory($current_user, AdminLog::ACTION_FAIL_LOGIN); $msg = '
- Wrong User/Password + Wrong Password
'; } } @@ -25,7 +30,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { - Paste - Login + PonePaste - Authenticate