edit.py 15 KB

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