edit.py 15 KB

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