edit.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. section = int(number_check(section))
  18. if acl_check(name) == 1:
  19. return re_error('/ban')
  20. edit_repeat = 0
  21. if flask.request.method == 'POST':
  22. edit_repeat = 1
  23. curs.execute(db_change("select id from history where title = ? order by id + 0 desc"), [name])
  24. old = curs.fetchall()
  25. if old and flask.request.form.get('ver', '') != old[0][0]:
  26. edit_repeat = 2
  27. if edit_repeat == 1:
  28. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  29. return re_error('/error/13')
  30. else:
  31. captcha_post('', 0)
  32. if slow_edit_check() == 1:
  33. return re_error('/error/24')
  34. today = get_time()
  35. content = flask.request.form.get('content', '').replace('\r\n', '\n')
  36. o_content = flask.request.form.get('o_content', '').replace('\r\n', '\n')
  37. if o_content == content:
  38. return redirect('/w/' + url_pas(name))
  39. if edit_filter_do(content) == 1:
  40. return re_error('/error/21')
  41. curs.execute(db_change('select data from other where name = "copyright_checkbox_text"'))
  42. copyright_checkbox_text_d = curs.fetchall()
  43. if copyright_checkbox_text_d and copyright_checkbox_text_d[0][0] != '' and flask.request.form.get('copyright_agreement', '') != 'yes':
  44. return re_error('/error/29')
  45. curs.execute(db_change("select data from data where title = ?"), [name])
  46. old = curs.fetchall()
  47. if old:
  48. o_data = old[0][0].replace('\r\n', '\n')
  49. if section:
  50. run_count = run_count_section(section, content)
  51. c_data = html.escape('\n' + o_data)
  52. c_data = re.sub(r'\n(?P<in>={1,6})', '<br>\g<in>', c_data)
  53. c_data = re.sub(r'<br>((?:(?:(?!<br>).)*\n*)*)', run_count, c_data)
  54. c_data = re.sub(r'^\n', '', c_data)
  55. c_data = html.unescape(c_data)
  56. content = c_data
  57. leng = leng_check(len(o_data), len(content))
  58. curs.execute(db_change("update data set data = ? where title = ?"), [content, name])
  59. else:
  60. leng = '+' + str(len(content))
  61. curs.execute(db_change("insert into data (title, data) values (?, ?)"), [name, content])
  62. curs.execute(db_change('select data from other where name = "count_all_title"'))
  63. curs.execute(db_change("update other set data = ? where name = 'count_all_title'"), [str(int(curs.fetchall()[0][0]) + 1)])
  64. curs.execute(db_change("select user from scan where title = ? and type = ''"), [name])
  65. for scan_user in curs.fetchall():
  66. curs.execute(db_change("insert into alarm (name, data, date) values (?, ?, ?)"), [
  67. scan_user[0],
  68. ip + ' | <a href="/w/' + url_pas(name) + '">' + name + '</a> | Edit',
  69. today
  70. ])
  71. history_plus(
  72. name,
  73. content,
  74. today,
  75. ip,
  76. flask.request.form.get('send', ''),
  77. leng
  78. )
  79. curs.execute(db_change("delete from back where link = ?"), [name])
  80. curs.execute(db_change("delete from back where title = ? and type = 'no'"), [name])
  81. render_set(
  82. title = name,
  83. data = content,
  84. num = 1
  85. )
  86. conn.commit()
  87. return redirect('/w/' + url_pas(name))
  88. else:
  89. curs.execute(db_change("select data, id from history where title = ? order by id + 0 desc"), [name])
  90. old = curs.fetchall()
  91. if old:
  92. if section:
  93. data = html.escape('\n' + old[0][0].replace('\r\n', '\n'))
  94. data = re.sub(r'\n(?P<in>={1,6})', '<br>\g<in>', data)
  95. section_data = re.findall(r'<br>((?:(?:(?!<br>).)*\n*)*)', data)
  96. if len(section_data) >= section:
  97. data = html.unescape(section_data[section - 1])
  98. else:
  99. return redirect('/edit/' + url_pas(name))
  100. else:
  101. data = old[0][0].replace('\r\n', '\n')
  102. else:
  103. data = ''
  104. data_old = data
  105. if edit_repeat == 2:
  106. data = flask.request.form.get('content', '')
  107. warring_edit = load_lang('exp_edit_conflict') + ' '
  108. if flask.request.form.get('ver', '0') == '0':
  109. warring_edit += '<a href="/raw/' + url_pas(name) + '">(r' + old[0][1] + ')</a>'
  110. else:
  111. warring_edit += '' + \
  112. '<a href="/diff/' + url_pas(name) + '?first=' + flask.request.form.get('ver', '1') + '&second=' + old[0][1] + '">(r' + old[0][1] + ')</a>' + \
  113. ''
  114. warring_edit += '<hr class="main_hr">'
  115. else:
  116. warring_edit = ''
  117. get_name = ''
  118. if not section and not flask.request.args.get('plus', None):
  119. get_name += '' + \
  120. '<a href="/manager/15?plus=' + url_pas(name) + '">(' + load_lang('load') + ')</a> ' + \
  121. ''
  122. get_name += '' + \
  123. '<a href="/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>' + \
  124. '<hr class="main_hr">' + \
  125. ''
  126. if flask.request.args.get('plus', None):
  127. curs.execute(db_change("select data from data where title = ?"), [flask.request.args.get('plus', 'test')])
  128. get_data = curs.fetchall()
  129. if get_data:
  130. data = get_data[0][0]
  131. save_button = load_lang('save')
  132. menu_plus = [
  133. ['delete/' + url_pas(name), load_lang('delete')],
  134. ['move/' + url_pas(name), load_lang('move')],
  135. ['upload', load_lang('upload')]
  136. ]
  137. sub = load_lang('edit')
  138. curs.execute(db_change('select data from other where name = "edit_bottom_text"'))
  139. sql_d = curs.fetchall()
  140. b_text = ('<hr class="main_hr">' + sql_d[0][0]) if sql_d and sql_d[0][0] != '' else ''
  141. curs.execute(db_change('select data from other where name = "copyright_checkbox_text"'))
  142. sql_d = curs.fetchall()
  143. if sql_d and sql_d[0][0] != '':
  144. cccb_text = '' + \
  145. '<hr class="main_hr">' + \
  146. '<input type="checkbox" name="copyright_agreement" value="yes"> ' + sql_d[0][0] + \
  147. '<hr class="main_hr">' + \
  148. ''
  149. else:
  150. cccb_text = ''
  151. curs.execute(db_change('select data from other where name = "edit_help"'))
  152. sql_d = curs.fetchall()
  153. p_text = sql_d[0][0] if sql_d and sql_d[0][0] != '' else load_lang('default_edit_help')
  154. data = re.sub(r'\n+$', '', data)
  155. data_old = re.sub(r'\n+$', '', data_old)
  156. return easy_minify(flask.render_template(skin_check(),
  157. imp = [name, wiki_set(), custom(), other2(['(' + sub + ')', 0])],
  158. data = get_name + '''
  159. <form method="post">
  160. <script>
  161. do_paste_image();
  162. do_not_out();
  163. </script>
  164. ''' + edit_button() + '''
  165. ''' + warring_edit + '''
  166. <textarea id="content" class="content" placeholder="''' + p_text + '''" name="content">''' + html.escape(data) + '''</textarea>
  167. <textarea id="origin" name="o_content">''' + html.escape(data_old) + '''</textarea>
  168. <hr class="main_hr">
  169. <input placeholder="''' + load_lang('why') + '''" name="send" type="text">
  170. <input id="origin" name="ver" value="''' + (old[0][1] if old else '0') + '''">
  171. <hr class="main_hr">
  172. ''' + captcha_get() + ip_warring() + cccb_text + '''
  173. <button id="save" type="submit" onclick="save_stop_exit();">''' + save_button + '''</button>
  174. <button id="preview" type="button" onclick="load_preview(\'''' + url_pas(name) + '\')">' + load_lang('preview') + '''</button>
  175. </form>
  176. ''' + b_text + '''
  177. <hr class="main_hr">
  178. <div id="see_preview"></div>
  179. ''',
  180. menu = [['w/' + url_pas(name), load_lang('return')]] + menu_plus
  181. ))