Advertisement
AceScottie

gui-tester.py

Jul 30th, 2018
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.66 KB | None | 0 0
  1. from Tkinter import *
  2. import os, time, sys, subprocess, webbrowser, random, struct, hashlib, pysftp, md5, stat, binascii, MySQLdb, datetime, threading
  3. from PIL import Image, ImageTk, ImageDraw, ImageFont
  4. from Crypto.Cipher import AES
  5. ##FIXES--------------------------------------------------------
  6. if getattr(sys, 'frozen', False):
  7.     os.chdir(sys._MEIPASS)
  8. def retag(tag, *args):
  9.     for widget in args:
  10.         widget.bindtags((tag,) + widget.bindtags())
  11. #--------------------------------------------------------------
  12. class GUI:
  13.     def __init__(self):
  14.         self.root = Tk()
  15.         self.DB = MySQLdb.connect(host="my host", user="user", passwd="password", db="pyencrypt")
  16.         self.sftp = SFTP()
  17.         self.sql = SQL(self.DB)
  18.         self.Thd = Threads(self.sftp, self.DB)
  19.        
  20.         self.active_window = None
  21.         self.mainCanvas = None
  22.         self.quit = False
  23.         self.icons = []
  24.         self.gen_icons()
  25.         self.cwd = str(os.environ['USERPROFILE'] + "/").replace("\\", "/")
  26.         self.rcwd = '/DataVolume/shares/Public/encrypt/main/'
  27.         self.LExplorer = None
  28.         self.RExplorer = None
  29.         self.Lcwd = None
  30.         self.Rcwd = None
  31.         self.LC = None
  32.         self.Rscroll = None
  33.         self.LScroll = None
  34.    
  35.     def quit(self):
  36.         print("quitting")
  37.         self.quit = True
  38.         self.root.destroy()
  39.         self.root.quit()
  40.  
  41.     def create_window(self):
  42.         if self.active_window != None:
  43.             clear_window()
  44.         window = Canvas(self.mainCanvas, bg="white", relief="groove", highlightbackground="black", highlightcolor="black", highlightthickness=1, borderwidth=4)
  45.         window.place(x=root.winfo_pointerx() - root.winfo_rootx()-100, y=root.winfo_pointery() - root.winfo_rooty())
  46.         windowFrame = Frame(mc)
  47.         windowFrame.pack(fill=BOTH, expand=1, padx= 5, pady=5)
  48.         self.active_window = mc
  49.  
  50.     def clear_window(self):
  51.         print("clearing Window")
  52.         self.active_window.destroy()
  53.  
  54.     def gen_icons(self): #icons[notify0, notify1, folder, default]
  55.         NotifyImage0 = Image.open("assets/notifacation0.gif")
  56.         NotifyImage0 = NotifyImage0.resize((32, 32), Image.ANTIALIAS)
  57.         NotifyIcon0 = ImageTk.PhotoImage(NotifyImage0)
  58.         NotifyImage1 = Image.open("assets/notifacation1.gif")
  59.         NotifyImage1 = NotifyImage1.resize((32, 32), Image.ANTIALIAS)
  60.         NotifyIcon1 = ImageTk.PhotoImage(NotifyImage1)
  61.         folderImage = Image.open("assets/folder.gif")
  62.         folderImage = folderImage.resize((32, 32), Image.ANTIALIAS)
  63.         folderIcon = ImageTk.PhotoImage(folderImage)
  64.         defaultImage = Image.open("assets/default.gif")
  65.         defaultImage = defaultImage.resize((32, 32), Image.ANTIALIAS)
  66.         defaultIcon = ImageTk.PhotoImage(defaultImage)
  67.         icons = [NotifyIcon0, NotifyIcon1, folderIcon, defaultIcon]
  68.         self.icons = icons
  69.  
  70.     def cButton(self, element, borderwidth=0, bg="white", text="", relief="groove", command=None, side=TOP, expand=0, fill=NONE):
  71.             b = Button(element, borderwidth=borderwidth, bg=bg, text=text, relief=relief, command=command)
  72.             b.pack(side=side, expand=expand, fill=fill)
  73.             return b
  74.  
  75.     def cFrame(self, element, bg="white", borderwidth=0, relief="groove", side=TOP, padx=0, pady=0, expand=0, fill=NONE):
  76.         f = Frame(element, bg=bg, borderwidth=borderwidth, relief=relief)
  77.         f.pack(side=side, padx=padx, pady=pady, expand=expand, fill=fill)
  78.         return f
  79.  
  80.     def create_main_window(self):
  81.         self.root.config(bg="white")
  82.         self.root.iconbitmap("assets/encryptor.ico")
  83.         self.root.title("pyEncryptor")
  84.         self.root.geometry("850x600")
  85.         mainCanvas = Canvas(self.root)
  86.         #mainCanvas.bind('<Button-1>', test)
  87.         #mainCanvas.bind('<Button-2>', test)
  88.         mainCanvas.pack(fill=BOTH, expand=1)
  89.         menuFrame=self.cFrame(mainCanvas, bg="white", borderwidth=2, relief="groove", padx=2, pady=2, fill=X, side=TOP)
  90.         folderbutton = self.cButton(menuFrame, borderwidth=0, bg="white", side=LEFT, expand=1)
  91.         folderbutton.config(image=self.icons[2], height=32, width=32)
  92.         notifacationbutton = self.cButton(menuFrame, borderwidth=0, bg="white", side=LEFT, expand=1)
  93.         notifacationbutton.config(image=self.icons[0], height=32, width=32)
  94.         LF = self.cFrame(mainCanvas, bg="white", side=TOP, fill=X)
  95.         LLF = self.cFrame(LF, side=LEFT, padx=2, pady=2, expand=1, fill=X)
  96.         RLF = self.cFrame(LF, side=LEFT, padx=2, pady=2, expand=1, fill=X)
  97.         LLabel = Label(LLF, text="Local Files", borderwidth=2, relief="raised", bg="white")
  98.         LLabel.pack(expand=1, fill=X)
  99.         RLabel = Label(RLF, text="Remote Files", borderwidth=2, relief="raised", bg="white")
  100.         RLabel.pack(expand=1, fill=X)
  101.         FrameHolder = self.cFrame(mainCanvas, expand=1, fill=BOTH)
  102.         localFrame = self.cFrame(FrameHolder, bg="white", borderwidth=2, relief="groove", expand=1, padx=2, pady=2, fill=BOTH, side=LEFT)
  103.         remoteFrame = self.cFrame(FrameHolder, bg="white", borderwidth=2, relief="groove", expand=1, padx=2, pady=2, fill=BOTH, side=RIGHT)
  104.         localNav = self.cFrame(localFrame, bg="white", side=TOP, fill=X)
  105.         remoteNav = self.cFrame(remoteFrame, bg="white", side=TOP, fill=X)
  106.         self.Lcwd = Entry(localNav, borderwidth=2, relief="groove", bg="white")
  107.         self.Lcwd.pack(side=LEFT, fill=X, expand=1)
  108.         self.Lcwd.insert(0, self.cwd)
  109.         self.Rcwd = Entry(remoteNav, borderwidth=2, relief="groove", bg="white")
  110.         self.Rcwd.pack(side=LEFT, fill=X, expand=1)
  111.         self.Rcwd.insert(0, self.rcwd)
  112.         lenter = self.cButton(localNav, text="Enter", borderwidth=2, relief="groove", side=RIGHT)
  113.         retag("localenter", lenter)
  114.         lenter.bind_class("localenter", "<Button-1>",lambda e=Event(), side="Local", textvar=""  :self.change_dir(e, side, textvar))
  115.         renter = self.cButton(remoteNav, text="Enter", borderwidth=2, relief="groove", side=RIGHT)
  116.         retag("remoteenter", renter)
  117.         renter.bind_class("remoteenter", "<Button-1>", lambda e=Event(), side="Remote", textvar=""  :self.change_dir(e, side, textvar))
  118.         LocalFrameExplorer = self.cFrame(localFrame, bg="white", fill=BOTH, expand=1)
  119.         def update_scrollregion(event):
  120.             self.LC.configure(scrollregion=self.LC.bbox("all"))
  121.         LFrame = self.cFrame(LocalFrameExplorer, fill=BOTH, expand=1)
  122.         LFrame.rowconfigure(0, weight=0)
  123.         LFrame.columnconfigure(0, weight=1)
  124.         self.LC = Canvas(LFrame, bg="white")
  125.         self.LC.pack(side=LEFT, expand=1, fill=BOTH)
  126.         self.LExplorer = self.cFrame(self.LC, bg="white", side=LEFT, expand=1, fill=BOTH)
  127.         self.LC.create_window(0, 0, window=self.LExplorer, anchor='nw')
  128.         self.LScroll = Scrollbar(LFrame, orient=VERTICAL)
  129.         self.LScroll.config(command=self.LC.yview)
  130.         self.LC.config(yscrollcommand=self.LScroll.set)
  131.         self.LScroll.pack(side=RIGHT, fill=Y)
  132.         self.LExplorer.bind("<Configure>", update_scrollregion)
  133.         RemoteFrameExplorer = self.cFrame(remoteFrame, bg="white", fill=BOTH, expand=1)
  134.         def update_scrollregion(event):
  135.             RC.configure(scrollregion=RC.bbox("all"))
  136.         RFrame = self.cFrame(RemoteFrameExplorer, fill=BOTH, expand=1)
  137.         RFrame.rowconfigure(0, weight=0)
  138.         RFrame.columnconfigure(0, weight=1)
  139.         RC = Canvas(RFrame, bg="white")
  140.         RC.pack(side=LEFT, expand=1, fill=BOTH)
  141.         self.RExplorer = self.cFrame(RC, bg="white", expand=1, fill=BOTH)
  142.         RC.create_window(0, 0, window=self.RExplorer, anchor='nw')
  143.         self.Rscroll = Scrollbar(RFrame, orient=VERTICAL)
  144.         self.Rscroll.config(command=RC.yview)
  145.         RC.config(yscrollcommand=self.Rscroll.set)
  146.         self.Rscroll.pack(side=RIGHT, fill=Y)
  147.         self.RExplorer.bind("<Configure>", update_scrollregion)
  148.         statusFrame = self.cFrame(mainCanvas, bg="#BBBBBB", borderwidth=2, relief="groove", fill=X, padx=2, pady=2, side=BOTTOM)
  149.         statusLabel = Label(statusFrame, text="Working", bg="#BBBBBB")
  150.         statusLabel.pack(side=LEFT)
  151.         lfolders, lfiles = self.sftp.get_files_folders("Local", self.cwd)
  152.         rfolders, rfiles = self.sftp.get_files_folders("Remote", self.rcwd)
  153.         self.gen_buttons(self.LExplorer, "Local", lfolders, lfiles)
  154.         self.gen_buttons(self.RExplorer, "Remote", rfolders, rfiles)
  155.        
  156.     def gen_buttons(self, element, side, folders, files):
  157.         colset = 0
  158.         size = (self.root.winfo_width())*2+25
  159.         Packer = self.cFrame(element, bg="white", borderwidth=2, relief=RAISED)
  160.         L= Label(Packer, image=self.icons[2], width=size, anchor=W, justify=LEFT, text="..", bg="white", borderwidth=0, compound=LEFT)
  161.         L.pack(side=TOP, fill=Y)
  162.         Packer.grid(padx=0, pady=5, sticky="w", row=0, column=0)
  163.         retag(side+".back", Packer, L)
  164.         Packer.bind_class(side+".back", "<Button-1>", lambda e=Event(), side=side, textvar=".." :self.change_dir(e, side, textvar))
  165.         rowset = 1
  166.         for i in folders:
  167.             if i[0:1] ==".":
  168.                 j = i[1::]
  169.             else:
  170.                 j = i
  171.             Packer = self.cFrame(element, bg="white", borderwidth=2, relief=RAISED)
  172.             L= Label(Packer, image=self.icons[2], width=size, anchor=W, justify=LEFT, text=i, bg="white", borderwidth=0, compound=LEFT)
  173.             L.pack(side=TOP, fill=Y)
  174.             Packer.grid(padx=0, pady=5, sticky="w", row=rowset, column=colset)
  175.             retag(j, Packer, L)
  176.             Packer.bind_class(j, "<Button-1>", lambda e=Event(), side=side, textvar=str(i) :self.change_dir(e, side, textvar))
  177.             rowset+=1
  178.         for i in files:
  179.             if i[0:1] ==".":
  180.                 j = i[1::]
  181.             else:
  182.                 j = i
  183.             Packer = self.cFrame(element, bg="white", borderwidth=2, relief=RAISED)
  184.             L= Label(Packer, image=self.icons[3], width=size, anchor=W, justify=LEFT, text=i, bg="white", borderwidth=0, compound=LEFT)
  185.             L.pack(side=TOP, fill=Y)
  186.             Packer.grid(padx=0, pady=5, sticky="w", row=rowset, column=colset)
  187.             retag(j, Packer, L)
  188.             Packer.bind_class(j, "<Button-1>", lambda e=Event(), side=side, textvar=str(i):createpopup(e, side, textvar))  
  189.             rowset+=1
  190.         self.reset_scroll(side)
  191.            
  192.     def login(self):
  193.         for widget in self.root.winfo_children():
  194.             widget.destroy()
  195.         self.create_main_window()
  196.        
  197.     def login_window(self):
  198.         self.root.protocol('WM_DELETE_WINDOW', self.quit)
  199.         self.root.iconbitmap("assets/encryptor.ico")
  200.         windowTFrame1= self.cFrame(self.root, side=TOP)
  201.         windowTFrame2= self.cFrame(self.root, side=TOP)
  202.         windowBFrame= self.cFrame(self.root, side=BOTTOM)
  203.         userL = Label(windowTFrame1, text="username:")
  204.         userE = Entry(windowTFrame1)
  205.         passE = Entry(windowTFrame2, show="*")
  206.         passL = Label(windowTFrame2, text="password:")
  207.         userL.pack(side=LEFT)
  208.         userE.pack(side=RIGHT)
  209.         passL.pack(side=LEFT)
  210.         passE.pack(side=RIGHT)
  211.         userE.insert(0, "AceScott")
  212.         passE.insert(0, "AceScott")
  213.         B = self.cButton(windowBFrame, text="Login", command=self.login)
  214.         self.root.mainloop()
  215.        
  216.     def change_dir(self, event, side, folder):
  217.         if folder == "..":
  218.             if side == "Local":
  219.                 cwd = str(self.Lcwd.get())
  220.                 tmp= cwd.split("/")
  221.                 cwd = ""
  222.                 for i in range(len(tmp)-2):
  223.                     cwd+=tmp[i]+"/"
  224.                 if cwd[-1:] != "/":
  225.                     eof = "/"
  226.                 elif cwd[-1:] == "/":
  227.                     eof = ""
  228.                 self.Lcwd.delete(0, END)
  229.                 self.Lcwd.insert(0, cwd)
  230.                 lfolders, lfiles= self.sftp.get_files_folders("Local", cwd)
  231.                 self.gen_buttons(self.LExplorer, "Local", lfolders, lfiles)
  232.             elif side == "Remote":
  233.                 cwd = str(self.Rcwd.get())
  234.                 tmp= cwd.split("/")
  235.                 cwd = ""
  236.                 for i in range(len(tmp)-2):
  237.                     cwd+=tmp[i]+"/"
  238.                 if cwd[-1:] != "/":
  239.                     eof = "/"
  240.                 elif cwd[-1:] == "/":
  241.                     eof = ""
  242.                 cwd = cwd+eof
  243.                 self.Rcwd.delete(0, END)
  244.                 self.Rcwd.insert(0, cwd)
  245.                 lfolders, lfiles= self.sftp.get_files_folders("Remote", cwd)
  246.                 self.gen_buttons(self.RExplorer, "Remote", lfolders, lfiles)
  247.         else:
  248.             try:
  249.                 self.clear_buttons(side)
  250.                 if side == "Local":
  251.                     if folder == "":
  252.                         eof = ""
  253.                     elif folder[-1:] != "/":
  254.                         eof = "/"
  255.                     elif folder[-1:] == "/":
  256.                         eof = ""
  257.                     cwd = str(self.Lcwd.get())+folder+eof
  258.                     self.Lcwd.delete(0, END)
  259.                     self.Lcwd.insert(0, cwd)
  260.                     lfolders, lfiles= self.sftp.get_files_folders("Local", cwd)
  261.                     self.gen_buttons(self.LExplorer, "Local", lfolders, lfiles)
  262.                 elif side == "Remote":
  263.                     if folder == "":
  264.                         eof = ""
  265.                     elif folder[-1:] != "/":
  266.                         eof = "/"
  267.                     elif folder[-1:] == "/":
  268.                         eof = ""
  269.                     cwd = str(self.Rcwd.get())+folder+eof
  270.                     self.Rcwd.delete(0, END)
  271.                     self.Rcwd.insert(0, cwd)
  272.                     rfolders, rfiles = self.sftp.get_files_folders("Remote", cwd)
  273.                     self.gen_buttons(self.RExplorer, "Remote", rfolders, rfiles)
  274.             except OSError as err:
  275.                 raise
  276.                 self.error("Sorry. There was an issue getting the cwd.\n Please contact your network administrator.\n %s" %err)
  277.            
  278.     def clear_buttons(self, side):
  279.         if side == "Local":
  280.             element = self.LExplorer
  281.         elif side == "Remote":
  282.             element = self.RExplorer
  283.         list = element.grid_slaves()
  284.         for l in list:
  285.             l.destroy()
  286.        
  287.  
  288.     def reset_scroll(self, side):
  289.         if side == "Local":
  290.             self.LScroll.set(0, 0)
  291.         elif side == "Remote":
  292.             self.RScroll.set(0, 0)
  293.            
  294.     def error(self, err=""):
  295.         window = Toplevel(self.root)
  296.         window.iconbitmap("assets/encryptor.ico")
  297.         windowTFrame= self.cFrame(window, side=TOP)
  298.         L = Label(windowTFrame, text=err, justify=CENTER, bg="white", borderwidth=0)
  299.         L.pack()
  300.         windowBFrame= GUI.cFrame(window, side=BOTTOM)
  301.         OK = self.cButton(windowBFrame, command=lambda w=window:close_window(w), text="Confirm", fg="#07a501" )
  302.  
  303. class SFTP:
  304.     def __init__(self):
  305.         cnopts = pysftp.CnOpts()
  306.         cnopts.hostkeys = None
  307.         self.sftp =  pysftp.Connection('my host', username='user', password='password', cnopts=cnopts)
  308.         self.sftp_busy = False
  309.        
  310.     def get_files_folders(self, side, location):
  311.         print("running get_files_folders with location as %s and side of %s" %(location, side))
  312.         files=[]
  313.         folders=[]
  314.         if side == "Local":
  315.             print(location)
  316.             objects = os.listdir(location)
  317.             for i in range(len(objects)):
  318.                 objects[i] = str(objects[i])
  319.             for i in objects:
  320.                 fileattr = os.lstat(location+i)
  321.                 if stat.S_ISDIR(fileattr.st_mode):
  322.                     folders.append(i)
  323.                 if stat.S_ISREG(fileattr.st_mode):
  324.                     files.append(i)
  325.         elif side == "Remote":
  326.             while self.sftp_busy:
  327.                 time.sleep(0.1)
  328.             self.sftp_busy = True
  329.             objects = self.sftp.listdir(location)
  330.             self.sftp_busy = False
  331.             for i in range(len(objects)):
  332.                 objects[i] = str(objects[i])
  333.             for i in objects:
  334.                 fileattr = self.sftp.lstat(location+i)
  335.                 if stat.S_ISDIR(fileattr.st_mode):
  336.                     folders.append(i)
  337.                 if stat.S_ISREG(fileattr.st_mode):
  338.                     files.append(i)
  339.         return folders, files
  340.  
  341. class SQL:
  342.     def __init__(self, DB):
  343.         self.notifacation = []
  344.     def sql_checklogin(self, user, passwd):
  345.         key2 = ""
  346.         level = 0
  347.         id = 0
  348.         try:
  349.             cur = self.DB.cursor()
  350.             cur.execute("SELECT * FROM users where username = '%s' and password = '%s' LIMIT 1;" %(user, passwd))
  351.             data = cur.fetchall()
  352.             data = data[0]
  353.             id = data[0]
  354.             key2 = data[3]
  355.             level = data[4]
  356.             return True, key2, level, id
  357.         except OSError as err:
  358.             GUI.error("Sorry. There was an issue checking your login.\n Please contact your network administrator.\n %s" %err)
  359.             return False, key2, level, id      
  360.     def sql_set(self, location, user, filepass, ownerid):
  361.         try:
  362.             cur = self.DB.cursor()
  363.             cur.execute("INSERT INTO files (id, location, filepass, owner, ownerid) VALUES (NULL, '%s', '%s', '%s', '%s')" %(location, filepass, user, ownerid))
  364.             self.DB.commit()
  365.             return True
  366.         except OSError as err:
  367.             GUI.error("Sorry. There was an issue processing your file.\n Please contact your network administrator.\n %s" %err)
  368.             return False
  369.     def sql_del(self, location, user, ownerid):
  370.         global DB
  371.         try:
  372.             cur = self.DB.cursor()
  373.             cur.execute("DELETE FROM files WHERE location = '%s' AND owner = '%s' AND ownerid = '%s'" %(location, user, ownerid))
  374.             self.DB.commit()
  375.             return True
  376.         except OSError as err:
  377.             GUI.error("Sorry. There was an issue deleting your file.\n Please contact your network administrator.\n %s" %err)
  378.             return False
  379.     def sql_key1(self, location, user, id):
  380.         try:
  381.             cur = self.DB.cursor()
  382.             cur.execute("SELECT * FROM files where location = '%s' and owner = '%s' and ownerid = '%s' LIMIT 1;" %(location, user, id))
  383.             data = cur.fetchall()
  384.             data = data[0]
  385.             key1 = data[2]
  386.             return True, key1
  387.         except OSError as err:
  388.             GUI.error("Sorry. There was an issue getting the key your file.\n Please contact your network administrator.\n %s" %err)
  389.             return False, ""
  390.         except:
  391.             return False, ""   
  392.     def sql_share(self, suser, user, ofile, file, password, recieved, sharedate, expiredate):
  393.         try:
  394.             cur = self.DB.cursor()
  395.             cur.execute("INSERT INTO share (id, recuser, sentfrom, location, ofile, password, recieved, sharedate, expiredate, notifie, ack) VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0, 0)" %(suser, user, file, ofile, password, recieved, sharedate, expiredate))
  396.             self.DB.commit()
  397.             return True
  398.         except OSError as err:
  399.             GUI.error("Sorry. There was an issue sharing your file.\n Please contact your network administrator.\n %s" %err)
  400.             return False
  401.     def sql_notifacation(self):
  402.         tmp = []
  403.         data = None
  404.         print data
  405.         try:
  406.             cur = self.DB.cursor()
  407.             cur.execute("SELECT * FROM share WHERE sentfrom = '%s' AND recieved = 1 and ack = 0;" %(user))
  408.             data = cur.fetchall()
  409.             if len(data) >= 1:
  410.                 for i in range(len(data)):
  411.                     #0=id   1=recuser   2=sentfrom  3=location  4=ofile 5=password  6=recieved  7=sharedate 8=expiredate    9=notifie   10=ack
  412.                     #recuser ofile notify ack
  413.                     tmp = (data[i][1], data[i][4], data[i][9], data[i][10])
  414.                     self.notifacation.append(tmp)
  415.             return self.notifacation
  416.         except OSError as err:
  417.             GUI.error("Sorry. There was an issue Checking Notifacations.\n Please contact your network administrator.\n %s" %err)
  418.             return self.notifacation
  419.     def sql_whoami(self, location, file):
  420.         try:
  421.             cur = self.DB.cursor()
  422.             cur.execute("SELECT * FROM files where location = '%s' LIMIT 1;" %(location+file))
  423.             data = cur.fetchall()
  424.             data = data[0]
  425.             whouser = data[3]
  426.             GUI.error('The owner of the file "%s" is %s' %(file, whouser))
  427.         except OSError as err:
  428.             GUI.error("Sorry. There was an issue getting the key your file.\n Please contact your network administrator.\n %s" %err)   
  429.  
  430. class Threads:
  431.     def __init__(self, sftp, DB):
  432.         self.sftp = sftp
  433.         self.DB = DB
  434.  
  435.  
  436. if __name__ == "__main__":
  437.     try:
  438.         app = GUI()
  439.         app.login_window()
  440.     except KeyboardInterrupt:
  441.         quit = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement