topic.py 9.9 KB

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