starting to clean up this code

This commit is contained in:
biglyderv 2025-01-07 15:19:55 -05:00
parent 162bd9c8cb
commit 38075fd0c0
14 changed files with 103 additions and 98 deletions

76
docs/api/comment.php Normal file
View file

@ -0,0 +1,76 @@
<?php
require(__DIR__ . "/../../libs/page.php");
$ref = 1732684297;
function getOut($typer, $idr) {
if ($typer == 'user' || $typer == 'comment') {
header("Location: " . '/' . $typer . '.php?id=' . htmlspecialchars($idr));
die();
}
header("Location: /");
die();
}
function post_handler() {
global $ref;
global $username;
global $db;
if (!$username || !array_key_exists('post',$_POST)) return;
$postie = $_POST['post'];
$postType = (array_key_exists('type',$_POST)) ? $_POST['type'] : 'root';
$postId = (array_key_exists('id',$_POST)) ? $_POST['id'] : 'root';
if (strlen($postie) < 1 || strlen($postie) > 1024) {
getOut($postType,$postId);
}
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$fid = bin2hex(random_bytes(16));
$type = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$type = strtolower($type);
$mime = mime_content_type('test' . $type);
$legal = ['png','gif','jpeg','jpg','mp4','webm','mp3','wav'];
if (in_array($type, $legal)) {
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"] . '/../docs/pic/' . $fid . '.' . $type);
$postie .= "\nhttps://" . $_SERVER['HTTP_HOST'] . '/pic/' . $fid . '.' . $type;
}
}
$poid = bin2hex(random_bytes(16));
$stmt = $db->prepare("INSERT INTO main.comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)");
$stmt->execute([$username,$postType,$postId,microtime(true) - $ref,$postie,$poid]);
$postieP = "https://nbg.dervland.net/comment.php?id=" . $poid;
$user10 = '%system';
if ($postType == 'user') {
$postie = "A user commented on your wall: " . $postieP;
$stmt = $db->prepare("INSERT INTO main.comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)");
$stmt->execute([$user10,'notification',$postId,microtime(true) - $ref,$postie,bin2hex(random_bytes(16))]);
} else if ($postType == 'comment') {
$stmt = $db->prepare("SELECT * FROM comment WHERE id = ? ORDER BY date DESC");
$stmt->execute([
$poid
]);
$posts1 = $stmt->fetchAll(PDO::FETCH_DEFAULT);
$posts2 = $posts1[0]['username'];
$postie = "A user replied to your post: " . $postieP;
$stmt = $db->prepare("INSERT INTO main.comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)");
$stmt->execute([$user10,'notification',$posts2,microtime(true) - $ref,$postie,bin2hex(random_bytes(16))]);
}
getOut($postType,$postId);
}
post_handler();
?>

View file

@ -1,5 +1,5 @@
<?php
require("../libs/auth.php");
require(__DIR__ . "/../libs/auth.php");
global $username;
echo $username;
?>

View file

@ -1,7 +1,7 @@
<?php
require("../libs/page.php");
require("../libs/form.php");
require("../libs/comment.php");
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
require(__DIR__ . "/../libs/comment.php");
$ref = 1732684297;

View file

@ -1,80 +1,9 @@
<?php
require("../libs/page.php");
require("../libs/form.php");
require("../libs/comment.php");
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
require(__DIR__ . "/../libs/comment.php");
$ref = 1732684297;
function getOut($typer, $idr) {
if ($typer == 'user' || $typer == 'comment') {
header("Location: " . '/' . $typer . '.php?id=' . htmlspecialchars($idr));
die();
}
header("Location: " . $_SERVER["REQUEST_URI"]);
die();
}
function post_handler() {
global $ref;
global $username;
global $db;
if (!$username || !array_key_exists('post',$_POST)) return;
$postie = $_POST['post'];
$postType = (array_key_exists('type',$_POST)) ? $_POST['type'] : 'root';
$postId = (array_key_exists('id',$_POST)) ? $_POST['id'] : 'root';
if (strlen($postie) < 1 || strlen($postie) > 1024) {
getOut($postType,$postId);
}
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$fid = bin2hex(random_bytes(16));
$type = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$type = strtolower($type);
$mime = mime_content_type('test' . $type);
$legal = ['png','gif','jpeg','jpg','mp4','webm','mp3','wav'];
if (in_array($type, $legal)) {
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"] . '/../docs/pic/' . $fid . '.' . $type);
$postie .= "\nhttps://" . $_SERVER['HTTP_HOST'] . '/pic/' . $fid . '.' . $type;
}
}
$poid = bin2hex(random_bytes(16));
$stmt = $db->prepare("INSERT INTO main.comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)");
$stmt->execute([$username,$postType,$postId,microtime(true) - $ref,$postie,$poid]);
$postieP = "https://nbg.dervland.net/comment.php?id=" . $poid;
$user10 = '%system';
if ($postType == 'user') {
$postie = "A user commented on your wall: " . $postieP;
$stmt = $db->prepare("INSERT INTO main.comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)");
$stmt->execute([$user10,'notification',$postId,microtime(true) - $ref,$postie,bin2hex(random_bytes(16))]);
} else if ($postType == 'comment') {
$stmt = $db->prepare("SELECT * FROM comment WHERE id = ? ORDER BY date DESC");
$stmt->execute([
$poid
]);
$posts1 = $stmt->fetchAll(PDO::FETCH_DEFAULT);
$posts2 = $posts1[0]['username'];
$postie = "A user replied to your post: " . $postieP;
$stmt = $db->prepare("INSERT INTO main.comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)");
$stmt->execute([$user10,'notification',$posts2,microtime(true) - $ref,$postie,bin2hex(random_bytes(16))]);
}
getOut($postType,$postId);
}
post_handler();
page_header();
?>

View file

@ -1,6 +1,6 @@
<?php
require("../libs/page.php");
require("../libs/form.php");
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
function post_handler() {
global $db;

View file

@ -1,5 +1,5 @@
<?php
require("../libs/page.php");
require(__DIR__ . "/../libs/page.php");
page_header();
if (isset($_COOKIE)) {
foreach ( $_COOKIE as $key => $value )
@ -10,4 +10,4 @@
die();
}
page_footer();
?>
?>

View file

@ -1,7 +1,7 @@
<?php
require("../libs/page.php");
require("../libs/form.php");
require("../libs/comment.php");
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
require(__DIR__ . "/../libs/comment.php");
$ref = 1732684297;

View file

@ -1,6 +1,6 @@
<?php
require("../libs/page.php");
require("../libs/form.php");
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
function post_handler() {
global $db;
@ -55,4 +55,4 @@
));
page_footer();
?>
?>

View file

@ -1,5 +1,5 @@
<?php
require("../libs/page.php");
require(__DIR__ . "/../libs/page.php");
page_header();
?>

View file

@ -1,7 +1,7 @@
<?php
require("../libs/page.php");
require("../libs/form.php");
require("../libs/comment.php");
require(__DIR__ . "/../libs/page.php");
require(__DIR__ . "/../libs/form.php");
require(__DIR__ . "/../libs/comment.php");
$roles = array();

View file

@ -1,5 +1,5 @@
<?php
require("../libs/test_secret.php");
require(__DIR__ . "/test_secret.php");
function auth($token) {
if (is_null($token)) return null;

View file

@ -1,5 +1,5 @@
<?php
require("../libs/markdown.php");
require(__DIR__ . "/markdown.php");
function comments($typer, $idr, $disable = false) {
$ref = 1732684297;
global $username;
@ -11,7 +11,7 @@
array('key' => '','type' => 'hidden','name' => 'type', 'default' => $typer),
array('key' => '','type' => 'hidden','name' => 'id', 'default' => $idr),
array('key' => 'Attachments', 'type' => 'file', 'name' => 'file', 'default' => '')
),'/index.php');
),'/api/comment.php');
}
$qs = parse_url($_SERVER['REQUEST_URI']);

View file

@ -1,5 +1,5 @@
<?php
require("../libs/auth.php");
require(__DIR__ . "/auth.php");
global $username;
function page_header() {
global $username; ?>

View file

@ -1,5 +1,5 @@
<?php
require("../libs/test_secret.php");
require(__DIR__ . "/../libs/test_secret.php");
global $db;
@ -8,4 +8,4 @@
$db->exec($sql);
echo "Database was set up.";
?>
?>