Moved both sanity check and fix into one file

This commit is contained in:
Michał Gdula 2022-11-05 18:02:54 +00:00
parent 45b79c9c33
commit fa7b2e2eb6
9 changed files with 312 additions and 428 deletions

View file

@ -3,11 +3,9 @@
use App\Account;
use App\Diff;
use App\Sanity;
$user_info = new Account();
$diff = new Diff();
$sanity = new Sanity();
$profile_info = $user_info->get_user_info($conn, $_SESSION['id']);
?>
@ -384,66 +382,30 @@
</script>
<br><h3>Sanity check</h3>
<?php
$check_sanity = $sanity->get_results();
<div id='sanityCheck'></div>
<button class='btn btn-good' onclick='sanityCheck(this)'>Run check</button>
<script>
function sanityCheck(thisButton) {
thisButton.innerHTML = "<img src='assets/icons/circle-notch.svg' class='svg loading'> Running...";
document.getElementById('sanityCheck').style.cssText = "transform: scale(0.8);opacity: 0;";
$("#sanityCheck").html("");
if (empty($check_sanity)) {
echo "<p class='alert alert-good'>No errors! Lookin' good :3</p>";
} else {
?>
<style>
.sanity-check {
border-color: var(--warning);
setTimeout(function() {
$.ajax ({
url: "app/sanity/sanity.php",
type: "POST",
data: { check: true},
success: function(response) {
$("#sanityCheck").html(response);
thisButton.innerHTML = "Run check";
document.getElementById('sanityCheck').style.cssText = "transform: scale(1);opacity: 1;";
}
</style>
<?php
foreach ($check_sanity as $result) {
if ($result['type'] == 'critical') {
echo "<p class='alert alert-bad'>
<span class='badge badge-critical'>Critical</span> ";
} elseif ($result['type'] == 'warning') {
echo "<p class='alert alert-warning'>
<span class='badge badge-warning'>Warning</span> ";
}
if ($result['fix'] == 'auto') {
echo "<span class='badge badge-primary'>Auto fix available</span> ";
} elseif ($result['fix'] == 'manual') {
echo "<span class='badge badge-critical'>Manual fix required</span> ";
}
if (isset($result['link'])) {
echo "<a class='link badge badge-primary' href='".$result['link']."'>Recources</a> ";
}
echo $result['message']."</p>";
}
?>
<button class='btn btn-bad' onclick="runAutofix()"><img class='svg' src='assets/icons/wrench.svg'>Attempt Autofix</button>
<div id="autofix">
<button onclick='closeAutofix()'><img src='assets/icons/cross.svg'></button>
<div id="autofix-log"></div>
</div>
<script>
function runAutofix() {
document.getElementById('autofix').style.display = "block";
document.getElementById('autofix-log').innerHTML = "<p><span style='color: var(--accent);'>[INFO]</span> Starting autofix</p>";
setTimeout(function() {
$("#autofix-log").load("app/sanity/sanity.php", {autofix: "true"});
}, 1000);
}
function closeAutofix() {
document.getElementById('autofix').style.display = "none";
}
</script>
<?php
});
}, 1000);
}
?>
</script>
</div>
<?php
$sql_end = microtime(true);

View file

@ -322,241 +322,4 @@ class Diff {
if (!$full_date) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
}
class Sanity {
function check_json(): array
{
$results = array();
if (!is_file(__DIR__."/../usr/conf/msg.json")) {
$results[] = array(
'type'=>'warning',
'message'=>'msg.json is missing',
'fix'=>'auto'
);
}
if (!is_file(__DIR__."/../usr/conf/conf.json")) {
if (is_file(__DIR__."/../usr/conf/manifest.json")) {
$results[] = array(
'type'=>'critical',
'message'=>'manifest.json is deprecated, please rename it to conf.json',
'fix'=>'manual'
);
} else {
$results[] = array(
'type'=>'critical',
'message'=>'conf.json is missing, using conf.default.json instead',
'fix'=>'auto'
);
}
} else {
$manifest = json_decode(file_get_contents(__DIR__."/../usr/conf/conf.json"), true);
if (empty($manifest['user_name']) || $manifest['user_name'] == "[your name]") {
$results[] = array(
'type'=>'warning',
'message'=>'conf.json is missing your name',
'fix'=>'manual'
);
}
if ($manifest['upload']['rename_on_upload']) {
if (empty($manifest['upload']['rename_to'])) {
$results[] = array(
'type'=>'critical',
'message'=>'conf.json doesnt know what to rename your files to',
'fix'=>'manual'
);
} else {
$rename_to = $manifest['upload']['rename_to'];
$rename_rate = 0;
if (str_contains($rename_to, '{{autoinc}}')) $rename_rate = 5;
if (str_contains($rename_to, '{{time}}')) $rename_rate = 5;
if (str_contains($rename_to, '{{date}}')) $rename_rate += 2;
if (str_contains($rename_to, '{{filename}}')) $rename_rate += 2;
if (str_contains($rename_to, '{{username}}') || str_contains($rename_to, '{{userid}}')) $rename_rate += 1;
if ($rename_rate < 2) {
$results[] = array(
'type'=>'critical',
'message'=>'You will encounter errors when uploading images due to filenames, update your conf.json',
'fix'=>'manual'
);
} elseif ($rename_rate < 5 && $rename_rate > 2) {
$results[] = array(
'type'=>'warning',
'message'=>'You may encounter errors when uploading images due to filenames, concider modifying your conf.json',
'fix'=>'manual'
);
}
}
}
if ($manifest['is_testing']) {
$results[] = array(
'type'=>'warning',
'message'=>'You are currently in testing mode, errors will be displayed to the user. This is not recommended for production use.'
);
}
}
return $results;
}
function check_files(): array
{
$results = array();
if (!is_dir("usr/images")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to setup an images folder.',
'fix'=>'auto'
);
}
if (!is_dir("usr/images/pfp")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to setup an pfp folder.',
'fix'=>'auto'
);
}
if (!is_dir("usr/images/previews")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to setup an previews folder.',
'fix'=>'auto'
);
}
if (!is_dir("usr/images/thumbnails")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to setup an thumbnails folder.',
'fix'=>'auto'
);
}
return $results;
}
function check_version(): array
{
$results = array();
// Local app info
$app_local = json_decode(file_get_contents(__DIR__."/gallery.json"), true);
// Repo app info
$curl_url = "https://raw.githubusercontent.com/Fluffy-Bean/image-gallery/".$app_local['branch']."/app/settings/manifest.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $curl_url);
$result = curl_exec($curl);
curl_close($curl);
$app_repo = json_decode($result, true);
if ($app_local['version'] < $app_repo['version']) {
$results[] = array(
'type'=>'critical',
'message'=>'You are not running the latest version of the app v'.$app_repo['version'],
'link'=>'https://github.com/Fluffy-Bean/image-gallery',
'fix'=>'manual'
);
} elseif ($app_local['version'] > $app_repo['version']) {
$results[] = array(
'type'=>'critical',
'message'=>'You are running a version of the app that is newer than the latest release v'.$app_repo['version'],
'link'=>'https://github.com/Fluffy-Bean/image-gallery',
'fix'=>'manual'
);
}
if (PHP_VERSION_ID < 80000) {
$results[] = array(
'type'=>'warning',
'message'=>'Your current version of PHP is '.PHP_VERSION.' The reccomended version is 8.0.0 or higher',
'link'=>'https://www.php.net/downloads.php',
'fix'=>'manual'
);
}
return $results;
}
function check_permissions(): array
{
$results = array();
if (!fileperms("usr")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to give PHP usr folder permissions',
'fix'=>'manual'
);
} else {
if (!fileperms("usr/images")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to give PHP images folder permissions',
'fix'=>'manual'
);
}
if (!fileperms("usr/images/pfp")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to give PHP pfp folder permissions',
'fix'=>'manual'
);
}
if (!fileperms("usr/images/previews")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to give PHP previews folder permissions',
'fix'=>'manual'
);
}
if (!fileperms("usr/images/thumbnails")) {
$results[] = array(
'type'=>'critical',
'message'=>'You need to give PHP thumbnails folder permissions',
'fix'=>'manual'
);
}
}
return $results;
}
function check_freshinstall(): array
{
$results = array();
if (!is_dir("usr/conf") && !is_dir("usr/images")) {
$results[] = array(
'type'=>'warning',
'message'=>'You are running a fresh install, please configure your app through the settings. Alternatively you can import usr folder from a previous install',
'fix'=>'manual'
);
}
return $results;
}
function get_results(): array
{
$results = array();
foreach ($this->check_json() as $result) $results[] = $result;
foreach ($this->check_files() as $result) $results[] = $result;
foreach ($this->check_permissions() as $result) $results[] = $result;
foreach ($this->check_version() as $result) $results[] = $result;
foreach ($this->check_freshinstall() as $result) $results[] = $result;
return $results;
}
}

View file

@ -2,13 +2,8 @@
session_start();
include dirname(__DIR__) . "/conn.php";
include dirname(__DIR__) . "/app.php";
use App\Sanity;
$sanity = new Sanity();
if (isset($_POST['autofix'])) {
if (isset($_POST['fix'])) {
$autofix_start = microtime(true);
echo "<p><span style='color: var(--accent);'>[INFO]</span> Starting autofix</p>";
@ -20,14 +15,6 @@ if (isset($_POST['autofix'])) {
echo "<p><span style='color: var(--warning);'>[ERRO]</span> You cannot use Autofix as an Admin currently.</p>";
exit();
}
$check_sanity = $sanity->get_results();
if (empty($check_sanity)) {
echo "<p><span style='color: var(--accent);'>[INFO]</span> Sanity check passed. No errors found.</p>";
exit();
} else {
echo "<p><span style='color: var(--alert);'>[WARN]</span> Sanity check failed, continuing...</p>";
}
define('ROOT', true); // Only run scripts from this file
@ -42,4 +29,236 @@ if (isset($_POST['autofix'])) {
$autofix_time = round($autofix_time, 6) * 1000;
echo "<p><span style='color: var(--accent);'>[INFO]</span> Autofix complete in $autofix_time ms</p>";
}
if (isset($_POST['check'])) {
function check_json(): array
{
$results = array();
if (!is_file(__DIR__."/../../usr/conf/msg.json")) {
$results[] = array(
'type'=>'warning',
'message'=>'msg.json is missing',
'fix'=>'auto'
);
}
if (!is_file(__DIR__."/../../usr/conf/conf.json")) {
if (is_file(__DIR__."/../../usr/conf/manifest.json")) {
$results[] = array(
'type'=>'critical',
'message'=>'manifest.json is deprecated, please rename it to conf.json',
'fix'=>'manual'
);
} else {
$results[] = array(
'type'=>'critical',
'message'=>'conf.json is missing, using conf.default.json instead',
'fix'=>'auto'
);
}
} else {
$manifest = json_decode(file_get_contents(__DIR__."/../../usr/conf/conf.json"), true);
if (empty($manifest['user_name']) || $manifest['user_name'] == "[your name]") {
$results[] = array(
'type'=>'warning',
'message'=>'conf.json is missing your name',
'fix'=>'manual'
);
}
if ($manifest['upload']['rename_on_upload']) {
if (empty($manifest['upload']['rename_to'])) {
$results[] = array(
'type'=>'critical',
'message'=>'conf.json doesnt know what to rename your files to',
'fix'=>'manual'
);
} else {
$rename_to = $manifest['upload']['rename_to'];
$rename_rate = 0;
if (str_contains($rename_to, '{{autoinc}}')) $rename_rate = 5;
if (str_contains($rename_to, '{{time}}')) $rename_rate = 5;
if (str_contains($rename_to, '{{date}}')) $rename_rate += 2;
if (str_contains($rename_to, '{{filename}}')) $rename_rate += 2;
if (str_contains($rename_to, '{{username}}') || str_contains($rename_to, '{{userid}}')) $rename_rate += 1;
if ($rename_rate < 2) {
$results[] = array(
'type'=>'critical',
'message'=>'You will encounter errors when uploading images due to filenames, update your conf.json',
'fix'=>'manual'
);
} elseif ($rename_rate < 5 && $rename_rate > 2) {
$results[] = array(
'type'=>'warning',
'message'=>'You may encounter errors when uploading images due to filenames, concider modifying your conf.json',
'fix'=>'manual'
);
}
}
}
if ($manifest['is_testing']) {
$results[] = array(
'type'=>'warning',
'message'=>'You are currently in testing mode, errors will be displayed to the user. This is not recommended for production use.'
);
}
}
return $results;
}
function check_dir(): array
{
$results = array();
$files = array(
'usr/images',
'usr/images/pfp',
'usr/images/previews',
'usr/images/thumbnails'
);
foreach ($files as $file) {
if (!is_dir(__DIR__."/../../$file")) {
$results[] = array(
'type'=>'critical',
'message'=>"$file is missing",
'fix'=>'auto'
);
}
}
return $results;
}
function check_version(): array
{
$results = array();
// Local app info
$app_local = json_decode(file_get_contents(__DIR__."/../gallery.json"), true);
// Repo app info
$curl_url = "https://raw.githubusercontent.com/Fluffy-Bean/image-gallery/".$app_local['branch']."/app/settings/manifest.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $curl_url);
$result = curl_exec($curl);
curl_close($curl);
$app_repo = json_decode($result, true);
if ($app_local['version'] < $app_repo['version']) {
$results[] = array(
'type'=>'critical',
'message'=>'You are not running the latest version of the app v'.$app_repo['version'],
'link'=>'https://github.com/Fluffy-Bean/image-gallery',
'fix'=>'manual'
);
} elseif ($app_local['version'] > $app_repo['version']) {
$results[] = array(
'type'=>'critical',
'message'=>'You are running a version of the app that is newer than the latest release v'.$app_repo['version'],
'link'=>'https://github.com/Fluffy-Bean/image-gallery',
'fix'=>'manual'
);
}
if (PHP_VERSION_ID < 80000) {
$results[] = array(
'type'=>'warning',
'message'=>'Your current version of PHP is '.PHP_VERSION.' The reccomended version is 8.0.0 or higher',
'link'=>'https://www.php.net/downloads.php',
'fix'=>'manual'
);
}
return $results;
}
function check_permissions(): array
{
$results = array();
$files = array(
'usr/images',
'usr/images/pfp',
'usr/images/previews',
'usr/images/thumbnails',
'usr/conf/conf.json',
'usr/conf/msg.json',
'usr/conf.default.json'
);
foreach ($files as $file) {
if (!is_writable(__DIR__."/../../$file")) {
$results[] = array(
'type'=>'critical',
'message'=>"$file is not writable",
'fix'=>'manual'
);
}
}
foreach ($files as $file) {
if (!fileperms(__DIR__."/../../$file")) {
$results[] = array(
'type'=>'critical',
'message'=>"PHP does not have permitions for $file",
'fix'=>'manual'
);
}
}
return $results;
}
if ($_SESSION['id'] == 1) {
$results = array();
foreach (check_json() as $result) $results[] = $result;
foreach (check_dir() as $result) $results[] = $result;
foreach (check_permissions() as $result) $results[] = $result;
foreach (check_version() as $result) $results[] = $result;
if (empty($results)) {
echo "<p class='alert alert-good'>No errors! Lookin' good :3</p>";
} else {
foreach ($results as $result) {
if ($result['type'] == 'critical') {
echo "<p class='alert alert-bad'><span class='badge badge-critical'>Critical</span> ";
} else {
echo "<p class='alert alert-warning'><span class='badge badge-warning'>Warning</span> ";
}
if ($result['fix'] == 'auto') {
echo "<span class='badge badge-primary'>Auto fix available</span> ";
$autofix_enable = true;
} elseif ($result['fix'] == 'manual') {
echo "<span class='badge badge-critical'>Manual fix required</span> ";
}
if (isset($result['link'])) {
echo "<a class='link badge badge-primary' href='".$result['link']."'>Recources</a> ";
}
echo $result['message']."</p>";
}
if ($autofix_enable) {
echo "<button class='btn btn-bad' onclick=\"$('#sanityCheck').load('app/sanity/sanity.php', {fix: 'true'});\">
Attempt Autofix
</button>";
}
}
} else {
echo "<p class='alert alert-bad'>You do not have permission todo this action!!!!!</p>";
}
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="192" height="192" fill="#e8e3e3" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"></rect><path d="M168,40.7a96,96,0,1,1-80,0" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></path></svg>

After

Width:  |  Height:  |  Size: 296 B

View file

@ -33,6 +33,9 @@
<!-- Stylesheets -->
<link rel="stylesheet" href="css/main.css">
<!-- Phosphor Icons! -->
<!--<script src="https://unpkg.com/phosphor-icons"></script>-->
<!-- Google Fonts -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lexend+Deca:wght@600">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Secular+One&display=swap">
@ -51,4 +54,4 @@
<!-- Flyout script! -->
<script src="Flyout/flyout.js"></script>
<link rel='stylesheet' href='Flyout/flyout.css'>
<link rel='stylesheet' href='Flyout/flyout.css'>

View file

@ -960,50 +960,20 @@ nav .btn {
content: "";
}
#autofix {
#sanityCheck {
padding: 0;
display: none;
background-color: #121212;
position: relative;
border-radius: 3px;
}
#autofix button {
padding: 0.25rem;
width: 1.5rem;
height: 1.5rem;
position: absolute;
top: 0.25rem;
right: 0.25rem;
display: block;
box-sizing: border-box;
border: none;
border-radius: 3px;
transition: outline 0.1s cubic-bezier(0.19, 1, 0.22, 1);
background-color: rgba(21, 21, 21, 0.7333333333);
z-index: 2;
}
#autofix button img {
width: 1rem;
display: block;
}
#autofix button:hover {
outline: #E8E3E3 0.2rem solid;
color: #E8E3E3;
cursor: pointer;
}
#autofix-log {
padding: 0.5rem 0.5rem 0 0.5rem;
display: flex;
flex-direction: column;
border-radius: 3px;
opacity: 0;
transform: scale(0.8);
transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
}
#autofix-log > * {
font-family: "JetBrains Mono", monospace;
font-size: 0.9rem;
line-height: 1.1rem;
#sanityCheck > * {
margin: 0 0 0.5rem 0;
}
#sanityCheck > *:last-child {
margin: 0;
}
/*
|-------------------------------------------------------------
@ -1269,6 +1239,28 @@ textarea {
border-radius: 3px;
}
.loading {
-webkit-animation: spin 1.5s ease-in-out infinite;
animation: spin 1.5s ease-in-out infinite;
}
@-webkit-keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/*
|-------------------------------------------------------------
| BR / BREAK

View file

@ -298,6 +298,19 @@ textarea {
}
}
.loading {
animation: spin 1.5s ease-in-out infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/*
|-------------------------------------------------------------
| BR / BREAK

View file

@ -994,79 +994,22 @@
}
}
#autofix {
#sanityCheck {
padding: 0;
display: none;
display: flex;
flex-direction: column;
//border-left: $border;
background-color: #121212;
opacity: 0;
transform: scale(0.8);
position: relative;
@if calc($rad - 0.5rem) > 0 {
border-radius: calc($rad - 0.5rem);
} @else {
border-radius: 3px;
}
button {
padding: 0.25rem;
width: 1.5rem; height: 1.5rem;
position: absolute;
top: 0.25rem;
right: 0.25rem;
display: block;
box-sizing: border-box;
border: none;
@if calc($rad - 0.5rem) > 0 {
border-radius: calc($rad - 0.5rem);
} @else {
border-radius: 3px;
}
transition: outline 0.1s cubic-bezier(.19, 1, .22, 1);
background-color: $bg-alt;
z-index: +2;
img {
width: 1rem;
display: block;
}
&:hover {
outline: $white 0.2rem solid;
color: $fg;
cursor: pointer;
}
}
}
#autofix-log {
padding: 0.5rem 0.5rem 0 0.5rem;
display: flex; flex-direction: column;
@if calc($rad - 0.5rem) > 0 {
border-radius: calc($rad - 0.5rem);
} @else {
border-radius: 3px;
}
transition: all 0.3s cubic-bezier(.19, 1, .22, 1);
> * {
font-family: $font-code;
font-size: 0.9rem;
line-height: 1.1rem;
margin: 0 0 0.5rem 0;
}
& > *:last-child {
margin: 0;
}
}

View file

@ -5,7 +5,6 @@
use App\Sanity;
$user_info = new Account();
$sanity = new Sanity();
?>
<!DOCTYPE html>
@ -41,17 +40,6 @@
</script>
<?php
}
if ($user_info->is_admin($conn, $_SESSION['id'])) {
$check_sanity = $sanity->get_results();
if (!empty($check_sanity)) {
?>
<script>
sniffleAdd('Uh oh', 'Website has not passed some Sanity checks, please check your settings for more information', 'var(--warning)', 'assets/icons/warning.svg');
</script>
<?php
}
}
unset($_SESSION['welc']);
}