2
0

func.py 14 KB

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