tool.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import urllib.parse
  2. import datetime
  3. import hashlib
  4. import flask
  5. import re
  6. def get_time():
  7. return str(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S"))
  8. def db_data_get(data):
  9. global set_data
  10. set_data = data
  11. def db_change(data):
  12. if set_data == 'mysql':
  13. data = data.replace('random()', 'rand()')
  14. data = data.replace('%', '%%')
  15. data = data.replace('?', '%s')
  16. return data
  17. def ip_check(d_type = 0):
  18. ip = ''
  19. if d_type == 0 and (flask.session and ('state' and 'id') in flask.session):
  20. ip = flask.session['id']
  21. if ip == '':
  22. ip = flask.request.environ.get('HTTP_X_REAL_IP', flask.request.environ.get('HTTP_X_FORWARDED_FOR', flask.request.remote_addr))
  23. if ip == '::1' or ip == '127.0.0.1':
  24. ip = flask.request.environ.get('HTTP_X_FORWARDED_FOR', flask.request.remote_addr)
  25. return str(ip)
  26. def link_fix(main_link):
  27. if re.search('^:', main_link):
  28. main_link = re.sub('^:', '', main_link)
  29. main_link = re.sub('^사용자:', 'user:', main_link)
  30. main_link = re.sub('^파일:', 'file:', main_link)
  31. main_link = re.sub('^분류:', 'category:', main_link)
  32. other_link = re.search('[^\\\\]?(#[^#]+)$', main_link)
  33. if other_link:
  34. other_link = other_link.groups()[0]
  35. main_link = re.sub('(#[^#]+)$', '', main_link)
  36. else:
  37. other_link = ''
  38. main_link = re.sub('\\\\#', '%23', main_link)
  39. return [main_link, other_link]
  40. def savemark(data):
  41. data = re.sub("\[date\(now\)\]", get_time(), data)
  42. ip = ip_check()
  43. if not re.search("\.", ip):
  44. name = '[[user:' + ip + '|' + ip + ']]'
  45. else:
  46. name = ip
  47. data = re.sub("\[name\]", name, data)
  48. return data
  49. def url_pas(data):
  50. return urllib.parse.quote(data).replace('/','%2F')
  51. def sha224(data):
  52. return hashlib.sha224(bytes(data, 'utf-8')).hexdigest()
  53. def md5_replace(data):
  54. return hashlib.md5(data.encode()).hexdigest()