admin.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #/usr/bin/env python3
  2. import webtools as wt
  3. import os, crypt, cgitb
  4. cgitb.enable()
  5. modes = {"0": "no mode",
  6. "1": "lock",
  7. "2": "sticky",
  8. "3": "stickylock",
  9. "4": "permasage"
  10. }
  11. settings = "./settings.txt"
  12. b_conf = []
  13. cd = {}
  14. with open(settings, "r") as settings:
  15. settings = settings.read().splitlines()
  16. for s in settings:
  17. if len(s) == 0 or s[0] == "#" or ": " not in s:
  18. continue
  19. elif "#" in s:
  20. s = s.split("#")[0]
  21. s = s.split(": ")
  22. if len(s) > 2:
  23. s[1] = ": ".join(s[1:])
  24. try:
  25. s[1] = int(s[1])
  26. except:
  27. pass
  28. b_conf.append(s[1])
  29. cd[s[0]] = s[1]
  30. with open("./admin/op.html", 'r') as op:
  31. op = op.read()
  32. def mode_icons(mo=""):
  33. micons = ["", "lock.png", "sticky.png",
  34. ["lock.png", "sticky.png"], "ghost.png"]
  35. ic = micons[int(mo)]
  36. if len(ic) == 2:
  37. ic = ["./img/" + i for i in ic if len(ic) == 2]
  38. elif len(ic):
  39. ic = ["./img/" + ic]
  40. return ic
  41. def login_admin():
  42. # if wt.get_cookie():
  43. cookies = wt.get_cookie()
  44. if 'pw' in cookies.keys():
  45. if tripcode(cookies['pw']) == b_conf[3]:
  46. return 1
  47. elif wt.get_form('pw') and \
  48. tripcode(wt.get_form('pw')) == b_conf[3]:
  49. print(wt.put_cookie('pw', wt.get_form('pw')))
  50. return 1
  51. else:
  52. if wt.get_form('pw'):
  53. print("Password incorrect.<br>")
  54. print("<h1>Login</h1>")
  55. print("<p>", wt.new_form('admin.py', 'post'))
  56. print("#" + wt.put_form('password', 'pw'))
  57. print(wt.put_form('submit', '', 'Submit'))
  58. print("</form><p>")
  59. return 0
  60. def admin_splash():
  61. print("""<pre>
  62. - change settings
  63. - moderate threads
  64. - modify wordfilter
  65. </pre>""")
  66. if not wt.get_form('mode') or not wt.get_form('thread'):
  67. print("<h2>Settings</h2>")
  68. print("\n".join(["<br> - "+str(i) for i in b_conf]))
  69. for s in cd.keys():
  70. print("<p>",s + ":<br>&emsp;", cd[s])
  71. print("<h2>Threads</h2>")
  72. if wt.get_form('more'):
  73. print("<a href='.'>Back</a><br>")
  74. print(wt.get_form('thread'), "<hr>")
  75. # print(load_thread(wt.get_form('thread')))
  76. show_thread(load_thread(wt.get_form('thread')))
  77. else:
  78. mod_threads()
  79. def mod_threads():
  80. print("<pre>")
  81. with open(b_conf[6]) as t_list:
  82. print(t_list.read())
  83. print("</pre>")
  84. ti = thread_index()
  85. for t in ti["ti"]:
  86. # t = filename
  87. # t[0] last reply time, t[1] thread title
  88. # t[2] reply count, t[3] thread mode
  89. mic = mode_icons(ti[t][3])
  90. tm = [f"<img src='{m}'>" for m in mic]
  91. if ti[t][3] in modes:
  92. ti[t][3] = modes[ti[t][3]]
  93. mk = list(modes.keys())
  94. mv = [modes[i] for i in mk]
  95. dropdown = wt.dropdown("mode", mk, mv)
  96. ti[t][3] = dropdown.replace(f">{ti[t][3]}", \
  97. f" selected>{ti[t][3]}")
  98. print(op.format(t, ti[t], " ".join(tm)))
  99. def thread_index():
  100. with open(b_conf[6]) as t_list:
  101. t_list = t_list.read().splitlines()
  102. t = {}
  103. t["ti"] = []
  104. for th in t_list:
  105. th = th.split(" >< ")
  106. t["ti"].append(th[0])
  107. t[th[0]] = th[1:]
  108. return t
  109. def load_thread(thr='0'):
  110. # print(b_conf[5] + thr)
  111. with open(b_conf[5] + thr, 'r') as thr:
  112. thr = thr.read().splitlines()
  113. for n, th in enumerate(thr):
  114. thr[n] = th.split(' >< ')
  115. return thr
  116. def show_thread(thr=[]):
  117. if not thr:
  118. return None
  119. table = ["<table>"]
  120. table.append("<tr><td><td>Name<td>Date<td>Comment")
  121. print("<tr><th colspan='4'>", thr.pop(0)[0])
  122. for n, t in enumerate(thr):
  123. t.pop(2)
  124. t = f"<tr><td>{n+1}.<td>" + "<td>".join(t)
  125. table.append(t)
  126. print("\n".join(table), "</table>")
  127. def tripcode(pw):
  128. pw = pw[:8]
  129. salt = (pw + "H..")[1:3]
  130. trip = crypt.crypt(pw, salt)
  131. return (trip[-10:])
  132. def main():
  133. print(wt.head(b_conf[0]))
  134. print("<h2>", b_conf[0], "admin</h2>")
  135. # print(wt.get_cookie())
  136. if login_admin() == 1:
  137. admin_splash()
  138. main()