pile/www/admin.php

160 lines
5.6 KiB
PHP
Raw Normal View History

2017-03-07 09:38:47 +01:00
<?php
require '_templates/Template.php';
require '_util/PileDB.php';
require '_util/Uploader.php';
$db = new PileDB();
$uploader = new Uploader();
session_start();
2018-07-31 15:58:23 +02:00
if (isset($_SESSION['ID'])) {
2017-03-07 09:38:47 +01:00
$page = new Template();
2018-07-31 15:58:23 +02:00
if (isset($_GET["action"])) {
switch ($_GET["action"]) {
2017-03-07 09:38:47 +01:00
case "new_tag":
2017-03-07 10:15:20 +01:00
$content = $page->render("admin_tag_edit.php");
2017-03-07 09:38:47 +01:00
break;
case "edit_tag":
2018-07-31 15:58:23 +02:00
if (isset($_POST["Name"])) {
2017-03-07 10:15:20 +01:00
$db->updateTag(
$_GET["tag"],
$_POST["Name"],
$_POST["Description"]
);
}
2018-07-31 15:58:23 +02:00
if (!empty($_GET["tag"])) {
2017-03-07 10:15:20 +01:00
$page->tag = $db->fetchTag($_GET["tag"]);
}
$content = $page->render("admin_tag_edit.php");
2017-03-07 09:38:47 +01:00
break;
2018-09-17 11:44:47 +02:00
case "delete_tag":
if (!empty($_GET["tag"])) {
$db->deleteTag($_GET["tag"]);
$page->text = "Tag deleted successfully.";
$page->redirect = "/admin.php";
echo $page->render('full_text.php');
return;
}
break;
2017-03-07 09:38:47 +01:00
case "new_item":
$content = $page->render("admin_doc_edit.php");
break;
case "edit_item":
2018-07-31 15:58:23 +02:00
if (isset($_POST["Title"]) || !empty($_FILES['upfile']['name'])) {
2017-03-08 09:49:25 +01:00
$title = $_POST["Title"];
2018-07-31 15:58:23 +02:00
if (!empty($_FILES['upfile']['name'])) {
2017-03-07 09:38:47 +01:00
try {
2018-07-31 15:58:23 +02:00
if (empty($title)) {
2017-03-08 09:49:25 +01:00
$title = pathinfo($_FILES['upfile']['name'], PATHINFO_FILENAME);
$title = str_replace("_", " ", $title);
$title = trim($title);
}
2017-03-07 10:25:14 +01:00
$url = "http://pile.sdbs.cz/docs/" . rawurlencode($uploader->handle($_FILES, "docs/"));
2018-07-31 15:58:23 +02:00
} catch (RuntimeException $ex) {
2017-03-07 09:38:47 +01:00
$page->text = $ex->getMessage();
echo $page->render('full_text.php');
return;
}
} else {
$url = $_POST["URL"];
}
$doc_tags = [];
2018-07-31 15:58:23 +02:00
foreach (explode(",", $_POST["Tags"]) as $tagName) {
2017-03-07 09:38:47 +01:00
$tagName = trim($tagName);
$tag = $db->findTag($tagName);
2018-07-31 15:58:23 +02:00
if (!in_array($tag["ID"], $doc_tags)) {
2017-03-07 09:38:47 +01:00
array_push($doc_tags, $tag["ID"]);
}
}
$db->updateDoc(
2017-03-07 10:15:20 +01:00
$_GET["item"],
2017-03-08 09:49:25 +01:00
$title,
2017-03-07 09:38:47 +01:00
$_POST["Author"],
$_POST["Description"],
$_POST["Published"],
$url,
$doc_tags
2017-03-07 10:15:20 +01:00
);
2017-03-07 09:38:47 +01:00
}
2017-03-07 10:15:20 +01:00
2018-07-31 15:58:23 +02:00
if (!empty($_GET["item"])) {
2017-03-07 09:38:47 +01:00
$page->doc = $db->fetchDoc($_GET["item"]);
}
2017-03-07 10:15:20 +01:00
$content = $page->render("admin_doc_edit.php");
2017-03-07 09:38:47 +01:00
break;
case "remove":
2018-07-31 15:58:23 +02:00
if (!empty($_GET["confirm"]) && $_GET["confirm"] == "yes") {
2017-03-07 09:38:47 +01:00
$db->removeDoc($_GET["item"]);
$page->text = "Document deleted.";
$page->redirect = $_GET["ret"];
echo $page->render("full_text.php");
return;
} else {
$page->doc = $db->fetchDoc($_GET["item"]);
$content = $page->render("admin_doc_remove.php");
}
break;
case "logout":
unset($_SESSION["ID"]);
$page->text = "See you.";
$page->redirect = "/";
echo $page->render("full_text.php");
return;
}
} elseif (isset($_GET["tag"])) {
$doc_list_template = new Template();
2018-07-31 15:58:23 +02:00
if ($_GET["tag"] == "*") {
2017-03-07 09:38:47 +01:00
$docs = $db->listDocs();
} elseif ($_GET["tag"] == "_") {
$docs = $db->listDocs(-1);
} else {
$tag = $db->fetchTag($_GET["tag"]);
2017-03-07 09:38:47 +01:00
$docs = $db->listDocs($tag["ID"]);
$doc_list_template->tag = $tag;
2017-03-07 09:38:47 +01:00
}
$doc_list_template->docs = $docs;
$content = $doc_list_template->render('admin_doc_listing.php');
} else {
$intro_template = new Template();
$content = $intro_template->render('admin_intro.php');
}
$all_count = $db->getDocCount();
2017-03-07 21:20:20 +01:00
$none_count = $db->getUntaggedDocCount();
2017-03-07 09:38:47 +01:00
$tags = $db->getTags();
$page->all_count = $all_count;
$page->none_count = $none_count;
$page->tags = $tags;
$page->content = $content;
echo $page->render('admin_wrap.php');
} else {
$page = new Template();
2018-07-31 15:58:23 +02:00
if (isset($_POST['username']) && isset($_POST['password'])) {
2017-03-07 09:38:47 +01:00
$ret_id = $db->authenticate($_POST["username"], $_POST["password"]);
2018-07-31 15:58:23 +02:00
if ($ret_id > 0) {
2017-03-07 09:38:47 +01:00
$_SESSION['ID'] = $ret_id;
$page->text = "You have logged in successfully.";
2018-07-31 15:58:23 +02:00
$page->redirect = "admin.php";
2017-03-07 09:38:47 +01:00
} else {
$page->text = "Username and/or password incorrect.";
2018-07-31 15:58:23 +02:00
$page->redirect = "/";
2017-03-07 09:38:47 +01:00
}
} else {
$page->text = "Please log in before accessing this page.";
2018-07-31 15:58:23 +02:00
$page->redirect = "/";
2017-03-07 09:38:47 +01:00
}
echo $page->render('full_text.php');
}
?>