topic.py 9.4 KB

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