2
0

func.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 edit_help_button():
  74. # https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  75. '''<script>
  76. function insertAtCursor(myField, myValue) {
  77. if (document.selection) {
  78. document.getElementById(myField).focus();
  79. sel = document.selection.createRange();
  80. sel.text = myValue;
  81. } else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') {
  82. var startPos = document.getElementById(myField).selectionStart;
  83. var endPos = document.getElementById(myField).selectionEnd;
  84. document.getElementById(myField).value = document.getElementById(myField).value.substring(0, startPos) + myValue + document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
  85. } else {
  86. document.getElementById(myField).value += myValue;
  87. }
  88. }
  89. </script>
  90. '''
  91. '<a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'[[]]\');">(링크)</a> <a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'[macro()]\');">(매크로)</a> <a href="javascript:void(0);" onclick="insertAtCursor(\'content\', \'{{{#! }}}\');">(중괄호)</a><hr>'
  92. return ['', '']
  93. def ip_warring():
  94. if custom()[2] == 0:
  95. curs.execute('select data from other where name = "no_login_warring"')
  96. data = curs.fetchall()
  97. if data and data[0][0] != '':
  98. text_data = '<span>' + data[0][0] + '</span><hr>'
  99. else:
  100. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr>'
  101. else:
  102. text_data = ''
  103. return text_data
  104. def skin_check():
  105. skin = './views/acme/'
  106. try:
  107. curs.execute('select skin from user where id = ?', [ip_check()])
  108. skin_exist = curs.fetchall()
  109. if skin_exist and skin_exist[0][0] != '':
  110. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  111. skin = './views/' + skin_exist[0][0] + '/'
  112. else:
  113. curs.execute('select data from other where name = "skin"')
  114. skin_exist = curs.fetchall()
  115. if skin_exist:
  116. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  117. skin = './views/' + skin_exist[0][0] + '/'
  118. except:
  119. pass
  120. return skin + 'index.html'
  121. def next_fix(link, num, page, end = 50):
  122. list_data = ''
  123. if num == 1:
  124. if len(page) == end:
  125. list_data += '<hr><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  126. elif len(page) != end:
  127. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  128. else:
  129. list_data += '<hr><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  130. return list_data
  131. def other2(origin):
  132. return origin + ['Deleted']
  133. def wiki_set(num):
  134. if num == 1:
  135. data_list = []
  136. curs.execute('select data from other where name = ?', ['name'])
  137. db_data = curs.fetchall()
  138. if db_data and db_data[0][0] != '':
  139. data_list += [db_data[0][0]]
  140. else:
  141. data_list += ['Wiki']
  142. curs.execute('select data from other where name = "license"')
  143. db_data = curs.fetchall()
  144. if db_data and db_data[0][0] != '':
  145. data_list += [db_data[0][0]]
  146. else:
  147. data_list += ['CC 0']
  148. data_list += ['', '']
  149. curs.execute('select data from other where name = "logo"')
  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 += [data_list[0]]
  155. curs.execute("select data from other where name = 'head'")
  156. db_data = curs.fetchall()
  157. if db_data and db_data[0][0] != '':
  158. data_list += [db_data[0][0]]
  159. else:
  160. data_list += ['']
  161. return data_list
  162. if num == 2:
  163. var_data = 'FrontPage'
  164. curs.execute('select data from other where name = "frontpage"')
  165. elif num == 3:
  166. var_data = '2'
  167. curs.execute('select data from other where name = "upload"')
  168. db_data = curs.fetchall()
  169. if db_data and db_data[0][0] != '':
  170. return db_data[0][0]
  171. else:
  172. return var_data
  173. def diff(seqm):
  174. output = []
  175. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  176. if opcode == 'equal':
  177. output += [seqm.a[a0:a1]]
  178. elif opcode == 'insert':
  179. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  180. elif opcode == 'delete':
  181. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  182. elif opcode == 'replace':
  183. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  184. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  185. return ''.join(output)
  186. def admin_check(num, what):
  187. ip = ip_check()
  188. curs.execute("select acl from user where id = ?", [ip])
  189. user = curs.fetchall()
  190. if user:
  191. reset = 0
  192. while 1:
  193. if num == 1 and reset == 0:
  194. check = 'ban'
  195. elif num == 2 and reset == 0:
  196. check = 'mdel'
  197. elif num == 3 and reset == 0:
  198. check = 'toron'
  199. elif num == 4 and reset == 0:
  200. check = 'check'
  201. elif num == 5 and reset == 0:
  202. check = 'acl'
  203. elif num == 6 and reset == 0:
  204. check = 'hidel'
  205. elif num == 7 and reset == 0:
  206. check = 'give'
  207. else:
  208. check = 'owner'
  209. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  210. if curs.fetchall():
  211. if what:
  212. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  213. conn.commit()
  214. return 1
  215. else:
  216. if reset == 0:
  217. reset = 1
  218. else:
  219. break
  220. def ip_pas(raw_ip):
  221. hide = 0
  222. if re.search("(\.|:)", raw_ip):
  223. if not re.search("^" + load_lang('tool') + ":", raw_ip):
  224. curs.execute("select data from other where name = 'ip_view'")
  225. data = curs.fetchall()
  226. if data and data[0][0] != '':
  227. ip = '<span style="font-size: 75%;">' + hashlib.md5(bytes(raw_ip, 'utf-8')).hexdigest() + '</span>'
  228. if not admin_check('ban', None):
  229. hide = 1
  230. else:
  231. ip = raw_ip
  232. else:
  233. ip = raw_ip
  234. hide = 1
  235. else:
  236. curs.execute("select title from data where title = ?", ['' + load_lang('user') + ':' + raw_ip])
  237. if curs.fetchall():
  238. ip = '<a href="/w/' + url_pas('' + load_lang('user') + ':' + raw_ip) + '">' + raw_ip + '</a>'
  239. else:
  240. ip = '<a id="not_thing" href="/w/' + url_pas('' + load_lang('user') + ':' + raw_ip) + '">' + raw_ip + '</a>'
  241. if hide == 0:
  242. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(' + load_lang('record') + ')</a>'
  243. return ip
  244. def custom():
  245. if 'MyMaiToNight' in flask.session:
  246. user_head = flask.session['MyMaiToNight']
  247. else:
  248. user_head = ''
  249. if 'Now' in flask.session and flask.session['Now'] == 1:
  250. curs.execute('select name from alarm where name = ? limit 1', [ip_check()])
  251. if curs.fetchall():
  252. user_icon = 2
  253. else:
  254. user_icon = 1
  255. else:
  256. user_icon = 0
  257. if user_icon != 0:
  258. curs.execute('select email from user where id = ?', [ip_check()])
  259. data = curs.fetchall()
  260. if data:
  261. email = data[0][0]
  262. else:
  263. email = ''
  264. else:
  265. email = ''
  266. if user_icon != 0:
  267. user_name = ip_check()
  268. else:
  269. user_name = '' + load_lang('user') + ''
  270. return ['', '', user_icon, user_head, email, user_name]
  271. def acl_check(name):
  272. ip = ip_check()
  273. if ban_check() == 1:
  274. return 1
  275. acl_c = re.search("^" + load_lang('user') + ":([^/]*)", name)
  276. if acl_c:
  277. acl_n = acl_c.groups()
  278. if admin_check(5, None) == 1:
  279. return 0
  280. curs.execute("select dec from acl where title = ?", ['' + load_lang('user') + ':' + acl_n[0]])
  281. acl_data = curs.fetchall()
  282. if acl_data:
  283. if acl_data[0][0] == 'all':
  284. return 0
  285. if acl_data[0][0] == 'user' and not re.search("(\.|:)", ip):
  286. return 0
  287. if ip != acl_n[0] or re.search("(\.|:)", ip):
  288. return 1
  289. if ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0]):
  290. return 0
  291. else:
  292. return 1
  293. file_c = re.search("^" + load_lang('file') + ":(.*)", name)
  294. if file_c and admin_check(5, 'edit (' + name + ')') != 1:
  295. return 1
  296. curs.execute("select acl from user where id = ?", [ip])
  297. user_data = curs.fetchall()
  298. curs.execute("select dec from acl where title = ?", [name])
  299. acl_data = curs.fetchall()
  300. if acl_data:
  301. if acl_data[0][0] == 'user':
  302. if not user_data:
  303. return 1
  304. if acl_data[0][0] == 'admin':
  305. if not user_data:
  306. return 1
  307. if not admin_check(5, 'edit (' + name + ')') == 1:
  308. return 1
  309. curs.execute('select data from other where name = "edit"')
  310. set_data = curs.fetchall()
  311. if set_data:
  312. if set_data[0][0] == 'user':
  313. if not user_data:
  314. return 1
  315. if set_data[0][0] == 'admin':
  316. if not user_data:
  317. return 1
  318. if not admin_check(5, None) == 1:
  319. return 1
  320. return 0
  321. def ban_check():
  322. ip = ip_check()
  323. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  324. if band:
  325. band_it = band.groups()[0]
  326. else:
  327. band_it = 'Not'
  328. curs.execute("select block from ban where block = ?", [band_it])
  329. band_d = curs.fetchall()
  330. curs.execute("select block from ban where block = ?", [ip])
  331. ban_d = curs.fetchall()
  332. if band_d or ban_d:
  333. return 1
  334. return 0
  335. def topic_check(name, sub):
  336. ip = ip_check()
  337. if ban_check() == 1:
  338. return 1
  339. curs.execute("select acl from user where id = ?", [ip])
  340. user_data = curs.fetchall()
  341. curs.execute("select dis from acl where title = ?", [name])
  342. acl_data = curs.fetchall()
  343. if acl_data:
  344. if acl_data[0][0] == 'user':
  345. if not user_data:
  346. return 1
  347. if acl_data[0][0] == 'admin':
  348. if not user_data:
  349. return 1
  350. if not admin_check(3, 'topic (' + name + ')') == 1:
  351. return 1
  352. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  353. if curs.fetchall():
  354. if not admin_check(3, 'topic (' + name + ')') == 1:
  355. return 1
  356. return 0
  357. def ban_insert(name, end, why, login, blocker):
  358. time = get_time()
  359. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  360. band = 'O'
  361. else:
  362. band = ''
  363. curs.execute("select block from ban where block = ?", [name])
  364. if curs.fetchall():
  365. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, '' + load_lang('release') + '', time, blocker, '', band])
  366. curs.execute("delete from ban where block = ?", [name])
  367. else:
  368. if login != '':
  369. login = 'O'
  370. else:
  371. login = ''
  372. if end != '':
  373. end += ' 00:00:00'
  374. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, end, time, blocker, why, band])
  375. curs.execute("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)", [name, end, why, band, login])
  376. conn.commit()
  377. def rd_plus(title, sub, date):
  378. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  379. if curs.fetchall():
  380. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  381. else:
  382. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  383. def history_plus(title, data, date, ip, send, leng):
  384. curs.execute("select id from history where title = ? order by id + 0 desc limit 1", [title])
  385. id_data = curs.fetchall()
  386. if id_data:
  387. 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])
  388. else:
  389. 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])
  390. def leng_check(first, second):
  391. if first < second:
  392. all_plus = '+' + str(second - first)
  393. elif second < first:
  394. all_plus = '-' + str(first - second)
  395. else:
  396. all_plus = '0'
  397. return all_plus
  398. def redirect(data):
  399. return '<meta http-equiv="refresh" content="0; url=' + data + '">'
  400. def re_error(data):
  401. if data == '/ban':
  402. ip = ip_check()
  403. end = '<li>Why : 권한이 맞지 않는 상태 입니다.</li>'
  404. if ban_check() == 1:
  405. curs.execute("select end, why from ban where block = ?", [ip])
  406. end_data = curs.fetchall()
  407. if not end_data:
  408. match = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  409. if match:
  410. curs.execute("select end, why from ban where block = ?", [match.groups()[0]])
  411. end_data = curs.fetchall()
  412. if end_data:
  413. end = '<li>Info : '
  414. if end_data[0][0]:
  415. now = int(re.sub('(\-| |:)', '', get_time()))
  416. day = int(re.sub('(\-| |:)', '', end_data[0][0]))
  417. if now >= day:
  418. curs.execute("delete from ban where block = ?", [ip])
  419. conn.commit()
  420. end += 'Re Try.'
  421. else:
  422. end += 'Ban : ' + end_data[0][0]
  423. else:
  424. end += 'Ban : No End'
  425. end += '</li>'
  426. if end_data[0][1] != '':
  427. end += '<li>Why : ' + end_data[0][1] + '</li>'
  428. return css_html_js_minify.html_minify(flask.render_template(skin_check(),
  429. imp = ['Authority Error', wiki_set(1), custom(), other2([0, 0])],
  430. data = '<h2>Info</h2><ul>' + end + '</ul>',
  431. menu = 0
  432. ))
  433. error_data = re.search('\/error\/([0-9]+)', data)
  434. if error_data:
  435. num = int(error_data.groups()[0])
  436. if num == 1:
  437. title = 'Authority Error'
  438. data = '비 로그인 상태 입니다.'
  439. elif num == 2:
  440. title = 'Authority Error'
  441. data = '이 계정이 없습니다.'
  442. elif num == 3:
  443. title = 'Authority Error'
  444. data = '권한이 모자랍니다.'
  445. elif num == 4:
  446. title = 'Authority Error'
  447. data = '관리자는 차단, 검사 할 수 없습니다.'
  448. elif num == 5:
  449. title = 'User Error'
  450. data = '그런 계정이 없습니다.'
  451. elif num == 6:
  452. title = 'Register Error'
  453. data = '동일한 아이디의 사용자가 있습니다.'
  454. elif num == 7:
  455. title = 'Register Error'
  456. data = '아이디는 20글자보다 짧아야 합니다.'
  457. elif num == 8:
  458. title = 'Register Error'
  459. data = '아이디에는 한글과 알파벳과 공백만 허용 됩니다.'
  460. elif num == 9:
  461. title = 'Upload Error'
  462. data = '파일이 없습니다.'
  463. elif num == 10:
  464. title = 'PassWord Error'
  465. data = '비밀번호가 다릅니다.'
  466. elif num == 11:
  467. title = 'Login Error'
  468. data = '이미 로그인 되어 있습니다.'
  469. elif num == 13:
  470. title = 'reCAPTCHA Error'
  471. data = '리캡차를 통과하세요.'
  472. elif num == 14:
  473. title = 'Upload Error'
  474. data = 'jpg, gif, jpeg, png, webp만 가능 합니다.'
  475. elif num == 15:
  476. title = 'Edit Error'
  477. data = '편집 기록은 500자를 넘을 수 없습니다.'
  478. elif num == 16:
  479. title = 'Upload Error'
  480. data = '동일한 이름의 파일이 있습니다.'
  481. elif num == 17:
  482. title = 'Upload Error'
  483. data = '파일 용량은 ' + wiki_set(3) + 'MB를 넘길 수 없습니다.'
  484. elif num == 18:
  485. title = 'Edit Error'
  486. data = '내용이 원래 문서와 동일 합니다.'
  487. elif num == 19:
  488. title = 'Move Error'
  489. data = '이동 하려는 곳에 문서가 이미 있습니다.'
  490. elif num == 20:
  491. title = 'PassWord Error'
  492. data = '재 확인이랑 비밀번호가 다릅니다.'
  493. elif num == 21:
  494. title = 'Edit Error'
  495. data = '편집 필터에 의해 검열 되었습니다.'
  496. elif num == 22:
  497. title = 'Upload Error'
  498. data = '파일 이름은 알파벳, 한글, 띄어쓰기, 언더바, 빼기표만 허용 됩니다.'
  499. else:
  500. title = 'Error'
  501. data = '???'
  502. if title:
  503. return css_html_js_minify.html_minify(flask.render_template(skin_check(),
  504. imp = [title, wiki_set(1), custom(), other2([0, 0])],
  505. data = '<h2>Error</h2><ul><li>' + data + '</li></ul>',
  506. menu = 0
  507. ))
  508. else:
  509. return redirect('/')
  510. else:
  511. return redirect('/')