added link editor and fixes to login system

This commit is contained in:
Conner Harkness 2025-06-23 07:19:09 -06:00
parent 664544fea4
commit 7b20cd13b6
12 changed files with 154 additions and 31 deletions

View File

@ -1,11 +1,18 @@
<?php
global $c;
$varFooterLinks = $c->query("SELECT * from links where position like 'footer' order by sort");
?>
<hr />
<div class="container">
<div class="row">
<div class="col-lg-4">
<?php foreach ($varFooterLinks as $varLink): ?>
<div>
Copyright &copy; 2025 Your Company.
<a class="link-underline link-underline-opacity-0" href="<?= $varLink["url"]; ?>"><i class="fa fa-fw fa-<?= $varLink["icon"]; ?> pe-2"></i> <?= $varLink["label"]; ?></a>
</div>
<?php endforeach; ?>
</div>
</div>
</div>

View File

@ -1,9 +1,16 @@
<?php
global $c;
$varNavbarLinks = [
["Home", "/"],
["Sign in", "/user/signin"],
];
$varNavbarLinks = $c->query("SELECT * from links where position like 'navbar' order by sort");
$varSidebarLinks = $c->query("SELECT * from links where position like 'sidebar' order by sort");
$varFirstNavbarLink = array_shift($varNavbarLinks);
?>
<script>
@ -11,17 +18,26 @@
$("body").first().attr("data-bs-theme", "dark");
</script>
<div class="offcanvas offcanvas-start">
<div class="offcanvas offcanvas-start" id="sidebar">
<div class="offcanvas-body">
Hello world <span data-bs-dismiss="offcanvas">x</span>
<?php foreach ($varSidebarLinks as $varLink): ?>
<a class="btn btn-outline-secondary d-block w-100 mb-2" href="<?= $varLink["url"]; ?>"><i class="fa fa-fw fa-<?= $varLink["icon"]; ?> pe-2"></i> <?= $varLink["label"]; ?></a>
<?php endforeach; ?>
</div>
</div>
<div class="navbar navbar-expand bg-secondary d-flex px-3">
<div class="container justify-content-between">
<div class="navbar-nav d-inline-flex align-items-center">
<div class="navbar-nav d-inline-flex">
<span class="navbar-brand">Home</span>
<a class="btn btn-secondary me-2" data-bs-toggle="offcanvas" data-bs-target="#sidebar"><i class="fa fa-fw fa-bars"></i></a>
</div>
<a class="navbar-brand" href="<?= $varFirstNavbarLink["url"]; ?>"><?= $varFirstNavbarLink["label"]; ?></a>
<div class="dropdown d-lg-none">
@ -29,13 +45,13 @@
<div class="dropdown-menu">
<?php foreach ($varNavbarLinks as $varLink): ?>
<a class="dropdown-item" href="<?= $varLink[1]; ?>"><i class="fa fa-fw fa-link pe-2"></i> <?= $varLink[0]; ?></a>
<a class="dropdown-item" href="<?= $varLink["url"]; ?>"><i class="fa fa-fw fa-<?= $varLink["icon"]; ?> pe-2"></i> <?= $varLink["label"]; ?></a>
<?php endforeach; ?>
</div>
</div>
<?php foreach ($varNavbarLinks as $varLink): ?>
<a class="nav-link d-none d-lg-inline" href="<?= $varLink[1]; ?>"><?= $varLink[0]; ?></a>
<a class="nav-link d-none d-lg-inline" href="<?= $varLink["url"]; ?>"><?= $varLink["label"]; ?></a>
<?php endforeach; ?>
</div>

View File

@ -5,6 +5,30 @@
"sqlite",
"sqlite.db");
$c->query(
"CREATE table if not exists links (
id integer primary key autoincrement,
label text not null,
url text not null,
icon text not null,
position text not null,
sort integer not null default 0)");
$varLinks = $c->query("SELECT * from links");
if (count($varLinks) < 1)
{
$c->query(
"INSERT into links (label, url, icon, position)
values
('Home', '/', 'home', 'navbar'),
('Post', '/edit', 'edit', 'navbar'),
('Links', '/edit/links', 'link', 'navbar'),
('Go home', '/', 'home', 'sidebar'),
('Copyright © 2025 Your Company.', '/', 'home', 'footer')");
}
class UserAuth
{
public static function getUser()

View File

@ -5,7 +5,7 @@
{
global $c;
$varRows = $c->query("SELECT * from {$strTableName}");
$varRows = $c->query("SELECT * from {$strTableName} order by `sort` asc");
$varKeys = [];
$strInput = file_get_contents("php://input");
@ -34,8 +34,6 @@
$strQMarks = preg_replace("/, $/", "", $strQMarks);
$strSetLns = preg_replace("/, $/", "", $strSetLns);
if (strlen($r["id"]) < 1)
{
$c->query(
@ -117,6 +115,8 @@
<tr>
<?php foreach($varRows[0] as $k => $v): ?>
<?php
if ($k == "sort")
continue;
$varKeys[] = $k;
?>
<th><?= $k; ?></th>
@ -141,6 +141,11 @@
<input type="hidden" name="delete" value="0" />
<a class="" onclick="fnCloneRow(this);"><i class="fa fa-fw fa-copy"></i></a>
<a class="" onclick="fnDeleteRow(this);"><i class="fa fa-fw fa-trash"></i></a>
<?php if (in_array("sort", $varColumns)): ?>
<a class="" onclick="fnMoveRowUp(this);"><i class="fa fa-fw fa-arrow-up"></i></a>
<a class="" onclick="fnMoveRowDown(this);"><i class="fa fa-fw fa-arrow-down"></i></a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
@ -156,9 +161,16 @@
<script>
$(function() {
$("[name='id']").each(function(i, x) {
x = $(x);
x.attr("readonly", 1);
});
fnSerialize = function() {
var a = [];
var sort = 0;
$("table tbody tr").each(function(i, x) {
x = $(x);
@ -172,7 +184,10 @@
o[key] = value;
});
o["sort"] = sort;
a.push(o);
sort++;
});
console.log(a);
@ -190,6 +205,7 @@
success: function(r)
{
console.log(r);
window.location.href = window.location.href;
}
});
};
@ -216,6 +232,20 @@
row.hide();
row.find("[name='delete']").first().val("1");
};
fnMoveRowUp = function(x)
{
x = $(x);
var row = x.parents("tr").first();
row.insertBefore(row.prev());
}
fnMoveRowDown = function(x)
{
x = $(x);
var row = x.parents("tr").first();
row.insertAfter(row.next());
}
});
</script>
<?php

View File

@ -1,6 +1,8 @@
<?php
global $c;
UserAuth::requirePermission("admin");
$strId = Request::getArg(0);
$strPath = "";
$strContent = "";
@ -12,6 +14,7 @@
if (count($varRows) !== 1)
{
BootstrapRender::message("Zero or more than one row returned", "danger");
Respond::redirect("/edit");
}
$varRow = $varRows[0];
@ -37,29 +40,35 @@
$strId = $c->query("SELECT * from post where rowid = last_insert_rowid()")[0]["id"];
}
if (strlen($strContent) < 1)
{
$c->query("DELETE from post where id = ?", $strId);
BootstrapRender::message("Post deleted successfully.", "success");
Respond::redirect("/edit");
}
$c->query(
"UPDATE post
set
path = ?,
content = ?,
updated = current_timestamp",
updated = current_timestamp
where
id = ?",
$strPath,
$strContent);
$strContent,
$strId);
Respond::redirect("/edit/{$strId}");
}
if (strlen($strId) > 0)
{
$varRows = $c->query("SELECT * from post where id = ?", $strId);
}
?>
<style>
textarea {
font-family: monospace;
}
</style>
<div class="container my-5">
<div class="row">
<div class="col-lg-6">

5
pages/edit/links.php Normal file
View File

@ -0,0 +1,5 @@
<?php
global $c;
UserAuth::requirePermission("admin");
TableEditor::render("links", ["label", "url", "icon", "position", "sort"]);
?>

View File

@ -29,11 +29,14 @@
<div class="container my-5">
<div class="row">
<div class="col-lg-12">
<div class="border border-secondary rounded p-3">
<?php
$strContent = $varParsedown->text($p["content"]);
echo $strContent;
?>
</div>
<a href="/edit/<?= $p["id"]; ?>">edit</a>
</div>
</div>
</div>
<?php endforeach; ?>

View File

@ -2,6 +2,9 @@
global $c;
$strError = null;
if (UserAuth::getUser() == null)
Respond::redirect("/user/signin");
$c->query(
"CREATE table if not exists user_info (
id integer primary key autoincrement,

View File

@ -3,4 +3,3 @@
UserAuth::requirePermission("hello_world");
TableEditor::render("user", ["email", "hash"]);
?>

View File

@ -1,5 +1,5 @@
<?php
global $c;
UserAuth::requirePermission("hello_world");
TableEditor::render("permission", ["email", "name"]);
TableEditor::render("permission", ["email", "name", "sort"]);
?>

View File

@ -3,6 +3,15 @@
try
{
$intUserCount = $c->query("SELECT count(*) as val from user")[0]["val"];
if ($intUserCount < 1)
{
BootstrapRender::message(
"Please create an administrator account.",
"warning");
}
if (Request::posts("email", "password", "repeat"))
{
$strEmail = Request::getPosted("email");
@ -36,6 +45,19 @@
$strEmail,
$strHash);
$intUserCount = $c->query("SELECT count(*) as val from user")[0]["val"];
if ($intUserCount == 1)
{
// Calling this ensures permission table:
UserAuth::hasPermission("dummy");
$c->query(
"INSERT into permission (email, name)
values (?, 'admin')",
$strEmail);
}
BootstrapRender::message("Registration was a success, please sign in to continue.");
Respond::redirect("/user/signin");

View File

@ -3,6 +3,11 @@
try
{
$intUserCount = $c->query("SELECT count(*) as val from user")[0]["val"];
if ($intUserCount < 1)
Respond::redirect("/user/register");
if (Request::posts("email", "password"))
{
$strEmail = Request::getPosted("email");