func.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. from bottle import request, app
  2. from bottle.ext import beaker
  3. from urllib import parse
  4. import json
  5. import sqlite3
  6. import time
  7. import re
  8. import hashlib
  9. import html
  10. json_data = open('set.json').read()
  11. set_data = json.loads(json_data)
  12. conn = sqlite3.connect(set_data['db'] + '.db')
  13. curs = conn.cursor()
  14. session_opts = {
  15. 'session.type': 'file',
  16. 'session.data_dir': './app_session/',
  17. 'session.auto': 1
  18. }
  19. app = beaker.middleware.SessionMiddleware(app(), session_opts)
  20. from mark import *
  21. def wiki_set(num):
  22. if(num == 1):
  23. d_data = '무명위키'
  24. curs.execute('select data from other where name = ?', ['name'])
  25. elif(num == 2):
  26. d_data = '위키:대문'
  27. curs.execute('select data from other where name = "frontpage"')
  28. elif(num == 3):
  29. d_data = 'CC 0'
  30. curs.execute('select data from other where name = "license"')
  31. elif(num == 4):
  32. d_data = '2'
  33. curs.execute('select data from other where name = "upload"')
  34. data = curs.fetchall()
  35. if(data):
  36. return(data[0][0])
  37. else:
  38. return(d_data)
  39. def diff(seqm, num):
  40. output= []
  41. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  42. if(opcode == 'equal' and num == 1):
  43. output.append(seqm.a[a0:a1])
  44. elif(opcode == 'insert' and num == 0):
  45. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  46. elif(opcode == 'delete' and num == 1):
  47. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  48. elif(opcode == 'replace'):
  49. if(num == 1):
  50. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  51. else:
  52. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  53. elif(num == 0):
  54. output.append(seqm.b[b0:b1])
  55. return(''.join(output))
  56. def admin_check(num, what):
  57. ip = ip_check()
  58. curs.execute("select acl from user where id = ?", [ip])
  59. user = curs.fetchall()
  60. if(user):
  61. reset = 0
  62. while(1):
  63. if(num == 1 and reset == 0):
  64. check = 'ban'
  65. elif(num == 2 and reset == 0):
  66. check = 'mdel'
  67. elif(num == 3 and reset == 0):
  68. check = 'toron'
  69. elif(num == 4 and reset == 0):
  70. check = 'check'
  71. elif(num == 5 and reset == 0):
  72. check = 'acl'
  73. elif(num == 6 and reset == 0):
  74. check = 'hidel'
  75. else:
  76. check = 'owner'
  77. curs.execute('select name from alist where name = ? and acl = ?', [user[0][0], check])
  78. acl_data = curs.fetchall()
  79. if(acl_data):
  80. if(what):
  81. curs.execute("insert into re_admin (who, what, time) values (?, ?, ?)", [ip, what, get_time()])
  82. conn.commit()
  83. return(1)
  84. else:
  85. if(reset == 0):
  86. reset = 1
  87. else:
  88. break
  89. def include_check(name, data):
  90. if(re.search('^틀:', name)):
  91. curs.execute("select link from back where title = ? and type = 'include'", [name])
  92. back = curs.fetchall()
  93. for back_p in back:
  94. namumark(back_p[0], data, 1, 1)
  95. def login_check():
  96. session = request.environ.get('beaker.session')
  97. if(session.get('Now') == 1):
  98. return(1)
  99. else:
  100. return(0)
  101. def ip_pas(raw_ip, num):
  102. if(re.search("(\.|:)", raw_ip)):
  103. ip = raw_ip
  104. else:
  105. curs.execute("select title from data where title = ?", ['사용자:' + raw_ip])
  106. row = curs.fetchall()
  107. if(row):
  108. ip = '<a href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  109. else:
  110. ip = '<a class="not_thing" href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  111. if(num == 1):
  112. ip += ' <a href="/user/' + url_pas(raw_ip) + '/topic">(기록)</a>'
  113. elif(num == 2):
  114. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(기록)</a> <a href="/user/' + url_pas(raw_ip) + '/topic">(토론 기록)</a>'
  115. else:
  116. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(기록)</a>'
  117. return(ip)
  118. def custom_css():
  119. session = request.environ.get('beaker.session')
  120. try:
  121. data = format(session['Daydream'])
  122. except:
  123. data = ''
  124. return(data)
  125. def custom_js():
  126. session = request.environ.get('beaker.session')
  127. try:
  128. data = format(session['AQUARIUM'])
  129. except:
  130. data = ''
  131. return(data)
  132. def acl_check(ip, name):
  133. m = re.search("^사용자:([^/]*)", name)
  134. n = re.search("^파일:(.*)", name)
  135. if(m):
  136. g = m.groups()
  137. if(ip == g[0]):
  138. if(re.search("(\.|:)", g[0])):
  139. return(1)
  140. else:
  141. curs.execute("select block from ban where block = ?", [ip])
  142. rows = curs.fetchall()
  143. if(rows):
  144. return(1)
  145. else:
  146. return(0)
  147. else:
  148. return(1)
  149. elif(n and admin_check(5, 'edit (' + name + ')') != 1):
  150. return(1)
  151. else:
  152. b = re.search("^([0-9](?:[0-9]?[0-9]?)\.[0-9](?:[0-9]?[0-9]?))", ip)
  153. if(b):
  154. results = b.groups()
  155. curs.execute("select block from ban where block = ? and band = 'O'", [results[0]])
  156. rowss = curs.fetchall()
  157. if(rowss):
  158. return(1)
  159. curs.execute("select block from ban where block = ?", [ip])
  160. rows = curs.fetchall()
  161. if(rows):
  162. return(1)
  163. else:
  164. curs.execute("select acl from data where title = ?", [name])
  165. row = curs.fetchall()
  166. if(row):
  167. curs.execute("select acl from user where id = ?", [ip])
  168. rows = curs.fetchall()
  169. if(row[0][0] == 'user'):
  170. if(rows):
  171. return(0)
  172. else:
  173. return(1)
  174. elif(row[0][0] == 'admin'):
  175. if(rows and admin_check(5, 'edit (' + name + ')') == 1):
  176. return(0)
  177. else:
  178. return(1)
  179. else:
  180. return(0)
  181. else:
  182. return(0)
  183. def ban_check(ip):
  184. b = re.search("^([0-9](?:[0-9]?[0-9]?)\.[0-9](?:[0-9]?[0-9]?))", ip)
  185. if(b):
  186. results = b.groups()
  187. curs.execute("select block from ban where block = ? and band = 'O'", [results[0]])
  188. rowss = curs.fetchall()
  189. if(rowss):
  190. return(1)
  191. curs.execute("select block from ban where block = ?", [ip])
  192. rows = curs.fetchall()
  193. if(rows):
  194. return(1)
  195. else:
  196. return(0)
  197. def topic_check(ip, name, sub):
  198. b = re.search("^([0-9](?:[0-9]?[0-9]?)\.[0-9](?:[0-9]?[0-9]?))", ip)
  199. if(b):
  200. results = b.groups()
  201. curs.execute("select block from ban where block = ? and band = 'O'", [results[0]])
  202. rowss = curs.fetchall()
  203. if(rowss):
  204. return(1)
  205. curs.execute("select block from ban where block = ?", [ip])
  206. rows = curs.fetchall()
  207. if(rows):
  208. return(1)
  209. else:
  210. curs.execute("select title from stop where title = ? and sub = ?", [name, sub])
  211. rows = curs.fetchall()
  212. if(rows):
  213. return(1)
  214. else:
  215. return(0)
  216. def rd_plus(title, sub, date):
  217. curs.execute("select title from rd where title = ? and sub = ?", [title, sub])
  218. rd = curs.fetchall()
  219. if(rd):
  220. curs.execute("update rd set date = ? where title = ? and sub = ?", [date, title, sub])
  221. else:
  222. curs.execute("insert into rd (title, sub, date) values (?, ?, ?)", [title, sub, date])
  223. conn.commit()
  224. def rb_plus(block, end, today, blocker, why):
  225. curs.execute("insert into rb (block, end, today, blocker, why) values (?, ?, ?, ?, ?)", [block, end, today, blocker, why])
  226. conn.commit()
  227. def history_plus(title, data, date, ip, send, leng):
  228. curs.execute("select id from history where title = ? order by id+0 desc limit 1", [title])
  229. rows = curs.fetchall()
  230. if(rows):
  231. number = int(rows[0][0]) + 1
  232. curs.execute("insert into history (id, title, data, date, ip, send, leng) values (?, ?, ?, ?, ?, ?, ?)", [str(number), title, data, date, ip, send, leng])
  233. else:
  234. curs.execute("insert into history (id, title, data, date, ip, send, leng) values ('1', ?, ?, ?, ?, ?, ?)", [title, data, date, ip, send + ' (새 문서)', leng])
  235. conn.commit()
  236. def leng_check(a, b):
  237. if(a < b):
  238. c = b - a
  239. c = '+' + str(c)
  240. elif(b < a):
  241. c = a - b
  242. c = '-' + str(c)
  243. else:
  244. c = '0'
  245. return(c)