edit.py 15 KB

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