edit.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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) == 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. leng = leng_check(len(o_data), len(content))
  41. curs.execute(db_change("update data set data = ? where title = ?"), [content, name])
  42. else:
  43. leng = '+' + str(len(content))
  44. curs.execute(db_change("insert into data (title, data) values (?, ?)"), [name, content])
  45. curs.execute(db_change('select data from other where name = "count_all_title"'))
  46. curs.execute(db_change("update other set data = ? where name = 'count_all_title'"), [str(int(curs.fetchall()[0][0]) + 1)])
  47. curs.execute(db_change("select user from scan where title = ? and type = ''"), [name])
  48. for scan_user in curs.fetchall():
  49. add_alarm(scan_user[0], ip + ' | <a href="/w/' + url_pas(name) + '">' + name + '</a> | Edit')
  50. history_plus(
  51. name,
  52. content,
  53. today,
  54. ip,
  55. send,
  56. leng
  57. )
  58. curs.execute(db_change("delete from back where link = ?"), [name])
  59. curs.execute(db_change("delete from back where title = ? and type = 'no'"), [name])
  60. render_set(
  61. doc_name = name,
  62. doc_data = content,
  63. data_type = 'backlink'
  64. )
  65. conn.commit()
  66. section = (('#edit_load_' + str(section)) if section != '' else '')
  67. return redirect('/w/' + url_pas(name) + section)
  68. else:
  69. editor_top_text = ''
  70. if edit_repeat == 'get':
  71. if do_type == 'load':
  72. if flask.session and 'edit_load_document' in flask.session:
  73. load_title = flask.session['edit_load_document']
  74. else:
  75. load_title = 0
  76. else:
  77. load_title = 0
  78. if load_title == 0 and section == '':
  79. load_title = name
  80. editor_top_text += '<a href="/manager/15/' + url_pas(name) + '">(' + load_lang('load') + ')</a> '
  81. elif section != '':
  82. load_title = name
  83. curs.execute(db_change("select data from data where title = ?"), [load_title])
  84. sql_d = curs.fetchall()
  85. data = sql_d[0][0] if sql_d else ''
  86. data = data.replace('\r\n', '\n')
  87. else:
  88. data = flask.request.form.get('content', '')
  89. warning_edit = load_lang('exp_edit_conflict') + ' '
  90. if flask.request.form.get('ver', '0') == '0':
  91. warning_edit += '<a href="/raw/' + url_pas(name) + '">(r' + doc_ver + ')</a>'
  92. else:
  93. warning_edit += '' + \
  94. '<a href="/diff/' + flask.request.form.get('ver', '1') + '/' + doc_ver + '/' + url_pas(name) + '">' + \
  95. '(r' + doc_ver + ')' + \
  96. '</a>' + \
  97. ''
  98. warning_edit += '<hr class="main_hr">'
  99. editor_top_text = warning_edit + editor_top_text
  100. editor_top_text += '' + \
  101. '<a href="/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>' + \
  102. '<hr class="main_hr">' + \
  103. ''
  104. curs.execute(db_change('select data from other where name = "edit_help"'))
  105. sql_d = curs.fetchall()
  106. p_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('default_edit_help')
  107. data = re.sub(r'\n+$', '', data)
  108. # 이 파트 JS로 이동 예정
  109. monaco_on = flask.request.cookies.get('main_css_monaco', '0')
  110. if monaco_on == '1':
  111. editor_display = 'style="display: none;"'
  112. monaco_display = ''
  113. add_get_file = '''
  114. <link rel="stylesheet"
  115. data-name="vs/editor/editor.main"
  116. href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.min.css">
  117. <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/loader.min.js"></script>
  118. '''
  119. if flask.request.cookies.get('main_css_darkmode', '0') == '1':
  120. monaco_thema = 'vs-dark'
  121. else:
  122. monaco_thema = ''
  123. add_script = '''
  124. require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' }});
  125. require(["vs/editor/editor.main"], function () {
  126. window.editor = monaco.editor.create(document.getElementById('monaco_editor'), {
  127. value: document.getElementById('textarea_edit_view').value,
  128. language: 'plaintext',
  129. wordWrap: true,
  130. theme: \'''' + monaco_thema + '''\',
  131. minimap: { enabled: false }
  132. });
  133. });
  134. '''
  135. else:
  136. editor_display = ''
  137. monaco_display = 'style="display: none;"'
  138. add_get_file = ''
  139. add_script = ''
  140. return easy_minify(flask.render_template(skin_check(),
  141. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('edit') + ')', 0])],
  142. data = editor_top_text + add_get_file + '''
  143. <form method="post">
  144. <textarea style="display: none;" id="opennamu_js_edit_origin">''' + html.escape(data) + '''</textarea>
  145. <textarea style="display: none;" id="opennamu_js_edit_textarea" name="content"></textarea>
  146. <input style="display: none;" name="ver" value="''' + doc_ver + '''">
  147. <div>''' + edit_button(monaco_on) + '''</div>
  148. <div id="monaco_editor" class="content" ''' + monaco_display + '''></div>
  149. <textarea id="opennamu_js_edit_textarea_view" ''' + editor_display + ''' class="content" placeholder="''' + p_text + '''">''' + html.escape(data) + '''</textarea>
  150. <hr class="main_hr">
  151. <input placeholder="''' + load_lang('why') + '''" name="send">
  152. <hr class="main_hr">
  153. ''' + captcha_get() + ip_warning() + get_edit_text_bottom_check_box() + get_edit_text_bottom() + '''
  154. <button id="opennamu_js_save" type="submit" onclick="monaco_to_content(); section_edit_do();">''' + load_lang('save') + '''</button>
  155. <button id="opennamu_js_preview" type="button" onclick="monaco_to_content(); load_preview(\'''' + url_pas(name) + '''\');">''' + load_lang('preview') + '''</button>
  156. </form>
  157. <hr class="main_hr">
  158. <div id="see_preview"></div>
  159. <script>
  160. section_edit_init();
  161. do_paste_image();
  162. ''' + add_script + '''
  163. </script>
  164. ''',
  165. menu = [
  166. ['w/' + url_pas(name), load_lang('return')],
  167. ['delete/' + url_pas(name), load_lang('delete')],
  168. ['move/' + url_pas(name), load_lang('move')],
  169. ['upload', load_lang('upload')]
  170. ]
  171. ))