bbs_w_post.py 18 KB

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