bbs_w_post.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. from .tool.func import *
  2. from .api_bbs_w_post import api_bbs_w_post
  3. from .api_bbs_w_comment import api_bbs_w_comment
  4. from .api_topic import api_topic_thread_make, api_topic_thread_pre_render
  5. from .edit import edit_editor
  6. def bbs_w_post_comment(user_id, sub_code, comment_num, bbs_num_str, post_num_str):
  7. comment_data = ''
  8. comment_select = ''
  9. comment_count = 0
  10. comment_add_count = 0
  11. thread_data = json.loads(api_bbs_w_comment(sub_code).data)
  12. comment_count += len(thread_data)
  13. comment_add_count += comment_count
  14. for temp_dict in thread_data:
  15. color = 'default'
  16. if user_id == temp_dict['comment_user_id']:
  17. color = 'green'
  18. sub_code_check = re.sub(r'^[0-9]+-[0-9]+-', '', sub_code + '-' + temp_dict['code'])
  19. margin_count = sub_code_check.count('-')
  20. date = ''
  21. date += '<a href="javascript:opennamu_change_comment(\'' + sub_code_check + '\');">(' + load_lang('comment') + ')</a> '
  22. date += '<a href="/bbs/tool/' + bbs_num_str + '/' + post_num_str + '/' + sub_code_check + '">(' + load_lang('tool') + ')</a> '
  23. date += temp_dict['comment_date']
  24. comment_data += '<span style="padding-left: 20px;"></span>' * margin_count
  25. comment_data += api_topic_thread_make(
  26. ip_pas(temp_dict['comment_user_id']),
  27. date,
  28. render_set(
  29. doc_data = temp_dict['comment'],
  30. data_in = 'bbs_comment_' + sub_code_check
  31. ),
  32. sub_code_check,
  33. color = color,
  34. add_style = 'width: calc(100% - ' + str(margin_count * 20) + 'px);'
  35. )
  36. comment_default = ''
  37. if comment_num == sub_code_check:
  38. comment_default = 'selected'
  39. comment_select += '<option value="' + sub_code_check + '" ' + comment_default + '>' + sub_code_check + '</option>'
  40. comment_data += '<hr class="main_hr">'
  41. temp_data = bbs_w_post_comment(user_id, sub_code + '-' + temp_dict['code'], comment_num, bbs_num_str, post_num_str)
  42. comment_data += temp_data[0]
  43. comment_select += temp_data[1]
  44. comment_add_count += temp_data[3]
  45. return (comment_data, comment_select, comment_count, comment_add_count)
  46. def bbs_w_post(bbs_num = '', post_num = '', do_type = ''):
  47. with get_db_connect() as conn:
  48. curs = conn.cursor()
  49. curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_name"'), [bbs_num])
  50. db_data_3 = curs.fetchall()
  51. if not db_data_3:
  52. return redirect('/bbs/main')
  53. bbs_name = db_data_3[0][0]
  54. bbs_num_str = str(bbs_num)
  55. post_num_str = str(post_num)
  56. bbs_comment_acl = acl_check(bbs_num_str, 'bbs_comment')
  57. ip = ip_check()
  58. temp_dict = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
  59. if temp_dict == {}:
  60. return redirect('/bbs/main')
  61. curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_type"'), [bbs_num])
  62. db_data_2 = curs.fetchall()
  63. if not db_data_2:
  64. return redirect('/bbs/main')
  65. elif db_data_2[0][0] == 'thread':
  66. if flask.request.method == 'POST' and do_type != 'preview':
  67. if bbs_comment_acl == 1:
  68. return redirect('/bbs/set/' + bbs_num_str)
  69. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  70. return re_error('/error/13')
  71. else:
  72. captcha_post('', 0)
  73. set_id = bbs_num_str + '-' + post_num_str
  74. curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? order by set_code + 0 desc'), [set_id])
  75. db_data_4 = curs.fetchall()
  76. id_data = str(int(db_data_4[0][0]) + 1) if db_data_4 else '1'
  77. data = flask.request.form.get('content', '')
  78. if data == '':
  79. # re_error로 대체 예정
  80. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  81. data = data.replace('\r', '')
  82. data = api_topic_thread_pre_render(curs, data, id_data, ip, set_id, bbs_name, temp_dict['title'], 'post')
  83. date = get_time()
  84. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  85. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  86. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_user_id', ?, ?, ?)"), [id_data, set_id, ip])
  87. add_alarm(temp_dict['user_id'], ip, 'BBS <a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + id_data + '">' + html.escape(bbs_name) + ' - ' + html.escape(temp_dict['title']) + '#' + id_data + '</a>')
  88. conn.commit()
  89. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + id_data)
  90. else:
  91. if acl_check(bbs_num_str, 'bbs_view') == 1:
  92. return re_error('/ban')
  93. text = ''
  94. data_preview = ''
  95. if do_type == 'preview':
  96. text = flask.request.form.get('content', '')
  97. text = text.replace('\r', '')
  98. data_preview = render_set(
  99. doc_data = text,
  100. data_type = 'thread',
  101. data_in = 'bbs_comment_preview'
  102. )
  103. date = ''
  104. date += temp_dict['date']
  105. data = ''
  106. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  107. data += api_topic_thread_make(
  108. ip_pas(temp_dict['user_id']),
  109. date,
  110. render_set(
  111. doc_data = temp_dict['data'],
  112. data_type = 'thread',
  113. data_in = 'bbs'
  114. ),
  115. '0',
  116. color = 'green'
  117. )
  118. data += '<hr class="main_hr">'
  119. user_id = temp_dict['user_id']
  120. count = 0
  121. thread_data = json.loads(api_bbs_w_comment(bbs_num_str + '-' + post_num_str).data)
  122. for temp_dict in thread_data:
  123. count += 1
  124. if user_id == temp_dict['comment_user_id']:
  125. color = 'green'
  126. else:
  127. color = 'default'
  128. date = ''
  129. date += '<a href="/bbs/tool/' + bbs_num_str + '/' + post_num_str + '/' + str(count) + '">(' + load_lang('tool') + ')</a> '
  130. date += temp_dict['comment_date']
  131. data += api_topic_thread_make(
  132. ip_pas(temp_dict['comment_user_id']),
  133. date,
  134. render_set(
  135. doc_data = temp_dict['comment'],
  136. data_type = 'thread',
  137. data_in = 'bbs_comment_' + str(count)
  138. ),
  139. str(count),
  140. color = color
  141. )
  142. data += '<hr class="main_hr">'
  143. bbs_comment_form = ''
  144. if bbs_comment_acl == 0:
  145. bbs_comment_form += '''
  146. ''' + edit_editor(curs, ip, text, 'bbs_comment') + '''
  147. <hr class="main_hr">
  148. ''' + captcha_get() + ip_warning() + '''
  149. <button id="opennamu_save_button" formaction="/bbs/w/''' + bbs_num_str + '''/''' + post_num_str + '''" type="submit" onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('send') + '''</button>
  150. <button id="opennamu_preview_button" formaction="/bbs/w/preview/''' + bbs_num_str + '''/''' + post_num_str + '''#opennamu_edit_textarea" type="submit" onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('preview') + '''</button>
  151. <hr class="main_hr">
  152. '''
  153. data += '''
  154. <form method="post">
  155. ''' + bbs_comment_form + '''
  156. ''' + data_preview + '''
  157. </form>
  158. '''
  159. return easy_minify(flask.render_template(skin_check(),
  160. imp = [bbs_name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('bbs') + ')', 0])],
  161. data = data,
  162. menu = [['bbs/w/' + bbs_num_str, load_lang('return')], ['bbs/edit/' + bbs_num_str + '/' + post_num_str, load_lang('edit')], ['bbs/tool/' + bbs_num_str + '/' + post_num_str, load_lang('tool')]]
  163. ))
  164. else:
  165. # db_data_2[0][0] == 'comment'
  166. if flask.request.method == 'POST' and do_type != 'preview':
  167. if bbs_comment_acl == 1:
  168. return redirect('/bbs/set/' + bbs_num_str)
  169. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  170. return re_error('/error/13')
  171. else:
  172. captcha_post('', 0)
  173. select = flask.request.form.get('comment_select', '0')
  174. select = '' if select == '0' else select
  175. comment_user_name = ''
  176. if select != '':
  177. select_split = select.split('-')
  178. if len(select_split) < 2:
  179. curs.execute(db_change('select set_data from bbs_data where set_name = "comment_user_id" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str, select_split[0]])
  180. db_data_6 = curs.fetchall()
  181. if not db_data_6:
  182. # re_error로 변경 예정
  183. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  184. else:
  185. set_id = bbs_num_str + '-' + post_num_str + '-' + select_split[0]
  186. comment_user_name = db_data_6[0][0]
  187. else:
  188. curs.execute(db_change('select set_data from bbs_data where set_name = "comment_user_id" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split[0:len(select_split) - 1]), select_split[len(select_split) - 1]])
  189. db_data_7 = curs.fetchall()
  190. if not db_data_7:
  191. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  192. else:
  193. set_id = bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split)
  194. comment_user_name = db_data_7[0][0]
  195. else:
  196. set_id = bbs_num_str + '-' + post_num_str
  197. curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? order by set_code + 0 desc limit 1'), [set_id])
  198. db_data_5 = curs.fetchall()
  199. id_data = str(int(db_data_5[0][0]) + 1) if db_data_5 else '1'
  200. data = flask.request.form.get('content', '')
  201. if data == '':
  202. # re_error로 대체 예정
  203. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  204. date = get_time()
  205. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  206. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  207. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_user_id', ?, ?, ?)"), [id_data, set_id, ip])
  208. conn.commit()
  209. if set_id == '':
  210. end_id = id_data
  211. else:
  212. set_id = re.sub(r'^[0-9]+-[0-9]+-?', '', set_id)
  213. set_id += '-' if set_id != '' else ''
  214. end_id = set_id + id_data
  215. add_alarm(temp_dict['user_id'], ip, 'BBS <a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id + '">' + html.escape(bbs_name) + ' - ' + html.escape(temp_dict['title']) + '#' + end_id + '</a>')
  216. if comment_user_name != '':
  217. add_alarm(comment_user_name, ip, 'BBS <a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id + '">' + html.escape(bbs_name) + ' - ' + html.escape(temp_dict['title']) + '#' + end_id + '</a>')
  218. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id)
  219. else:
  220. if acl_check(bbs_num_str, 'bbs_view') == 1:
  221. return re_error('/ban')
  222. text = ''
  223. comment_num = ''
  224. data_preview = ''
  225. if do_type == 'preview':
  226. text = flask.request.form.get('content', '')
  227. text = text.replace('\r', '')
  228. comment_num = flask.request.form.get('comment_select', '')
  229. data_preview = render_set(
  230. doc_data = text,
  231. data_in = 'bbs_comment_preview'
  232. )
  233. date = ''
  234. date += '<a href="javascript:opennamu_change_comment(\'0\');">(' + load_lang('comment') + ')</a> '
  235. date += temp_dict['date']
  236. data = ''
  237. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  238. data += api_topic_thread_make(
  239. ip_pas(temp_dict['user_id']),
  240. date,
  241. render_set(
  242. doc_data = temp_dict['data'],
  243. data_in = 'bbs'
  244. ),
  245. '0',
  246. color = 'red'
  247. )
  248. user_id = temp_dict['user_id']
  249. comment_data = ''
  250. comment_select = '<select id="opennamu_comment_select" name="comment_select">'
  251. comment_select += '<option value="0">' + load_lang('normal') + '</option>'
  252. comment_count = 0
  253. comment_add_count = 0
  254. temp_data = bbs_w_post_comment(user_id, bbs_num_str + '-' + post_num_str, comment_num, bbs_num_str, post_num_str)
  255. comment_data += temp_data[0]
  256. comment_select += temp_data[1]
  257. comment_count += temp_data[2]
  258. comment_add_count += temp_data[3]
  259. comment_add_count -= comment_count
  260. if comment_data != '':
  261. data += '<hr class="main_hr"><hr>'
  262. comment_select += '</select>'
  263. if comment_data != '':
  264. data += load_lang('comment') + ' : ' + str(comment_count) + '<hr class="main_hr">'
  265. data += load_lang('reply') + ' : ' + str(comment_add_count) + '<hr class="main_hr">'
  266. data += comment_data
  267. else:
  268. data += '<hr class="main_hr">'
  269. bbs_comment_form = ''
  270. if bbs_comment_acl == 0:
  271. bbs_comment_form += '''
  272. ''' + comment_select + ''' <a href="javascript:opennamu_return_comment();">(''' + load_lang('return') + ''')</a>
  273. <hr class="main_hr">
  274. ''' + edit_editor(curs, ip, text, 'bbs_comment') + '''
  275. <hr class="main_hr">
  276. ''' + captcha_get() + ip_warning() + '''
  277. <button id="opennamu_save_button" formaction="/bbs/w/''' + bbs_num_str + '''/''' + post_num_str + '''" type="submit" onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('send') + '''</button>
  278. <button id="opennamu_preview_button" formaction="/bbs/w/preview/''' + bbs_num_str + '''/''' + post_num_str + '''#opennamu_edit_textarea" type="submit" onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('preview') + '''</button>
  279. <hr class="main_hr">
  280. '''
  281. data += '''
  282. <form method="post">
  283. ''' + bbs_comment_form + '''
  284. ''' + data_preview + '''
  285. </form>
  286. <script src="/views/main_css/js/route/bbs_w_post.js"></script>
  287. '''
  288. return easy_minify(flask.render_template(skin_check(),
  289. imp = [bbs_name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('bbs') + ')', 0])],
  290. data = data,
  291. menu = [['bbs/w/' + bbs_num_str, load_lang('return')], ['bbs/edit/' + bbs_num_str + '/' + post_num_str, load_lang('edit')], ['bbs/tool/' + bbs_num_str + '/' + post_num_str, load_lang('tool')]]
  292. ))