Archived
1
0
This repository has been archived on 2020-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
old/application/controllers/admin/Blog.php

461 lines
19 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once './vendor/Diff/htmLawed.php';
use SebastianBergmann\Diff\Differ;
class Blog extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('BlogModel', '', TRUE);
$this->load->model('FileModel', '', TRUE);
}
public function index()
{
$this->neededPermission('blog.view');
$posts = $this->BlogModel->getPostList(false);
$this->load->view('admin/sidebar', ['title' => 'Alle Blog-Posts']);
$this->load->view('admin/blog_posts', ['posts' => $posts]);
$this->load->view('admin/footer');
}
public function tags()
{
$this->neededPermission('blog.view');
$tags = $this->BlogModel->getAllTags();
$tags = $this->BlogModel->mergeTagInfo($tags);
$this->load->view('admin/sidebar', ['title' => 'Alle Blog-Tags']);
$this->load->view('admin/blog_tags', ['tags' => $tags]);
$this->load->view('admin/footer', ['additionalScripts' => 'all-blog-tags.js']);
}
public function sendEdit()
{
header('Content-Type: application/json');
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo json_encode(['success' => false, 'message' => 'Du musst eingeloggt sein, um Blog-Posts zu verfassen']);
exit;
}
$postID = $this->input->post('postID');
$postID = is_numeric($postID) && is_int(intval($postID)) ? intval($postID) : -2;
if ($postID == -2) {
echo json_encode(['success' => false, 'message' => 'Ungültige Post-ID angegeben. Bitte versuche es später erneut']);
exit;
}
$versionID = $this->input->post('versionID');
$versionID = is_numeric($versionID) && is_int(intval($versionID)) ? intval($versionID) : -2;
$image = $this->input->post('postImage');
$title = $this->input->post('postTitle');
$description = $this->input->post('postDescription');
$content = $this->input->post('postContent');
$initialRelease = $this->input->post('postPublishDate');
$initialRelease = date("Y-m-d H:i:s", strtotime($initialRelease));
$url = $this->input->post('postUrl');
$categories = $this->input->post('postCategories');
$tags = $this->input->post('postTags');
$lang = $this->input->post('postLanguage');
$lang = $lang !== NULL ? $lang : 'de';
if (strlen($url) == 0) {
echo json_encode(['success' => false, 'message' => 'Es wurde keine Post-URL angegeben.']);
exit;
}
if (strlen($url) < 4) {
echo json_encode(['success' => false, 'message' => 'Die angegebene Post-URL ist zu kurz. Sie muss mindestens 4 Zeichen umfassen, um eine eindeutige Zuordnung zu ermöglichen.']);
exit;
}
if ($postID == -1) { // Create new blog post
if ($postID == -1) {
$postID = $this->BlogModel->createNewPostDraft($_SESSION['user']['ID']);
if ($this->BlogModel->postUrlExisting($url)) {
echo json_encode(['success' => false, 'message' => 'Die angegebene Post-URL bereits vorhanden.']);
exit;
}
}
}
if ($versionID < 0) {
$versionID = $this->BlogModel->createNewTranslationDraft($postID, $_SESSION['user']['ID'], $lang);
}
$this->BlogModel->updatePostDraft($postID, $initialRelease, $image);
$this->BlogModel->updateTranslationDraft($versionID, $url, $title, $description, $content, $lang);
if(!empty($categories)) {
$this->BlogModel->deleteAllPostCategories($postID);
foreach ($categories as $category) {
if($category == 'new-category') {
$name = strtolower($this->input->post('newCategoryName'));
$displayname = $this->input->post('newCategoryDisplayName');
$category = $this->BlogModel->createCategory($name, $displayname, 'de');
$newCategoryID = $category;
}
$this->BlogModel->addPostCategoryByID($postID, $category);
}
}
if (!empty($tags)) {
$this->BlogModel->deleteAllPostTags($postID);
foreach ($tags as $postTag) {
$tagID = $this->BlogModel->createTagIfNotExists($postTag);
$this->BlogModel->addPostTagByID($postID, $tagID);
}
}
$result = ['success' => true, 'message' => 'Der Entwurf wurde erfolgreich gespeichert.', 'postID' => $postID, 'versionID' => $versionID];
if(isset($newCategoryID))
$result['newCategoryID'] = $newCategoryID;
echo json_encode($result);
}
public function publishPost()
{
header('Content-Type: application/json');
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo json_encode(['success' => false, 'message' => 'Du musst eingeloggt sein, um Blog-Posts zu verfassen']);
exit;
}
$postID = $this->input->post('postID');
$postID = is_numeric($postID) && is_int(intval($postID)) ? intval($postID) : -2;
if ($postID < 0) {
echo json_encode(['success' => false, 'message' => 'Ungültige Post-ID angegeben. Bitte versuche es später erneut']);
exit;
}
$versionIDs = $this->input->post('versionIDs');
$contentPublished = FALSE;
foreach ($versionIDs as $lang => $versionID) {
$versionID = is_numeric($versionID) && is_int(intval($versionID)) ? intval($versionID) : -2;
if ($versionID < 0) {
continue;
}
$this->BlogModel->publishTranslationDraft($postID, $versionID, $_SESSION['user']['ID'], $lang);
$contentPublished = TRUE;
}
if (!$contentPublished) {
echo json_encode(['success' => false, 'message' => 'Ungültige Content-ID angegeben. Bitte versuche es später erneut']);
exit;
}
$this->BlogModel->publishPostDraft($postID);
echo json_encode(['success' => true, 'message' => 'Der Post wurde erfolgreich veröffentlicht.']);
}
public function getTranslations()
{
header('Content-Type: application/json');
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo json_encode(['status' => 'error', 'message' => 'Du musst eingeloggt sein, um Blog-Posts zu verfassen']);
exit;
}
$postID = $this->input->post('postID');
$postID = is_numeric($postID) && is_int(intval($postID)) ? intval($postID) : -2;
if ($postID < 0) {
echo json_encode(['status' => 'error', 'message' => 'Ungültige Post-ID angegeben. Bitte versuche es später erneut']);
exit;
}
$translations = $this->BlogModel->getPostTranslations($postID);
echo json_encode(['status' => 'success', 'translations' => $translations]);
}
public function getPost()
{
header('Content-Type: application/json');
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo json_encode(['status' => 'error', 'message' => 'Du musst eingeloggt sein, um Blog-Posts zu verfassen.']);
exit;
}
$postID = intval($this->input->post('postID'));
if (!is_numeric($postID)) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde eine ungültige Post-ID angegeben.']);
exit;
}
$postData = $this->BlogModel->getPostDataByID($postID);
if (empty($postData)) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde kein Post mit der angegebenen Post-ID gefunden.']);
exit;
}
echo json_encode(['status' => 'success', 'postData' => $postData[0]]);
}
public function getVersion() {
header('Content-Type: application/json');
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo json_encode(['success' => false, 'message' => 'Du musst eingeloggt sein, um Blog-Posts zu verfassen.']);
exit;
}
$postID = intval($this->input->post('postID'));
$versionID = intval($this->input->post('versionID'));
$language = $this->input->post('lang');
if ($postID == 0) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde eine ungültige Post-ID angegeben.']);
exit;
}
if ($versionID == 0) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde keine Version mit der angegebenen Versions-ID gefunden.']);
exit;
}
$versionData = $this->BlogModel->getPostTranslationByID($postID, $versionID, $language);
if (empty($versionData)) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde keine Version mit der angegebenen Versions-ID gefunden.']);
exit;
}
echo json_encode(array_merge(['status' => 'success'], $versionData[0]));
}
public function getPostTags()
{
header('Content-Type: application/json');
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo json_encode(['success' => false, 'message' => 'Du musst eingeloggt sein, um Blog-Posts zu verfassen.']);
exit;
}
$postID = intval($this->input->post('postID'));
if (!is_numeric($postID)) {
echo json_encode(['success' => false, 'message' => 'Es wurde eine ungültige Post-ID angegeben.']);
exit;
}
$postTags = $this->BlogModel->getTags($postID);
echo json_encode(['success' => true, 'tags' => $postTags]);
}
public function edit($postID = -1, $lang = "de")
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
$versions = ['de' => -1];
if (!$postID != -1) {
if ($this->BlogModel->postIDExisting($postID)) {
$postVersions = $this->BlogModel->getPostVersionIDs($postID);
foreach ($postVersions as $postVersion) {
$versions[$postVersion['lang']] = $postVersion['ID'];
}
}
}
$categories = $this->BlogModel->getCategories();
$this->load->view('admin/sidebar', ['title' => 'Blog-Post erstellen', 'additionalStyles' => ['lib/bootstrap-tagsinput.css', 'lib/bootstrap-tagsinput-typeahead.css']]);
$this->load->view('admin/blog_edit', ['categories' => $categories, 'postID' => $postID, 'versions' => $versions, 'lang' => $lang]);
$this->load->view('admin/footer', ['additionalScripts' => ['lib/typeahead.bundle.min.js', 'lib/bootstrap-tagsinput.min.js', 'lib/highlight.pack.js', 'lib/quill.min.js', 'blog-edit.js']]);
}
public function history($postID = NULL)
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
if ($postID === NULL) redirect(base_url('admin/blog'));
$content['de'] = $this->BlogModel->getAllPostVersions($postID, 'de');
$content['en'] = $this->BlogModel->getAllPostVersions($postID, 'en');
$content['fr'] = $this->BlogModel->getAllPostVersions($postID, 'fr');
$this->load->view('admin/sidebar', ['title' => 'Änderungen']);
$this->load->view('admin/blog_history', ['content' => $content]);
$this->load->view('admin/footer', ['additionalScripts' => ['blog-history.js']]);
}
public function history_compare($postID = NULL, $version1 = NULL, $version2 = NULL) {
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
if ($postID === NULL) redirect(base_url('admin/blog'));
if ($version1 === NULL || $version2 === NULL) redirect(base_url('admin/blog/' . $postID));
$content[] = $this->BlogModel->getPostTranslationByHashID($version1);
$content[] = $this->BlogModel->getPostTranslationByHashID($version2);
$differ = new Differ;
$diff['content'] = $differ->diff(
htmlspecialchars(hl_tidy($content[0]['content'], 't', 'div')),
htmlspecialchars(hl_tidy($content[1]['content'], 't', 'div'))
);
// var_dump($diff);
var_dump(htmlspecialchars($content[0]['content']),
htmlspecialchars($content[1]['content']));
$this->load->view('admin/sidebar', ['title' => 'Vergleich']);
$this->load->view('admin/footer', ['additionalScripts' => ['blog-history.js']]);
}
public function new_category()
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] !== 'admin') redirect(base_url('login'));
$name = filter_input(INPUT_POST, "name");
$display_name = filter_input(INPUT_POST, "display_name");
if ($name !== NULL && $display_name !== NULL) {
$category = $this->BlogModel->getCategoryIDAfterInsert($name, $display_name);
echo $category;
}
}
public function delete()
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
$id = filter_input(INPUT_POST, "id");
echo $this->BlogModel->deletePost($id);
}
public function deleteFinally()
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
$id = filter_input(INPUT_POST, "id");
$this->BlogModel->deletePostFinally($id);
}
public function restore()
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
$id = filter_input(INPUT_POST, "id");
echo $this->BlogModel->restorePost($id);
}
public function trashbin()
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
$posts = $this->BlogModel->getPostList(true);
$this->load->view('admin/sidebar', ['title' => 'Alle Blog-Posts']);
$this->load->view('admin/blog_posts', ['posts' => $posts, 'trashbin' => true]);
$this->load->view('admin/footer');
}
public function tagsList()
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) {
echo '{"type":"error", "message":"<b>Fehler beim Upload!</b> Aufgrund von zu geringen Zugriffsrechten konnte das Bild leider nicht hochgeladen werden <i>Sollte es sich dabei um ein Irrtum handeln, kontaktiere bitte einen Admin über das Kontakformular.</i>"}';
header("Content-Type: application/json");
exit;
}
$result = array_map(function ($value) {
return $value['displayname'];
}, $this->BlogModel->getAllTags());
echo json_encode($result);
// echo json_encode($this->BlogModel->getAllTags());
header("Content-Type: application/json");
}
public function updatePreview()
{
header('Content-Type: application/json');
if (!$this->hasPermission('blog.create')) {
echo json_encode(['success' => false, 'message' => 'Du hast nicht genügend Rechte, um die Vorschau anzusehen.']);
exit;
}
if (!isset($_POST['postTitle']) || !isset($_POST['postDesc']) || !isset($_POST['postContent'])) {
exit;
}
if (!isset($_POST['previewID'])) {
$previewID = substr(md5(uniqid() . date(time())), 0, 16);
} else {
$previewID = $_POST['previewID'];
}
$_SESSION['preview_' . $previewID] = [
'title' => $_POST['postTitle'],
'desc' => $_POST['postDesc'],
'content' => $_POST['postContent'],
];
echo json_encode(['success' => true, 'previewID' => $previewID, 'session' => $_SESSION['preview_' . $previewID]]);
}
public function getTemplates()
{
header('Content-Type: application/json');
if (!$this->hasPermission('blog.create')) {
echo json_encode([]);
exit;
}
$templates = [
new Template('Verweis auf anderen Post', 'Verlinkungs-Karte für weiteren Blog-Post', 'post_reference'),
];
foreach ($templates as $template) {
$template->content = $this->load->view('admin/blog/templates/' . $template->content, '', true);
}
echo json_encode($templates);
}
public function preview()
{
$this->neededPermission('blog.create');
$previewID = $_GET['id'];
if (!isset($_SESSION['preview_' . $previewID])) {
redirect('admin/blog');
}
$this->load->view('header', ['active' => 'blog', 'title' => 'Vorschau', 'additionalStyles' => ['posts_list.css', 'blog.css']]);
$this->load->view('blog/first', ['categoryPosts' => [], 'categories' => $this->BlogModel->getCategories()]);
$this->load->view('admin/blog_post_preview', $_SESSION['preview_' . $previewID]);
$this->load->view('footer', ['additionalScripts' => ['lib/prism.js', 'blog.js']]);
}
}
class Template
{
public $title;
public $desc;
public $content;
/**
* Template constructor.
* @param $title
* @param $desc
* @param $content
*/
public function __construct($title, $desc, $content)
{
$this->title = $title;
$this->desc = $desc;
$this->content = $content;
}
}