diff --git a/default.css b/default.css index 0a39f32..6325eb7 100644 --- a/default.css +++ b/default.css @@ -3,9 +3,6 @@ This file could be used under both CC-BY-SA 4.0 or at your chosing GNU GPLv3 or */ html {background-color: #353849; - background-image: url("/pictures/new_pattern.jpg"); - background-size: cover; - background-attachment: fixed; } } diff --git a/modules/Common.py b/modules/Common.py index 9321e78..a6d88f5 100644 --- a/modules/Common.py +++ b/modules/Common.py @@ -61,3 +61,44 @@ def Simplify(text): ntext = ntext + "_" return ntext + +def RandString(n=50): + # this will make a random string + s = "" + good = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890" + + for i in range(n): + s = s + random.choice(good) + + return s + + +IDColors = {} + +def consoleForm(obj): + + # Function that prints a pretty object info about + + ID = obj + NAME = obj[-10:] + + c = IDcolor(ID) + + return c + " " + NAME + " " + clr["norm"] + +def IDcolor(ID): + + # Coloring + if ID not in IDColors: + r = random.random() + g = random.random() + b = random.random() + + ct = clr["tbwh"] + if max(r, g, b ) > 0.9 or sum((r, g, b))/3 > 0.5: + ct = clr["tdbl"] + + c = clr["bold"] + "\033[48;2;"+str(int(r*256))+";"+str(int(g*256))+";"+str(int(b*256))+"m"+ct + IDColors[ID] = c + c = IDColors[ID] + return c diff --git a/modules/Help.py b/modules/Help.py index bd5cea9..1709a57 100644 --- a/modules/Help.py +++ b/modules/Help.py @@ -4,19 +4,30 @@ from modules.Common import * def Help(): - print(clr["bold"]+" BDServer Help Page"+clr["norm"]) + + print() + print(clr["bold"]+" BDServer Help"+clr["norm"]) + print() + print(clr["tdyl"]+"--help"+clr["norm"]+" - Prints this Help Page.") + print(clr["tdyl"]+"--run"+clr["norm"]+" - Run the server.") + print(clr["tdyl"]+"--set"+clr["norm"]+" - Use for changing settings.") + print(clr["tdyl"]+"--config"+clr["norm"]+" - Edit config file directly, bypassing --set.") + print(clr["tdyl"]+"--folder"+clr["norm"]+" - Open the folder of the website's data.") print() - print(clr["tdyl"]+"--help"+clr["norm"]+" - Prints this Help Page.") - print(clr["tdyl"]+"--run"+clr["norm"]+ " - Run the server.") - print(clr["tdyl"]+"--set"+clr["norm"]+ " - Use for changing settings.") def Set(): - print(clr["bold"]+" BDServer --set Help Page"+clr["norm"]) print() - print(" --set is used for setting up server configurations.") + print(clr["bold"]+" BDServer Set Help "+clr["norm"]) + print() + print(" Set is used for setting up server configurations.") print() print(clr["tdyl"]+"--title"+clr["norm"]+" - Set title of the website.") + print(clr["tdyl"]+"--tagline"+clr["norm"]+" - Set tagline of the website.") print(clr["tdyl"]+"--port"+clr["norm"]+" - Set port where to run the website.") + print(clr["tdyl"]+"--css"+clr["norm"]+" - Set a CSS file.") + print(clr["tdyl"]+"--css_edit"+clr["norm"]+" - edit a CSS file.") + print(clr["tdyl"]+"--favicon"+clr["norm"]+" - Set a favicon file.") print(clr["tdyl"]+"--add_tab"+clr["norm"]+" - Adds a category of articles.") print(clr["tdyl"]+"--edit_tab"+clr["norm"]+" - Edit the config of a category.") + print() diff --git a/modules/Render.py b/modules/Render.py index ff13c5d..6fb45ae 100644 --- a/modules/Render.py +++ b/modules/Render.py @@ -1,7 +1,11 @@ # AGPL 3 or any later version # (C) J.Y.Amihud ( Blender Dumbass ) +import os +import json + from modules import Set +from modules.Common import * def guess_type(path): @@ -18,17 +22,26 @@ def guess_type(path): def headers(server, code): + # Basic cookie for logins to work + cookie = False + if "Cookie" not in str(server.headers): + cookie = True + server.send_response(code) server.send_header("Content-type", guess_type(server.path)) + + if cookie: + server.send_header("Set-Cookie", RandString()) + server.end_headers() def head(title="", description="", image="", config={}): if image.startswith("/"): image = config.get("url","")+image - html = """ - + favicon = config.get("favicon", "/icon/internet") + html = """ @@ -37,7 +50,7 @@ def head(title="", description="", image="", config={}): """+title+""" - + @@ -59,7 +72,22 @@ def send(server, html, code): headers(server, code) server.wfile.write(html.encode("utf-8")) +def tabs(): + folder = Set.Folder()+"/tabs" + tabs = {} + for tab in sorted(list(os.walk(folder))[0][1]): + try: + with open(folder+"/"+tab+"/config.json") as o: + data = json.load(o) + tabs[tab] = data + except Exception as e: + print(e) + pass + + return tabs + + def MainPage(server): # Reading config @@ -72,9 +100,42 @@ def MainPage(server): ) - html = html + "hello world" + html = html + """ + +
+
+
+ [LOGO] +
+
+ +

"""+config.get("title", "My Website")+"""

+ """+config.get("tagline", "")+""" +
+
+ + + """ + Tabs = tabs() + for tab in Tabs: + + html = html + Button(Tabs[tab].get("title", tab), + "/"+tab, + Tabs[tab].get("icon", "folder")) + send(server, html, 200) +def Button(text, link, icon=""): + html = """ + """ + if icon: + html = html + """ + """ + html = html + """ + """+text+""" + + """ + return html diff --git a/modules/Run.py b/modules/Run.py index 8211f82..8517bcf 100644 --- a/modules/Run.py +++ b/modules/Run.py @@ -20,12 +20,38 @@ from modules.Common import * class handler(BaseHTTPRequestHandler): + def log_message(self, format, *args): + + if "?" in self.path: + self.path = self.path[:self.path.find("?")] + + q = "" + for i in self.parsed: + if i != "password": + q = q + "[ "+i+" : "+self.parsed[i][0]+" ] " + else: + q = q + "[ "+i+" : ******* ] " + + toprint = consoleForm(self.cookie)+" "+"[ "+str(datetime.datetime.now())+" ] "+self.path+" "+q + print(toprint) + + folder = Set.Folder() + filename = folder+"/logs/"+datetime.datetime.now().strftime('%Y-%m-%d')+".log" + logfile = open(filename, "ab") + logfile.write((toprint+"\n").encode('utf-8')) + logfile.close() + def do_GET(self): self.path = self.path.replace("/..", "/") self.path = self.path.replace("%27", "'") - if self.path == "/": + parsed_url = urllib.parse.urlparse(self.path) + + self.parsed = urllib.parse.parse_qs(parsed_url.query) + self.cookie = self.headers.get("Cookie") + + if self.path[:self.path.find("?")] == "/" or self.path == "/": Render.MainPage(self) elif self.path.startswith("/pictures/"): @@ -38,10 +64,23 @@ class handler(BaseHTTPRequestHandler): elif self.path == "/css": - cssfile = open("default.css", "rb") + config = Set.Load() + filename = config.get("css", "default.css") + cssfile = open(filename, "rb") cssfile = cssfile.read() Render.headers(self, 200) self.wfile.write(cssfile) + + elif self.path.startswith("/icon/"): + + folder = "icons" + icon = folder+"/"+self.path[6:]+".png" + f = open(icon, "rb") + f = f.read() + Render.headers(self, 200) + self.wfile.write(f) + + config = Set.Load() PORT = config.get("port", 8080) diff --git a/modules/Set.py b/modules/Set.py index 4cc1250..5457ea2 100644 --- a/modules/Set.py +++ b/modules/Set.py @@ -21,6 +21,13 @@ def Folder(): except: pass + # Adding nessesary subcategories + for i in ["pictures", "tabs", "logs"]: + try: + os.makedirs(data_dir+"/"+i) + except: + pass + return data_dir def Load(): @@ -56,7 +63,18 @@ def Set(): Title(title) except Exception as e: - print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Title Wasn't Specified!") + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Title Wasn't Specified!") + print('Use: $ python3 run.py --set --title "My Website"') + + if "--tagline" in sys.argv: + try: + tagline = sys.argv[ sys.argv.index("--tagline") + 1] + if "--" in tagline: 1/0 # Failing this for the error message. + Tagline(tagline) + + except Exception as e: + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tagline Wasn't Specified!") + print('Use: $ python3 run.py --set --title "Ultimate Club for Ultimate Hackers"') if "--port" in sys.argv: try: @@ -64,8 +82,8 @@ def Set(): Port(port) except Exception as e: - print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Port Wasn't Specified Correctly!", e) - + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Port Wasn't Specified Correctly!") + print('Use: $ python3 run.py --set --port 8080') if "--add_tab" in sys.argv: try: @@ -75,6 +93,7 @@ def Set(): except Exception as e: print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!") + print('Use: $ python3 run.py --set --add_tab "Articles"') if "--edit_tab" in sys.argv: try: @@ -84,7 +103,32 @@ def Set(): except Exception as e: print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!") + print('Use: $ python3 run.py --set --edit_tab "Articles"') + if "--css" in sys.argv: + try: + filename = sys.argv[ sys.argv.index("--css") + 1] + if "--" in filename: 1/0 # Failing this for the error message. + SetCSS(filename) + + except Exception as e: + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" CSS File Wasn't Specified!", e) + print('Use: $ python3 run.py --set --css ~/mycssfile.css') + + if "--favicon" in sys.argv: + try: + filename = sys.argv[ sys.argv.index("--favicon") + 1] + if "--" in filename: 1/0 # Failing this for the error message. + SetFavicon(filename) + + except Exception as e: + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" File Wasn't Specified!", e) + print('Use: $ python3 run.py --set --css ~/favicon.png') + + if "--css_edit" in sys.argv: + config = Load() + os.system("nano "+config.get("css", "default.css")) + def Title(title): data = Load() @@ -94,6 +138,15 @@ def Title(title): print(clr["bold"]+clr["tbyl"]+title+clr["norm"]+" is set as title.") +def Tagline(tagline): + + data = Load() + data["tagline"] = tagline + + Save(data) + + print(clr["bold"]+clr["tbyl"]+tagline+clr["norm"]+" is set as tagline.") + def Port(port): data = Load() @@ -131,7 +184,40 @@ def EditTab(tab): tabsFolder = Folder()+"/tabs/"+Simplify(tab) os.system("nano "+tabsFolder+"/config.json") - +def SetCSS(filename): - - + try: + f = open(filename, "r") + f = f.read() + except Exception as e: + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot Read CSS file!", e) + return + + css = open(Folder()+"/style.css", "w") + css.write(f) + css.close() + + config = Load() + config["css"] = Folder()+"/style.css" + Save(config) + + print("New CSS is set at "+clr["bold"]+Folder()+"/style.css"+clr["norm"]) + +def SetFavicon(filename): + + try: + f = open(filename, "rb") + f = f.read() + except Exception as e: + print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot Read Image!", e) + return + + css = open(Folder()+"/pictures/favicon.png", "wb") + css.write(f) + css.close() + + config = Load() + config["favicon"] = "/pictures/favicon.png" + Save(config) + + print("New favicon is set at "+clr["bold"]+Folder()+"/pictures/favicon.png"+clr["norm"]) diff --git a/run.py b/run.py index f9d6161..e6ec45b 100644 --- a/run.py +++ b/run.py @@ -2,6 +2,7 @@ # (C) J.Y.Amihud ( Blender Dumbass ) # Basic sys to get through to different modules +import os import sys if len(sys.argv) < 2 or "--help" in sys.argv: from modules import Help @@ -11,5 +12,15 @@ elif "--set" in sys.argv: from modules import Set Set.Set() +elif "--config" in sys.argv: + from modules import Set + data = Set.Load() # Making sure + Set.Save(data) # that config.json exists + os.system("nano "+Set.Folder()+"/config.json") + elif "--run" in sys.argv: from modules import Run + +elif "--folder" in sys.argv: + from modules import Set + os.system("xdg-open "+Set.Folder())