edit.py 17 KB

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