2
0

func.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. from bottle import request, app
  2. from bottle.ext import beaker
  3. from urllib import parse
  4. import json
  5. import pymysql
  6. import time
  7. import re
  8. import hashlib
  9. json_data = open('set.json').read()
  10. set_data = json.loads(json_data)
  11. session_opts = {
  12. 'session.type': 'file',
  13. 'session.data_dir': './app_session/',
  14. 'session.auto': True
  15. }
  16. app = beaker.middleware.SessionMiddleware(app(), session_opts)
  17. db_pas = pymysql.escape_string
  18. from mark import *
  19. def diff(seqm):
  20. output= []
  21. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  22. if(opcode == 'equal'):
  23. output.append(seqm.a[a0:a1])
  24. elif(opcode == 'insert'):
  25. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span>")
  26. elif(opcode == 'delete'):
  27. output.append("<span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  28. elif(opcode == 'replace'):
  29. output.append("<span style='background:#CFC;'>" + seqm.b[b0:b1] + "</span><span style='background:#FDD;'>" + seqm.a[a0:a1] + "</span>")
  30. else:
  31. output.append(seqm.a[a0:a1])
  32. return(''.join(output))
  33. def admin_check(num):
  34. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  35. curs = conn.cursor(pymysql.cursors.DictCursor)
  36. ip = ip_check()
  37. curs.execute("select acl from user where id = '" + db_pas(ip) + "'")
  38. user = curs.fetchall()
  39. if(user):
  40. reset = False
  41. while(True):
  42. if(num == 1 and reset == False):
  43. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "ban"')
  44. acl_data = curs.fetchall()
  45. if(acl_data):
  46. conn.close()
  47. return(1)
  48. else:
  49. reset = True
  50. elif(num == 2 and reset == False):
  51. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "mdel"')
  52. acl_data = curs.fetchall()
  53. if(acl_data):
  54. conn.close()
  55. return(1)
  56. else:
  57. reset = True
  58. elif(num == 3 and reset == False):
  59. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "toron"')
  60. acl_data = curs.fetchall()
  61. if(acl_data):
  62. conn.close()
  63. return(1)
  64. else:
  65. reset = True
  66. elif(num == 4 and reset == False):
  67. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "check"')
  68. acl_data = curs.fetchall()
  69. if(acl_data):
  70. conn.close()
  71. return(1)
  72. else:
  73. reset = True
  74. elif(num == 5 and reset == False):
  75. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "acl"')
  76. acl_data = curs.fetchall()
  77. if(acl_data):
  78. conn.close()
  79. return(1)
  80. else:
  81. reset = True
  82. elif(num == 6 and reset == False):
  83. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "hidel"')
  84. acl_data = curs.fetchall()
  85. if(acl_data):
  86. conn.close()
  87. return(1)
  88. else:
  89. reset = True
  90. else:
  91. curs.execute('select name from alist where name = "' + db_pas(user[0]["acl"]) + '" and acl = "owner"')
  92. acl_data = curs.fetchall()
  93. if(acl_data):
  94. conn.close()
  95. return(1)
  96. else:
  97. break
  98. conn.close()
  99. def include_check(name, data):
  100. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  101. curs = conn.cursor(pymysql.cursors.DictCursor)
  102. if(re.search('^틀:', name)):
  103. curs.execute("select link from back where title = '" + db_pas(name) + "' and type = 'include'")
  104. back = curs.fetchall()
  105. for backp in back:
  106. namumark(backp['link'], data, 1)
  107. conn.close()
  108. def login_check():
  109. session = request.environ.get('beaker.session')
  110. if(session.get('Now') == True):
  111. return(1)
  112. else:
  113. return(0)
  114. def ip_pas(raw_ip, num):
  115. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  116. curs = conn.cursor(pymysql.cursors.DictCursor)
  117. if(re.search("(\.|:)", raw_ip)):
  118. ip = raw_ip
  119. else:
  120. curs.execute("select title from data where title = '사용자:" + db_pas(raw_ip) + "'")
  121. row = curs.fetchall()
  122. if(row):
  123. ip = '<a href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  124. else:
  125. ip = '<a class="not_thing" href="/w/' + url_pas('사용자:' + raw_ip) + '">' + raw_ip + '</a>'
  126. if(num == 1):
  127. ip += ' <a href="/user/' + url_pas(raw_ip) + '/topic">(기록)</a>'
  128. elif(num == 2):
  129. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(기록)</a> <a href="/user/' + url_pas(raw_ip) + '/topic">(토론 기록)</a>'
  130. else:
  131. ip += ' <a href="/record/' + url_pas(raw_ip) + '">(기록)</a>'
  132. conn.close()
  133. return(ip)
  134. def custom_css_user():
  135. session = request.environ.get('beaker.session')
  136. try:
  137. data = format(session['Daydream'])
  138. except:
  139. data = ''
  140. return(data)
  141. def acl_check(ip, name):
  142. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  143. curs = conn.cursor(pymysql.cursors.DictCursor)
  144. m = re.search("^사용자:([^/]*)", name)
  145. n = re.search("^파일:(.*)", name)
  146. if(m):
  147. g = m.groups()
  148. if(ip == g[0]):
  149. if(re.search("(\.|:)", g[0])):
  150. conn.close()
  151. return(1)
  152. else:
  153. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  154. rows = curs.fetchall()
  155. if(rows):
  156. conn.close()
  157. return(1)
  158. else:
  159. conn.close()
  160. return(0)
  161. else:
  162. conn.close()
  163. return(1)
  164. elif(n):
  165. if(not admin_check(None) == 1):
  166. conn.close()
  167. return(1)
  168. else:
  169. b = re.search("^([0-9](?:[0-9]?[0-9]?)\.[0-9](?:[0-9]?[0-9]?))", ip)
  170. if(b):
  171. results = b.groups()
  172. curs.execute("select * from ban where block = '" + db_pas(results[0]) + "' and band = 'O'")
  173. rowss = curs.fetchall()
  174. if(rowss):
  175. conn.close()
  176. return(1)
  177. else:
  178. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  179. rows = curs.fetchall()
  180. if(rows):
  181. conn.close()
  182. return(1)
  183. else:
  184. curs.execute("select acl from data where title = '" + db_pas(name) + "'")
  185. row = curs.fetchall()
  186. if(row):
  187. curs.execute("select * from user where id = '" + db_pas(ip) + "'")
  188. rows = curs.fetchall()
  189. if(row[0]['acl'] == 'user'):
  190. if(rows):
  191. conn.close()
  192. return(0)
  193. else:
  194. conn.close()
  195. return(1)
  196. elif(row[0]['acl'] == 'admin'):
  197. if(rows):
  198. if(rows[0]['acl'] == 'admin' or rows[0]['acl'] == 'owner'):
  199. conn.close()
  200. return(0)
  201. else:
  202. conn.close()
  203. return(1)
  204. else:
  205. conn.close()
  206. return(1)
  207. else:
  208. conn.close()
  209. return(0)
  210. else:
  211. conn.close()
  212. return(0)
  213. else:
  214. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  215. rows = curs.fetchall()
  216. if(rows):
  217. conn.close()
  218. return(1)
  219. else:
  220. curs.execute("select acl from data where title = '" + db_pas(name) + "'")
  221. row = curs.fetchall()
  222. if(row):
  223. curs.execute("select * from user where id = '" + db_pas(ip) + "'")
  224. rows = curs.fetchall()
  225. if(row[0]['acl'] == 'user'):
  226. if(rows):
  227. conn.close()
  228. return(0)
  229. else:
  230. conn.close()
  231. return(1)
  232. elif(row[0]['acl'] == 'admin'):
  233. if(rows):
  234. if(rows[0]['acl'] == 'admin' or rows[0]['acl'] == 'owner'):
  235. conn.close()
  236. return(0)
  237. else:
  238. conn.close()
  239. return(1)
  240. else:
  241. conn.close()
  242. return(1)
  243. else:
  244. conn.close()
  245. return(0)
  246. else:
  247. conn.close()
  248. return(0)
  249. conn.close()
  250. def ban_check(ip):
  251. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  252. curs = conn.cursor(pymysql.cursors.DictCursor)
  253. b = re.search("^([0-9](?:[0-9]?[0-9]?)\.[0-9](?:[0-9]?[0-9]?))", ip)
  254. if(b):
  255. results = b.groups()
  256. curs.execute("select * from ban where block = '" + db_pas(results[0]) + "' and band = 'O'")
  257. rowss = curs.fetchall()
  258. if(rowss):
  259. conn.close()
  260. return(1)
  261. else:
  262. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  263. rows = curs.fetchall()
  264. if(rows):
  265. conn.close()
  266. return(1)
  267. else:
  268. conn.close()
  269. return(0)
  270. else:
  271. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  272. rows = curs.fetchall()
  273. if(rows):
  274. conn.close()
  275. return(1)
  276. else:
  277. conn.close()
  278. return(0)
  279. conn.close()
  280. def topic_check(ip, name, sub):
  281. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  282. curs = conn.cursor(pymysql.cursors.DictCursor)
  283. b = re.search("^([0-9](?:[0-9]?[0-9]?)\.[0-9](?:[0-9]?[0-9]?))", ip)
  284. if(b):
  285. results = b.groups()
  286. curs.execute("select * from ban where block = '" + db_pas(results[0]) + "' and band = 'O'")
  287. rowss = curs.fetchall()
  288. if(rowss):
  289. conn.close()
  290. return(1)
  291. else:
  292. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  293. rows = curs.fetchall()
  294. if(rows):
  295. conn.close()
  296. return(1)
  297. else:
  298. curs.execute("select * from stop where title = '" + db_pas(name) + "' and sub = '" + db_pas(sub) + "'")
  299. rows = curs.fetchall()
  300. if(rows):
  301. conn.close()
  302. return(1)
  303. else:
  304. conn.close()
  305. return(0)
  306. else:
  307. curs.execute("select * from ban where block = '" + db_pas(ip) + "'")
  308. rows = curs.fetchall()
  309. if(rows):
  310. conn.close()
  311. return(1)
  312. else:
  313. curs.execute("select * from stop where title = '" + db_pas(name) + "' and sub = '" + db_pas(sub) + "'")
  314. rows = curs.fetchall()
  315. if(rows):
  316. conn.close()
  317. return(1)
  318. else:
  319. conn.close()
  320. return(0)
  321. conn.close()
  322. def rd_plus(title, sub, date):
  323. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  324. curs = conn.cursor(pymysql.cursors.DictCursor)
  325. curs.execute("select * from rd where title = '" + db_pas(title) + "' and sub = '" + db_pas(sub) + "'")
  326. rd = curs.fetchall()
  327. if(rd):
  328. curs.execute("update rd set date = '" + db_pas(date) + "' where title = '" + db_pas(title) + "' and sub = '" + db_pas(sub) + "'")
  329. else:
  330. curs.execute("insert into rd (title, sub, date) value ('" + db_pas(title) + "', '" + db_pas(sub) + "', '" + db_pas(date) + "')")
  331. conn.commit()
  332. conn.close()
  333. def rb_plus(block, end, today, blocker, why):
  334. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  335. curs = conn.cursor(pymysql.cursors.DictCursor)
  336. curs.execute("insert into rb (block, end, today, blocker, why) value ('" + db_pas(block) + "', '" + db_pas(end) + "', '" + today + "', '" + db_pas(blocker) + "', '" + db_pas(why) + "')")
  337. conn.commit()
  338. conn.close()
  339. def history_plus(title, data, date, ip, send, leng):
  340. conn = pymysql.connect(user = set_data['user'], password = set_data['pw'], charset = 'utf8mb4', db = set_data['db'])
  341. curs = conn.cursor(pymysql.cursors.DictCursor)
  342. curs.execute("select * from history where title = '" + db_pas(title) + "' order by id+0 desc limit 1")
  343. rows = curs.fetchall()
  344. if(rows):
  345. number = int(rows[0]['id']) + 1
  346. curs.execute("insert into history (id, title, data, date, ip, send, leng) value ('" + str(number) + "', '" + db_pas(title) + "', '" + db_pas(data) + "', '" + date + "', '" + db_pas(ip) + "', '" + db_pas(send) + "', '" + leng + "')")
  347. else:
  348. curs.execute("insert into history (id, title, data, date, ip, send, leng) value ('1', '" + db_pas(title) + "', '" + db_pas(data) + "', '" + date + "', '" + db_pas(ip) + "', '" + db_pas(send + ' (새 문서)') + "', '" + leng + "')")
  349. conn.commit()
  350. conn.close()
  351. def leng_check(a, b):
  352. if(a < b):
  353. c = b - a
  354. c = '+' + str(c)
  355. elif(b < a):
  356. c = a - b
  357. c = '-' + str(c)
  358. else:
  359. c = '0'
  360. return(c)