edit.py 14 KB

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