func.py 8.9 KB

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