func.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. # 모듈들 불러옴
  2. import css_html_js_minify
  3. import flask
  4. import json
  5. import sqlite3
  6. import hashlib
  7. import requests
  8. import re
  9. import html
  10. import os
  11. # 일부 툴 불러옴
  12. from set_mark.tool import *
  13. # 나무마크 불러옴
  14. from mark import *
  15. # 서브 언어팩 불러옴
  16. json_data = open(os.path.join('language', 'en-US.json'), 'rt', encoding='utf-8').read()
  17. else_lang = json.loads(json_data)
  18. def load_conn(data):
  19. global conn
  20. global curs
  21. conn = data
  22. curs = conn.cursor()
  23. load_conn2(data)
  24. def captcha_get():
  25. data = ''
  26. if custom()[2] == 0:
  27. curs.execute('select data from other where name = "recaptcha"')
  28. recaptcha = curs.fetchall()
  29. if recaptcha and recaptcha[0][0] != '':
  30. curs.execute('select data from other where name = "sec_re"')
  31. sec_re = curs.fetchall()
  32. if sec_re and sec_re[0][0] != '':
  33. data += recaptcha[0][0] + '<hr>'
  34. return data
  35. def captcha_post(re_data, num = 1):
  36. if num == 1:
  37. if custom()[2] == 0 and captcha_get() != '':
  38. curs.execute('select data from other where name = "sec_re"')
  39. sec_re = curs.fetchall()
  40. if sec_re and sec_re[0][0] != '':
  41. data = requests.get('https://www.google.com/recaptcha/api/siteverify', params = { 'secret' : sec_re, 'response' : re_data })
  42. if not data:
  43. return 0
  44. else:
  45. json_data = data.json()
  46. if data.status_code == 200 and json_data['success'] == True:
  47. return 0
  48. else:
  49. return 1
  50. else:
  51. return 0
  52. else:
  53. return 0
  54. else:
  55. pass
  56. def load_lang(data):
  57. global lang
  58. try:
  59. if lang:
  60. pass
  61. except:
  62. curs.execute("select data from other where name = 'language'")
  63. rep_data = curs.fetchall()
  64. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), 'rt', encoding='utf-8').read()
  65. lang = json.loads(json_data)
  66. if data == 'please_all':
  67. return lang
  68. else:
  69. if data in lang:
  70. return lang[data]
  71. else:
  72. return else_lang[data]
  73. def ip_or_user(data):
  74. if re.search('(\.|:)', data):
  75. return 1
  76. else:
  77. return 0
  78. def namumark_re(name = '', data = '', set_num = 0):
  79. return namumark(name, data, set_num, load_lang('please_all'))
  80. def edit_help_button():
  81. # https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  82. '''
  83. <script>
  84. function insertAtCursor(myField, myValue) {
  85. if (document.selection) {
  86. document.getElementById(myField).focus();
  87. sel = document.selection.createRange();
  88. sel.text = myValue;
  89. } else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') {
  90. var startPos = document.getElementById(myField).selectionStart;
  91. var endPos = document.getElementById(myField).selectionEnd;
  92. document.getElementById(myField).value = document.getElementById(myField).value.substring(0, startPos) + myValue + document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
  93. } else {
  94. document.getElementById(myField).value += myValue;
  95. }
  96. }
  97. </script>
  98. '''
  99. insert_list = [['[[]]', '링크'], ['[()]', '매크로'], ['{{{#!}}}', '중괄호']]
  100. '<a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'B\');">(A)</a>'
  101. return ['', '']
  102. def ip_warring():
  103. if custom()[2] == 0:
  104. curs.execute('select data from other where name = "no_login_warring"')
  105. data = curs.fetchall()
  106. if data and data[0][0] != '':
  107. text_data = '<span>' + data[0][0] + '</span><hr>'
  108. else:
  109. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr>'
  110. else:
  111. text_data = ''
  112. return text_data
  113. def skin_check():
  114. skin = './views/acme/'
  115. try:
  116. curs.execute('select skin from user where id = ?', [ip_check()])
  117. skin_exist = curs.fetchall()
  118. if skin_exist and skin_exist[0][0] != '':
  119. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  120. skin = './views/' + skin_exist[0][0] + '/'
  121. else:
  122. curs.execute('select data from other where name = "skin"')
  123. skin_exist = curs.fetchall()
  124. if skin_exist:
  125. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  126. skin = './views/' + skin_exist[0][0] + '/'
  127. except:
  128. pass
  129. return skin + 'index.html'
  130. def next_fix(link, num, page, end = 50):
  131. list_data = ''
  132. if num == 1:
  133. if len(page) == end:
  134. list_data += '<hr><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  135. elif len(page) != end:
  136. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  137. else:
  138. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  139. return list_data
  140. def other2(data):
  141. return data + ['Deleted']
  142. def wiki_set(num = 1):
  143. if num == 1:
  144. data_list = []
  145. curs.execute('select data from other where name = ?', ['name'])
  146. db_data = curs.fetchall()
  147. if db_data and db_data[0][0] != '':
  148. data_list += [db_data[0][0]]
  149. else:
  150. data_list += ['Wiki']
  151. curs.execute('select data from other where name = "license"')
  152. db_data = curs.fetchall()
  153. if db_data and db_data[0][0] != '':
  154. data_list += [db_data[0][0]]
  155. else:
  156. data_list += ['CC 0']
  157. data_list += ['', '']
  158. curs.execute('select data from other where name = "logo"')
  159. db_data = curs.fetchall()
  160. if db_data and db_data[0][0] != '':
  161. data_list += [db_data[0][0]]
  162. else:
  163. data_list += [data_list[0]]
  164. curs.execute("select data from other where name = 'head'")
  165. db_data = curs.fetchall()
  166. if db_data and db_data[0][0] != '':
  167. data_list += [db_data[0][0]]
  168. else:
  169. data_list += ['']
  170. return data_list
  171. if num == 2:
  172. var_data = 'FrontPage'
  173. curs.execute('select data from other where name = "frontpage"')
  174. elif num == 3:
  175. var_data = '2'
  176. curs.execute('select data from other where name = "upload"')
  177. db_data = curs.fetchall()
  178. if db_data and db_data[0][0] != '':
  179. return db_data[0][0]
  180. else:
  181. return var_data
  182. def diff(seqm):
  183. output = []
  184. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  185. if opcode == 'equal':
  186. output += [seqm.a[a0:a1]]
  187. elif opcode == 'insert':
  188. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  189. elif opcode == 'delete':
  190. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  191. elif opcode == 'replace':
  192. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  193. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  194. return ''.join(output)
  195. def admin_check(num, what):
  196. ip = ip_check()
  197. curs.execute("select acl from user where id = ?", [ip])
  198. user = curs.fetchall()
  199. if user:
  200. reset = 0
  201. while 1:
  202. if num == 1 and reset == 0:
  203. check = 'ban'
  204. elif num == 2 and reset == 0:
  205. check = 'mdel'
  206. elif num == 3 and reset == 0:
  207. check = 'toron'
  208. elif num == 4 and reset == 0:
  209. check = 'check'
  210. elif num == 5 and reset == 0:
  211. check = 'acl'
  212. elif num == 6 and reset == 0:
  213. check = 'hidel'
  214. elif num == 7 and reset == 0:
  215. check = 'give'
  216. else:
  217. check = 'owner'
  218. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  219. if curs.fetchall():
  220. if what:
  221. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  222. conn.commit()
  223. return 1
  224. else:
  225. if reset == 0:
  226. reset = 1
  227. else:
  228. break
  229. def ip_pas(raw_ip):
  230. hide = 0
  231. if re.search("(\.|:)", raw_ip):
  232. if not re.search("^" + load_lang('tool') + ":", raw_ip):
  233. curs.execute("select data from other where name = 'ip_view'")
  234. data = curs.fetchall()
  235. if data and data[0][0] != '':
  236. ip = '<span style="font-size: 75%;">' + hashlib.md5(bytes(raw_ip, 'utf-8')).hexdigest() + '</span>'
  237. if not admin_check('ban', None):
  238. hide = 1
  239. else:
  240. ip = raw_ip
  241. else:
  242. ip = raw_ip
  243. hide = 1
  244. else:
  245. curs.execute("select title from data where title = ?", [load_lang('user') + ':' + raw_ip])
  246. if curs.fetchall():
  247. ip = '<a href="/w/' + url_pas(load_lang('user') + ':' + raw_ip) + '">' + raw_ip + '</a>'
  248. else:
  249. ip = '<a id="not_thing" href="/w/' + url_pas(load_lang('user') + ':' + raw_ip) + '">' + raw_ip + '</a>'
  250. if hide == 0:
  251. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(' + load_lang('record') + ')</a>'
  252. return ip
  253. def custom():
  254. if 'MyMaiToNight' in flask.session:
  255. user_head = flask.session['MyMaiToNight']
  256. else:
  257. user_head = ''
  258. if 'Now' in flask.session and flask.session['Now'] == 1:
  259. curs.execute('select name from alarm where name = ? limit 1', [ip_check()])
  260. if curs.fetchall():
  261. user_icon = 2
  262. else:
  263. user_icon = 1
  264. else:
  265. user_icon = 0
  266. if user_icon != 0:
  267. curs.execute('select email from user where id = ?', [ip_check()])
  268. data = curs.fetchall()
  269. if data:
  270. email = data[0][0]
  271. else:
  272. email = ''
  273. else:
  274. email = ''
  275. if user_icon != 0:
  276. user_name = ip_check()
  277. else:
  278. user_name = load_lang('user')
  279. return ['', '', user_icon, user_head, email, user_name]
  280. def acl_check(name):
  281. ip = ip_check()
  282. if ban_check() == 1:
  283. return 1
  284. acl_c = re.search("^" + load_lang('user') + ":([^/]*)", name)
  285. if acl_c:
  286. acl_n = acl_c.groups()
  287. if admin_check(5, None) == 1:
  288. return 0
  289. curs.execute("select dec from acl where title = ?", [load_lang('user') + ':' + acl_n[0]])
  290. acl_data = curs.fetchall()
  291. if acl_data:
  292. if acl_data[0][0] == 'all':
  293. return 0
  294. if acl_data[0][0] == 'user' and not re.search("(\.|:)", ip):
  295. return 0
  296. if ip != acl_n[0] or re.search("(\.|:)", ip):
  297. return 1
  298. if ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0]):
  299. return 0
  300. else:
  301. return 1
  302. file_c = re.search("^" + load_lang('file') + ":(.*)", name)
  303. if file_c and admin_check(5, 'edit (' + name + ')') != 1:
  304. return 1
  305. curs.execute("select acl from user where id = ?", [ip])
  306. user_data = curs.fetchall()
  307. curs.execute("select dec from acl where title = ?", [name])
  308. acl_data = curs.fetchall()
  309. if acl_data:
  310. if acl_data[0][0] == 'user':
  311. if not user_data:
  312. return 1
  313. if acl_data[0][0] == 'admin':
  314. if not user_data:
  315. return 1
  316. if not admin_check(5, 'edit (' + name + ')') == 1:
  317. return 1
  318. curs.execute('select data from other where name = "edit"')
  319. set_data = curs.fetchall()
  320. if set_data:
  321. if set_data[0][0] == 'user':
  322. if not user_data:
  323. return 1
  324. if set_data[0][0] == 'admin':
  325. if not user_data:
  326. return 1
  327. if not admin_check(5, None) == 1:
  328. return 1
  329. return 0
  330. def ban_check():
  331. ip = ip_check()
  332. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  333. if band:
  334. band_it = band.groups()[0]
  335. else:
  336. band_it = 'Not'
  337. curs.execute("select block from ban where block = ?", [band_it])
  338. band_d = curs.fetchall()
  339. curs.execute("select block from ban where block = ?", [ip])
  340. ban_d = curs.fetchall()
  341. if band_d or ban_d:
  342. return 1
  343. return 0
  344. def topic_check(name, sub):
  345. ip = ip_check()
  346. if ban_check() == 1:
  347. return 1
  348. curs.execute("select acl from user where id = ?", [ip])
  349. user_data = curs.fetchall()
  350. curs.execute("select dis from acl where title = ?", [name])
  351. acl_data = curs.fetchall()
  352. if acl_data:
  353. if acl_data[0][0] == 'user':
  354. if not user_data:
  355. return 1
  356. if acl_data[0][0] == 'admin':
  357. if not user_data:
  358. return 1
  359. if not admin_check(3, 'topic (' + name + ')') == 1:
  360. return 1
  361. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  362. if curs.fetchall():
  363. if not admin_check(3, 'topic (' + name + ')') == 1:
  364. return 1
  365. return 0
  366. def ban_insert(name, end, why, login, blocker):
  367. time = get_time()
  368. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  369. band = 'O'
  370. else:
  371. band = ''
  372. curs.execute("select block from ban where block = ?", [name])
  373. if curs.fetchall():
  374. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, load_lang('release'), time, blocker, '', band])
  375. curs.execute("delete from ban where block = ?", [name])
  376. else:
  377. if login != '':
  378. login = 'O'
  379. else:
  380. login = ''
  381. if end != '':
  382. end += ' 00:00:00'
  383. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, end, time, blocker, why, band])
  384. curs.execute("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)", [name, end, why, band, login])
  385. conn.commit()
  386. def rd_plus(title, sub, date):
  387. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  388. if curs.fetchall():
  389. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  390. else:
  391. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  392. def history_plus(title, data, date, ip, send, leng):
  393. curs.execute("select id from history where title = ? order by id + 0 desc limit 1", [title])
  394. id_data = curs.fetchall()
  395. if id_data:
  396. curs.execute("insert into history (id, title, data, date, ip, send, leng) values (?, ?, ?, ?, ?, ?, ?)", [str(int(id_data[0][0]) + 1), title, data, date, ip, send, leng])
  397. else:
  398. curs.execute("insert into history (id, title, data, date, ip, send, leng) values ('1', ?, ?, ?, ?, ?, ?)", [title, data, date, ip, send + ' (' + load_lang('new') + ' ' + load_lang('document') + ')', leng])
  399. def leng_check(first, second):
  400. if first < second:
  401. all_plus = '+' + str(second - first)
  402. elif second < first:
  403. all_plus = '-' + str(first - second)
  404. else:
  405. all_plus = '0'
  406. return all_plus
  407. def redirect(data):
  408. return '<meta http-equiv="refresh" content="0; url=' + data + '">'
  409. def re_error(data):
  410. if data == '/ban':
  411. ip = ip_check()
  412. end = '<li>' + load_lang('why') + ' : ' + load_lang('authority_error') + '</li>'
  413. if ban_check() == 1:
  414. curs.execute("select end, why from ban where block = ?", [ip])
  415. end_data = curs.fetchall()
  416. if not end_data:
  417. match = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  418. if match:
  419. curs.execute("select end, why from ban where block = ?", [match.groups()[0]])
  420. end_data = curs.fetchall()
  421. if end_data:
  422. end = '<li>' + load_lang('state') + ' : '
  423. if end_data[0][0]:
  424. now = int(re.sub('(\-| |:)', '', get_time()))
  425. day = int(re.sub('(\-| |:)', '', end_data[0][0]))
  426. if now >= day:
  427. curs.execute("delete from ban where block = ?", [ip])
  428. conn.commit()
  429. end += 'Re Try.'
  430. else:
  431. end += load_lang('why') + ' : ' + end_data[0][0]
  432. else:
  433. end += load_lang('why') + ' : ' + load_lang('limitless')
  434. end += '</li>'
  435. if end_data[0][1] != '':
  436. end += '<li>' + load_lang('why') + ' : ' + end_data[0][1] + '</li>'
  437. return css_html_js_minify.html_minify(flask.render_template(skin_check(),
  438. imp = ['Error', wiki_set(1), custom(), other2([0, 0])],
  439. data = '<h2>Error</h2><ul>' + end + '</ul>',
  440. menu = 0
  441. ))
  442. else:
  443. error_data = re.search('\/error\/([0-9]+)', data)
  444. if error_data:
  445. num = int(error_data.groups()[0])
  446. if num == 1:
  447. data = load_lang('no_login_error')
  448. elif num == 2:
  449. data = load_lang('no_exist_user_error')
  450. elif num == 3:
  451. data = load_lang('authority_error')
  452. elif num == 4:
  453. data = load_lang('no_admin_block_error')
  454. elif num == 6:
  455. data = load_lang('same_id_exist_error')
  456. elif num == 7:
  457. data = load_lang('long_id_error')
  458. elif num == 8:
  459. data = load_lang('id_char_error')
  460. elif num == 9:
  461. data = load_lang('file_exist_error')
  462. elif num == 10:
  463. data = load_lang('password_error')
  464. elif num == 13:
  465. data = load_lang('recaptcha_error')
  466. elif num == 14:
  467. data = load_lang('file_extension_error')
  468. elif num == 15:
  469. data = load_lang('edit_record_error')
  470. elif num == 16:
  471. data = load_lang('same_file_error')
  472. elif num == 17:
  473. data = load_lang('file_capacity_error') + ' ' + wiki_set(3)
  474. elif num == 19:
  475. data = load_lang('decument_exist_error')
  476. elif num == 20:
  477. data = load_lang('password_diffrent_error')
  478. elif num == 21:
  479. data = load_lang('edit_filter_error')
  480. elif num == 22:
  481. data = load_lang('file_name_error')
  482. else:
  483. data = '???'
  484. return css_html_js_minify.html_minify(flask.render_template(skin_check(),
  485. imp = ['Error', wiki_set(1), custom(), other2([0, 0])],
  486. data = '<h2>Error</h2><ul><li>' + data + '</li></ul>',
  487. menu = 0
  488. ))
  489. else:
  490. return redirect('/')