bbs_w_post.py 17 KB

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