func.py 8.9 KB

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