Add support for history, and some other small changes.

This commit is contained in:
Jacob 2024-02-04 22:43:39 +01:00
parent cbf28ac578
commit bf398bdfc8
7 changed files with 194 additions and 20 deletions

View file

@ -10,6 +10,8 @@ logo = logo.svg
favicon = favicon.svg
document_location = documents/
attachment_location = attachments/
modified_location = modified/
history_location = history/
[public]
public_account_create = true
@ -30,3 +32,4 @@ store_last_usage = true
[format]
date_format = Y/m/d
time_format = h:i:sa

View file

@ -14,11 +14,14 @@ $publicAccountCreation = true;
$allowPasswordChange = true;
$logoHeaderSize = 24;
$dateFormat = "Y/m/d";
$timeFormat = "h:i:sa";
$instanceName = "csgen";
$instanceDescription = "This is a csgen instance.";
$footerText = "Licensed under the GNU Affero General Public License version 3.0.<br><br>Made in Sweden";
$documentLocation = "documents/";
$attachmentLocation = "attachments/";
$modifiedLocation = "modified/";
$historyLocation = "history/";
$configFile = "";
@ -46,10 +49,13 @@ $storeCreated = $configEntries['store_created'];
$storeLastUsage = $configEntries['store_last_usage'];
$logoHeaderSize = $configEntries['logo_header_size'];
$dateFormat = $configEntries['date_format'];
$timeFormat = $configEntries['time_format'];
$instanceName = $configEntries['instance_name'];
$instanceDescription = $configEntries['instance_description'];
$documentLocation = $configEntries['document_location'];
$attachmentLocation = $configEntries['attachment_location'];
$modifiedLocation = $configEntries['modified_location'];
$historyLocation = $configEntries['history_location'];
$footerText = $configEntries['footer_text'];
$allowUsernameChange = $configEntries['allow_change_username'];
$allowPasswordChange = $configEntries['allow_change_password'];

View file

@ -55,6 +55,25 @@ function createTables($sqlDB) {
*/
$Database->exec("CREATE TABLE IF NOT EXISTS pages(id INTEGER PRIMARY KEY, username TEXT, date TEXT, endpoint TEXT, file TEXT)");
/* modified table
* id (INTEGER PRIMARY KEY)
* pageid (INT)
* author (TEXT)
* endpoint (TEXT)
* file (TEXT)
*/
$Database->exec("CREATE TABLE IF NOT EXISTS modified(id INTEGER PRIMARY KEY, pageid INT, author TEXT, endpoint TEXT, file TEXT)");
/* history table
* id (INTEGER PRIMARY KEY)
* pageid (INT)
* username (TEXT)
* date (TEXT)
* endpoint (TEXT)
* file (TEXT)
*/
$Database->exec("CREATE TABLE IF NOT EXISTS history(id INTEGER PRIMARY KEY, pageid INT, username TEXT, date TEXT, endpoint TEXT, file TEXT)");
return $Database;
}
@ -595,6 +614,8 @@ function checkIfAdminExists() {
if (!is_dir($documentLocation)) mkdir($documentLocation, 0777, true);
if (!is_dir($attachmentLocation)) mkdir($attachmentLocation, 0777, true);
if (!is_dir($historyLocation)) mkdir($historyLocation, 0777, true);
if (!is_dir($modifiedLocation)) mkdir($modifiedLocation, 0777, true);
$adminExists = 0;
while ($line = $DatabaseQuery->fetchArray()) {

View file

@ -8,6 +8,7 @@ $Authorized = 0;
$Primary = 0;
$postID = -1;
$Error = "";
$History = "false";
if (!isset($_SESSION['username']) || !isset($_SESSION['password']) || !isset($_SESSION['type'])) {
header('Location: login.php?redir=edit');
@ -23,6 +24,12 @@ if (!isset($_REQUEST['action'])) {
$Action = htmlspecialchars($_REQUEST['action']);
}
if (!isset($_REQUEST['history'])) {
$History = "false";
} else {
$History = htmlspecialchars($_REQUEST['history']);
}
if (!isset($_REQUEST['id'])) {
$postID = -1;
} else {
@ -60,21 +67,21 @@ $html .= "\t\t\t\t<div class=\"pageLinks\">\n";
$html .= "\t\t\t\t\t<span id=\"pageSpan\" class=\"title\">\n";
if ($Action == "write") {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=write\" id='sel'>Write</a>\n";
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=write&id=$postID\" id='sel'>Write</a>\n";
} else {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=write\">Write</a>\n";
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=write&id=$postID\">Write</a>\n";
}
if ($Action == "attachments") {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=attachments\" id='sel'>Attachments</a>\n";
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=attachments&id=$postID\" id='sel'>Attachments</a>\n";
} else {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=attachments\">Attachments</a>\n";
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=attachments&id=$postID\">Attachments</a>\n";
}
if ($Action == "articles") {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=articles\" id='sel'>Articles</a>\n";
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=articles&id=$postID\" id='sel'>Articles</a>\n";
} else {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=articles\">Articles</a>\n";
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=articles&id=$postID\">Articles</a>\n";
}
$html .= "\t\t\t\t\t</span>\n";
@ -98,6 +105,23 @@ if ($Action == "write") {
}
}
if ($History == "true") {
$DatabaseQuery = $Database->query('SELECT * FROM history');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['id'] == $postID && $postID != -1) {
$theFile = $line['file'];
if (file_exists($theFile)) {
$defaultText = file_get_contents($theFile);
}
$defaultEndpoint = $line['endpoint'];
$postID = $line['pageid'];
break;
}
}
}
$html .= "\t\t\t\t<p class=\"pageWarning\"><strong>Warning: Switching tab will delete changes made to the Markdown document. Press 'Save' to avoid this.</strong></p>\n";
if ($postID == -1) {
@ -120,13 +144,23 @@ if ($Action == "write") {
$html .= "\t\t\t\t\t<br><input type=\"submit\" value=\"Save\"><br><br>\n";
$html .= "\t\t\t\t</form>\n";
// add history button if we're editing an existing page
if ($defaultEndpoint != "") {
$html .= "\t\t\t\t\t\t<a href=\"/edit.php?action=history&id=$postID\">History</a>\n";
}
// handle errors
if ($Error == "endpoint") {
$html .= "\t\t\t\t<p class=\"pageError\">You must specify a valid endpoint (e.g. /blog/article1)</p>\n";
} else if ($Error == "file") {
$html .= "\t\t\t\t<p class=\"pageError\">Failed to upload file.</p>\n";
} else if ($Error == "ofile") {
$html .= "\t\t\t\t<p class=\"pageError\">Failed to back up file, aborting.</p>\n";
} else if ($Error == "exists") {
$html .= "\t\t\t\t<p class=\"pageError\">A file with this endpoint already exists.</p>\n";
} else if ($Error == "saved") { // not actually an error but i don't want to make this too complicated
$Date = date($dateFormat) . " at " . date($timeFormat);
$html .= "\t\t\t\t<p class=\"pageSuccess\">$Date: Page at endpoint '$defaultEndpoint' saved.</p>\n";
}
} else if ($Action == "attachments") {
$html .= "\t\t\t\t<form class=\"pageFileUploadForm\" action=\"/upload.php?redir=edit\" method=\"post\" enctype=\"multipart/form-data\">\n";
@ -166,9 +200,45 @@ if ($Action == "write") {
$html .= "\t\t\t\t<p class=\"pageError\">You must specify a valid endpoint (e.g. /blog/article1)</p>\n";
} else if ($Error == "file") {
$html .= "\t\t\t\t<p class=\"pageError\">Failed to upload file.</p>\n";
} else if ($Error == "ofile") {
$html .= "\t\t\t\t<p class=\"pageError\">Failed to back up file, aborting.</p>\n";
} else if ($Error == "exists") {
$html .= "\t\t\t\t<p class=\"pageError\">A file with this endpoint already exists.</p>\n";
}
} else if ($Action == "history") {
$html .= "\t\t\t\t<table class=\"historyUserView\">\n";
$html .= "\t\t\t\t\t<tr class=\"historyArticleView\">\n";
$html .= "\t\t\t\t\t\t<th class=\"historyUser\">User</th>\n";
$html .= "\t\t\t\t\t\t<th class=\"historyDate\">Date</th>\n";
$html .= "\t\t\t\t\t\t<th class=\"historyEndpoint\">Location</th>\n";
$html .= "\t\t\t\t\t\t<th class=\"historyFile\">File</th>\n";
$html .= "\t\t\t\t\t</tr>\n";
$DatabaseQuery = $Database->query('SELECT * FROM history');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['pageid'] != $postID) {
continue;
}
$ID = $line['id'];
$Username = $line['username'];
$Date = $line['date'];
$Endpoint = $line['endpoint'];
$File = $line['file'];
$baseFile = basename($File);
$html .= "\t\t\t\t\t<tr class=\"historyArticleView\">\n";
$html .= "\t\t\t\t\t\t<td class=\"historyUser\">$Username</td>\n";
$html .= "\t\t\t\t\t\t<td class=\"historyDate\">$Date</td>\n";
$html .= "\t\t\t\t\t\t<td class=\"historyEndpoint\"><a href=\"../$Endpoint\">$Endpoint</a></td>\n";
$html .= "\t\t\t\t\t\t<td class=\"historyFile\"><a href=\"$File\">$baseFile</a></td>\n";
$html .= "\t\t\t\t\t\t<td class=\"historyRestore\"><a href=\"/edit.php?id=$ID&history=true\">Restore</a></td>\n";
$html .= "\t\t\t\t\t\t<td class=\"historyRemove\"><a href=\"/remove.php?redir=edit&id=$ID&history=true\">Remove</a></td>\n";
$html .= "\t\t\t\t\t</tr>\n";
}
$html .= "\t\t\t\t</table>\n";
} else if ($Action == "articles") {
$html .= "\t\t\t\t<table class=\"pageUserView\">\n";
$html .= "\t\t\t\t\t<tr class=\"pageArticleView\">\n";
@ -207,6 +277,8 @@ if ($Action == "write") {
$html .= "\t\t\t\t<p class=\"pageError\">You must specify a valid endpoint (e.g. /blog/article1)</p>\n";
} else if ($Error == "file") {
$html .= "\t\t\t\t<p class=\"pageError\">Failed to upload file.</p>\n";
} else if ($Error == "ofile") {
$html .= "\t\t\t\t<p class=\"pageError\">Failed to back up file, aborting.</p>\n";
} else if ($Error == "exists") {
$html .= "\t\t\t\t<p class=\"pageError\">A file with this endpoint already exists.</p>\n";
}

View file

@ -168,6 +168,10 @@ input[type=file]::file-selector-button {
color: #ff0000;
}
.pageSuccess {
color: #00ff51;
}
.content {
font-size: 12pt;
font-family: Noto Sans;

View file

@ -4,6 +4,7 @@ include "config.php";
$id = -1;
$History = "false";
$Redirect = "";
$AuthorizedCreation = 0;
@ -11,8 +12,12 @@ if (isset($_REQUEST['redir'])) {
$Redirect = htmlspecialchars($_REQUEST['redir']);
}
if (isset($_REQUEST['history'])) {
$History = htmlspecialchars($_REQUEST['history']);
}
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$id = htmlspecialchars($_REQUEST['id']);
} else {
if ($Redirect == "admin") {
header("Location: admin.php?e=endpoint");
@ -52,26 +57,56 @@ if ($AuthorizedCreation != 1) {
die();
}
$DatabaseQuery = $Database->query('SELECT * FROM pages');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['id'] == $id && $id != -1) {
$File = $line['file'];
$Directory = dirname($File);
if ($History == "false") {
$DatabaseQuery = $Database->query('SELECT * FROM pages');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['id'] == $id && $id != -1) {
$File = $line['file'];
$Directory = dirname($File);
if (is_dir($Directory)) {
rmdir($Directory);
} else if (is_file($File)) {
unlink($File);
if (is_file($File)) {
unlink($File);
if (is_dir($Directory)) {
rmdir($Directory);
}
}
$Database->exec("DELETE FROM pages WHERE id='$id'");
break;
}
}
} else {
$DatabaseQuery = $Database->query('SELECT * FROM history');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['id'] == $id && $id != -1) {
$File = $line['file'];
$Directory = dirname($File);
if (is_file($File)) {
unlink($File);
if (is_dir($Directory)) {
rmdir($Directory);
}
}
$Database->exec("DELETE FROM history WHERE id='$id'");
break;
}
}
}
$Database->exec("DELETE FROM pages WHERE id='$id'");
if ($Redirect == "admin") {
header("Location: admin.php?action=users");
} else if ($Redirect == "edit") {
header("Location: edit.php?action=articles");
if ($History == "true") {
header("Location: edit.php?action=history&id=$id");
} else {
header("Location: edit.php?action=articles");
}
} else {
header("Location: /");
}

View file

@ -8,6 +8,8 @@ $Endpoint = "";
$File = "";
$id = -1;
$noHist = false;
$Redirect = "";
$AuthorizedCreation = 0;
@ -85,6 +87,8 @@ while ($line = $DatabaseQuery->fetchArray()) {
}
}
$OldFile = "";
if (isset($_REQUEST['body']) && htmlspecialchars($_REQUEST['body']) != "") {
$Body = htmlspecialchars($_REQUEST['body']);
@ -93,6 +97,31 @@ if (isset($_REQUEST['body']) && htmlspecialchars($_REQUEST['body']) != "") {
die();
}
// back up the old file first
$OldFileContents = file_get_contents($File);
$Hash = hash('sha256', $OldFileContents);
$OldFile = "$historyLocation/$Hash/$Hash.md";
if (file_exists($OldFile)) {
$noHist = true;
} else {
if (!is_dir("$historyLocation/$Hash")) {
mkdir("$historyLocation/$Hash", 0777, true);
}
if (!file_put_contents($OldFile, $OldFileContents)) {
if ($Redirect == "admin") {
header("Location: admin.php?e=ofile");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=ofile&action=articles");
} else {
header("Location: /");
}
die();
}
}
// now write to the new file
if (!file_put_contents($File, $Body)) {
if ($Redirect == "admin") {
header("Location: admin.php?e=file");
@ -119,10 +148,14 @@ if (isset($_REQUEST['body']) && htmlspecialchars($_REQUEST['body']) != "") {
$Database->exec("UPDATE pages SET date='$Date' WHERE id='$id'");
$Database->exec("UPDATE pages SET endpoint='$Endpoint' WHERE id='$id'");
if ($noHist == false) {
$Database->exec("INSERT INTO history(username, pageid, date, endpoint, file) VALUES('$Username', '$id', '$Date', '$Endpoint', '$OldFile')");
}
if ($Redirect == "admin") {
header("Location: admin.php?action=users");
} else if ($Redirect == "edit") {
header("Location: edit.php?action=articles");
header("Location: edit.php?action=write&id=$id&e=saved");
} else {
header("Location: /");
}