topic.py 7.3 KB

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