func.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. # 모듈들 불러옴
  2. from css_html_js_minify import html_minify, js_minify, css_minify
  3. from flask import session, render_template
  4. from urllib import parse
  5. import json
  6. import sqlite3
  7. import hashlib
  8. import requests
  9. import re
  10. import html
  11. import time
  12. import os
  13. # 일부 툴 불러옴
  14. from set_mark.tool import get_time
  15. from set_mark.tool import ip_check
  16. from set_mark.tool import url_pas
  17. from set_mark.tool import sha224
  18. # 나무마크 불러옴
  19. from mark import *
  20. def captcha_get(conn):
  21. curs = conn.cursor()
  22. data = ''
  23. if custom(conn)[2] == 0:
  24. curs.execute('select data from other where name = "recaptcha"')
  25. recaptcha = curs.fetchall()
  26. if recaptcha and recaptcha[0][0] != '':
  27. curs.execute('select data from other where name = "sec_re"')
  28. sec_re = curs.fetchall()
  29. if sec_re and sec_re[0][0] != '':
  30. data += recaptcha[0][0] + '<hr>'
  31. return data
  32. def captcha_post(test, conn, num = 1):
  33. curs = conn.cursor()
  34. if num == 1:
  35. if custom(conn)[2] == 0 and captcha_get(conn) != '':
  36. curs.execute('select data from other where name = "sec_re"')
  37. sec_re = curs.fetchall()
  38. if sec_re and sec_re[0][0] != '':
  39. data = requests.get('https://www.google.com/recaptcha/api/siteverify', params = { 'secret' : sec_re, 'response' : test })
  40. if not data:
  41. return 0
  42. else:
  43. json_data = data.json()
  44. if data.status_code == 200 and json_data['success'] == True:
  45. return 0
  46. else:
  47. return 1
  48. else:
  49. return 0
  50. else:
  51. return 0
  52. else:
  53. pass
  54. def ip_warring(conn):
  55. curs = conn.cursor()
  56. if custom(conn)[2] == 0:
  57. curs.execute('select data from other where name = "no_login_warring"')
  58. data = curs.fetchall()
  59. if data and data[0][0] != '':
  60. text_data = '<span>' + data[0][0] + '</span><hr>'
  61. else:
  62. text_data = '<span>비 로그인 상태입니다. 비 로그인으로 진행 시 아이피가 기록됩니다.</span><hr>'
  63. else:
  64. text_data = ''
  65. return text_data
  66. def skin_check(conn):
  67. curs = conn.cursor()
  68. skin = './views/acme/'
  69. try:
  70. curs.execute('select data from other where name = "skin"')
  71. skin_exist = curs.fetchall()
  72. if skin_exist:
  73. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  74. skin = './views/' + skin_exist[0][0] + '/'
  75. except:
  76. pass
  77. return skin + 'index.html'
  78. def next_fix(link, num, page, end = 50):
  79. list_data = ''
  80. if num == 1:
  81. if len(page) == end:
  82. list_data += '<hr><a href="' + link + str(num + 1) + '">(이후)</a>'
  83. elif len(page) != end:
  84. list_data += '<hr><a href="' + link + str(num - 1) + '">(이전)</a>'
  85. else:
  86. list_data += '<hr><a href="' + link + str(num - 1) + '">(이전)</a> <a href="' + link + str(num + 1) + '">(이후)</a>'
  87. return list_data
  88. def other2(origin):
  89. return origin + ['제거 되었음. 스킨 업데이트 필요함.']
  90. def wiki_set(conn, num):
  91. curs = conn.cursor()
  92. if num == 1:
  93. data_list = []
  94. curs.execute('select data from other where name = ?', ['name'])
  95. db_data = curs.fetchall()
  96. if db_data and db_data[0][0] != '':
  97. data_list += [db_data[0][0]]
  98. else:
  99. data_list += ['무명위키']
  100. curs.execute('select data from other where name = "license"')
  101. db_data = curs.fetchall()
  102. if db_data and db_data[0][0] != '':
  103. data_list += [db_data[0][0]]
  104. else:
  105. data_list += ['CC 0']
  106. data_list += ['', '']
  107. curs.execute('select data from other where name = "logo"')
  108. db_data = curs.fetchall()
  109. if db_data and db_data[0][0] != '':
  110. data_list += [db_data[0][0]]
  111. else:
  112. data_list += [data_list[0]]
  113. curs.execute("select data from other where name = 'head'")
  114. db_data = curs.fetchall()
  115. if db_data and db_data[0][0] != '':
  116. data_list += [db_data[0][0]]
  117. else:
  118. data_list += ['']
  119. return data_list
  120. if num == 2:
  121. var_data = '위키:대문'
  122. curs.execute('select data from other where name = "frontpage"')
  123. elif num == 3:
  124. var_data = '2'
  125. curs.execute('select data from other where name = "upload"')
  126. db_data = curs.fetchall()
  127. if db_data and db_data[0][0] != '':
  128. return db_data[0][0]
  129. else:
  130. return var_data
  131. def diff(seqm):
  132. output = []
  133. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  134. if opcode == 'equal':
  135. output += [seqm.a[a0:a1]]
  136. elif opcode == 'insert':
  137. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  138. elif opcode == 'delete':
  139. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  140. elif opcode == 'replace':
  141. output += ["<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>"]
  142. output += ["<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>"]
  143. return ''.join(output)
  144. def admin_check(conn, num, what):
  145. ip = ip_check()
  146. curs = conn.cursor()
  147. curs.execute("select acl from user where id = ?", [ip])
  148. user = curs.fetchall()
  149. if user:
  150. reset = 0
  151. while 1:
  152. if num == 1 and reset == 0:
  153. check = 'ban'
  154. elif num == 2 and reset == 0:
  155. check = 'mdel'
  156. elif num == 3 and reset == 0:
  157. check = 'toron'
  158. elif num == 4 and reset == 0:
  159. check = 'check'
  160. elif num == 5 and reset == 0:
  161. check = 'acl'
  162. elif num == 6 and reset == 0:
  163. check = 'hidel'
  164. elif num == 7 and reset == 0:
  165. check = 'give'
  166. else:
  167. check = 'owner'
  168. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  169. if curs.fetchall():
  170. if what:
  171. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  172. conn.commit()
  173. return 1
  174. else:
  175. if reset == 0:
  176. reset = 1
  177. else:
  178. break
  179. def ip_pas(conn, raw_ip):
  180. hide = 0
  181. curs = conn.cursor()
  182. if re.search("(\.|:)", raw_ip):
  183. if not re.search("^도구:", raw_ip):
  184. curs.execute("select data from other where name = 'ip_view'")
  185. data = curs.fetchall()
  186. if data and data[0][0] != '':
  187. ip = '<span style="font-size: 75%;">' + hashlib.md5(bytes(raw_ip, 'utf-8')).hexdigest() + '</span>'
  188. if not admin_check(conn, 'ban', None):
  189. hide = 1
  190. else:
  191. ip = raw_ip
  192. else:
  193. ip = raw_ip
  194. hide = 1
  195. else:
  196. curs.execute("select title from data where title = ?", ['사용자:' + raw_ip])
  197. if curs.fetchall():
  198. ip = '<a href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  199. else:
  200. ip = '<a id="not_thing" href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  201. if hide == 0:
  202. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(기록)</a>'
  203. return ip
  204. def custom(conn):
  205. curs = conn.cursor()
  206. if 'MyMaiToNight' in session:
  207. user_head = session['MyMaiToNight']
  208. else:
  209. user_head = ''
  210. if 'Now' in session and session['Now'] == 1:
  211. curs.execute('select name from alarm where name = ? limit 1', [ip_check()])
  212. if curs.fetchall():
  213. user_icon = 2
  214. else:
  215. user_icon = 1
  216. else:
  217. user_icon = 0
  218. if user_icon == 1:
  219. curs.execute('select email from user where id = ?', [ip_check()])
  220. data = curs.fetchall()
  221. if data:
  222. email = data[0][0]
  223. else:
  224. email = ''
  225. else:
  226. email = ''
  227. return ['', '', user_icon, user_head, email]
  228. def acl_check(conn, name):
  229. curs = conn.cursor()
  230. ip = ip_check()
  231. if ban_check(conn) == 1:
  232. return 1
  233. acl_c = re.search("^사용자:([^/]*)", name)
  234. if acl_c:
  235. acl_n = acl_c.groups()
  236. if admin_check(conn, 5, None) == 1:
  237. return 0
  238. curs.execute("select dec from acl where title = ?", ['사용자:' + acl_n[0]])
  239. acl_data = curs.fetchall()
  240. if acl_data:
  241. if acl_data[0][0] == 'all':
  242. return 0
  243. if acl_data[0][0] == 'user' and not re.search("(\.|:)", ip):
  244. return 0
  245. if ip != acl_n[0] or re.search("(\.|:)", ip):
  246. return 1
  247. if ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0]):
  248. return 0
  249. else:
  250. return 1
  251. file_c = re.search("^파일:(.*)", name)
  252. if file_c and admin_check(conn, 5, 'edit (' + name + ')') != 1:
  253. return 1
  254. curs.execute("select acl from user where id = ?", [ip])
  255. user_data = curs.fetchall()
  256. curs.execute("select dec from acl where title = ?", [name])
  257. acl_data = curs.fetchall()
  258. if acl_data:
  259. if acl_data[0][0] == 'user':
  260. if not user_data:
  261. return 1
  262. if acl_data[0][0] == 'admin':
  263. if not user_data:
  264. return 1
  265. if not admin_check(conn, 5, 'edit (' + name + ')') == 1:
  266. return 1
  267. curs.execute('select data from other where name = "edit"')
  268. set_data = curs.fetchall()
  269. if set_data:
  270. if set_data[0][0] == 'user':
  271. if not user_data:
  272. return 1
  273. if set_data[0][0] == 'admin':
  274. if not user_data:
  275. return 1
  276. if not admin_check(conn, 5, None) == 1:
  277. return 1
  278. return 0
  279. def ban_check(conn):
  280. ip = ip_check()
  281. curs = conn.cursor()
  282. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  283. if band:
  284. band_it = band.groups()
  285. else:
  286. band_it = ['Not']
  287. curs.execute("select block from ban where block = ? and band = 'O'", [band_it[0]])
  288. band_d = curs.fetchall()
  289. curs.execute("select block from ban where block = ?", [ip])
  290. ban_d = curs.fetchall()
  291. if band_d or ban_d:
  292. return 1
  293. return 0
  294. def topic_check(conn, name, sub):
  295. ip = ip_check()
  296. curs = conn.cursor()
  297. if ban_check(conn) == 1:
  298. return 1
  299. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  300. if curs.fetchall():
  301. return 1
  302. return 0
  303. def ban_insert(conn, name, end, why, login, blocker = ip_check()):
  304. curs = conn.cursor()
  305. time = get_time()
  306. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  307. band = 'O'
  308. else:
  309. band = ''
  310. if end != '':
  311. year = int(int(end) / 525600)
  312. month = int(int(end) % 525600 / 40320)
  313. day = int(int(end) % 525600 % 40320 / 1440)
  314. hour = int(int(end) % 525600 % 40320 % 1440 / 60)
  315. minute = int(int(end) % 525600 % 40320 % 1440 % 60)
  316. end_data = [month, day, hour, minute]
  317. match = re.search("^([^-]+)-([^-]+)-([^ ]+) ([^:]+):([^:]+):(.+)$", time)
  318. time_data = match.groups()
  319. time_cut = [11, 27, 23, 59]
  320. test_list = [int(time_data[0]) + year, 0, 0, 0, 0]
  321. for number in range(0, 4):
  322. if month + int(time_data[number + 1]) > time_cut[number]:
  323. test_list[number] += 1
  324. test_list[number + 1] = end_data[number] + int(time_data[number + 1]) - (time_cut[number] + 1)
  325. else:
  326. test_list[number + 1] = end_data[number] + int(time_data[number + 1])
  327. time_list = [test_list[1], test_list[2], test_list[3], test_list[4]]
  328. number = 0
  329. for time_fix in time_list:
  330. if not re.search("[0-9]{2}", str(time_fix)):
  331. time_list[number] = '0' + str(time_fix)
  332. else:
  333. time_list[number] = str(time_fix)
  334. number += 1
  335. end = str(test_list[0]) + '-' + time_list[0] + '-' + time_list[1] + ' ' + time_list[2] + ':' + time_list[3] + ':' + time_data[5]
  336. else:
  337. end = ''
  338. curs.execute("select block from ban where block = ?", [name])
  339. if curs.fetchall():
  340. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, '해제', time, blocker, '', band])
  341. curs.execute("delete from ban where block = ?", [name])
  342. else:
  343. if login:
  344. login = 'O'
  345. else:
  346. login = ''
  347. curs.execute("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)", [name, end, time, blocker, why, band])
  348. curs.execute("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)", [name, end, why, band, login])
  349. conn.commit()
  350. def rd_plus(conn, title, sub, date):
  351. curs = conn.cursor()
  352. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  353. if curs.fetchall():
  354. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  355. else:
  356. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  357. def history_plus(conn, title, data, date, ip, send, leng):
  358. curs = conn.cursor()
  359. curs.execute("select id from history where title = ? order by id + 0 desc limit 1", [title])
  360. id_data = curs.fetchall()
  361. if id_data:
  362. 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])
  363. else:
  364. curs.execute("insert into history (id, title, data, date, ip, send, leng) values ('1', ?, ?, ?, ?, ?, ?)", [title, data, date, ip, send + ' (새 문서)', leng])
  365. def leng_check(first, second):
  366. if first < second:
  367. all_plus = '+' + str(second - first)
  368. elif second < first:
  369. all_plus = '-' + str(first - second)
  370. else:
  371. all_plus = '0'
  372. return all_plus
  373. def redirect(data):
  374. return '<meta http-equiv="refresh" content="0; url=' + data + '">'
  375. def re_error(conn, data):
  376. curs = conn.cursor()
  377. if data == '/ban':
  378. ip = ip_check()
  379. end = '<li>사유 : 권한이 맞지 않는 상태 입니다.</li>'
  380. if ban_check(conn) == 1:
  381. curs.execute("select end, why from ban where block = ?", [ip])
  382. end_data = curs.fetchall()
  383. if not end_data:
  384. match = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  385. if match:
  386. curs.execute("select end, why from ban where block = ? and band = 'O'", [m.groups()[0]])
  387. end_data = curs.fetchall()
  388. if end_data:
  389. end = '<li>상태 : '
  390. if end_data[0][0]:
  391. now = int(re.sub('(:|-| )', '', get_time()))
  392. day = re.sub('\-', '', end_data[0][0])
  393. if re.search(':', day):
  394. day = re.sub('( |:)', '', day)
  395. else:
  396. day += '000000'
  397. if now >= int(day):
  398. curs.execute("delete from ban where block = ?", [ip])
  399. conn.commit()
  400. end += '차단이 풀렸습니다. 다시 시도 해 보세요.'
  401. else:
  402. end += end_data[0][0] + ' 까지 차단 상태 입니다.'
  403. else:
  404. end += '영구 차단 상태 입니다.'
  405. end += '</li>'
  406. if end_data[0][1] != '':
  407. end += '<li>사유 : ' + end_data[0][1] + '</li>'
  408. return html_minify(render_template(skin_check(conn),
  409. imp = ['권한 오류', wiki_set(conn, 1), custom(conn), other2([0, 0])],
  410. data = '<h2>권한 상태</h2><ul>' + end + '</ul>',
  411. menu = 0
  412. ))
  413. error_data = re.search('\/error\/([0-9]+)', data)
  414. if error_data:
  415. num = int(error_data.groups()[0])
  416. if num == 1:
  417. title = '권한 오류'
  418. data = '비 로그인 상태 입니다.'
  419. elif num == 2:
  420. title = '권한 오류'
  421. data = '이 계정이 없습니다.'
  422. elif num == 3:
  423. title = '권한 오류'
  424. data = '권한이 모자랍니다.'
  425. elif num == 4:
  426. title = '권한 오류'
  427. data = '관리자는 차단, 검사 할 수 없습니다.'
  428. elif num == 5:
  429. title = '사용자 오류'
  430. data = '그런 계정이 없습니다.'
  431. elif num == 6:
  432. title = '가입 오류'
  433. data = '동일한 아이디의 사용자가 있습니다.'
  434. elif num == 7:
  435. title = '가입 오류'
  436. data = '아이디는 20글자보다 짧아야 합니다.'
  437. elif num == 8:
  438. title = '가입 오류'
  439. data = '아이디에는 한글과 알파벳과 공백만 허용 됩니다.'
  440. elif num == 9:
  441. title = '파일 올리기 오류'
  442. data = '파일이 없습니다.'
  443. elif num == 10:
  444. title = '변경 오류'
  445. data = '비밀번호가 다릅니다.'
  446. elif num == 11:
  447. title = '로그인 오류'
  448. data = '이미 로그인 되어 있습니다.'
  449. elif num == 12:
  450. title = '편집 오류'
  451. data = '누군가 먼저 편집 했습니다.'
  452. elif num == 13:
  453. title = '리캡차 오류'
  454. data = '리캡차를 통과하세요.'
  455. elif num == 14:
  456. title = '파일 올리기 오류'
  457. data = 'jpg, gif, jpeg, png, webp만 가능 합니다.'
  458. elif num == 15:
  459. title = '편집 오류'
  460. data = '편집 기록은 500자를 넘을 수 없습니다.'
  461. elif num == 16:
  462. title = '파일 올리기 오류'
  463. data = '동일한 이름의 파일이 있습니다.'
  464. elif num == 17:
  465. title = '파일 올리기 오류'
  466. data = '파일 용량은 ' + wiki_set(conn, 3) + 'MB를 넘길 수 없습니다.'
  467. elif num == 18:
  468. title = '편집 오류'
  469. data = '내용이 원래 문서와 동일 합니다.'
  470. elif num == 19:
  471. title = '이동 오류'
  472. data = '이동 하려는 곳에 문서가 이미 있습니다.'
  473. elif num == 20:
  474. title = '비밀번호 오류'
  475. data = '재 확인이랑 비밀번호가 다릅니다.'
  476. elif num == 21:
  477. title = '편집 오류'
  478. data = '편집 필터에 의해 검열 되었습니다.'
  479. elif num == 22:
  480. title = '파일 올리기 오류'
  481. data = '파일 이름은 알파벳, 한글, 띄어쓰기, 언더바, 빼기표만 허용 됩니다.'
  482. else:
  483. title = '정체 불명의 오류'
  484. data = '???'
  485. if title:
  486. return html_minify(render_template(skin_check(conn),
  487. imp = [title, wiki_set(conn, 1), custom(conn), other2([0, 0])],
  488. data = '<h2>오류 발생</h2><ul><li>' + data + '</li></ul>',
  489. menu = 0
  490. ))
  491. else:
  492. return redirect('/')
  493. else:
  494. return redirect('/')