2
0

func.py 14 KB

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