tool.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 savemark(data):
  27. data = re.sub("\[date\(now\)\]", get_time(), data)
  28. ip = ip_check()
  29. if not re.search("\.", ip):
  30. name = '[[user:' + ip + '|' + ip + ']]'
  31. else:
  32. name = ip
  33. data = re.sub("\[name\]", name, data)
  34. return data
  35. def url_pas(data):
  36. return urllib.parse.quote(data).replace('/','%2F')
  37. def sha224_replace(data):
  38. return hashlib.sha224(bytes(data, 'utf-8')).hexdigest()
  39. def md5_replace(data):
  40. return hashlib.md5(data.encode()).hexdigest()