edit.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. edit_repeat = 'error' if do_type == 'preview' else 'post'
  18. else:
  19. edit_repeat = 'get'
  20. if edit_repeat == 'post':
  21. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  22. return re_error('/error/13')
  23. else:
  24. captcha_post('', 0)
  25. if do_edit_slow_check() == 1:
  26. return re_error('/error/24')
  27. today = get_time()
  28. content = flask.request.form.get('content', '').replace('\r\n', '\n')
  29. send = flask.request.form.get('send', '')
  30. agree = flask.request.form.get('copyright_agreement', '')
  31. if do_edit_filter(content) == 1:
  32. return re_error('/error/21')
  33. if do_edit_send_check(send) == 1:
  34. return re_error('/error/37')
  35. if do_edit_text_bottom_check_box_check(agree) == 1:
  36. return re_error('/error/29')
  37. curs.execute(db_change("select data from data where title = ?"), [name])
  38. old = curs.fetchall()
  39. if old:
  40. o_data = old[0][0].replace('\r\n', '\n')
  41. if section != '':
  42. if flask.request.form.get('doc_section_edit_apply', 'X') != 'X':
  43. if flask.request.form.get('doc_section_data_where', '') != '':
  44. data_match_where = flask.request.form.get('doc_section_data_where', '').split(',')
  45. if len(data_match_where) == 2:
  46. data_match_a = int(number_check(data_match_where[0]))
  47. if data_match_where[1] != 'inf':
  48. data_match_b = int(number_check(data_match_where[1]))
  49. else:
  50. data_match_b = 'inf'
  51. try:
  52. if data_match_b != 'inf':
  53. content = o_data[ : data_match_a] + content + o_data[data_match_b : ]
  54. else:
  55. content = o_data[ : data_match_a] + content
  56. except:
  57. pass
  58. leng = leng_check(len(o_data), len(content))
  59. curs.execute(db_change("update data set data = ? where title = ?"), [content, name])
  60. else:
  61. leng = '+' + str(len(content))
  62. curs.execute(db_change("insert into data (title, data) values (?, ?)"), [name, content])
  63. curs.execute(db_change('select data from other where name = "count_all_title"'))
  64. curs.execute(db_change("update other set data = ? where name = 'count_all_title'"), [str(int(curs.fetchall()[0][0]) + 1)])
  65. curs.execute(db_change("select user from scan where title = ? and type = ''"), [name])
  66. for scan_user in curs.fetchall():
  67. add_alarm(scan_user[0], ip + ' | <a href="/w/' + url_pas(name) + '">' + html.escape(name) + '</a> | Edit')
  68. history_plus(
  69. name,
  70. content,
  71. today,
  72. ip,
  73. send,
  74. leng
  75. )
  76. curs.execute(db_change("delete from back where link = ?"), [name])
  77. curs.execute(db_change("delete from back where title = ? and type = 'no'"), [name])
  78. render_set(
  79. doc_name = name,
  80. doc_data = content,
  81. data_type = 'backlink'
  82. )
  83. conn.commit()
  84. section = (('#edit_load_' + str(section)) if section != '' else '')
  85. return redirect('/w/' + url_pas(name) + section)
  86. else:
  87. editor_top_text = ''
  88. doc_section_edit_apply = 'X'
  89. data_section = ''
  90. data_section_where = ''
  91. data_preview = ''
  92. if edit_repeat == 'get':
  93. if do_type == 'load':
  94. if flask.session and 'edit_load_document' in flask.session:
  95. load_title = flask.session['edit_load_document']
  96. else:
  97. load_title = 0
  98. else:
  99. load_title = 0
  100. if load_title == 0 and section == '':
  101. load_title = name
  102. editor_top_text += '<a href="/manager/15/' + url_pas(name) + '">(' + load_lang('load') + ')</a> '
  103. elif section != '':
  104. load_title = name
  105. curs.execute(db_change("select data from data where title = ?"), [load_title])
  106. db_data = curs.fetchall()
  107. data = db_data[0][0] if db_data else ''
  108. data = data.replace('\r\n', '\n')
  109. if section != '':
  110. curs.execute(db_change('select data from other where name = "markup"'))
  111. db_data = curs.fetchall()
  112. db_data = db_data[0][0] if db_data else 'namumark'
  113. if db_data in ('namumark', 'namumark_beta'):
  114. count = 1
  115. data_section = '\n' + data + '\n'
  116. while 1:
  117. data_match_re = r'\n((={1,6})(#?) ?([^\n]+))\n'
  118. data_match = re.search(data_match_re, data_section)
  119. if not data_match:
  120. data_section = ''
  121. break
  122. elif count > section:
  123. data_section = ''
  124. break
  125. if section == count:
  126. data_section_sub = data_section
  127. data_section_sub = re.sub(data_match_re, ('.' * (len(data_match.group(0)) - 1)) + '\n', data_section_sub, 1)
  128. data_match_plus = re.search(data_match_re, data_section_sub)
  129. if data_match_plus:
  130. data_section = data[data_match.span()[0] : data_match_plus.span()[0] - 1]
  131. data_section_where = str(data_match.span()[0]) + ',' + str(data_match_plus.span()[0] - 1)
  132. else:
  133. data_section = data[data_match.span()[0] : ]
  134. data_section_where = str(data_match.span()[0]) + ',inf'
  135. doc_section_edit_apply = 'O'
  136. break
  137. else:
  138. data_section = re.sub(data_match_re, ('.' * (len(data_match.group(0)) - 1)) + '\n', data_section, 1)
  139. count += 1
  140. else:
  141. data = flask.request.form.get('content', '')
  142. data = data.replace('\r\n', '\n')
  143. data_section_where = flask.request.form.get('doc_section_data_where', '')
  144. doc_section_edit_apply = flask.request.form.get('doc_section_edit_apply', '')
  145. doc_ver = flask.request.form.get('ver', '')
  146. if do_type != 'preview':
  147. warning_edit = load_lang('exp_edit_conflict') + ' '
  148. if flask.request.form.get('ver', '0') == '0':
  149. warning_edit += '<a href="/raw/' + url_pas(name) + '">(r' + doc_ver + ')</a>'
  150. else:
  151. warning_edit += '' + \
  152. '<a href="/diff/' + flask.request.form.get('ver', '1') + '/' + doc_ver + '/' + url_pas(name) + '">' + \
  153. '(r' + doc_ver + ')' + \
  154. '</a>' + \
  155. ''
  156. warning_edit += '<hr class="main_hr">'
  157. editor_top_text = warning_edit + editor_top_text
  158. else:
  159. data_preview = render_set(
  160. doc_name = name,
  161. doc_data = data,
  162. data_in = ''
  163. )
  164. if data_section == '':
  165. data_section = data
  166. if section == '':
  167. form_action = 'formaction="/edit/' + url_pas(name) + '"'
  168. form_action_preview = 'formaction="/edit_preview/' + url_pas(name) + '"'
  169. else:
  170. form_action = 'formaction="/edit_section/' + str(section) + '/' + url_pas(name) + '"'
  171. form_action_preview = 'formaction="/edit_section_preview/' + str(section) + '/' + url_pas(name) + '"'
  172. editor_top_text += '<a href="/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>'
  173. curs.execute(db_change('select data from other where name = "edit_help"'))
  174. sql_d = curs.fetchall()
  175. p_text = html.escape(sql_d[0][0]) if sql_d and sql_d[0][0] != '' else load_lang('default_edit_help')
  176. monaco_on = flask.request.cookies.get('main_css_monaco', '0')
  177. if monaco_on == '1':
  178. editor_display = 'style="display: none;"'
  179. monaco_display = ''
  180. add_get_file = '''
  181. <link rel="stylesheet"
  182. data-name="vs/editor/editor.main"
  183. href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.1/min/vs/editor/editor.main.min.css">
  184. <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.1/min/vs/loader.min.js"></script>
  185. '''
  186. editor_top_text += ' <a href="javascript:opennamu_edit_turn_off_monaco();">(' + load_lang('turn_off_monaco') + ')</a>'
  187. if flask.request.cookies.get('main_css_darkmode', '0') == '1':
  188. monaco_thema = 'vs-dark'
  189. else:
  190. monaco_thema = ''
  191. add_script = '''
  192. require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.1/min/vs' }});
  193. require.config({ 'vs/nls': { availableLanguages: { '*': 'ko' } }});
  194. require(["vs/editor/editor.main"], function () {
  195. window.editor = monaco.editor.create(document.getElementById('opennamu_monaco_editor'), {
  196. value: document.getElementById('opennamu_edit_textarea').value,
  197. language: 'plaintext',
  198. automaticLayout: true,
  199. wordWrap: true,
  200. theme: \'''' + monaco_thema + '''\'
  201. });
  202. });
  203. '''
  204. else:
  205. editor_display = ''
  206. monaco_display = 'style="display: none;"'
  207. add_get_file = ''
  208. add_script = 'opennamu_edit_turn_off_monaco();'
  209. if editor_top_text != '':
  210. editor_top_text += '<hr class="main_hr">'
  211. sub_menu = ' (' + str(section) + ')' if section != '' else ''
  212. return easy_minify(flask.render_template(skin_check(),
  213. imp = [name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('edit') + ')' + sub_menu, 0])],
  214. data = editor_top_text + add_get_file + '''
  215. <script>
  216. function opennamu_edit_turn_off_monaco() {
  217. do_monaco_to_textarea();
  218. document.getElementById('opennamu_edit_textarea').style.display = 'block';
  219. document.getElementById('opennamu_monaco_editor').style.display = 'none';
  220. document.getElementById('opennamu_monaco_editor').remove();
  221. }
  222. function do_monaco_to_textarea() {
  223. if(document.getElementById('opennamu_monaco_editor')) {
  224. try {
  225. document.getElementById('opennamu_edit_textarea').value = window.editor.getValue();
  226. } catch(e) {}
  227. }
  228. }
  229. </script>
  230. <form method="post">
  231. <textarea style="display: none;" id="opennamu_edit_origin" name="doc_data_org">''' + html.escape(data_section) + '''</textarea>
  232. <textarea style="display: none;" name="doc_section_data_where">''' + data_section_where + '''</textarea>
  233. <input style="display: none;" name="doc_section_edit_apply" value="''' + doc_section_edit_apply + '''">
  234. <input style="display: none;" name="ver" value="''' + doc_ver + '''">
  235. <div>''' + edit_button('opennamu_edit_textarea', 'opennamu_monaco_editor') + '''</div>
  236. <div id="opennamu_monaco_editor" class="opennamu_textarea_500" ''' + monaco_display + '''></div>
  237. <textarea id="opennamu_edit_textarea" ''' + editor_display + ''' class="opennamu_textarea_500" name="content" placeholder="''' + p_text + '''">''' + html.escape(data_section) + '''</textarea>
  238. <hr class="main_hr">
  239. <input placeholder="''' + load_lang('why') + '''" name="send">
  240. <hr class="main_hr">
  241. ''' + captcha_get() + ip_warning() + get_edit_text_bottom_check_box() + get_edit_text_bottom() + '''
  242. <button id="opennamu_save_button" type="submit" ''' + form_action + ''' onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('save') + '''</button>
  243. <button id="opennamu_preview_button" type="submit" ''' + form_action_preview + ''' onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('preview') + '''</button>
  244. </form>
  245. <hr class="main_hr">
  246. <div id="opennamu_preview_area">''' + data_preview + '''</div>
  247. <script>
  248. function do_stop_exit() {
  249. window.onbeforeunload = function() {
  250. do_monaco_to_textarea();
  251. let data = document.getElementById('opennamu_edit_textarea').value;
  252. let origin = document.getElementById('opennamu_edit_origin').value;
  253. if(data !== origin) {
  254. return '';
  255. }
  256. }
  257. }
  258. function do_stop_exit_release() {
  259. window.onbeforeunload = function () {}
  260. }
  261. do_stop_exit();
  262. do_paste_image();
  263. ''' + add_script + '''
  264. </script>
  265. ''',
  266. menu = [
  267. ['w/' + url_pas(name), load_lang('return')],
  268. ['delete/' + url_pas(name), load_lang('delete')],
  269. ['move/' + url_pas(name), load_lang('move')],
  270. ['upload', load_lang('upload')]
  271. ]
  272. ))