edit.py 9.0 KB

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