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. session = request.environ.get('beaker.session')
  19. if(session.get('View_List')):
  20. m = re.findall('(?:(?:([^\n]+)\n))', session.get('View_List'))
  21. if(m):
  22. g = ''
  23. for z in m[-6:-1]:
  24. g += '<a href="/w/' + url_pas(z) + '">' + html.escape(z) + '</a> / '
  25. g = re.sub(' / $', '', g)
  26. r = d + [g]
  27. return(r)
  28. def include(title, old, new):
  29. if(re.search('^틀:', title)):
  30. old_d = mid_pas(old, 0, 1, 1)[0]
  31. new_d = mid_pas(new, 0, 1, 1)[0]
  32. m1 = re.findall("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", old_d)
  33. m2 = re.findall("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", new_d)
  34. curs.execute("select link from back where title = ? and type = 'include'", [title])
  35. d1 = curs.fetchall()
  36. for x in d1:
  37. for y in m1:
  38. curs.execute("delete from back where link = ? and type = 'cat'", [y])
  39. for z in m2:
  40. backlink_plus(x[0], z, 'cat', 1)
  41. conn.commit()
  42. def wiki_set(num):
  43. if(num == 1):
  44. r = []
  45. curs.execute('select data from other where name = ?', ['name'])
  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 = "license"')
  52. d = curs.fetchall()
  53. if(d):
  54. r += [d[0][0]]
  55. else:
  56. r += ['CC 0']
  57. return(r)
  58. if(num == 2):
  59. d = '위키:대문'
  60. curs.execute('select data from other where name = "frontpage"')
  61. elif(num == 3):
  62. d = '2'
  63. curs.execute('select data from other where name = "upload"')
  64. r = curs.fetchall()
  65. if(r):
  66. return(r[0][0])
  67. else:
  68. return(d)
  69. def diff(seqm, num):
  70. output= []
  71. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  72. if(opcode == 'equal' and num == 1):
  73. output.append(seqm.a[a0:a1])
  74. elif(opcode == 'insert' and num == 0):
  75. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  76. elif(opcode == 'delete' and num == 1):
  77. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  78. elif(opcode == 'replace'):
  79. if(num == 1):
  80. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  81. else:
  82. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  83. elif(num == 0):
  84. output.append(seqm.b[b0:b1])
  85. return(''.join(output))
  86. def admin_check(num, what):
  87. ip = ip_check()
  88. curs.execute("select acl from user where id = ?", [ip])
  89. user = curs.fetchall()
  90. if(user):
  91. reset = 0
  92. while(1):
  93. if(num == 1 and reset == 0):
  94. check = 'ban'
  95. elif(num == 2 and reset == 0):
  96. check = 'mdel'
  97. elif(num == 3 and reset == 0):
  98. check = 'toron'
  99. elif(num == 4 and reset == 0):
  100. check = 'check'
  101. elif(num == 5 and reset == 0):
  102. check = 'acl'
  103. elif(num == 6 and reset == 0):
  104. check = 'hidel'
  105. else:
  106. check = 'owner'
  107. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  108. acl_data = curs.fetchall()
  109. if(acl_data):
  110. if(what):
  111. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  112. conn.commit()
  113. return(1)
  114. else:
  115. if(reset == 0):
  116. reset = 1
  117. else:
  118. break
  119. def ip_pas(raw_ip):
  120. if(re.search("(\.|:)", raw_ip)):
  121. ip = raw_ip
  122. else:
  123. curs.execute("select title from data where title = ?", ['사용자:' + raw_ip])
  124. data = curs.fetchall()
  125. if(data):
  126. ip = '<a href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  127. else:
  128. ip = '<a class="not_thing" href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  129. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(기록)</a>'
  130. return(ip)
  131. def custom():
  132. session = request.environ.get('beaker.session')
  133. try:
  134. d1 = format(session['Daydream'])
  135. except:
  136. d1 = ''
  137. try:
  138. d2 = format(session['AQUARIUM'])
  139. except:
  140. d2 = ''
  141. if(session.get('Now') == 1):
  142. curs.execute('select name from alarm limit 1')
  143. if(curs.fetchall()):
  144. d3 = 2
  145. else:
  146. d3 = 1
  147. else:
  148. d3 = 0
  149. return([d1, d2, d3])
  150. def acl_check(name):
  151. ip = ip_check()
  152. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  153. if(band):
  154. band_it = band.groups()
  155. else:
  156. band_it = ['Not']
  157. curs.execute("select block from ban where block = ? and band = 'O'", [band_it[0]])
  158. band_d = curs.fetchall()
  159. curs.execute("select block from ban where block = ?", [ip])
  160. ban_d = curs.fetchall()
  161. if(band_d or ban_d):
  162. return(1)
  163. acl_c = re.search("^사용자:([^/]*)", name)
  164. if(acl_c):
  165. acl_n = acl_c.groups()
  166. if(admin_check(5, None) == 1):
  167. return(0)
  168. curs.execute("select acl from data where title = ?", ['사용자:' + acl_n[0]])
  169. acl_d = curs.fetchall()
  170. if(acl_d):
  171. if(acl_d[0][0] == 'all'):
  172. return(0)
  173. if(acl_d[0][0] == 'user' and not re.search("(\.|:)", ip)):
  174. return(0)
  175. if(not ip == acl_n[0] or re.search("(\.|:)", ip)):
  176. return(1)
  177. if(ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0])):
  178. return(0)
  179. else:
  180. return(1)
  181. file_c = re.search("^파일:(.*)", name)
  182. if(file_c and admin_check(5, 'edit (' + name + ')') != 1):
  183. return(1)
  184. curs.execute("select acl from data where title = ?", [name])
  185. acl_d = curs.fetchall()
  186. if(not acl_d):
  187. return(0)
  188. curs.execute("select acl from user where id = ?", [ip])
  189. user_d = curs.fetchall()
  190. curs.execute('select data from other where name = "edit"')
  191. set_d = curs.fetchall()
  192. if(acl_d[0][0] == 'user'):
  193. if(not user_d):
  194. return(1)
  195. if(acl_d[0][0] == 'admin'):
  196. if(not user_d):
  197. return(1)
  198. if(not admin_check(5, 'edit (' + name + ')') == 1):
  199. return(1)
  200. if(set_d):
  201. if(set_d[0][0] == 'user'):
  202. if(not user_d):
  203. return(1)
  204. if(set_d[0][0] == 'admin'):
  205. if(not user_d):
  206. return(1)
  207. if(not admin_check(5, None) == 1):
  208. return(1)
  209. return(0)
  210. def ban_check():
  211. ip = ip_check()
  212. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  213. if(band):
  214. band_it = band.groups()
  215. else:
  216. band_it = ['Not']
  217. curs.execute("select block from ban where block = ? and band = 'O'", [band_it[0]])
  218. band_d = curs.fetchall()
  219. curs.execute("select block from ban where block = ?", [ip])
  220. ban_d = curs.fetchall()
  221. if(band_d or ban_d):
  222. return(1)
  223. return(0)
  224. def topic_check(name, sub):
  225. ip = ip_check()
  226. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  227. if(band):
  228. band_it = band.groups()
  229. else:
  230. band_it = ['Not']
  231. curs.execute("select block from ban where block = ? and band = 'O'", [band_it[0]])
  232. band_d = curs.fetchall()
  233. curs.execute("select block from ban where block = ?", [ip])
  234. ban_d = curs.fetchall()
  235. if(band_d or ban_d):
  236. return(1)
  237. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  238. topic_s = curs.fetchall()
  239. if(topic_s):
  240. return(1)
  241. return(0)
  242. def rd_plus(title, sub, date):
  243. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  244. rd = curs.fetchall()
  245. if(rd):
  246. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  247. else:
  248. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  249. conn.commit()
  250. def rb_plus(block, end, today, blocker, why):
  251. curs.execute("insert into rb (block, end, today, blocker, why) values (?, ?, ?, ?, ?)", [block, end, today, blocker, why])
  252. conn.commit()
  253. def history_plus(title, data, date, ip, send, leng):
  254. curs.execute("select id from history where title = ? order by id+0 desc limit 1", [title])
  255. rows = curs.fetchall()
  256. if(rows):
  257. 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])
  258. else:
  259. curs.execute("insert into history (id, title, data, date, ip, send, leng) values ('1', ?, ?, ?, ?, ?, ?)", [title, data, date, ip, send + ' (새 문서)', leng])
  260. conn.commit()
  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 == 14):
  344. title = '파일 올리기 오류'
  345. data = 'jpg, gif, jpeg, png, webp만 가능 합니다.'
  346. elif(num == 15):
  347. title = '편집 오류'
  348. data = '편집 기록은 500자를 넘을 수 없습니다.'
  349. elif(num == 16):
  350. title = '파일 올리기 오류'
  351. data = '동일한 이름의 파일이 있습니다.'
  352. elif(num == 17):
  353. title = '파일 올리기 오류'
  354. data = '파일 용량은 ' + wiki_set(3) + 'MB를 넘길 수 없습니다.'
  355. elif(num == 18):
  356. title = '편집 오류'
  357. data = '내용이 원래 문서와 동일 합니다.'
  358. elif(num == 19):
  359. title = '이동 오류'
  360. data = '이동 하려는 곳에 문서가 이미 있습니다.'
  361. elif(num == 20):
  362. title = '비밀번호 오류'
  363. data = '재 확인이랑 비밀번호가 다릅니다.'
  364. if(title):
  365. return(
  366. html_minify(
  367. template(
  368. 'index',
  369. imp = [title, wiki_set(1), custom(), other2([0, 0])],
  370. data = data,
  371. menu = 0
  372. )
  373. )
  374. )
  375. else:
  376. return(redirect('/'))
  377. else:
  378. return(redirect('/'))