func.py 17 KB

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