bbs_w_post.py 19 KB

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