edit.py 17 KB

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