topic.py 7.0 KB

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