Add power

This commit is contained in:
Jay Trees 2022-01-18 13:23:37 +01:00
parent 521ba07b08
commit fe43855ac3
6 changed files with 79 additions and 72 deletions

View file

@ -1,32 +0,0 @@
$(function() {
const urlParams = new URLSearchParams(window.location.search);
var retryIn = urlParams.has('interval') ? urlParams.get('interval') : 5;
var value = 0;
var total = retryIn * 1000;
if (!$('.ui.progress').length) {
return;
}
$('#retryIn').html(retryIn);
setTimeout(function step() {
$('.ui.progress').progress({
total: total,
value: value
});
if (value >= total) {
setTimeout(function() {
urlParams.set('interval', parseInt(retryIn) + 5);
window.location.href = window.location.origin + '/?' + urlParams.toString();
}, 1000);
} else {
value += 100;
setTimeout(step, 100);
}
}, 100)
});

View file

@ -18,13 +18,15 @@ class Page
* @param string $filepath The filepath (__FILE__) of the page. * @param string $filepath The filepath (__FILE__) of the page.
* @param string $title The HTML title of the page. * @param string $title The HTML title of the page.
*/ */
public function __construct(string $filepath, public string $title = 'wishthis') public function __construct(string $filepath, public string $title = 'wishthis', public int $power = 0)
{ {
$this->name = pathinfo($filepath, PATHINFO_FILENAME); $this->name = pathinfo($filepath, PATHINFO_FILENAME);
/** /**
* Session * Session
*/ */
global $user;
$disableRedirect = array( $disableRedirect = array(
'home', 'home',
'login', 'login',
@ -35,6 +37,14 @@ class Page
header('Location: /?page=login'); header('Location: /?page=login');
die(); die();
} }
/**
* Power
*/
if ($user->power < $this->power) {
header('Location: /?page=power&required=' . $this->power);
die();
}
} }
public function header(): void public function header(): void
@ -162,7 +172,7 @@ class Page
</div> </div>
</div> </div>
<?php global $options; ?> <?php global $options; ?>
<?php if ($options->updateAvailable && $user && $user->isLoggedIn()) { ?> <?php if ($options->updateAvailable && $user && 100 === $user->power) { ?>
<a class="item" href="/?page=update"> <a class="item" href="/?page=update">
<i class="upload icon"></i> Update <i class="upload icon"></i> Update
</a> </a>

View file

@ -13,6 +13,7 @@ namespace wishthis;
class User class User
{ {
public int $id; public int $id;
public int $power = 0;
public function __construct(int $id = -1) public function __construct(int $id = -1)
{ {
@ -23,6 +24,18 @@ class User
} else { } else {
$this->id = $id; $this->id = $id;
} }
if (!isset($this->id)) {
return;
}
global $database;
$user = $database->query('SELECT * FROM `users`
WHERE `id` = ' . $this->id . ';')
->fetch();
$this->power = $user['power'];
} }
/** /**

32
includes/pages/power.php Normal file
View file

@ -0,0 +1,32 @@
<?php
/**
* power.php
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\Page;
$page = new page(__FILE__, 'Insufficient power');
$page->header();
$page->navigation();
?>
<main>
<div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1>
<div class="ui segment">
<h2 class="ui header">Restricted access</h2>
<p>
You do not have enough power to view this page.
You need <strong><?= $_GET['required'] ?></strong> to see this page, but only have <strong><?= $user->power ?></strong>.
</p>
</div>
</div>
</main>
<?php
$page->footer();
?>

View file

@ -15,13 +15,13 @@ if (isset($_POST['email'], $_POST['password'])) {
if (0 === count($users)) { if (0 === count($users)) {
$database->query('INSERT INTO `users` $database->query('INSERT INTO `users`
(`email`, `password`) VALUES (`email`, `password`, `power`) VALUES
("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '") ("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '", 100)
;'); ;');
} else { } else {
$database->query('INSERT INTO `users` $database->query('INSERT INTO `users`
(`email`, `password`, `power`) VALUES (`email`, `password`) VALUES
("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '", 100) ("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '")
;'); ;');
} }

View file

@ -8,7 +8,7 @@
use wishthis\{Page, User}; use wishthis\{Page, User};
$page = new page(__FILE__, 'Update'); $page = new page(__FILE__, 'Update', 100);
$page->header(); $page->header();
$page->navigation(); $page->navigation();
@ -48,42 +48,26 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
<div class="ui container"> <div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1> <h1 class="ui header"><?= $page->title ?></h1>
<?php if ($user->isLoggedIn()) { ?> <div class="ui segment">
<div class="ui segment"> <h2 class="ui header">New version detected</h2>
<h2 class="ui header">New version detected</h2> <p>Thank you for updating to <strong>v<?= VERSION ?></strong>!</p>
<p>Thank you for updating to <strong>v<?= VERSION ?></strong>!</p> <p>There have been some changes in the database, please run the updater.</p>
<p>There have been some changes in the database, please run the updater.</p> <div class="ui icon warning message">
<div class="ui icon warning message"> <i class="exclamation triangle icon"></i>
<i class="exclamation triangle icon"></i> <div class="content">
<div class="content"> <div class="header">
<div class="header"> Use at own risk
Use at own risk
</div>
<p>Be sure to make backups before proceeding.</p>
</div> </div>
</div> <p>Be sure to make backups before proceeding.</p>
<form class="ui form" method="post">
<button class="ui orange button" type="submit">
<i class="upload icon"></i>
Run the updater
</button>
</form>
</div>
<?php } else { ?>
<div class="ui segment">
<h2 class="ui header">Maintenance</h2>
<p>
The administrator of this site is currently running an update.
This usually just takes a couple of seconds.
</p>
<p>
Trying again in <span id="retryIn">5</span> seconds...
</p>
<div class="ui primary progress nolabel">
<div class="bar"></div>
</div> </div>
</div> </div>
<?php } ?> <form class="ui form" method="post">
<button class="ui orange button" type="submit">
<i class="upload icon"></i>
Run the updater
</button>
</form>
</div>
</div> </div>
</main> </main>