2
0

edit.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import multiprocessing
  2. from .tool.func import *
  3. def edit_render_set(name, content):
  4. render_set(
  5. doc_name = name,
  6. doc_data = content
  7. )
  8. # https://stackoverflow.com/questions/13821156/timeout-function-using-threading-in-python-does-not-work
  9. def edit_timeout(func, args = (), timeout = 3):
  10. pool = multiprocessing.Pool(processes = 1)
  11. result = pool.apply_async(func, args = args)
  12. try:
  13. result.get(timeout = timeout)
  14. except multiprocessing.TimeoutError:
  15. pool.terminate()
  16. return 1
  17. else:
  18. pool.close()
  19. pool.join()
  20. return 0
  21. def edit_editor(curs, ip, data_main = '', do_type = 'edit', addon = ''):
  22. monaco_editor_top = ''
  23. editor_display = ''
  24. add_get_file = ''
  25. monaco_display = ''
  26. if do_type == 'edit':
  27. curs.execute(db_change('select data from other where name = "edit_help"'))
  28. sql_d = curs.fetchall()
  29. elif do_type == 'bbs':
  30. curs.execute(db_change('select data from other where name = "bbs_help"'))
  31. sql_d = curs.fetchall()
  32. elif do_type == 'bbs_comment':
  33. curs.execute(db_change('select data from other where name = "bbs_comment_help"'))
  34. sql_d = curs.fetchall()
  35. else:
  36. curs.execute(db_change('select data from other where name = "topic_text"'))
  37. sql_d = curs.fetchall()
  38. if do_type == 'bbs_comment':
  39. do_type = 'thread'
  40. elif do_type == 'bbs':
  41. do_type = 'edit'
  42. p_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('default_edit_help')
  43. monaco_editor_top += '<a href="javascript:opennamu_do_editor_temp_save();">(' + load_lang('load_temp_save') + ')</a> <a href="javascript:opennamu_do_editor_temp_save_load();">(' + load_lang('load_temp_save_load') + ')</a> '
  44. monaco_editor_top += '<a href="javascript:opennamu_edit_turn_off_monaco();">(' + load_lang('turn_off_monaco') + ')</a>'
  45. add_get_file = '''
  46. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.41.0/min/vs/editor/editor.main.min.css" integrity="sha512-MFDhxgOYIqLdcYTXw7en/n5BshKoduTitYmX8TkQ+iJOGjrWusRi8+KmfZOrgaDrCjZSotH2d1U1e/Z1KT6nWw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  47. <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.41.0/min/vs/loader.min.js" integrity="sha512-A+6SvPGkIN9Rf0mUXmW4xh7rDvALXf/f0VtOUiHlDUSPknu2kcfz1KzLpOJyL2pO+nZS13hhIjLqVgiQExLJrw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  48. '''
  49. darkmode = flask.request.cookies.get('main_css_darkmode', '0')
  50. monaco_thema = 'vs-dark' if darkmode == '1' else ''
  51. add_script = 'do_monaco_init("' + monaco_thema + '");'
  52. monaco_on = get_main_skin_set(curs, flask.session, 'main_css_monaco', ip)
  53. if monaco_on == 'use':
  54. editor_display = 'style="display: none;"'
  55. else:
  56. monaco_display = 'style="display: none;"'
  57. textarea_size = 'opennamu_textarea_500' if do_type == 'edit' else 'opennamu_textarea_100'
  58. return add_get_file + '''
  59. <textarea style="display: none;" id="opennamu_edit_origin" name="doc_data_org">''' + html.escape(data_main) + '''</textarea>
  60. <div>
  61. ''' + monaco_editor_top + '''
  62. <hr class="main_hr">
  63. ''' + edit_button() + '''
  64. </div>
  65. <div id="opennamu_monaco_editor" class="''' + textarea_size + '''" ''' + monaco_display + '''></div>
  66. <textarea id="opennamu_edit_textarea" ''' + editor_display + ''' class="''' + textarea_size + '''" name="content" placeholder="''' + p_text + '''">''' + html.escape(data_main) + '''</textarea>
  67. <hr class="main_hr">
  68. ''' + captcha_get() + ip_warning() + addon + '''
  69. <hr class="main_hr">
  70. <script>
  71. do_stop_exit();
  72. do_paste_image();
  73. ''' + add_script + '''
  74. </script>
  75. <button id="opennamu_save_button" type="submit" onclick="do_stop_exit_release();">''' + load_lang('send') + '''</button>
  76. <button id="opennamu_preview_button" type="button" onclick="opennamu_do_editor_preview();">''' + load_lang('preview') + '''</button>
  77. <hr class="main_hr">
  78. <div id="opennamu_preview_area"></div>
  79. '''
  80. def edit(name = 'Test', section = 0, do_type = ''):
  81. with get_db_connect() as conn:
  82. curs = conn.cursor()
  83. ip = ip_check()
  84. edit_req_mode = 0
  85. if acl_check(name, 'document_edit') == 1:
  86. edit_req_mode = 1
  87. if do_title_length_check(name) == 1:
  88. return re_error('/error/38')
  89. curs.execute(db_change("select id from history where title = ? order by id + 0 desc"), [name])
  90. doc_ver = curs.fetchall()
  91. doc_ver = doc_ver[0][0] if doc_ver else '0'
  92. curs.execute(db_change("select set_data from data_set where doc_name = ? and doc_rev = ? and set_name = 'edit_request_data'"), [name, doc_ver])
  93. if curs.fetchall():
  94. return redirect('/edit_request_from/' + url_pas(name))
  95. section = '' if section == 0 else section
  96. post_ver = flask.request.form.get('ver', '')
  97. if flask.request.method == 'POST':
  98. edit_repeat = 'error' if post_ver != doc_ver else 'post'
  99. else:
  100. edit_repeat = 'get'
  101. if edit_repeat == 'post':
  102. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  103. return re_error('/error/13')
  104. else:
  105. captcha_post('', 0)
  106. if do_edit_slow_check() == 1:
  107. return re_error('/error/24')
  108. today = get_time()
  109. content = flask.request.form.get('content', '').replace('\r', '')
  110. send = flask.request.form.get('send', '')
  111. agree = flask.request.form.get('copyright_agreement', '')
  112. if do_edit_filter(content) == 1:
  113. return re_error('/error/21')
  114. if do_edit_send_check(send) == 1:
  115. return re_error('/error/37')
  116. if do_edit_text_bottom_check_box_check(agree) == 1:
  117. return re_error('/error/29')
  118. curs.execute(db_change("select data from data where title = ?"), [name])
  119. db_data = curs.fetchall()
  120. if db_data:
  121. o_data = db_data[0][0].replace('\r', '')
  122. if section != '':
  123. if flask.request.form.get('doc_section_edit_apply', 'X') != 'X':
  124. if flask.request.form.get('doc_section_data_where', '') != '':
  125. data_match_where = flask.request.form.get('doc_section_data_where', '').split(',')
  126. if len(data_match_where) == 2:
  127. data_match_a = int(number_check(data_match_where[0]))
  128. if data_match_where[1] != 'inf':
  129. data_match_b = int(number_check(data_match_where[1]))
  130. else:
  131. data_match_b = 'inf'
  132. try:
  133. if data_match_b != 'inf':
  134. content = o_data[ : data_match_a] + content + o_data[data_match_b : ]
  135. else:
  136. content = o_data[ : data_match_a] + content
  137. except:
  138. pass
  139. leng = leng_check(len(o_data), len(content))
  140. else:
  141. leng = '+' + str(len(content))
  142. curs.execute(db_change("select data from other where name = 'document_content_max_length'"))
  143. db_data_3 = curs.fetchall()
  144. if db_data_3 and db_data_3[0][0] != '':
  145. if int(number_check(db_data_3[0][0])) < len(content):
  146. return re_error('/error/44')
  147. curs.execute(db_change("select data from other where name = 'edit_timeout'"))
  148. db_data_2 = curs.fetchall()
  149. db_data_2 = number_check(db_data_2[0][0]) if db_data_2 and db_data_2[0][0] != '' else ''
  150. if db_data_2 != '' and platform.system() == 'Linux':
  151. timeout = edit_timeout(edit_render_set, (name, content), timeout = int(db_data_2))
  152. else:
  153. timeout = 0
  154. if timeout == 1:
  155. return re_error('/error/41')
  156. if edit_req_mode == 0:
  157. # 진짜 기록 부분
  158. curs.execute(db_change("delete from data where title = ?"), [name])
  159. curs.execute(db_change("insert into data (title, data) values (?, ?)"), [name, content])
  160. curs.execute(db_change("select user from scan where title = ? and type = ''"), [name])
  161. for scan_user in curs.fetchall():
  162. add_alarm(scan_user[0], ip, '<a href="/w/' + url_pas(name) + '">' + html.escape(name) + '</a>')
  163. history_plus(
  164. name,
  165. content,
  166. today,
  167. ip,
  168. send,
  169. leng
  170. )
  171. render_set(
  172. doc_name = name,
  173. doc_data = content,
  174. data_type = 'backlink'
  175. )
  176. else:
  177. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'edit_request_data', ?)"), [name, doc_ver, content])
  178. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'edit_request_user', ?)"), [name, doc_ver, ip])
  179. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'edit_request_date', ?)"), [name, doc_ver, today])
  180. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'edit_request_send', ?)"), [name, doc_ver, send])
  181. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'edit_request_leng', ?)"), [name, doc_ver, leng])
  182. curs.execute(db_change("select user from scan where title = ? and type = ''"), [name])
  183. for scan_user in curs.fetchall():
  184. add_alarm(scan_user[0], ip, '<a href="/edit_request/' + url_pas(name) + '">' + html.escape(name) + '</a> edit_request')
  185. conn.commit()
  186. section = (('#edit_load_' + str(section)) if section != '' else '')
  187. return redirect('/w/' + url_pas(name) + section)
  188. else:
  189. editor_top_text = ''
  190. doc_section_edit_apply = 'X'
  191. data_section = ''
  192. data_section_where = ''
  193. if edit_repeat == 'get':
  194. if do_type == 'load':
  195. if flask.session and 'edit_load_document' in flask.session:
  196. load_title = flask.session['edit_load_document']
  197. else:
  198. load_title = 0
  199. else:
  200. load_title = 0
  201. if load_title == 0 and section == '':
  202. load_title = name
  203. editor_top_text += '<a href="/manager/15/' + url_pas(name) + '">(' + load_lang('load') + ')</a> '
  204. elif section != '':
  205. load_title = name
  206. curs.execute(db_change("select data from data where title = ?"), [load_title])
  207. db_data = curs.fetchall()
  208. data = db_data[0][0] if db_data else ''
  209. data = data.replace('\r', '')
  210. if section != '':
  211. curs.execute(db_change('select data from other where name = "markup"'))
  212. db_data = curs.fetchall()
  213. db_data = db_data[0][0] if db_data else 'namumark'
  214. if db_data in ('namumark', 'namumark_beta'):
  215. count = 1
  216. data_section = '\n' + data + '\n'
  217. while 1:
  218. data_match_re = r'\n((={1,6})(#?) ?([^\n]+))\n'
  219. data_match = re.search(data_match_re, data_section)
  220. if not data_match:
  221. data_section = ''
  222. break
  223. elif count > section:
  224. data_section = ''
  225. break
  226. if section == count:
  227. data_section_sub = data_section
  228. data_section_sub = re.sub(data_match_re, ('.' * (len(data_match.group(0)) - 1)) + '\n', data_section_sub, 1)
  229. data_match_plus = re.search(data_match_re, data_section_sub)
  230. if data_match_plus:
  231. data_section = data[data_match.span()[0] : data_match_plus.span()[0] - 1]
  232. data_section_where = str(data_match.span()[0]) + ',' + str(data_match_plus.span()[0] - 1)
  233. else:
  234. data_section = data[data_match.span()[0] : ]
  235. data_section_where = str(data_match.span()[0]) + ',inf'
  236. doc_section_edit_apply = 'O'
  237. break
  238. else:
  239. data_section = re.sub(data_match_re, ('.' * (len(data_match.group(0)) - 1)) + '\n', data_section, 1)
  240. count += 1
  241. else:
  242. data = flask.request.form.get('content', '')
  243. data = data.replace('\r', '')
  244. data_section_where = flask.request.form.get('doc_section_data_where', '')
  245. doc_section_edit_apply = flask.request.form.get('doc_section_edit_apply', '')
  246. doc_ver = flask.request.form.get('ver', '')
  247. warning_edit = load_lang('exp_edit_conflict') + ' '
  248. if flask.request.form.get('ver', '0') == '0':
  249. warning_edit += '<a href="/raw/' + url_pas(name) + '">(r' + doc_ver + ')</a>'
  250. else:
  251. warning_edit += '' + \
  252. '<a href="/diff/' + flask.request.form.get('ver', '1') + '/' + doc_ver + '/' + url_pas(name) + '">' + \
  253. '(r' + doc_ver + ')' + \
  254. '</a>' + \
  255. ''
  256. warning_edit += '<hr class="main_hr">'
  257. editor_top_text = warning_edit + editor_top_text
  258. if data_section == '':
  259. data_section = data
  260. editor_top_text += '<a href="/filter/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>'
  261. if editor_top_text != '':
  262. editor_top_text += '<hr class="main_hr">'
  263. sub_menu = ' (' + str(section) + ')' if section != '' else ''
  264. sub_title = '(' + load_lang('edit_request') + ')' if edit_req_mode == 1 else '(' + load_lang('edit') + ')'
  265. return easy_minify(flask.render_template(skin_check(),
  266. imp = [name, wiki_set(), wiki_custom(), wiki_css([sub_title + sub_menu, 0])],
  267. data = editor_top_text + '''
  268. <form method="post">
  269. <textarea style="display: none;" name="doc_section_data_where">''' + data_section_where + '''</textarea>
  270. <input style="display: none;" name="doc_section_edit_apply" value="''' + doc_section_edit_apply + '''">
  271. <input style="display: none;" id="opennamu_editor_doc_name" value="''' + html.escape(name) + '''">
  272. <input style="display: none;" name="ver" value="''' + doc_ver + '''">
  273. <input placeholder="''' + load_lang('why') + '''" name="send">
  274. <hr class="main_hr">
  275. ''' + edit_editor(curs, ip, data_section, addon = get_edit_text_bottom_check_box() + get_edit_text_bottom()) + '''
  276. </form>
  277. ''',
  278. menu = [
  279. ['w/' + url_pas(name), load_lang('return')],
  280. ['delete/' + url_pas(name), load_lang('delete')],
  281. ['move/' + url_pas(name), load_lang('move')],
  282. ['upload', load_lang('upload')]
  283. ]
  284. ))