edit.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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, 'document_edit') == 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. if section != '':
  41. if flask.request.form.get('doc_section_edit_apply', 'X') != 'X':
  42. if flask.request.form.get('doc_section_data_where', '') != '':
  43. data_match_where = flask.request.form.get('doc_section_data_where', '').split(',')
  44. if len(data_match_where) == 2:
  45. data_match_a = int(number_check(data_match_where[0]))
  46. if data_match_where[1] != 'inf':
  47. data_match_b = int(number_check(data_match_where[1]))
  48. else:
  49. data_match_b = 'inf'
  50. try:
  51. if data_match_b != 'inf':
  52. content = o_data[ : data_match_a] + content + o_data[data_match_b : ]
  53. else:
  54. content = o_data[ : data_match_a] + content
  55. except:
  56. pass
  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. add_alarm(scan_user[0], ip + ' | <a href="/w/' + url_pas(name) + '">' + html.escape(name) + '</a> | Edit')
  67. history_plus(
  68. name,
  69. content,
  70. today,
  71. ip,
  72. send,
  73. leng
  74. )
  75. curs.execute(db_change("delete from back where link = ?"), [name])
  76. curs.execute(db_change("delete from back where title = ? and type = 'no'"), [name])
  77. render_set(
  78. doc_name = name,
  79. doc_data = content,
  80. data_type = 'backlink'
  81. )
  82. conn.commit()
  83. section = (('#edit_load_' + str(section)) if section != '' else '')
  84. return redirect('/w/' + url_pas(name) + section)
  85. else:
  86. editor_top_text = ''
  87. doc_section_edit_apply = 'X'
  88. data_section = ''
  89. data_section_where = ''
  90. if edit_repeat == 'get':
  91. if do_type == 'load':
  92. if flask.session and 'edit_load_document' in flask.session:
  93. load_title = flask.session['edit_load_document']
  94. else:
  95. load_title = 0
  96. else:
  97. load_title = 0
  98. if load_title == 0 and section == '':
  99. load_title = name
  100. editor_top_text += '<a href="/manager/15/' + url_pas(name) + '">(' + load_lang('load') + ')</a> '
  101. elif section != '':
  102. load_title = name
  103. curs.execute(db_change("select data from data where title = ?"), [load_title])
  104. db_data = curs.fetchall()
  105. data = db_data[0][0] if db_data else ''
  106. data = data.replace('\r\n', '\n')
  107. if section != '':
  108. curs.execute(db_change('select data from other where name = "markup"'))
  109. db_data = curs.fetchall()
  110. db_data = db_data[0][0] if db_data else 'namumark'
  111. if db_data in ('namumark', 'namumark_beta'):
  112. count = 1
  113. data_section = '\n' + data + '\n'
  114. while 1:
  115. data_match_re = r'\n((={1,6})(#?) ?([^\n]+))\n'
  116. data_match = re.search(data_match_re, data_section)
  117. if not data_match:
  118. data_section = ''
  119. break
  120. elif count > section:
  121. data_section = ''
  122. break
  123. if section == count:
  124. data_section_sub = data_section
  125. data_section_sub = re.sub(data_match_re, '.' * len(data_match.group(0)), data_section_sub, 1)
  126. data_match_plus = re.search(data_match_re, data_section_sub)
  127. if data_match_plus:
  128. data_section = data[data_match.span()[0] : data_match_plus.span()[0] - 1]
  129. data_section_where = str(data_match.span()[0]) + ',' + str(data_match_plus.span()[0] - 1)
  130. else:
  131. data_section = data[data_match.span()[0] : ]
  132. data_section_where = str(data_match.span()[0]) + ',inf'
  133. doc_section_edit_apply = 'O'
  134. break
  135. else:
  136. data_section = re.sub(data_match_re, '.' * len(data_match.group(0)), data_section, 1)
  137. count += 1
  138. else:
  139. data = flask.request.form.get('content', '')
  140. warning_edit = load_lang('exp_edit_conflict') + ' '
  141. if flask.request.form.get('ver', '0') == '0':
  142. warning_edit += '<a href="/raw/' + url_pas(name) + '">(r' + doc_ver + ')</a>'
  143. else:
  144. warning_edit += '' + \
  145. '<a href="/diff/' + flask.request.form.get('ver', '1') + '/' + doc_ver + '/' + url_pas(name) + '">' + \
  146. '(r' + doc_ver + ')' + \
  147. '</a>' + \
  148. ''
  149. warning_edit += '<hr class="main_hr">'
  150. editor_top_text = warning_edit + editor_top_text
  151. if data_section == '':
  152. data_section = data
  153. editor_top_text += '<a href="/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>'
  154. curs.execute(db_change('select data from other where name = "edit_help"'))
  155. sql_d = curs.fetchall()
  156. p_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('default_edit_help')
  157. monaco_on = flask.request.cookies.get('main_css_monaco', '0')
  158. if monaco_on == '1':
  159. editor_display = 'style="display: none;"'
  160. monaco_display = ''
  161. add_get_file = '''
  162. <link rel="stylesheet"
  163. data-name="vs/editor/editor.main"
  164. href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.min.css">
  165. <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/loader.min.js"></script>
  166. <script>
  167. function opennamu_edit_turn_off_monaco() {
  168. do_monaco_to_textarea();
  169. document.getElementById('opennamu_js_edit_textarea_view').style.display = 'block';
  170. document.getElementById('opennamu_monaco_editor').style.display = 'none';
  171. document.getElementById('opennamu_monaco_editor').remove();
  172. }
  173. </script>
  174. '''
  175. editor_top_text += ' <a href="javascript:opennamu_edit_turn_off_monaco();">(' + load_lang('turn_off_monaco') + ')</a>'
  176. if flask.request.cookies.get('main_css_darkmode', '0') == '1':
  177. monaco_thema = 'vs-dark'
  178. else:
  179. monaco_thema = ''
  180. add_script = '''
  181. require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' }});
  182. require(["vs/editor/editor.main"], function () {
  183. window.editor = monaco.editor.create(document.getElementById('opennamu_monaco_editor'), {
  184. value: document.getElementById('opennamu_js_edit_textarea_view').value,
  185. language: 'plaintext',
  186. wordWrap: true,
  187. theme: \'''' + monaco_thema + '''\',
  188. minimap: { enabled: false }
  189. });
  190. });
  191. '''
  192. else:
  193. editor_display = ''
  194. monaco_display = 'style="display: none;"'
  195. add_get_file = ''
  196. add_script = ''
  197. if editor_top_text != '':
  198. editor_top_text += '<hr class="main_hr">'
  199. sub_menu = ' (' + str(section) + ')' if section != '' else ''
  200. return easy_minify(flask.render_template(skin_check(),
  201. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('edit') + ')' + sub_menu, 0])],
  202. data = editor_top_text + add_get_file + '''
  203. <form method="post">
  204. <textarea style="display: none;" id="opennamu_js_edit_origin" name="doc_data_org">''' + html.escape(data_section) + '''</textarea>
  205. <textarea style="display: none;" name="doc_section_data_where">''' + data_section_where + '''</textarea>
  206. <input style="display: none;" name="doc_section_edit_apply" value="''' + doc_section_edit_apply + '''">
  207. <textarea style="display: none;" id="opennamu_js_edit_textarea" name="content"></textarea>
  208. <input style="display: none;" name="ver" value="''' + doc_ver + '''">
  209. <div>''' + edit_button(monaco_on) + '''</div>
  210. <div id="opennamu_monaco_editor" class="content" ''' + monaco_display + '''></div>
  211. <textarea id="opennamu_js_edit_textarea_view" ''' + editor_display + ''' class="content" placeholder="''' + p_text + '''">''' + html.escape(data_section) + '''</textarea>
  212. <hr class="main_hr">
  213. <input placeholder="''' + load_lang('why') + '''" name="send">
  214. <hr class="main_hr">
  215. ''' + captcha_get() + ip_warning() + get_edit_text_bottom_check_box() + get_edit_text_bottom() + '''
  216. <button id="opennamu_js_save" type="submit">''' + load_lang('save') + '''</button>
  217. <button id="opennamu_js_preview" type="button">''' + load_lang('preview') + '''</button>
  218. </form>
  219. <hr class="main_hr">
  220. <div id="opennamu_js_preview_area"></div>
  221. <script>
  222. do_paste_image();
  223. ''' + add_script + '''
  224. </script>
  225. <!-- JS : edit.js -->
  226. ''',
  227. menu = [
  228. ['w/' + url_pas(name), load_lang('return')],
  229. ['delete/' + url_pas(name), load_lang('delete')],
  230. ['move/' + url_pas(name), load_lang('move')],
  231. ['upload', load_lang('upload')]
  232. ]
  233. ))