topic.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. cate_re = re.compile(r'\[\[((?:분류|category):(?:(?:(?!\]\]).)*))\]\]', re.I)
  57. data = cate_re.sub('[br]', flask.request.form.get('content', 'Test').replace('\r', ''))
  58. call_thread_regex = r"( |\n|^)(?:#([0-9]+))( |\n|$)"
  59. call_thread_count = len(re.findall(call_thread_regex, data)) * 3
  60. while 1:
  61. rd_data = re.search(call_thread_regex, data)
  62. if call_thread_count < 0:
  63. break
  64. elif not rd_data:
  65. break
  66. else:
  67. rd_data = rd_data.groups()
  68. curs.execute(db_change("select ip from topic where code = ? and id = ?"), [topic_num, rd_data[1]])
  69. ip_data = curs.fetchall()
  70. if ip_data and ip_or_user(ip_data[0][0]) == 0 and ip != ip_data[0][0]:
  71. add_alarm(ip_data[0][0], ip + ' | <a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' | ' + html.escape(sub) + ' | #' + num + '</a>')
  72. data = re.sub(call_thread_regex, rd_data[0] + '<topic_a>#' + rd_data[1] + '</topic_a>' + rd_data[2], data, 1)
  73. call_thread_count -= 1
  74. call_user_regex = r"( |\n|^)(?:@([^ ]+))( |\n|$)"
  75. call_user_count = len(re.findall(call_user_regex, data)) * 3
  76. while 1:
  77. rd_data = re.search(call_user_regex, data)
  78. if call_user_count < 0:
  79. break
  80. elif not rd_data:
  81. break
  82. else:
  83. rd_data = rd_data.groups()
  84. curs.execute(db_change("select ip from history where ip = ? limit 1"), [rd_data[1]])
  85. ip_data = curs.fetchall()
  86. if not ip_data:
  87. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [rd_data[1]])
  88. ip_data = curs.fetchall()
  89. if ip_data and ip_or_user(ip_data[0][0]) == 0 and ip != ip_data[0][0]:
  90. add_alarm(ip_data[0][0], ip + ' | <a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' | ' + html.escape(sub) + ' | #' + num + '</a>')
  91. data = re.sub(call_user_regex, rd_data[0] + '<topic_call>@' + rd_data[1] + '</topic_call>' + rd_data[2], data, 1)
  92. call_user_count -= 1
  93. do_add_thread(
  94. topic_num,
  95. data,
  96. '',
  97. num
  98. )
  99. do_reload_recent_thread(
  100. topic_num,
  101. today,
  102. name,
  103. sub
  104. )
  105. conn.commit()
  106. return redirect('/thread/' + topic_num + '#' + num)
  107. else:
  108. thread_data = ''
  109. thread_data_preview = ''
  110. if topic_num == '0':
  111. name = load_lang('make_new_topic')
  112. sub = load_lang('make_new_topic')
  113. if do_type == 'preview':
  114. name_value = flask.request.form.get('topic', '')
  115. sub_value = flask.request.form.get('title', '')
  116. else:
  117. name_value = doc_name
  118. sub_value = ''
  119. else:
  120. curs.execute(db_change("select title, sub from rd where code = ?"), [topic_num])
  121. name = curs.fetchall()
  122. if name:
  123. sub = name[0][1]
  124. name = name[0][0]
  125. name_value = name
  126. sub_value = sub
  127. else:
  128. return redirect('/')
  129. if do_type == 'preview':
  130. thread_data = flask.request.form.get('content', 'Test')
  131. thread_data = thread_data.replace('\r', '')
  132. thread_data_preview = render_set(
  133. doc_name = '',
  134. doc_data = thread_data,
  135. data_in = ''
  136. )
  137. acl_display = 'display: none;' if topic_acl == 1 else ''
  138. name_display = 'display: none;' if topic_num != '0' else ''
  139. curs.execute(db_change('select data from other where name = "topic_text"'))
  140. sql_d = curs.fetchall()
  141. topic_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('content')
  142. shortcut = '<div class="opennamu_thread_shortcut" id="thread_shortcut">'
  143. curs.execute(db_change(
  144. "select id from topic where code = ? order by id + 0 asc"
  145. ), [topic_num])
  146. db_data = curs.fetchall()
  147. for for_a in db_data:
  148. shortcut += '<a href="#' + for_a[0] + '">#' + for_a[0] + '</a> '
  149. shortcut += '</div>'
  150. top_topic = ''
  151. json_data = json.loads(api_topic(int(topic_num), 'top', '', 'render').data)
  152. top_topic += json_data['data'] if 'data' in json_data else ''
  153. main_topic = ''
  154. json_data = json.loads(api_topic(int(topic_num), 'normal', '', 'render').data)
  155. main_topic += json_data['data'] if 'data' in json_data else ''
  156. return easy_minify(flask.render_template(skin_check(),
  157. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('discussion') + ')', 0])],
  158. data = '''
  159. ''' + shortcut + '''
  160. <h2 id="topic_top_title">''' + html.escape(sub) + '''</h2>
  161. <div id="top_topic">''' + top_topic + '''</div>
  162. <div id="main_topic">''' + main_topic + '''</div>
  163. <div id="plus_topic"></div>
  164. <a href="/thread/''' + topic_num + '/tool">(' + load_lang('topic_tool') + ''')</a>
  165. <hr class="main_hr">
  166. <form style="''' + acl_display + '''" method="post">
  167. <div style="''' + name_display + '''">
  168. <input placeholder="''' + load_lang('document_name') + '''" name="topic" value="''' + html.escape(name_value) + '''">
  169. <hr class="main_hr">
  170. <input placeholder="''' + load_lang('discussion_name') + '''" name="title" value="''' + html.escape(sub_value) + '''">
  171. <hr class="main_hr">
  172. </div>
  173. <div>''' + edit_button('opennamu_edit_textarea') + '''</div>
  174. <textarea id="opennamu_edit_textarea" class="opennamu_textarea_100" placeholder="''' + topic_text + '''" name="content">''' + html.escape(thread_data) + '''</textarea>
  175. <hr class="main_hr">
  176. ''' + captcha_get() + ip_warning() + '''
  177. <button id="opennamu_save_button" formaction="/thread/''' + topic_num + '''" type="submit">''' + load_lang('send') + '''</button>
  178. <button id="opennamu_preview_button" formaction="/thread_preview/''' + topic_num + '''#opennamu_edit_textarea" type="submit">''' + load_lang('preview') + '''</button>
  179. </form>
  180. <hr class="main_hr">
  181. <div id="opennamu_preview_area">''' + thread_data_preview + '''</div>
  182. <!-- JS : opennamu_do_thread_make -->
  183. ''',
  184. menu = [['topic/' + url_pas(name), load_lang('list')]]
  185. ))