S2OJ/uoj/1/app/controllers/subdomain/blog/blog_write.php
TRCYX a8cec5a42f fix(blog): fix incorrect blog links from writing page (#38)
When a blogs was saved, only links of form "/blog/id/write" was
returned, ignoring username. Under a subdomain
("user.blog.site/blog/id/write") this is fine, but it does not work if
a subdomain is not used ("site/blog/id/write"). `HTML::blog_url` should
be called to return the links.

Also, DB::insert_id is called before actually inserting the blogs to get
the new blog's id. This sometimes return a 0, especially when the user
has just deleted a blog. DB::insert_id should be called after the
insert instead.
2019-04-26 00:07:47 +08:00

89 lines
3.0 KiB
PHP

<?php
requirePHPLib('form');
if (!UOJContext::hasBlogPermission()) {
become403Page();
}
if (isset($_GET['id'])) {
if (!validateUInt($_GET['id']) || !($blog = queryBlog($_GET['id'])) || !UOJContext::isHisBlog($blog)) {
become404Page();
}
} else {
$blog = DB::selectFirst("select * from blogs where poster = '".UOJContext::user()['username']."' and type = 'B' and is_draft = true");
}
$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(
'title' => '新博客',
'content_md' => '',
'content' => '',
'tags' => array(),
'is_hidden' => true
);
}
if ($blog && !$blog['is_draft']) {
$blog_editor->blog_url = "/blog/{$blog['id']}";
} 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) {
DB::insert("insert into blogs (title, content, content_md, poster, is_hidden, is_draft, post_time) values ('".DB::escape($data['title'])."', '".DB::escape($data['content'])."', '".DB::escape($data['content_md'])."', '".Auth::id()."', {$data['is_hidden']}, {$data['is_draft']}, now())");
}
$blog_editor->save = function($data) {
global $blog;
$ret = array();
if ($blog) {
if ($blog['is_draft']) {
if ($data['is_hidden']) {
updateBlog($blog['id'], $data);
} else {
deleteBlog($blog['id']);
insertBlog(array_merge($data, array('is_draft' => 0)));
$blog = array('id' => DB::insert_id(), 'tags' => array());
$ret['blog_write_url'] = HTML::blog_url(UOJContext::user()['username'], "/blog/{$blog['id']}/write");
$ret['blog_url'] = HTML::blog_url(UOJContext::user()['username'], "/blog/{$blog['id']}");
}
} else {
updateBlog($blog['id'], $data);
}
} else {
insertBlog(array_merge($data, array('is_draft' => $data['is_hidden'] ? 1 : 0)));
$blog = array('id' => DB::insert_id(), 'tags' => array());
if (!$data['is_hidden']) {
$ret['blog_write_url'] = HTML::blog_url(UOJContext::user()['username'], "/blog/{$blog['id']}/write");
$ret['blog_url'] = HTML::blog_url(UOJContext::user()['username'], "/blog/{$blog['id']}");
}
}
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();
?>
<?php echoUOJPageHeader('写博客') ?>
<div class="text-right">
<a href="http://uoj.ac/blog/7">这玩意儿怎么用?</a>
</div>
<?php $blog_editor->printHTML() ?>
<?php echoUOJPageFooter() ?>