edit.py 17 KB

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