topic.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. rd_plus(topic_num, today, name, sub)
  86. curs.execute(db_change("insert into topic (id, data, date, ip, code) values (?, ?, ?, ?, ?)"), [
  87. num,
  88. data,
  89. today,
  90. ip,
  91. topic_num
  92. ])
  93. conn.commit()
  94. return redirect('/thread/' + topic_num + '#' + num)
  95. else:
  96. display = 'display: none;' if topic_acl == 1 else ''
  97. data_input_topic_name = ''
  98. if topic_num == '0':
  99. data_input_topic_name = '' + \
  100. '<input placeholder="' + load_lang('discussion_name') + '" name="title">' + \
  101. '<hr class="main_hr">' + \
  102. '<input placeholder="' + load_lang('document_name') + '" name="topic">' + \
  103. '<hr class="main_hr">' + \
  104. ''
  105. curs.execute(db_change('select data from other where name = "topic_text"'))
  106. sql_d = curs.fetchall()
  107. topic_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('content')
  108. return easy_minify(flask.render_template(skin_check(),
  109. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('discussion') + ')', 0])],
  110. data = '''
  111. <h2 id="topic_top_title">''' + html.escape(sub) + '''</h2>
  112. <div id="top_topic"></div>
  113. <div id="main_topic"></div>
  114. <div id="plus_topic"></div>
  115. <script>opennamu_do_thread_make("''' + topic_num + '''");</script>
  116. <a href="/thread/''' + topic_num + '/tool">(' + load_lang('topic_tool') + ''')</a>
  117. <hr class="main_hr">
  118. <form style="''' + display + '''" method="post">
  119. ''' + data_input_topic_name + '''
  120. <textarea id="textarea_edit_view" class="opennamu_comment_textarea" placeholder="''' + topic_text + '''" name="content"></textarea>
  121. <hr class="main_hr">
  122. ''' + captcha_get() + (ip_warning() if display == '' else '') + '''
  123. <input style="display: none;" name="topic" value="''' + name + '''">
  124. <input style="display: none;" name="title" value="''' + sub + '''">
  125. <button id="save" type="submit">''' + load_lang('send') + '''</button>
  126. <button id="preview" type="button" onclick="load_preview(\'\')">''' + load_lang('preview') + '''</button>
  127. </form>
  128. <hr class="main_hr">
  129. <div id="see_preview"></div>
  130. ''',
  131. menu = [['topic/' + url_pas(name), load_lang('list')]]
  132. ))