func.py 15 KB

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