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

120 lines
3.6 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
2022-11-06 02:26:21 +00:00
requireLib('bootstrap5');
requirePHPLib('form');
2022-03-17 04:00:03 +00:00
2022-11-06 02:26:21 +00:00
UOJUserBlog::userCanManage(Auth::user()) || UOJResponse::page403();
2022-03-17 04:00:03 +00:00
2022-11-06 02:26:21 +00:00
if (!Auth::check()) {
redirectToLogin();
}
2022-10-08 06:10:14 +00:00
2022-11-06 02:26:21 +00:00
if (isset($_GET['id'])) {
UOJBlog::init(UOJRequest::get('id')) || UOJResponse::page404();
UOJBlog::cur()->belongsToUserBlog() || UOJResponse::page404();
UOJBlog::info('type') == 'B' || UOJResponse::page404();
$blog = UOJBlog::info();
$blog['content'] = UOJBlog::cur()->queryContent()['content'];
$blog['content_md'] = UOJBlog::cur()->queryContent()['content_md'];
}
$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' => UOJBlog::cur()->queryTags(),
'is_hidden' => $blog['is_hidden']
);
} else {
$blog_editor->cur_data = array(
'title' => $_GET['title'] ?: '新博客',
'content_md' => '',
'content' => '',
'tags' => [],
'is_hidden' => isset($_GET['is_hidden']) ? $_GET['is_hidden'] : true,
);
}
if ($blog) {
$blog_editor->blog_url = HTML::blog_url(UOJUserBlog::id(), "/post/{$blog['id']}");
} else {
$blog_editor->blog_url = null;
}
function updateBlog($id, $data) {
DB::update([
"update blogs",
"set", [
"title" => $data['title'],
"content" => $data['content'],
"content_md" => $data['content_md'],
"is_hidden" => $data['is_hidden']
],
"where", ["id" => $id]
]);
}
function insertBlog($data) {
DB::insert([
"insert into blogs",
"(title, content, content_md, poster, is_hidden, post_time, active_time)",
"values", DB::tuple([
$data['title'], $data['content'], $data['content_md'],
UOJUserBlog::id(), $data['is_hidden'], DB::now(), DB::now()
])
]);
}
$blog_editor->save = function ($data) {
global $blog;
$ret = [];
2022-03-17 08:38:39 +00:00
if ($blog) {
2022-11-06 02:26:21 +00:00
updateBlog($blog['id'], $data);
2016-07-18 16:39:37 +00:00
} else {
2022-11-06 02:26:21 +00:00
insertBlog($data);
$blog_id = DB::insert_id();
2022-11-06 22:30:50 +00:00
UOJBlog::query(strval($blog_id))->setAsCur();
2022-11-06 02:26:21 +00:00
$ret['blog_id'] = $blog_id;
$ret['blog_write_url'] = UOJBlog::cur()->getUriForWrite();
$ret['blog_url'] = UOJBlog::cur()->getBlogUri();
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
UOJBlog::cur()->updateTags($data['tags']);
return $ret;
};
$blog_editor->runAtServer();
?>
2016-07-18 16:39:37 +00:00
<?php echoUOJPageHeader('写博客') ?>
<div class="card">
2022-11-06 02:26:21 +00:00
<div class="card-header bg-transparent d-flex justify-content-between">
<div class="fw-bold">写博客</div>
<div id="div-blog-id" <?php if (!$blog) : ?> style="display: none" <?php endif ?>>
<?php if ($blog) : ?>
<small>博客 ID<b><?= $blog['id'] ?></b></small>
<?php endif ?>
</div>
</div>
<div class="card-body">
<?php $blog_editor->printHTML() ?>
2022-10-16 12:38:03 +00:00
</div>
</div>
<!-- 提示信息 -->
<div class="card mt-3">
2022-11-06 02:26:21 +00:00
<div class="card-body">
<h2 class="h3 card-title">提示</h2>
<ol>
<li>题解发布后还需要返回对应题目的题解页面 <b>手动输入博客 ID</b> 来将本文添加到题目的题解列表中(博客 ID 可以在右上角找到);</li>
<li>请勿引用不稳定的外部资源(如来自个人服务器的图片或文档等),以便备份及后期维护;</li>
<li>请勿在博文中直接插入大段 HTML 代码,这可能会破坏页面的显示,可以考虑使用 <a class="text-decoration-none" href="/html2markdown" target="_blank">转换工具</a> 转换后再作修正;</li>
<li>图片上传推荐使用 <a class="text-decoration-none" href="/image_hosting" target="_blank">S2OJ 图床</a>,以免后续产生外链图片大量失效的情况。</li>
</ol>
<p class="card-text">
帮助:<a class="text-decoration-none" href="http://uoj.ac/blog/7">UOJ 博客使用教程</a>
</p>
</div>
</div>
2016-07-18 16:39:37 +00:00
<?php echoUOJPageFooter() ?>