bbs_w_post.py 18 KB

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