topic.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. name = flask.request.form.get('topic', 'Test')
  13. sub = flask.request.form.get('title', 'Test')
  14. if do_title_length_check(name) == 1:
  15. return re_error('/error/38')
  16. if do_title_length_check(sub, 'topic') == 1:
  17. return re_error('/error/38')
  18. if topic_num == '0':
  19. curs.execute(db_change("select code from topic order by code + 0 desc limit 1"))
  20. t_data = curs.fetchall()
  21. topic_num = str(int(t_data[0][0]) + 1) if t_data else '1'
  22. if flask.request.form.get('content', 'Test') == '':
  23. return redirect('/thread/' + topic_num)
  24. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  25. return re_error('/error/13')
  26. else:
  27. captcha_post('', 0)
  28. ip = ip_check()
  29. today = get_time()
  30. if topic_acl == 1:
  31. return re_error('/ban')
  32. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [topic_num])
  33. old_num = curs.fetchall()
  34. num = str((int(old_num[0][0]) + 1) if old_num else 1)
  35. match = re.search(r'^user:([^/]+)', name)
  36. if match:
  37. match = match.group(1)
  38. y_check = 0
  39. if ip_or_user(match) == 1:
  40. curs.execute(db_change("select ip from history where ip = ? limit 1"), [match])
  41. u_data = curs.fetchall()
  42. if u_data:
  43. y_check = 1
  44. else:
  45. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [match])
  46. u_data = curs.fetchall()
  47. if u_data:
  48. y_check = 1
  49. else:
  50. curs.execute(db_change("select id from user_set where id = ?"), [match])
  51. u_data = curs.fetchall()
  52. if u_data:
  53. y_check = 1
  54. if y_check == 1:
  55. add_alarm(match, ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  56. curs.execute(db_change("select ip from topic where code = ? and id = '1'"), [topic_num])
  57. ip_data = curs.fetchall()
  58. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  59. add_alarm(ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  60. data = flask.request.form.get('content', 'Test').replace('\r', '')
  61. data = get_thread_pre_render(data, num, ip, topic_num, name, sub)
  62. do_add_thread(
  63. topic_num,
  64. data,
  65. '',
  66. num
  67. )
  68. do_reload_recent_thread(
  69. topic_num,
  70. today,
  71. name,
  72. sub
  73. )
  74. conn.commit()
  75. return redirect('/thread/' + topic_num + '#' + num)
  76. else:
  77. thread_data = ''
  78. thread_data_preview = ''
  79. if topic_num == '0':
  80. name = load_lang('make_new_topic')
  81. sub = load_lang('make_new_topic')
  82. if do_type == 'preview':
  83. name_value = flask.request.form.get('topic', '')
  84. sub_value = flask.request.form.get('title', '')
  85. else:
  86. name_value = doc_name
  87. sub_value = ''
  88. else:
  89. curs.execute(db_change("select title, sub from rd where code = ?"), [topic_num])
  90. name = curs.fetchall()
  91. if name:
  92. sub = name[0][1]
  93. name = name[0][0]
  94. name_value = name
  95. sub_value = sub
  96. else:
  97. return redirect('/')
  98. if do_type == 'preview':
  99. thread_data = flask.request.form.get('content', 'Test')
  100. thread_data = thread_data.replace('\r', '')
  101. thread_data_preview = render_set(
  102. doc_data = thread_data
  103. )
  104. acl_display = 'display: none;' if topic_acl == 1 else ''
  105. name_display = 'display: none;' if topic_num != '0' else ''
  106. curs.execute(db_change('select data from other where name = "topic_text"'))
  107. sql_d = curs.fetchall()
  108. topic_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('content')
  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. ''' + shortcut + '''
  127. <h2 id="topic_top_title">''' + html.escape(sub) + '''</h2>
  128. <div id="top_topic">''' + top_topic + '''</div>
  129. <div id="main_topic">''' + main_topic + '''</div>
  130. <div id="plus_topic"></div>
  131. <a href="/thread/''' + topic_num + '/tool">(' + load_lang('topic_tool') + ''')</a>
  132. <hr class="main_hr">
  133. <form style="''' + acl_display + '''" method="post">
  134. <div style="''' + name_display + '''">
  135. <input placeholder="''' + load_lang('document_name') + '''" name="topic" value="''' + html.escape(name_value) + '''">
  136. <hr class="main_hr">
  137. <input placeholder="''' + load_lang('discussion_name') + '''" name="title" value="''' + html.escape(sub_value) + '''">
  138. <hr class="main_hr">
  139. </div>
  140. <div>''' + edit_button('opennamu_edit_textarea') + '''</div>
  141. <textarea id="opennamu_edit_textarea" class="opennamu_textarea_100" placeholder="''' + topic_text + '''" name="content">''' + html.escape(thread_data) + '''</textarea>
  142. <hr class="main_hr">
  143. ''' + captcha_get() + ip_warning() + '''
  144. <button id="opennamu_save_button" formaction="/thread/''' + topic_num + '''" type="submit">''' + load_lang('send') + '''</button>
  145. <button id="opennamu_preview_button" formaction="/thread_preview/''' + topic_num + '''#opennamu_edit_textarea" type="submit">''' + load_lang('preview') + '''</button>
  146. </form>
  147. <hr class="main_hr">
  148. <div id="opennamu_preview_area">''' + thread_data_preview + '''</div>
  149. <!-- JS : opennamu_do_thread_make -->
  150. ''',
  151. menu = [['topic/' + url_pas(name), load_lang('list')]]
  152. ))