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
Raw Normal View History

2018-10-16 16:28:42 +00:00
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
2018-10-16 16:28:42 +00:00
require_once './vendor/Diff/htmLawed.php';
use SebastianBergmann\Diff\Differ;
2018-10-16 16:28:42 +00:00
class Blog extends MY_Controller
2018-10-16 16:28:42 +00:00
{
public function __construct()
{
parent::__construct();
$this->load->model('BlogModel', '', TRUE);
$this->load->model('FileModel', '', TRUE);
}
2018-10-16 16:28:42 +00:00
public function index()
{
$this->neededPermission('blog.view');
$posts = $this->BlogModel->getPostList(false);
2018-10-16 16:28:42 +00:00
$this->load->view('admin/sidebar', ['title' => 'Alle Blog-Posts']);
$this->load->view('admin/blog_posts', ['posts' => $posts]);
$this->load->view('admin/footer');
}
2018-10-16 16:28:42 +00:00
public function tags()
{
$this->neededPermission('blog.view');
2018-10-16 16:28:42 +00:00
$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']);
2018-10-16 16:28:42 +00:00
}
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;
}
2018-10-16 16:28:42 +00:00
$postID = $this->input->post('postID');
$postID = is_numeric($postID) && is_int(intval($postID)) ? intval($postID) : -2;
2018-10-16 16:28:42 +00:00
if ($postID == -2) {
echo json_encode(['success' => false, 'message' => 'Ungültige Post-ID angegeben. Bitte versuche es später erneut']);
exit;
}
2018-10-16 16:28:42 +00:00
$versionID = $this->input->post('versionID');
$versionID = is_numeric($versionID) && is_int(intval($versionID)) ? intval($versionID) : -2;
2018-10-16 16:28:42 +00:00
$image = $this->input->post('postImage');
$title = $this->input->post('postTitle');
$description = $this->input->post('postDescription');
$content = $this->input->post('postContent');
2018-10-16 16:28:42 +00:00
$initialRelease = $this->input->post('postPublishDate');
$initialRelease = date("Y-m-d H:i:s", strtotime($initialRelease));
2018-10-16 16:28:42 +00:00
$url = $this->input->post('postUrl');
$categories = $this->input->post('postCategories');
$tags = $this->input->post('postTags');
2018-10-16 16:28:42 +00:00
$lang = $this->input->post('postLanguage');
$lang = $lang !== NULL ? $lang : 'de';
2018-10-16 16:28:42 +00:00
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;
}
2018-10-16 16:28:42 +00:00
if ($postID == -1) { // Create new blog post
if ($postID == -1) {
$postID = $this->BlogModel->createNewPostDraft($_SESSION['user']['ID']);
2018-10-16 16:28:42 +00:00
if ($this->BlogModel->postUrlExisting($url)) {
echo json_encode(['success' => false, 'message' => 'Die angegebene Post-URL bereits vorhanden.']);
exit;
}
2018-10-16 16:28:42 +00:00
}
}
if ($versionID < 0) {
$versionID = $this->BlogModel->createNewTranslationDraft($postID, $_SESSION['user']['ID'], $lang);
}
2018-10-16 16:28:42 +00:00
$this->BlogModel->updatePostDraft($postID, $initialRelease, $image);
$this->BlogModel->updateTranslationDraft($versionID, $url, $title, $description, $content, $lang);
2018-10-16 16:28:42 +00:00
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);
}
2018-10-16 16:28:42 +00:00
}
if (!empty($tags)) {
$this->BlogModel->deleteAllPostTags($postID);
foreach ($tags as $postTag) {
$tagID = $this->BlogModel->createTagIfNotExists($postTag);
$this->BlogModel->addPostTagByID($postID, $tagID);
}
}
2018-10-16 16:28:42 +00:00
$result = ['success' => true, 'message' => 'Der Entwurf wurde erfolgreich gespeichert.', 'postID' => $postID, 'versionID' => $versionID];
if(isset($newCategoryID))
$result['newCategoryID'] = $newCategoryID;
echo json_encode($result);
2018-10-16 16:28:42 +00:00
}
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;
}
2018-10-16 16:28:42 +00:00
$postID = $this->input->post('postID');
$postID = is_numeric($postID) && is_int(intval($postID)) ? intval($postID) : -2;
2018-10-16 16:28:42 +00:00
if ($postID < 0) {
echo json_encode(['success' => false, 'message' => 'Ungültige Post-ID angegeben. Bitte versuche es später erneut']);
exit;
2018-10-16 16:28:42 +00:00
}
$versionIDs = $this->input->post('versionIDs');
$contentPublished = FALSE;
foreach ($versionIDs as $lang => $versionID) {
$versionID = is_numeric($versionID) && is_int(intval($versionID)) ? intval($versionID) : -2;
2018-10-16 16:28:42 +00:00
if ($versionID < 0) {
continue;
}
2018-10-16 16:28:42 +00:00
$this->BlogModel->publishTranslationDraft($postID, $versionID, $_SESSION['user']['ID'], $lang);
$contentPublished = TRUE;
}
2018-10-16 16:28:42 +00:00
if (!$contentPublished) {
echo json_encode(['success' => false, 'message' => 'Ungültige Content-ID angegeben. Bitte versuche es später erneut']);
exit;
}
2018-10-16 16:28:42 +00:00
$this->BlogModel->publishPostDraft($postID);
2018-10-16 16:28:42 +00:00
echo json_encode(['success' => true, 'message' => 'Der Post wurde erfolgreich veröffentlicht.']);
2018-10-16 16:28:42 +00:00
}
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;
}
2018-10-16 16:28:42 +00:00
$postID = $this->input->post('postID');
$postID = is_numeric($postID) && is_int(intval($postID)) ? intval($postID) : -2;
2018-10-16 16:28:42 +00:00
if ($postID < 0) {
echo json_encode(['status' => 'error', 'message' => 'Ungültige Post-ID angegeben. Bitte versuche es später erneut']);
exit;
}
2018-10-16 16:28:42 +00:00
$translations = $this->BlogModel->getPostTranslations($postID);
echo json_encode(['status' => 'success', 'translations' => $translations]);
2018-10-16 16:28:42 +00:00
}
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;
}
2018-10-16 16:28:42 +00:00
$postID = intval($this->input->post('postID'));
2018-10-16 16:28:42 +00:00
if (!is_numeric($postID)) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde eine ungültige Post-ID angegeben.']);
exit;
}
2018-10-16 16:28:42 +00:00
$postData = $this->BlogModel->getPostDataByID($postID);
2018-10-16 16:28:42 +00:00
if (empty($postData)) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde kein Post mit der angegebenen Post-ID gefunden.']);
exit;
}
2018-10-16 16:28:42 +00:00
echo json_encode(['status' => 'success', 'postData' => $postData[0]]);
2018-10-16 16:28:42 +00:00
}
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;
}
2018-10-16 16:28:42 +00:00
$postID = intval($this->input->post('postID'));
$versionID = intval($this->input->post('versionID'));
$language = $this->input->post('lang');
2018-10-16 16:28:42 +00:00
if ($postID == 0) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde eine ungültige Post-ID angegeben.']);
exit;
}
2018-10-16 16:28:42 +00:00
if ($versionID == 0) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde keine Version mit der angegebenen Versions-ID gefunden.']);
exit;
}
2018-10-16 16:28:42 +00:00
$versionData = $this->BlogModel->getPostTranslationByID($postID, $versionID, $language);
2018-10-16 16:28:42 +00:00
if (empty($versionData)) {
echo json_encode(['status' => 'error', 'message' => 'Es wurde keine Version mit der angegebenen Versions-ID gefunden.']);
exit;
}
2018-10-16 16:28:42 +00:00
echo json_encode(array_merge(['status' => 'success'], $versionData[0]));
2018-10-16 16:28:42 +00:00
}
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;
}
2018-10-16 16:28:42 +00:00
$postID = intval($this->input->post('postID'));
2018-10-16 16:28:42 +00:00
if (!is_numeric($postID)) {
echo json_encode(['success' => false, 'message' => 'Es wurde eine ungültige Post-ID angegeben.']);
exit;
}
2018-10-16 16:28:42 +00:00
$postTags = $this->BlogModel->getTags($postID);
2018-10-16 16:28:42 +00:00
echo json_encode(['success' => true, 'tags' => $postTags]);
2018-10-16 16:28:42 +00:00
}
public function edit($postID = -1, $lang = "de")
{
if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 6) redirect(base_url('login'));
2018-10-16 16:28:42 +00:00
$versions = ['de' => -1];
2018-10-16 16:28:42 +00:00
if (!$postID != -1) {
if ($this->BlogModel->postIDExisting($postID)) {
$postVersions = $this->BlogModel->getPostVersionIDs($postID);
foreach ($postVersions as $postVersion) {
$versions[$postVersion['lang']] = $postVersion['ID'];
}
}
}
2018-10-16 16:28:42 +00:00
$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']]);
2018-10-16 16:28:42 +00:00
}
2018-10-16 16:28:42 +00:00
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'));
2018-10-16 16:28:42 +00:00
$content['de'] = $this->BlogModel->getAllPostVersions($postID, 'de');
$content['en'] = $this->BlogModel->getAllPostVersions($postID, 'en');
$content['fr'] = $this->BlogModel->getAllPostVersions($postID, 'fr');
2018-10-16 16:28:42 +00:00
$this->load->view('admin/sidebar', ['title' => 'Änderungen']);
$this->load->view('admin/blog_history', ['content' => $content]);
$this->load->view('admin/footer', ['additionalScripts' => ['blog-history.js']]);
2018-10-16 16:28:42 +00:00
}
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));
2018-12-30 17:35:04 +00:00
$content[] = $this->BlogModel->getPostTranslationByHashID($version1);
$content[] = $this->BlogModel->getPostTranslationByHashID($version2);
2018-10-16 16:28:42 +00:00
$differ = new Differ;
$diff['content'] = $differ->diff(
htmlspecialchars(hl_tidy($content[0]['content'], 't', 'div')),
htmlspecialchars(hl_tidy($content[1]['content'], 't', 'div'))
);
2018-10-16 16:28:42 +00:00
// var_dump($diff);
var_dump(htmlspecialchars($content[0]['content']),
htmlspecialchars($content[1]['content']));
2018-10-16 16:28:42 +00:00
$this->load->view('admin/sidebar', ['title' => 'Vergleich']);
$this->load->view('admin/footer', ['additionalScripts' => ['blog-history.js']]);
2018-10-16 16:28:42 +00:00
}
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;
}
}
2018-10-16 16:28:42 +00:00
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);
}
2018-10-16 16:28:42 +00:00
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);
2018-10-16 16:28:42 +00:00
}
2018-12-30 17:35:04 +00:00
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);
2018-12-30 17:35:04 +00:00
}
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');
2018-12-30 17:35:04 +00:00
}
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");
2018-12-30 17:35:04 +00:00
}
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;
}
2018-12-30 17:35:04 +00:00
if (!isset($_POST['postTitle']) || !isset($_POST['postDesc']) || !isset($_POST['postContent'])) {
exit;
}
2018-12-30 17:35:04 +00:00
if (!isset($_POST['previewID'])) {
$previewID = substr(md5(uniqid() . date(time())), 0, 16);
} else {
$previewID = $_POST['previewID'];
}
2018-12-30 17:35:04 +00:00
$_SESSION['preview_' . $previewID] = [
'title' => $_POST['postTitle'],
'desc' => $_POST['postDesc'],
'content' => $_POST['postContent'],
];
2018-12-30 17:35:04 +00:00
echo json_encode(['success' => true, 'previewID' => $previewID, 'session' => $_SESSION['preview_' . $previewID]]);
2018-12-30 17:35:04 +00:00
}
public function getTemplates()
{
header('Content-Type: application/json');
2018-12-30 17:35:04 +00:00
if (!$this->hasPermission('blog.create')) {
echo json_encode([]);
exit;
}
2018-12-30 17:35:04 +00:00
$templates = [
new Template('Verweis auf anderen Post', 'Verlinkungs-Karte für weiteren Blog-Post', 'post_reference'),
];
2018-12-30 17:35:04 +00:00
foreach ($templates as $template) {
$template->content = $this->load->view('admin/blog/templates/' . $template->content, '', true);
}
2018-12-30 17:35:04 +00:00
echo json_encode($templates);
2018-12-30 17:35:04 +00:00
}
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']]);
}
2018-12-30 17:35:04 +00:00
}
class Template
2018-12-30 17:35:04 +00:00
{
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;
}
2018-12-30 17:35:04 +00:00
}