edit.py 10 KB

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