blog_write.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. requirePHPLib('form');
  3. if (!UOJContext::hasBlogPermission()) {
  4. become403Page();
  5. }
  6. if (isset($_GET['id'])) {
  7. if (!validateUInt($_GET['id']) || !($blog = queryBlog($_GET['id'])) || !UOJContext::isHisBlog($blog)) {
  8. become404Page();
  9. }
  10. } else {
  11. $blog = DB::selectFirst("select * from blogs where poster = '".UOJContext::user()['username']."' and type = 'B' and is_draft = true");
  12. }
  13. $blog_editor = new UOJBlogEditor();
  14. $blog_editor->name = 'blog';
  15. if ($blog) {
  16. $blog_editor->cur_data = array(
  17. 'title' => $blog['title'],
  18. 'content_md' => $blog['content_md'],
  19. 'content' => $blog['content'],
  20. 'tags' => queryBlogTags($blog['id']),
  21. 'is_hidden' => $blog['is_hidden']
  22. );
  23. } else {
  24. $blog_editor->cur_data = array(
  25. 'title' => '新博客',
  26. 'content_md' => '',
  27. 'content' => '',
  28. 'tags' => array(),
  29. 'is_hidden' => true
  30. );
  31. }
  32. if ($blog && !$blog['is_draft']) {
  33. $blog_editor->blog_url = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}");
  34. } else {
  35. $blog_editor->blog_url = null;
  36. }
  37. function updateBlog($id, $data) {
  38. 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}");
  39. }
  40. function insertBlog($data) {
  41. 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())");
  42. }
  43. $blog_editor->save = function($data) {
  44. global $blog;
  45. $ret = array();
  46. if ($blog) {
  47. if ($blog['is_draft']) {
  48. if ($data['is_hidden']) {
  49. updateBlog($blog['id'], $data);
  50. } else {
  51. deleteBlog($blog['id']);
  52. insertBlog(array_merge($data, array('is_draft' => 0)));
  53. $blog = array('id' => DB::insert_id(), 'tags' => array());
  54. $ret['blog_write_url'] = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}/write");
  55. $ret['blog_url'] = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}");
  56. }
  57. } else {
  58. updateBlog($blog['id'], $data);
  59. }
  60. } else {
  61. insertBlog(array_merge($data, array('is_draft' => $data['is_hidden'] ? 1 : 0)));
  62. $blog = array('id' => DB::insert_id(), 'tags' => array());
  63. if (!$data['is_hidden']) {
  64. $ret['blog_write_url'] = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}/write");
  65. $ret['blog_url'] = HTML::blog_url(UOJContext::user()['username'], "/post/{$blog['id']}");
  66. }
  67. }
  68. if ($data['tags'] !== $blog['tags']) {
  69. DB::delete("delete from blogs_tags where blog_id = {$blog['id']}");
  70. foreach ($data['tags'] as $tag) {
  71. DB::insert("insert into blogs_tags (blog_id, tag) values ({$blog['id']}, '".DB::escape($tag)."')");
  72. }
  73. }
  74. return $ret;
  75. };
  76. $blog_editor->runAtServer();
  77. ?>
  78. <?php echoUOJPageHeader('写博客') ?>
  79. <div class="text-right">
  80. <a href="http://uoj.ac/blog/7">这玩意儿怎么用?</a>
  81. </div>
  82. <?php $blog_editor->printHTML() ?>
  83. <?php echoUOJPageFooter() ?>