2
0

func.py 19 KB

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