topic.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. thread_data_preview = ''
  82. if topic_num == '0':
  83. name = load_lang('make_new_topic')
  84. sub = load_lang('make_new_topic')
  85. if do_type == 'preview':
  86. name_value = flask.request.form.get('topic', '')
  87. sub_value = flask.request.form.get('title', '')
  88. else:
  89. name_value = doc_name
  90. sub_value = ''
  91. else:
  92. curs.execute(db_change("select title, sub from rd where code = ?"), [topic_num])
  93. name = curs.fetchall()
  94. if name:
  95. sub = name[0][1]
  96. name = name[0][0]
  97. name_value = name
  98. sub_value = sub
  99. else:
  100. return redirect('/')
  101. if do_type == 'preview':
  102. thread_data = flask.request.form.get('content', 'Test')
  103. thread_data = thread_data.replace('\r', '')
  104. thread_data_preview = render_set(
  105. doc_data = thread_data
  106. )
  107. acl_display = 'display: none;' if topic_acl == 1 else ''
  108. name_display = 'display: none;' if topic_num != '0' else ''
  109. shortcut = '<div class="opennamu_thread_shortcut" id="thread_shortcut">'
  110. curs.execute(db_change(
  111. "select id from topic where code = ? order by id + 0 asc"
  112. ), [topic_num])
  113. db_data = curs.fetchall()
  114. for for_a in db_data:
  115. shortcut += '<a href="#' + for_a[0] + '">#' + for_a[0] + '</a> '
  116. shortcut += '</div>'
  117. top_topic = ''
  118. json_data = json.loads(api_topic(int(topic_num), 'top', '', 'render').data)
  119. top_topic += json_data['data'] if 'data' in json_data else ''
  120. main_topic = ''
  121. json_data = json.loads(api_topic(int(topic_num), 'normal', '', 'render').data)
  122. main_topic += json_data['data'] if 'data' in json_data else ''
  123. return easy_minify(flask.render_template(skin_check(),
  124. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('discussion') + ')', 0])],
  125. data = '''
  126. <style id="opennamu_remove_blind">
  127. .opennamu_comment_blind_js {
  128. display: none;
  129. }
  130. </style>
  131. <input type="checkbox" onclick="opennamu_do_remove_blind_thread();" checked> ''' + load_lang('remove_blind_thread') + '''
  132. ''' + shortcut + '''
  133. <h2 id="topic_top_title">''' + html.escape(sub) + '''</h2>
  134. <div id="top_topic">''' + top_topic + '''</div>
  135. <div id="main_topic">''' + main_topic + '''</div>
  136. <div id="plus_topic"></div>
  137. <a href="/thread/''' + topic_num + '/tool">(' + load_lang('topic_tool') + ''')</a>
  138. <hr class="main_hr">
  139. <form style="''' + acl_display + '''" method="post">
  140. <div style="''' + name_display + '''">
  141. <input placeholder="''' + load_lang('document_name') + '''" name="topic" value="''' + html.escape(name_value) + '''">
  142. <hr class="main_hr">
  143. <input placeholder="''' + load_lang('discussion_name') + '''" name="title" value="''' + html.escape(sub_value) + '''">
  144. <hr class="main_hr">
  145. </div>
  146. ''' + edit_editor(curs, ip, thread_data, 'thread') + '''
  147. <hr class="main_hr">
  148. ''' + captcha_get() + ip_warning() + '''
  149. <button id="opennamu_save_button" formaction="/thread/''' + topic_num + '''" type="submit" onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('send') + '''</button>
  150. <button id="opennamu_preview_button" formaction="/thread_preview/''' + topic_num + '''#opennamu_edit_textarea" type="submit" onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('preview') + '''</button>
  151. </form>
  152. <hr class="main_hr">
  153. <div id="opennamu_preview_area">''' + thread_data_preview + '''</div>
  154. ''',
  155. menu = [['topic/' + url_pas(name), load_lang('list')]]
  156. ))