topic.py 8.5 KB

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