func.py 19 KB

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