edit.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from .tool.func import *
  2. class run_count_section:
  3. def __init__(self, key, change):
  4. self.counter = key
  5. self.change = change
  6. def __call__(self, match):
  7. self.counter -= 1
  8. if self.counter == 0:
  9. return '\n' + self.change + '\n'
  10. else:
  11. return '\n' + match[1]
  12. def edit_2(conn, name):
  13. curs = conn.cursor()
  14. ip = ip_check()
  15. section = flask.request.args.get('section', None)
  16. if section:
  17. curs.execute(db_change("select data from other where name = 'markup'"))
  18. markup = curs.fetchall()
  19. if markup[0][0] == 'namumark':
  20. section = int(number_check(section))
  21. else:
  22. return redirect('/edit/' + url_pas(name))
  23. if acl_check(name) == 1:
  24. return re_error('/ban')
  25. curs.execute(db_change("select id from history where title = ? order by id + 0 desc"), [name])
  26. doc_ver = curs.fetchall()
  27. doc_ver = doc_ver[0][0] if doc_ver else '0'
  28. edit_repeat = 0
  29. if flask.request.method == 'POST':
  30. edit_repeat = 1
  31. if flask.request.form.get('ver', '') != doc_ver:
  32. edit_repeat = 2
  33. if edit_repeat == 1:
  34. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  35. return re_error('/error/13')
  36. else:
  37. captcha_post('', 0)
  38. if slow_edit_check() == 1:
  39. return re_error('/error/24')
  40. today = get_time()
  41. content = flask.request.form.get('content', '').replace('\r\n', '\n')
  42. if edit_filter_do(content) == 1:
  43. return re_error('/error/21')
  44. curs.execute(db_change('select data from other where name = "copyright_checkbox_text"'))
  45. copyright_checkbox_text_d = curs.fetchall()
  46. if copyright_checkbox_text_d and copyright_checkbox_text_d[0][0] != '' and flask.request.form.get('copyright_agreement', '') != 'yes':
  47. return re_error('/error/29')
  48. curs.execute(db_change("select data from data where title = ?"), [name])
  49. old = curs.fetchall()
  50. if old:
  51. o_data = old[0][0].replace('\r\n', '\n')
  52. if section:
  53. run_count = run_count_section(section, content)
  54. c_data = html.escape('\n' + o_data)
  55. c_data = re.sub(r'\n(?P<in>={1,6})', '<br>\g<in>', c_data)
  56. c_data = re.sub(r'<br>((?:(?:(?!<br>).)*\n*)*)', run_count, c_data)
  57. c_data = re.sub(r'^\n', '', c_data)
  58. c_data = html.unescape(c_data)
  59. content = c_data
  60. leng = leng_check(len(o_data), len(content))
  61. curs.execute(db_change("update data set data = ? where title = ?"), [content, name])
  62. else:
  63. leng = '+' + str(len(content))
  64. curs.execute(db_change("insert into data (title, data) values (?, ?)"), [name, content])
  65. curs.execute(db_change('select data from other where name = "count_all_title"'))
  66. curs.execute(db_change("update other set data = ? where name = 'count_all_title'"), [str(int(curs.fetchall()[0][0]) + 1)])
  67. curs.execute(db_change("select user from scan where title = ? and type = ''"), [name])
  68. for scan_user in curs.fetchall():
  69. add_alarm(scan_user[0], ip + ' | <a href="/w/' + url_pas(name) + '">' + name + '</a> | Edit')
  70. history_plus(
  71. name,
  72. content,
  73. today,
  74. ip,
  75. flask.request.form.get('send', ''),
  76. leng
  77. )
  78. curs.execute(db_change("delete from back where link = ?"), [name])
  79. curs.execute(db_change("delete from back where title = ? and type = 'no'"), [name])
  80. render_set(
  81. doc_name = name,
  82. doc_data = content,
  83. data_type = 'backlink'
  84. )
  85. conn.commit()
  86. return redirect('/w/' + url_pas(name) + (('#edit_load_' + str(section)) if section else ''))
  87. else:
  88. editor_top_text = ''
  89. if edit_repeat != 2:
  90. load_title = flask.request.args.get('plus', None)
  91. if load_title:
  92. curs.execute(db_change("select data from data where title = ?"), [load_title])
  93. get_data = curs.fetchall()
  94. data = get_data[0][0] if get_data else ''
  95. else:
  96. curs.execute(db_change("select data, id from history where title = ? order by id + 0 desc"), [name])
  97. old = curs.fetchall()
  98. old = old if old else [['']]
  99. if section:
  100. data = html.escape('\n' + old[0][0].replace('\r\n', '\n'))
  101. data = re.sub(r'\n(?P<in>={1,6})', '<br>\g<in>', data)
  102. section_data = re.findall(r'<br>((?:(?:(?!<br>).)*\n*)*)', data)
  103. if len(section_data) >= section:
  104. data = html.unescape(section_data[section - 1])
  105. else:
  106. return redirect('/edit/' + url_pas(name))
  107. else:
  108. data = old[0][0].replace('\r\n', '\n')
  109. editor_top_text += '<a href="/manager/15?plus=' + url_pas(name) + '">(' + load_lang('load') + ')</a> '
  110. else:
  111. data = flask.request.form.get('content', '')
  112. warring_edit = load_lang('exp_edit_conflict') + ' '
  113. if flask.request.form.get('ver', '0') == '0':
  114. warring_edit += '<a href="/raw/' + url_pas(name) + '">(r' + doc_ver + ')</a>'
  115. else:
  116. warring_edit += '' + \
  117. '<a href="/diff/' + url_pas(name) + '?first=' + flask.request.form.get('ver', '1') + '&second=' + doc_ver + '">(r' + doc_ver + ')</a>' + \
  118. ''
  119. warring_edit += '<hr class="main_hr">'
  120. editor_top_text = warring_edit + editor_top_text
  121. editor_top_text += '' + \
  122. '<a href="/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>' + \
  123. '<hr class="main_hr">' + \
  124. ''
  125. curs.execute(db_change('select data from other where name = "edit_bottom_text"'))
  126. sql_d = curs.fetchall()
  127. b_text = ('<hr class="main_hr">' + sql_d[0][0]) if sql_d and sql_d[0][0] != '' else ''
  128. curs.execute(db_change('select data from other where name = "copyright_checkbox_text"'))
  129. sql_d = curs.fetchall()
  130. if sql_d and sql_d[0][0] != '':
  131. cccb_text = '' + \
  132. '<hr class="main_hr">' + \
  133. '<input type="checkbox" name="copyright_agreement" value="yes"> ' + sql_d[0][0] + \
  134. '<hr class="main_hr">' + \
  135. ''
  136. else:
  137. cccb_text = ''
  138. curs.execute(db_change('select data from other where name = "edit_help"'))
  139. sql_d = curs.fetchall()
  140. p_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('default_edit_help')
  141. data = re.sub(r'\n+$', '', data)
  142. if flask.request.cookies.get('main_css_monaco', '0') == '1':
  143. editor_display = 'style="display: none;"'
  144. monaco_display = ''
  145. add_get_file = '''
  146. <link rel="stylesheet"
  147. data-name="vs/editor/editor.main"
  148. href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.min.css">
  149. <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/loader.min.js"></script>
  150. '''
  151. if flask.request.cookies.get('main_css_darkmode', '0') == '1':
  152. monaco_thema = 'vs-dark'
  153. else:
  154. monaco_thema = ''
  155. add_script = '''
  156. require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' }});
  157. require(["vs/editor/editor.main"], function () {
  158. window.editor = monaco.editor.create(document.getElementById('monaco_editor'), {
  159. value: document.getElementById('content').value,
  160. language: 'plaintext',
  161. theme: \'''' + monaco_thema + '''\'
  162. });
  163. });
  164. '''
  165. else:
  166. editor_display = ''
  167. monaco_display = 'style="display: none;"'
  168. add_get_file = ''
  169. add_script = ''
  170. return easy_minify(flask.render_template(skin_check(),
  171. imp = [name, wiki_set(), custom(), other2(['(' + load_lang('edit') + ')', 0])],
  172. data = editor_top_text + add_get_file + '''
  173. <form method="post">
  174. <script>
  175. do_paste_image();
  176. do_not_out();
  177. ''' + add_script + '''
  178. </script>
  179. <div ''' + editor_display + '''>''' + edit_button() + '''</div>
  180. <div id="monaco_editor"
  181. class="content"
  182. ''' + monaco_display + '''></div>
  183. <textarea id="content"
  184. ''' + editor_display + '''
  185. class="content"
  186. placeholder="''' + p_text + '''"
  187. name="content">''' + html.escape(data) + '''</textarea>
  188. <hr class="main_hr">
  189. <input placeholder="''' + load_lang('why') + '''" name="send" type="text">
  190. <textarea style="display: none;" id="origin">''' + html.escape(data) + '''</textarea>
  191. <input style="display: none;" name="ver" value="''' + doc_ver + '''">
  192. <hr class="main_hr">
  193. ''' + captcha_get() + ip_warring() + cccb_text + '''
  194. <button id="save"
  195. type="submit"
  196. onclick="monaco_to_content(); save_stop_exit();">''' + load_lang('save') + '''</button>
  197. <button id="preview"
  198. type="button"
  199. onclick="monaco_to_content(); load_preview(\'''' + url_pas(name) + '\');">' + load_lang('preview') + '''</button>
  200. </form>
  201. ''' + b_text + '''
  202. <hr class="main_hr">
  203. <div id="see_preview"></div>
  204. ''',
  205. menu = [
  206. ['w/' + url_pas(name), load_lang('return')],
  207. ['delete/' + url_pas(name), load_lang('delete')],
  208. ['move/' + url_pas(name), load_lang('move')],
  209. ['upload', load_lang('upload')]
  210. ]
  211. ))