2
0

func.py 14 KB

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