func.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 include(title, old, new):
  30. if(re.search('^틀:', title)):
  31. old_d = mid_pas(old, 0, 1, 1)[0]
  32. new_d = mid_pas(new, 0, 1, 1)[0]
  33. m1 = re.findall("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", old_d)
  34. m2 = re.findall("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", new_d)
  35. curs.execute("select link from back where title = ? and type = 'include'", [title])
  36. d1 = curs.fetchall()
  37. for x in d1:
  38. for y in m1:
  39. curs.execute("delete from back where link = ? and type = 'cat'", [y])
  40. for z in m2:
  41. backlink_plus(x[0], z, 'cat', 1)
  42. conn.commit()
  43. def wiki_set(num):
  44. if(num == 1):
  45. r = []
  46. curs.execute('select data from other where name = ?', ['name'])
  47. d = curs.fetchall()
  48. if(d):
  49. r += [d[0][0]]
  50. else:
  51. r += ['무명위키']
  52. curs.execute('select data from other where name = "license"')
  53. d = curs.fetchall()
  54. if(d):
  55. r += [d[0][0]]
  56. else:
  57. r += ['CC 0']
  58. curs.execute("select data from other where name = 'css'")
  59. d = curs.fetchall()
  60. if(d):
  61. r += [d[0][0]]
  62. else:
  63. r += ['']
  64. curs.execute("select data from other where name = 'js'")
  65. d = curs.fetchall()
  66. if(d):
  67. r += [d[0][0]]
  68. else:
  69. r += ['']
  70. return(r)
  71. if(num == 2):
  72. d = '위키:대문'
  73. curs.execute('select data from other where name = "frontpage"')
  74. elif(num == 3):
  75. d = '2'
  76. curs.execute('select data from other where name = "upload"')
  77. r = curs.fetchall()
  78. if(r):
  79. return(r[0][0])
  80. else:
  81. return(d)
  82. def diff(seqm, num):
  83. output= []
  84. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  85. if(opcode == 'equal' and num == 1):
  86. output.append(seqm.a[a0:a1])
  87. elif(opcode == 'insert' and num == 0):
  88. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  89. elif(opcode == 'delete' and num == 1):
  90. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  91. elif(opcode == 'replace'):
  92. if(num == 1):
  93. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  94. else:
  95. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  96. elif(num == 0):
  97. output.append(seqm.b[b0:b1])
  98. return(''.join(output))
  99. def admin_check(num, what):
  100. ip = ip_check()
  101. curs.execute("select acl from user where id = ?", [ip])
  102. user = curs.fetchall()
  103. if(user):
  104. reset = 0
  105. while(1):
  106. if(num == 1 and reset == 0):
  107. check = 'ban'
  108. elif(num == 2 and reset == 0):
  109. check = 'mdel'
  110. elif(num == 3 and reset == 0):
  111. check = 'toron'
  112. elif(num == 4 and reset == 0):
  113. check = 'check'
  114. elif(num == 5 and reset == 0):
  115. check = 'acl'
  116. elif(num == 6 and reset == 0):
  117. check = 'hidel'
  118. else:
  119. check = 'owner'
  120. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  121. acl_data = curs.fetchall()
  122. if(acl_data):
  123. if(what):
  124. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  125. conn.commit()
  126. return(1)
  127. else:
  128. if(reset == 0):
  129. reset = 1
  130. else:
  131. break
  132. def ip_pas(raw_ip):
  133. if(re.search("(\.|:)", raw_ip)):
  134. ip = raw_ip
  135. else:
  136. curs.execute("select title from data where title = ?", ['사용자:' + raw_ip])
  137. data = curs.fetchall()
  138. if(data):
  139. ip = '<a href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  140. else:
  141. ip = '<a class="not_thing" href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  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. rd = curs.fetchall()
  258. if(rd):
  259. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  260. else:
  261. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  262. conn.commit()
  263. def rb_plus(block, end, today, blocker, why):
  264. curs.execute("insert into rb (block, end, today, blocker, why) values (?, ?, ?, ?, ?)", [block, end, today, blocker, why])
  265. conn.commit()
  266. def history_plus(title, data, date, ip, send, leng):
  267. curs.execute("select id from history where title = ? order by id+0 desc limit 1", [title])
  268. rows = curs.fetchall()
  269. if(rows):
  270. curs.execute("insert into history (id, title, data, date, ip, send, leng) values (?, ?, ?, ?, ?, ?, ?)", [str(int(rows[0][0]) + 1), title, data, date, ip, send, leng])
  271. else:
  272. curs.execute("insert into history (id, title, data, date, ip, send, leng) values ('1', ?, ?, ?, ?, ?, ?)", [title, data, date, ip, send + ' (새 문서)', leng])
  273. conn.commit()
  274. def leng_check(a, b):
  275. if(a < b):
  276. c = b - a
  277. c = '+' + str(c)
  278. elif(b < a):
  279. c = a - b
  280. c = '-' + str(c)
  281. else:
  282. c = '0'
  283. return(c)
  284. def redirect(data):
  285. return('<meta http-equiv="refresh" content="0;url=' + data + '" />')
  286. def re_error(data):
  287. if(data == '/ban'):
  288. ip = ip_check()
  289. end = '권한이 맞지 않는 상태 입니다.'
  290. if(ban_check() == 1):
  291. curs.execute("select end, why from ban where block = ?", [ip])
  292. d = curs.fetchall()
  293. if(not d):
  294. m = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  295. if(m):
  296. curs.execute("select end, why from ban where block = ? and band = 'O'", [m.groups()[0]])
  297. d = curs.fetchall()
  298. if(d):
  299. if(d[0][0]):
  300. end = d[0][0] + ' 까지 차단 상태 입니다. / 사유 : ' + d[0][1]
  301. now = re.sub(':', '', get_time())
  302. now = re.sub('\-', '', now)
  303. now = int(re.sub(' ', '', now))
  304. day = re.sub('\-', '', d[0][0])
  305. if(now >= int(day + '000000')):
  306. curs.execute("delete from ban where block = ?", [ip])
  307. conn.commit()
  308. end = '차단이 풀렸습니다. 다시 시도 해 보세요.'
  309. else:
  310. end = '영구 차단 상태 입니다. / 사유 : ' + d[0][1]
  311. return(
  312. html_minify(
  313. template('index',
  314. imp = ['권한 오류', wiki_set(1), custom(), other2([0, 0])],
  315. data = end,
  316. menu = 0
  317. )
  318. )
  319. )
  320. d = re.search('\/error\/([0-9]+)', data)
  321. if(d):
  322. num = int(d.groups()[0])
  323. if(num == 1):
  324. title = '권한 오류'
  325. data = '비 로그인 상태 입니다.'
  326. elif(num == 2):
  327. title = '권한 오류'
  328. data = '이 계정이 없습니다.'
  329. elif(num == 3):
  330. title = '권한 오류'
  331. data = '권한이 모자랍니다.'
  332. elif(num == 4):
  333. title = '권한 오류'
  334. data = '관리자는 차단, 검사 할 수 없습니다.'
  335. elif(num == 5):
  336. title = '사용자 오류'
  337. data = '그런 계정이 없습니다.'
  338. elif(num == 6):
  339. title = '가입 오류'
  340. data = '동일한 아이디의 사용자가 있습니다.'
  341. elif(num == 7):
  342. title = '가입 오류'
  343. data = '아이디는 20글자보다 짧아야 합니다.'
  344. elif(num == 8):
  345. title = '가입 오류'
  346. data = '아이디에는 한글과 알파벳과 공백만 허용 됩니다.'
  347. elif(num == 9):
  348. title = '파일 올리기 오류'
  349. data = '파일이 없습니다.'
  350. elif(num == 10):
  351. title = '변경 오류'
  352. data = '비밀번호가 다릅니다.'
  353. elif(num == 11):
  354. title = '로그인 오류'
  355. data = '이미 로그인 되어 있습니다.'
  356. elif(num == 14):
  357. title = '파일 올리기 오류'
  358. data = 'jpg, gif, jpeg, png, webp만 가능 합니다.'
  359. elif(num == 15):
  360. title = '편집 오류'
  361. data = '편집 기록은 500자를 넘을 수 없습니다.'
  362. elif(num == 16):
  363. title = '파일 올리기 오류'
  364. data = '동일한 이름의 파일이 있습니다.'
  365. elif(num == 17):
  366. title = '파일 올리기 오류'
  367. data = '파일 용량은 ' + wiki_set(3) + 'MB를 넘길 수 없습니다.'
  368. elif(num == 18):
  369. title = '편집 오류'
  370. data = '내용이 원래 문서와 동일 합니다.'
  371. elif(num == 19):
  372. title = '이동 오류'
  373. data = '이동 하려는 곳에 문서가 이미 있습니다.'
  374. elif(num == 20):
  375. title = '비밀번호 오류'
  376. data = '재 확인이랑 비밀번호가 다릅니다.'
  377. if(title):
  378. return(
  379. html_minify(
  380. template(
  381. 'index',
  382. imp = [title, wiki_set(1), custom(), other2([0, 0])],
  383. data = data,
  384. menu = 0
  385. )
  386. )
  387. )
  388. else:
  389. return(redirect('/'))
  390. else:
  391. return(redirect('/'))