topic.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from .tool.func import *
  2. from .api_topic import api_topic, api_topic_thread_pre_render
  3. from .edit import edit_editor
  4. def topic(topic_num = 0, do_type = '', doc_name = 'Test'):
  5. with get_db_connect() as conn:
  6. curs = conn.cursor()
  7. topic_num = str(topic_num)
  8. topic_acl = acl_check('', 'topic', topic_num)
  9. topic_view_acl = acl_check('', 'topic_view', topic_num)
  10. if topic_view_acl == 1:
  11. return re_error('/ban')
  12. ip = ip_check()
  13. if flask.request.method == 'POST' and do_type == '':
  14. if do_edit_slow_check('thread') == 1:
  15. return re_error('/error/42')
  16. name = flask.request.form.get('topic', 'Test')
  17. sub = flask.request.form.get('title', 'Test')
  18. if do_title_length_check(name) == 1:
  19. return re_error('/error/38')
  20. if do_title_length_check(sub, 'topic') == 1:
  21. return re_error('/error/38')
  22. if topic_num == '0':
  23. curs.execute(db_change("select code from topic order by code + 0 desc limit 1"))
  24. t_data = curs.fetchall()
  25. topic_num = str(int(t_data[0][0]) + 1) if t_data else '1'
  26. if flask.request.form.get('content', 'Test') == '':
  27. return redirect('/thread/' + topic_num)
  28. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  29. return re_error('/error/13')
  30. else:
  31. captcha_post('', 0)
  32. today = get_time()
  33. if topic_acl == 1:
  34. return re_error('/ban')
  35. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [topic_num])
  36. old_num = curs.fetchall()
  37. num = str((int(old_num[0][0]) + 1) if old_num else 1)
  38. match = re.search(r'^user:([^/]+)', name)
  39. if match:
  40. match = match.group(1)
  41. y_check = 0
  42. if ip_or_user(match) == 1:
  43. curs.execute(db_change("select ip from history where ip = ? limit 1"), [match])
  44. u_data = curs.fetchall()
  45. if u_data:
  46. y_check = 1
  47. else:
  48. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [match])
  49. u_data = curs.fetchall()
  50. if u_data:
  51. y_check = 1
  52. else:
  53. curs.execute(db_change("select id from user_set where id = ?"), [match])
  54. u_data = curs.fetchall()
  55. if u_data:
  56. y_check = 1
  57. if y_check == 1:
  58. add_alarm(match, ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  59. curs.execute(db_change("select ip from topic where code = ? and id = '1'"), [topic_num])
  60. ip_data = curs.fetchall()
  61. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  62. add_alarm(ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  63. data = flask.request.form.get('content', 'Test').replace('\r', '')
  64. data = api_topic_thread_pre_render(curs, data, num, ip, topic_num, name, sub)
  65. do_add_thread(
  66. topic_num,
  67. data,
  68. '',
  69. num
  70. )
  71. do_reload_recent_thread(
  72. topic_num,
  73. today,
  74. name,
  75. sub
  76. )
  77. conn.commit()
  78. return redirect('/thread/' + topic_num + '#' + num)
  79. else:
  80. thread_data = ''
  81. if topic_num == '0':
  82. name = load_lang('make_new_topic')
  83. sub = load_lang('make_new_topic')
  84. name_value = doc_name
  85. sub_value = ''
  86. else:
  87. curs.execute(db_change("select title, sub from rd where code = ?"), [topic_num])
  88. name = curs.fetchall()
  89. if name:
  90. sub = name[0][1]
  91. name = name[0][0]
  92. name_value = name
  93. sub_value = sub
  94. else:
  95. return redirect('/')
  96. acl_display = 'display: none;' if topic_acl == 1 else ''
  97. name_display = 'display: none;' if topic_num != '0' else ''
  98. shortcut = '<div class="opennamu_thread_shortcut" id="thread_shortcut">'
  99. curs.execute(db_change("select id from topic where code = ? order by id + 0 asc"), [topic_num])
  100. db_data = curs.fetchall()
  101. for for_a in db_data:
  102. shortcut += '<a href="#' + for_a[0] + '">#' + for_a[0] + '</a> '
  103. shortcut += '</div>'
  104. top_topic = ''
  105. json_data = json.loads(api_topic(int(topic_num), 'top', '', 'render').data)
  106. top_topic += json_data['data'] if 'data' in json_data else ''
  107. main_topic = ''
  108. json_data = json.loads(api_topic(int(topic_num), 'normal', '', 'render').data)
  109. main_topic += json_data['data'] if 'data' in json_data else ''
  110. return easy_minify(flask.render_template(skin_check(),
  111. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('discussion') + ')', 0])],
  112. data = '''
  113. <style id="opennamu_remove_blind">
  114. .opennamu_comment_blind_js {
  115. display: none;
  116. }
  117. </style>
  118. <input type="checkbox" onclick="opennamu_do_remove_blind_thread();" checked> ''' + load_lang('remove_blind_thread') + '''
  119. ''' + shortcut + '''
  120. <h2 id="topic_top_title">''' + html.escape(sub) + '''</h2>
  121. <div id="top_topic">''' + top_topic + '''</div>
  122. <div id="main_topic">''' + main_topic + '''</div>
  123. <div id="plus_topic"></div>
  124. <a href="/thread/''' + topic_num + '/tool">(' + load_lang('topic_tool') + ''')</a>
  125. <hr class="main_hr">
  126. <form style="''' + acl_display + '''" method="post">
  127. <div style="''' + name_display + '''">
  128. <input placeholder="''' + load_lang('document_name') + '''" name="topic" value="''' + html.escape(name_value) + '''">
  129. <hr class="main_hr">
  130. <input placeholder="''' + load_lang('discussion_name') + '''" name="title" value="''' + html.escape(sub_value) + '''">
  131. <hr class="main_hr">
  132. </div>
  133. ''' + edit_editor(curs, ip, thread_data, 'thread') + '''
  134. </form>
  135. ''',
  136. menu = [['topic/' + url_pas(name), load_lang('list')]]
  137. ))