func.py 16 KB

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