Moved functions into classes

This commit is contained in:
Michał 2022-09-12 14:15:16 +00:00
parent 951871b983
commit 92a1c3500e
22 changed files with 224 additions and 234 deletions

View file

@ -35,15 +35,15 @@
<h2>Where to find me</h2>
<a class='link' href="https://gay.fluffybean.gay">
<img class='svg' src='<?php echo $root_dir; ?>assets/icons/link.svg'>
<img class='svg' src='assets/icons/link.svg'>
My website!
</a>
<a class='link' href="https://t.me/Fluffy_Bean">
<img class='svg' src='<?php echo $root_dir; ?>assets/icons/telegram-logo.svg'>
<img class='svg' src='assets/icons/telegram-logo.svg'>
Telegram
</a>
<a class='link' href="https://twitter.com/fluffybeanUwU">
<img class='svg' src='<?php echo $root_dir; ?>assets/icons/twitter-logo.svg'>
<img class='svg' src='assets/icons/twitter-logo.svg'>
Twitter
</a>

View file

@ -9,10 +9,14 @@
<?php
include "ui/required.php";
include "ui/nav.php";
use App\Account;
$user_info = new Account();
?>
<?php
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
if ($user_info->is_loggedin()) {
?>
<div class="account-root">
<h2>Settings</h2>
@ -24,7 +28,7 @@
<a class='btn btn-bad' href='app/account/logout.php'><img class='svg' src='assets/icons/sign-out.svg'>Logout</a>
</div>
<?php
if ($_SESSION["id"] == 1) {
if ($user_info->is_admin($_SESSION['id'])) {
?>
<div class="admin-root">
<h2>Admin controlls</h2>
@ -37,7 +41,7 @@
<script>
function copyCode() {
navigator.clipboard.writeText("<?php echo $token['code']; ?>");
sniffleAdd("Info", "Invite code has been copied!", "var(--green)", "<?php echo $root_dir; ?>assets/icons/clipboard-text.svg");
sniffleAdd("Info", "Invite code has been copied!", "var(--green)", "assets/icons/clipboard-text.svg");
}
</script>
<?php

View file

@ -1,16 +0,0 @@
<?php
/*
Get full user info from database
Returns array with user info
*/
function get_user_info($conn, $id) {
// Setting SQL query
$sql = "SELECT * FROM users WHERE id = ".$id;
// Getting results
$query = mysqli_query($conn, $sql);
// Fetching associated info
$user_array = mysqli_fetch_assoc($query);
return($user_array);
}

View file

@ -1,18 +0,0 @@
<?php
/*
Check if user is admin
Returns True if user is privilaged
Returns False if user is NOT privilaged
*/
function is_admin($id) {
if (isset($id) || !empty($id)) {
if ($id == 1) {
return True;
} else {
return False;
}
} else {
return False;
}
}

View file

@ -1,14 +0,0 @@
<?php
/*
Check if user is loggedin
Returns True if user is
Returns False if user is NOT
*/
function loggedin() {
if (isset($_SESSION["loggedin"]) == true && $_SESSION["loggedin"] == true) {
return True;
} else {
return False;
}
}

View file

@ -90,7 +90,7 @@ if (isset($_POST['submit'])) {
?>
<script>
sniffleAdd('Password updated', 'Now goodbye.... you will be redirected in a moment', 'var(--green)', '../assets/icons/check.svg');
setTimeout(function(){window.location.href = "../account/login.php";}, 4000);
setTimeout(function(){window.location.href = "../account/login.php";}, 2000);
</script>
<?php
} else {

127
app/app.php Normal file
View file

@ -0,0 +1,127 @@
<?php
namespace App;
class Make {
/*
|-------------------------------------------------------------
| Create Thumbnails
|-------------------------------------------------------------
| Default resolution for a preview image is 300px (max-width)
| ** Not yet implemented **
|-------------------------------------------------------------
*/
function thumbnail($image_path, $thumbnail_path, $resolution) {
try {
$thumbnail = new Imagick($image_path);
$thumbnail->resizeImage($resolution,null,null,1,null);
$thumbnail->writeImage($thumbnail_path);
return "success";
} catch (Exception $e) {
return $e;
}
}
/*
Clean up long text input and turn into an array for tags
Returns clean string of words with equal white space between it
*/
function tags($string) {
// Replace hyphens
$string = str_replace('-', '_', $string);
// Regex
$string = preg_replace('/[^A-Za-z0-9\_ ]/', '', $string);
// Change to lowercase
$string = strtolower($string);
// Removing extra spaces
$string = preg_replace('/ +/', ' ', $string);
return $string;
}
}
class Account {
/*
Check if user is loggedin
Returns True if user is
Returns False if user is NOT
*/
function is_loggedin() {
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
return True;
} else {
return False;
}
}
/*
Get full user info from database
Returns array with user info
*/
function get_user_info($conn, $id) {
// Setting SQL query
$sql = "SELECT * FROM users WHERE id = ".$id;
// Getting results
$query = mysqli_query($conn, $sql);
// Fetching associated info
$user_array = mysqli_fetch_assoc($query);
return($user_array);
}
/*
Check if user is admin
Returns True if user is privilaged
Returns False if user is NOT privilaged
*/
function is_admin($id) {
if (isset($id) || !empty($id)) {
if ($id == 1) {
return True;
} else {
return False;
}
} else {
return False;
}
}
}
class Image {
/*
Get full image info from database
Returns array with image info
*/
function get_image_info($conn, $id) {
// Setting SQL query
$sql = "SELECT * FROM swag_table WHERE id = ".$id;
// Getting results
$query = mysqli_query($conn, $sql);
// Fetching associated info
$image_array = mysqli_fetch_assoc($query);
return($image_array);
}
/*
Check if user is image owner
Returns True if user is privilaged
Returns False if user is NOT privilaged
*/
function image_privilage($id) {
$session_id = $_SESSION['id'];
if (isset($session_id) || !empty($session_id)) {
if ($session_id == $id) {
return True;
} else {
return False;
}
} else {
return False;
}
}
}

View file

@ -1,20 +0,0 @@
<?php
/*
|-------------------------------------------------------------
| Create Thumbnails
|-------------------------------------------------------------
| Default resolution for a preview image is 300px (max-width)
| ** Not yet implemented **
|-------------------------------------------------------------
*/
function make_thumbnail($image_path, $thumbnail_path, $resolution) {
try {
$thumbnail = new Imagick($image_path);
$thumbnail->resizeImage($resolution,null,null,1,null);
$thumbnail->writeImage($thumbnail_path);
return "success";
} catch (Exception $e) {
return $e;
}
}

View file

@ -1,18 +0,0 @@
<?php
/*
Clean up long text input and turn into an array for tags
Returns clean string of words with equal white space between it
*/
function tag_clean($string) {
// Replace hyphens
$string = str_replace('-', '_', $string);
// Regex
$string = preg_replace('/[^A-Za-z0-9\_ ]/', '', $string);
// Change to lowercase
$string = strtolower($string);
// Removing extra spaces
$string = preg_replace('/ +/', ' ', $string);
return $string;
}

View file

@ -1,16 +0,0 @@
<?php
/*
Get full image info from database
Returns array with image info
*/
function get_image_info($conn, $id) {
// Setting SQL query
$sql = "SELECT * FROM swag_table WHERE id = ".$id;
// Getting results
$query = mysqli_query($conn, $sql);
// Fetching associated info
$image_array = mysqli_fetch_assoc($query);
return($image_array);
}

View file

@ -2,11 +2,15 @@
session_start();
// Include server connection
include "../server/conn.php";
// Include required checks
include "get_image_info.php";
include "image_privilage.php";
// Required to format tags correctly
include "../format/string_to_tags.php";
include "../app.php";
use App\Account;
use App\Image;
use App\Make;
$user_info = new Account();
$image_info = new Image();
$make_stuff = new Make();
/*
|-------------------------------------------------------------
@ -18,10 +22,10 @@ include "../format/string_to_tags.php";
*/
if (isset($_POST['submit_delete'])) {
// Get all image info
$image_array = get_image_info($conn, $_POST['id']);
$image_array = $image_info->get_image_info($conn, $_POST['id']);
// If user owns image or has the ID of 1
if (image_privilage($image_array['author']) || $_SESSION['id'] == 1) {
if ($image_info->image_privilage($image_array['author']) || $_SESSION['id'] == 1) {
// Delete from table
$sql = "DELETE FROM swag_table WHERE id = ?";
if ($stmt = mysqli_prepare($conn, $sql)) {
@ -92,9 +96,9 @@ if (isset($_POST['submit_delete'])) {
*/
if (isset($_POST['submit_description'])) {
// Get all image info
$image_array = get_image_info($conn, $_POST['id']);
$image_array = $image_info->get_image_info($conn, $_POST['id']);
// If user owns image or has the ID of 1
if (image_privilage($image_array['author']) || $_SESSION['id'] == 1) {
if ($image_info->image_privilage($image_array['author']) || $_SESSION['id'] == 1) {
// getting ready forSQL asky asky
$sql = "UPDATE swag_table SET alt=? WHERE id=?";
@ -150,11 +154,11 @@ if (isset($_POST['submit_description'])) {
*/
if (isset($_POST['submit_tags'])) {
// Get all image info
$image_array = get_image_info($conn, $_POST['id']);
$image_array = $image_info->get_image_info($conn, $_POST['id']);
// If user owns image or has the ID of 1
if (image_privilage($image_array['author']) || $_SESSION['id'] == 1) {
if ($image_info->image_privilage($image_array['author']) || $_SESSION['id'] == 1) {
// Clean input
$tags_string = tag_clean(trim($_POST['input']));
$tags_string = $make_stuff->tags(trim($_POST['input']));
// getting ready forSQL asky asky
$sql = "UPDATE swag_table SET tags=? WHERE id=?";
@ -211,7 +215,7 @@ if (isset($_POST['submit_tags'])) {
*/
if (isset($_POST['submit_author'])) {
// If user has the ID of 1
if ($_SESSION['id'] == 1) {
if ($user_info->is_admin($_SESSION['id'])) {
// getting ready forSQL asky asky
$sql = "UPDATE swag_table SET author=? WHERE id=?";

View file

@ -1,19 +0,0 @@
<?php
/*
Check if user is image owner
Returns True if user is privilaged
Returns False if user is NOT privilaged
*/
function image_privilage($id) {
$session_id = $_SESSION['id'];
if (isset($session_id) || !empty($session_id)) {
if ($session_id == $id) {
return True;
} else {
return False;
}
} else {
return False;
}
}

View file

@ -9,8 +9,11 @@
session_start();
// Include server connection
include "../server/conn.php";
include "../format/string_to_tags.php";
include "../format/create_thumbnail.php";
include "../app.php";
use App\Make;
$make_stuff = new Make();
if (isset($_POST['submit'])) {
if (isset($_SESSION['id'])) {
@ -25,7 +28,7 @@ if (isset($_POST['submit'])) {
$image_path = $dir.$image_newname;
// Clean tags
$tags = tag_clean(trim($_POST['tags']));
$tags = $make_stuff->tags(trim($_POST['tags']));
// Allowed file types
$allowed_types = array('jpg', 'jpeg', 'png', 'webp');
@ -35,7 +38,7 @@ if (isset($_POST['submit'])) {
// Attempt making a thumbnail
list($width, $height) = getimagesize($image_path);
if ($width > 300) {
$make_thumbnail = make_thumbnail($image_path, $thumb_dir.$image_newname, 300);
$make_stuff->thumbnail($image_path, $thumb_dir.$image_newname, 300);
if ($make_thumbnail != "success") {
?>
<script>
@ -45,7 +48,7 @@ if (isset($_POST['submit'])) {
}
}
if ($width > 1100) {
$make_preview = make_thumbnail($image_path, $preview_dir.$image_newname, 900);
$make_stuff->thumbnail($image_path, $preview_dir.$image_newname, 900);
if ($make_preview != "success") {
?>
<script>

View file

@ -2,8 +2,7 @@
/*
Connect to database
In the future I want this section to be configurable, but that'll require some work to be done.
For now it's hard-coded, shouldn't be an issue as most people wont be changing this often anyway
Dunno what else to put here lol
*/
$conn_ip = "192.168.0.79:3306";
$conn_username = "uwu";
@ -11,13 +10,8 @@ $conn_password = "fennec621";
$conn_database = "gallery";
/*
$conn_ip = $database['ip'].":".$database['port'];
$conn_username = $database['username'];
$conn_password = $database['password'];
$conn_database = $database['database'];
echo $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['SCRIPT_NAME']);
echo $_SERVER['PHP_SELF'];
echo $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['SCRIPT_NAME']);
echo $_SERVER['PHP_SELF'];
*/
$conn = mysqli_connect($conn_ip, $conn_username, $conn_password , $conn_database);
@ -29,9 +23,4 @@ if ($conn->connect_error) {
<?php
}
/*
Start session
This is important as most pages use the PHP session and will complain if its not possible to access.
*/
session_start();

View file

@ -39,41 +39,3 @@ class GetEnv {
}
}
namespace Aaa;
class GetEnv {
protected $path;
public function __construct(string $path)
{
if(!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
}
$this->path = $path;
}
public function load() :void
{
if (!is_readable($this->path)) {
throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
}
$lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value);
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
}
}
}
}

View file

@ -27,15 +27,14 @@
],
"license":"GPL 3.0",
"database": {
"ip": "192.168.0.79",
"port": "3306",
"ip": "192.168.0.79:3306",
"username": "uwu",
"password": "fennec621",
"database": "gallery"
},
"debug": {
"testing": true,
"version": "22.09.08"
"version": "22.09.12"
}
},
"plugins": {

View file

@ -21,11 +21,11 @@
include "ui/required.php";
include "ui/nav.php";
include "app/image/get_image_info.php";
include "app/image/image_privilage.php";
include "app/format/string_to_tags.php";
use App\Account;
use App\Image;
$image_info = new Image;
$user_info = new Account;
/*
|-------------------------------------------------------------
@ -37,7 +37,7 @@
*/
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
// Get all image info
$image = get_image_info($conn, $_GET['id']);
$image = $image_info->get_image_info($conn, $_GET['id']);
// Check if image is avalible
if (isset($image['imagename'])) {
@ -45,17 +45,17 @@
} else {
?>
<script>
sniffleAdd('Woops', 'Something happened, either image with the ID <?php echo $_GET['id']; ?> was deleted or never existed, either way it could not be found!', 'var(--red)', '<?php echo $root_dir; ?>assets/icons/cross.svg');
sniffleAdd('Woops', 'Something happened, either image with the ID <?php echo $_GET['id']; ?> was deleted or never existed, either way it could not be found!', 'var(--red)', 'assets/icons/cross.svg');
</script>
<?php
<?php
$image_present = False;
}
} else {
?>
<script>
sniffleAdd('Where is da image?', 'The link you followed seems to be broken, or there was some other error, who knows!', 'var(--red)', '<?php echo $root_dir; ?>assets/icons/cross.svg');
sniffleAdd('Where is da image?', 'The link you followed seems to be broken, or there was some other error, who knows!', 'var(--red)', 'assets/icons/cross.svg');
</script>
<?php
<?php
$image_present = False;
}
@ -76,7 +76,7 @@
*/
if (isset($image['author'])) {
// Get all information on the user
$user = get_user_info($conn, $image['author']);
$user = $user_info->get_user_info($conn, $image['author']);
if (isset($user['username'])) {
$image_author = $user['username'];
@ -123,7 +123,7 @@
| Check user privilge
|-------------------------------------------------------------
*/
if (image_privilage($image['author']) || is_admin($_SESSION['id'])) {
if ($image_info->image_privilage($image['author']) || $user_info->is_admin($_SESSION['id'])) {
$privilaged = True;
} else {
$privilaged = False;
@ -363,7 +363,7 @@
|-------------------------------------------------------------
-->
<?php
if (is_admin($_SESSION['id'])) {
if ($user_info->is_admin($_SESSION['id'])) {
?>
<button id='authorButton' class='btn btn-bad'><img class='svg' src='assets/icons/edit.svg'>Edit author</button>
<script>

View file

@ -83,7 +83,6 @@ include "ui/nav.php";
?>
</div>
<?php include "ui/footer.php"; ?>
</body>
</html>

View file

@ -11,8 +11,12 @@
include "ui/required.php";
include "ui/nav.php";
use App\Account;
$user_info = new Account();
// Check if the user is logged in, otherwise redirect to login page
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
if ($user_info->is_loggedin() != true) {
header("location: account.php");
exit;
}

View file

@ -1,3 +1,9 @@
<?php
use App\Account;
$loggedin = new Account();
?>
<nav class="nav-root flex-left">
<div class="nav-name flex-left">
<p><?php echo $user_settings['website']['name']; ?></p>
@ -8,12 +14,16 @@
<a class='btn' href='search.php'><img class='svg' src='assets/icons/binoculars.svg'><span class='nav-hide'>Search</span></a>
<hr>
<?php
if (loggedin()) {
echo "<a class='btn' href='upload.php'><img class='svg' src='assets/icons/upload.svg'><span class='nav-hide'>Upload</span></a>";
echo "<hr>";
echo "<a class='btn' href='account.php'><img class='svg' src='assets/icons/user-circle.svg'><span class='nav-hide'>".substr($_SESSION["username"], 0, 15)."</span></a>";
if ($loggedin->is_loggedin()) {
?>
<a class='btn' href='upload.php'><img class='svg' src='assets/icons/upload.svg'><span class='nav-hide'>Upload</span></a>
<hr>
<a class='btn' href='account.php'><img class='svg' src='assets/icons/user-circle.svg'><span class='nav-hide'><?php echo substr($_SESSION["username"], 0, 15); ?></span></a>
<?php
} else {
echo "<a class='btn' href='account.php'><img class='svg' src='assets/icons/sign-in.svg'><span class='nav-hide'>Login</span></a>";
?>
<a class='btn' href='account.php'><img class='svg' src='assets/icons/sign-in.svg'><span class='nav-hide'>Login</span></a>
<?php
}
?>
</div>

View file

@ -4,10 +4,8 @@
*/
include "app/settings/settings.php";
if ($debug["testing"]) {
/*
Used for testing, do not use this in production
*/
/*if ($debug["testing"]) {
// Used for testing, do not use this in production
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ERROR | E_PARSE | E_NOTICE);
@ -16,10 +14,18 @@ if ($debug["testing"]) {
sniffleAdd('Notice', 'This website is currently in a testing state, bugs may occur', 'var(--red)', 'assets/icons/cross.svg');
</script>
<?php
}
}*/
ini_set('post_max_size', '20M');
ini_set('upload_max_filesize', '20M');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ERROR | E_PARSE | E_NOTICE);
?>
<script>
sniffleAdd('Notice', 'This website is currently in a testing state, bugs may occur', 'var(--red)', 'assets/icons/cross.svg');
</script>
<?php
if (is_file("index.php")) {
$root_dir = "";
@ -33,9 +39,10 @@ if (is_file("index.php")) {
include "app/server/conn.php";
include "app/server/secrete.php";
include "app/account/get_info.php";
include "app/account/is_admin.php";
include "app/account/login_status.php";
/*
Classes
*/
include 'app/app.php';
?>
<script>

View file

@ -58,13 +58,16 @@
include "ui/required.php";
include "ui/nav.php";
use App\Account;
$user_info = new Account();
// Check if user is logged in
if (!loggedin()) {
echo "
<script>
sniffleAdd('Who are you!', 'You must be loggedin to upload things, sowwy!', 'var(--red)', 'assets/icons/cross.svg');
</script>
";
if (!$user_info->is_loggedin()) {
?>
<script>
sniffleAdd('Who are you!', 'You must be loggedin to upload things, sowwy!', 'var(--red)', 'assets/icons/cross.svg');
</script>
<?php
}
?>