S2OJ/web/app/controllers/subdomain/blog/blog_write.php

89 lines
2.7 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
requirePHPLib('form');
2022-03-17 04:00:03 +00:00
if (!Auth::check()) {
redirectToLogin();
2022-03-17 04:00:03 +00:00
}
2016-07-18 16:39:37 +00:00
if (!UOJContext::hasBlogPermission()) {
become403Page();
}
if (isset($_GET['id'])) {
if (!validateUInt($_GET['id']) || !($blog = queryBlog($_GET['id'])) || !UOJContext::isHisBlog($blog)) {
become404Page();
}
}
2022-10-07 05:17:33 +00:00
if (!isset($_COOKIE['bootstrap4'])) {
$REQUIRE_LIB['bootstrap5'] = '';
}
2016-07-18 16:39:37 +00:00
$blog_editor = new UOJBlogEditor();
$blog_editor->name = 'blog';
if ($blog) {
$blog_editor->cur_data = array(
'title' => $blog['title'],
'content_md' => $blog['content_md'],
'content' => $blog['content'],
'tags' => queryBlogTags($blog['id']),
'is_hidden' => $blog['is_hidden']
);
} else {
$blog_editor->cur_data = array(
2022-09-28 12:38:16 +00:00
'title' => $_GET['title'] ?: '新博客',
2016-07-18 16:39:37 +00:00
'content_md' => '',
'content' => '',
'tags' => array(),
2022-09-28 12:38:16 +00:00
'is_hidden' => isset($_GET['is_hidden']) ? $_GET['is_hidden'] : true,
2016-07-18 16:39:37 +00:00
);
}
2022-03-17 08:38:39 +00:00
if ($blog) {
$blog_editor->blog_url = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}");
2016-07-18 16:39:37 +00:00
} else {
$blog_editor->blog_url = null;
}
function updateBlog($id, $data) {
DB::update("update blogs set title = '".DB::escape($data['title'])."', content = '".DB::escape($data['content'])."', content_md = '".DB::escape($data['content_md'])."', is_hidden = {$data['is_hidden']} where id = {$id}");
}
function insertBlog($data) {
2022-03-17 08:38:39 +00:00
DB::insert("insert into blogs (title, content, content_md, poster, is_hidden, post_time) values ('".DB::escape($data['title'])."', '".DB::escape($data['content'])."', '".DB::escape($data['content_md'])."', '".Auth::id()."', {$data['is_hidden']}, now())");
2016-07-18 16:39:37 +00:00
}
$blog_editor->save = function($data) {
global $blog;
$ret = array();
if ($blog) {
2022-03-17 08:38:39 +00:00
updateBlog($blog['id'], $data);
2016-07-18 16:39:37 +00:00
} else {
2022-03-17 08:38:39 +00:00
insertBlog($data);
2016-07-18 16:39:37 +00:00
$blog = array('id' => DB::insert_id(), 'tags' => array());
2022-03-17 08:38:39 +00:00
$ret['blog_write_url'] = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}/write");
2022-03-20 09:19:07 +00:00
$ret['blog_url'] = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}");
2016-07-18 16:39:37 +00:00
}
if ($data['tags'] !== $blog['tags']) {
DB::delete("delete from blogs_tags where blog_id = {$blog['id']}");
foreach ($data['tags'] as $tag) {
DB::insert("insert into blogs_tags (blog_id, tag) values ({$blog['id']}, '".DB::escape($tag)."')");
}
}
return $ret;
};
$blog_editor->runAtServer();
2022-09-18 04:58:35 +00:00
?>
2016-07-18 16:39:37 +00:00
<?php echoUOJPageHeader('写博客') ?>
2022-10-07 05:17:33 +00:00
<div class="
<?php if (isset($REQUIRE_LIB['bootstrap5'])): ?>
text-end
<?php else: ?>
text-right
<?php endif ?>">
<a class="
<?php if (isset($REQUIRE_LIB['bootstrap5'])): ?>
text-decoration-none
<?php endif ?>" href="http://uoj.ac/blog/7">这玩意儿怎么用?</a>
2016-07-18 16:39:37 +00:00
</div>
<?php $blog_editor->printHTML() ?>
<?php echoUOJPageFooter() ?>