Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- state1 = "Actions: [Download] Run Cancel"
- state2 = "Actions: Download [Run] Cancel"
- state3 = "Actions: Download Run [Cancel]"
- welcome1 = "[Previous] Enter URL Server List Exit"
- welcome2 = " Previous [Enter URL] Server List Exit"
- welcome3 = " Previous Enter URL [Server List] Exit"
- welcome4 = " Previous Enter URL Server List [Exit]"
- remove1 = "Remove server? [Yes] No"
- remove2 = "Remove server? Yes [No]"
- local items = {}
- local types = {}
- local href = {}
- local selection = nil
- local title = nil
- local url = nil
- local scroll = nil
- local home = nil
- local start = nil
- -- THE FOLLOWING CODE WAS WRITTEN BY XXM0DZXX --
- local WebsiteDatabase = "http://www.ccnet.netau.net/php/folder/" --Folder with all the sites
- local ApiPath = "http://www.ccnet.netau.net/php/mcmain.php" --Path API is stored
- function upload(path, domain, debug, user, pass)
- if user and pass then else
- user = "guest"
- pass = "guest"
- end
- if fs.exists(path) then
- file = fs.open(path, "r")
- if file then
- data = file.readAll()
- file.close()
- if not data then
- print("Failed to read " ..path)
- return
- end
- local response = http.post(
- ApiPath,
- "type=upload&"..
- "user=guest&"..
- "pass=guest&"..
- "name=".. textutils.urlEncode(domain) .. "&"..
- "data=".. textutils.urlEncode(data)
- )
- if response then
- local sResponse = response.readAll()
- response.close()
- print("Done!")
- print("Log: ")
- sleep(0.5)
- if sResponse == "success" then
- if debug then
- print("Uploading " ..shell.resolve(path).. " complete!")
- print("Errors: 0")
- print("\nGo to " ..domain.. " to check the site!")
- end
- return sResponse
- else
- if debug then
- print("Failed : " ..sResponse)
- end
- return sResponse
- end
- else
- if debug then
- print("Failed to connect to database.")
- end
- end
- else
- if debug then
- print("Failed to open " ..path)
- end
- end
- else
- if debug then
- print(path.. " doesn't exist.")
- end
- end
- end
- function delete(path, user, pass)
- local response = http.post(
- ApiPath,
- "type=delete&"..
- "user=" ..user.. "&"..
- "pass=" ..pass.. "&"..
- "name=".. textutils.urlEncode(path)
- )
- return response.readAll()
- end
- function lister()
- local response = http.post(
- ApiPath,
- "type=list&"..
- "user=guest&"..
- "pass=guest"
- )
- return response.readAll()
- end
- ------------------------------------------------
- -- CODE WRITTEN BY PAYMENTOPTION --
- local tMenuItems = { [1] = { Title = "Upload" }, [2] = { Title = "Delete" } } -- These are the extra menu options to be added.
- -- ^^ The above table holds a small menu that can be accessed anytime while connected.
- local nMenuSelection = 1 -- This is the current selection in the sub menu that contains the 'upload' and 'delete' options.
- local ScreenWidth, ScreenHeight = term.getSize() -- The x and y dimensions of the scree we're operating on.
- function DrawMenuItems() -- Draws the 'upload' and 'delete' menu.
- -- Draw the menu in the lower left hand corner of the screen.
- term.setCursorPos( 1, ScreenHeight )
- for i=1, #tMenuItems do -- Iterate through the menu that is stored in the tMenuItems table.
- if nMenuSelection == i then -- If the current item of this iteration is selected.
- term.write( "[" .. tMenuItems[i].Title .. "] " ) -- Draw the menu item that is currently selected with brackets around it.
- else -- If the current item of this iteration is not selected.
- term.write( " " .. tMenuItems[i].Title .. " " ) -- Draw the menu item that is not currently selection without brackets around it.
- end
- end
- end
- function DrawUploadMenu() -- Draws the menu presented when the upload option is activated.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "[Upload] Delete " ) ) ) -- Clear the line of all previous selections.
- -- Reposition the cursor after clearing the line.
- term.setCursorPos( 1, ScreenHeight )
- term.write( "File to upload: " )
- local UploadPath = shell.resolve( tostring( read() ) ) -- Get the absolute path of the file selected.
- if fs.exists( UploadPath ) then -- If the file path entered exists.
- -- Clear the line of the previously entered information.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "File to upload: " ) + string.len( UploadPath ) ) )
- -- Get the username for the apache server.
- term.setCursorPos( 1, ScreenHeight )
- term.write( "Username: " )
- local UserName = tostring( read() )
- -- Reclear the line to get the password.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Username: " ) + string.len( UserName ) ) )
- -- Get the password for the apache server.
- term.setCursorPos( 1, ScreenHeight )
- term.write( "Password: " )
- local PassWord = tostring( read() )
- -- Reclear the line for later usage.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Password: " ) + string.len( PassWord ) ) )
- -- If the uploading was succesfull.
- if upload( UploadPath, WebsiteDatabase, false, UserName, Password ) == "succes" then -- Upload the selected file to the current directory.
- term.setCursorPos( 1, ScreenHeight )
- term.write( "Success!" )
- sleep( 0.8 )
- -- Clear the line of the success message.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Success!" ) ) )
- -- If the uploading was unsuccessfull.
- else
- term.setCursorPos( 1, ScreenHeight )
- term.write( "Failure!" )
- sleep( 0.8 )
- -- Clear the line of the failure message.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Failure!" ) ) )
- end
- else -- If the file path entered does not exist.
- -- Clear the line of the previously entered information.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "File to upload: " ) + string.len( UploadPath ) ) )
- -- Draw the failure message due to an invalid file path.
- term.setCursorPos( 1, ScreenHeight )
- term.write( "File does not exist." )
- sleep( 0.8 )
- -- Clear the line of the failure message.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "File does not exist." ) ) )
- end
- end
- function DrawDeleteMenu()
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "[Upload] Delete " ) ) ) -- Clear the line of all previous selections.
- -- Reposition the cursor after clearing the line.
- term.write( "File to delete: " )
- local DeletionPath = read() -- Get the filename that we'll be deleting.
- -- Clear the line.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "File to delete: " ) + string.len( DeletionPath ) ) )
- -- Get the username for the apache server.
- term.write( "Username: " )
- local UserName = read()
- -- Clear the line.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Username: " ) + string.len( UserName ) ) )
- -- Get the password for the apache server.
- term.write( "Password: " )
- local PassWord = read()
- -- Attempt to delete the file.
- -- Clear the line.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Password: " ) + string.len( PassWord ) ) )
- -- Inform the user of the attempt.
- term.setCursorPos( 1, ScreenHeight )
- term.write( "Deletion attempted." )
- sleep( 0.8 )
- -- Clear the line.
- term.setCursorPos( 1, ScreenHeight )
- term.write( string.rep( " ", string.len( "Deletion attempted." ) ) )
- end
- function HandleKeyPress( Key )
- if Key == 205 then -- Right key.
- if nMenuSelection < #tMenuItems then
- nMenuSelection = nMenuSelection + 1
- end
- elseif Key == 203 then -- Left key.
- if nMenuSelection > 1 then
- nMenuSelection = nMenuSelection - 1
- end
- elseif Key == 43 then -- Enter key.
- if nMenuSelection == 1 then -- If the selection is on 'upload'.
- DrawUploadMenu()
- elseif nMenuSelection == 2 then -- If the selection is on 'delete'.
- DrawDeleteMenu()
- end
- end
- end
- -----------------------------------
- -- THE REST WAS WRITTEN BY 1LANN AND EPIC CODERZ --
- function welcomeSelect(wSel)
- if wSel == 1 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome1)
- event, key = os.pullEvent("key")
- if key == 205 then
- return welcomeSelect(2)
- elseif key == 28 then
- return 1
- else
- return welcomeSelect(1)
- end
- elseif wSel == 2 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome2)
- event, key = os.pullEvent("key")
- if key == 203 then
- return welcomeSelect(1)
- elseif key == 205 then
- return welcomeSelect(3)
- elseif key == 28 then
- return 2
- else
- return welcomeSelect(2)
- end
- elseif wSel == 3 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome3)
- event, key = os.pullEvent("key")
- if key == 203 then
- return welcomeSelect(2)
- elseif key == 205 then
- return welcomeSelect(4)
- elseif key == 28 then
- return 3
- else
- return welcomeSelect(3)
- end
- elseif wSel == 4 then
- term.setCursorPos(5, 8)
- term.clearLine()
- write(welcome4)
- event, key = os.pullEvent("key")
- if key == 203 then
- return welcomeSelect(3)
- elseif key == 28 then
- return 4
- else
- return welcomeSelect(4)
- end
- end
- end
- function debug(msg)
- term.setCursorPos(1,1)
- term.clearLine()
- write(msg)
- sleep(2)
- end
- local function contains(string, pattern)
- local a = string.find(string, pattern)
- if a ~= nil then
- return true
- else
- return false
- end
- end
- local function downloadrun(side)
- term.setCursorPos(1, 1)
- term.clearLine()
- if side == "state1" then
- write(state1)
- event, key = os.pullEvent("key")
- if key == 205 then
- return downloadrun("state2")
- elseif key == 28 then
- return "download"
- else
- return downloadrun("state1")
- end
- elseif side == "state2" then
- write(state2)
- event, key = os.pullEvent("key")
- if key == 203 then
- return downloadrun("state1")
- elseif key == 205 then
- return downloadrun("state3")
- elseif key == 28 then
- return "run"
- else
- return downloadrun("state2")
- end
- elseif side == "state3" then
- write(state3)
- event, key = os.pullEvent("key")
- if key == 203 then
- return downloadrun("state2")
- elseif key == 28 then
- return "cancel"
- else
- return downloadrun("state3")
- end
- end
- end
- local function drawGUI(items, scroll, selection, title, types)
- while true do
- term.clear()
- term.setCursorPos(1,1)
- write(title)
- term.setCursorPos(1,2)
- write("=================================================")
- for i = 1+scroll, #types do
- term.setCursorPos(45, (i+2)-scroll)
- write(types[i])
- end
- for d = 1+scroll, #items do
- term.setCursorPos(3, (d+2)-scroll)
- write(items[d])
- end
- local length = string.len(items[selection])
- term.setCursorPos(2, (selection+2)-scroll)
- term.write("[")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write("]")
- -- PAYMENT'S CODE --
- DrawMenuItems()
- --------------------
- event, key = os.pullEvent("key")
- -- PAYMENT'S CODE --
- HandleKeyPress( key )
- --------------------
- if key == 208 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == #items then
- elseif selection == scroll+16 then
- selection = selection+1
- scroll = scroll+1
- else
- selection = selection+1
- end
- elseif key == 200 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == 1 then
- elseif selection == scroll+1 then
- selection = selection-1
- scroll = scroll-1
- else
- selection = selection-1
- end
- elseif key == 28 then
- return(items[selection])
- elseif key == 16 then
- sleep(0.2)
- return("Press Q to Exit")
- else
- end
- end
- end
- local function serverDrawGUI(items, scroll, selection, title)
- while true do
- term.clear()
- term.setCursorPos(1,1)
- write(title)
- term.setCursorPos(1,2)
- write("=================================================")
- for d = 1+scroll, #items do
- term.setCursorPos(3, (d+2)-scroll)
- write(items[d])
- end
- local length = string.len(items[selection])
- term.setCursorPos(2, (selection+2)-scroll)
- term.write("[")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write("]")
- event, key = os.pullEvent("key")
- if key == 208 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == #items then
- elseif selection == scroll+16 then
- selection = selection+1
- scroll = scroll+1
- else
- selection = selection+1
- end
- elseif key == 200 then
- term.setCursorPos(2, (selection+2)-scroll)
- term.write(" ")
- term.setCursorPos(3+length, (selection+2)-scroll)
- term.write(" ")
- if selection == 1 then
- elseif selection == scroll+1 then
- selection = selection-1
- scroll = scroll-1
- else
- selection = selection-1
- end
- elseif key == 28 then
- return(items[selection])
- elseif key == 16 then
- sleep(0.2)
- return("exit")
- elseif key == 19 and selection ~= #items then
- return "remove", selection
- end
- end
- end
- local function process()
- local f= io.open("/.tmpdata", "r")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- f:read("*l")
- local data = f:read("*l")
- if data == nil then
- term.setCursorPos(1,1)
- term.clearLine()
- print("Not an Apache File Server! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- local titlestart = nil
- local derp = nil
- if string.find(data, "<h1>") == nil then
- term.setCursorPos(1,1)
- term.clearLine()
- print("Not an Apache File Server! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- titlestart = (string.find(data, "<h1>")+4)
- local titleend = (string.find(data, "</h1>")-1)
- title = string.sub(data, titlestart, titleend)
- items = {}
- types = {}
- href = {}
- local line = f:read("*l")
- apachev = 1
- if contains(line, "<table><tr><th>") then
- apachev = 2
- line = f:read("*l")
- end
- while contains(line, "<li>") or contains(line, "</td><td>") do
- rStart = (string.find(line, "<a href=")-4)
- lStart = string.find(line, "\"> ", rStart)
- if apachev == 2 then
- lStart = (string.find(line, "\">", rStart) + 2)
- else
- lStart = (string.find(line, "\"> ", rStart)+3)
- end
- lEnd = (string.find(line, "</a>", rStart) - 1)
- lData = string.sub(line, lStart, lEnd)
- table.insert(items, lData)
- hStart = (string.find(line, "<a href=\"", rStart) + 9)
- hEnd = (string.find(line, "\">", rStart) - 1)
- hData = string.sub(line, hStart, hEnd)
- table.insert(href, hData)
- if contains(lData, "Parent Directory") then
- lPStart = (string.find(line, "<a href=\"", rStart) + 9)
- lPEnd = (string.find(line, "\">", rStart) - 1)
- lPData = string.sub(line, lPStart, lPEnd)
- table.insert(types, "back")
- elseif contains(lData, "/") then
- table.insert(types, "dir")
- else
- table.insert(types, "file")
- end
- line = f:read("*l")
- end
- return title, items, types
- end
- local function isFile(filename, items, types)
- for i = 1, #items do
- if filename == items[i] and types[i] == "file" then
- return true
- end
- end
- return false
- end
- local function gethref(ref)
- for i = 1, #items do
- if items[i] == ref then
- return href[i]
- end
- end
- end
- local function loadPage(url)
- term.setCursorBlink(false)
- term.setCursorPos(1, 1)
- term.clearLine()
- write("Connecting...")
- http.request(url)
- while true do
- event, a, code = os.pullEvent()
- if event == "http_failure" then
- term.setCursorPos(1,1)
- term.clearLine()
- print("Connection failed! [Ok]")
- os.pullEvent("key")
- return "error"
- elseif event == "http_success" then
- break
- elseif event == "key" and a == 16 then
- return "error"
- end
- end
- if code == nil then
- term.setCursorPos(1, 1)
- term.clearLine()
- print("Page not found! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- f = io.open("/.tmpdata", "w")
- f:write(code:readAll())
- f:close()
- code:close()
- title, items, types = process()
- if title == "error" then
- return "error"
- end
- newpage = drawGUI(items, 0, 1, title, types)
- if isFile(newpage, items, types) then
- npage = gethref(newpage)
- dr = downloadrun("state1")
- if dr == "download" then
- term.setCursorPos(1, 1)
- term.clearLine()
- write("Download as: ")
- saveas = read()
- if contains(saveas, " ") or saveas == "" then
- return(url)
- else
- if contains(saveas, "/") then
- local reverse = string.reverse(saveas)
- local fDir = (string.len(saveas) - string.find(reverse, "/"))
- local cDir = string.sub(saveas, 1, fDir)
- if fs.exists(cDir) then
- local tmpF = http.get(url .. npage)
- local code = tmpF:readAll()
- tmpF:close()
- f = io.open(saveas , "w")
- f:write(code)
- f:close()
- return(url)
- else
- term.setCursorPos(1,1)
- term.clearLine()
- write("Directory does not exist! Press any key...")
- os.pullEvent("key")
- return(url)
- end
- end
- local tmpF = http.get(url .. npage)
- local code = tmpF:readAll()
- tmpF:close()
- f = io.open(saveas , "w")
- f:write(code)
- f:close()
- return(url)
- end
- elseif dr == "run" then
- local tmpF = http.get(url .. npage)
- local code = tmpF:readAll()
- tmpF:close()
- f = io.open("/.tmprun" , "w")
- f:write(code)
- f:close()
- term.clear()
- term.setCursorPos(1, 1)
- shell.run("/.tmprun")
- x, y = term.getCursorPos()
- if x > 6 then
- if y == 18 then
- term.setCursorPos(1, y)
- term.clearLine()
- else
- term.setCursorPos(1, y+1)
- end
- end
- print("Program ended. Press any key to continue")
- os.pullEvent("key")
- return(url)
- else
- return url
- end
- else
- if newpage == "Parent Directory" then
- return(start .. lPData)
- elseif newpage == "Press Q to Exit" then
- return("exitcode")
- else
- npage = gethref(newpage)
- return(url .. npage)
- end
- end
- end
- function main()
- start = string.sub(home, 1, string.len(home)-1)
- f = io.open("/.previous", "w")
- f:write(home)
- f:close()
- rhome = home
- hTest = string.gsub(home, "http://", "")
- a, b = string.find(hTest, "/")
- if b ~= nil then home = string.sub(home, 1, a+7)
- start = string.sub(home, 1, string.len(home)-1)
- end
- finish = loadPage(rhome)
- if finish == "error" then
- return "error"
- end
- while true do
- if finish == "exitcode" then
- term.clear()
- welcome()
- break
- end
- finish = loadPage(finish)
- if finish == "error" then
- return "error"
- end
- end
- end
- function removeSel(rSel)
- if rSel == 1 then
- term.setCursorPos(1,1)
- term.clearLine()
- write(remove1)
- event, key = os.pullEvent("key")
- if key == 205 then
- return removeSel(2)
- elseif key == 28 then
- return true
- else
- return removeSel(1)
- end
- elseif rSel == 2 then
- term.setCursorPos(1,1)
- term.clearLine()
- write(remove2)
- event, key = os.pullEvent("key")
- if key == 203 then
- return removeSel(1)
- elseif key == 28 then
- return false
- else
- return removeSel(2)
- end
- end
- end
- function saveList(fsList)
- f = io.open("/.serverlist", "w")
- toWrite = textutils.serialize(fsList)
- f:write(toWrite)
- f:close()
- end
- function serverList()
- if not fs.exists("/.serverlist") then
- f = io.open("/.serverlist", "w")
- serTable = textutils.serialize({"Add new server"})
- serverNew = true
- f:write(serTable)
- f:close()
- end
- f = io.open("/.serverlist", "r")
- serStart = f:read("*l")
- f:close()
- serList = textutils.unserialize(serStart)
- if type(serList) == "table" then
- local toRemove = nil
- serReturn, toRemove = serverDrawGUI(serList, 0, 1, "Server List")
- if serReturn == "Add new server" then
- term.setCursorPos(1, 1)
- write("Add: http://")
- local newAddress = read()
- start = ("http://" .. newAddress)
- if string.sub(string.len(start), string.len(start)) == "/" then
- home = start
- else
- home = (start .. "/")
- end
- table.insert(serList, 1, home)
- saveList(serList)
- return serverList()
- elseif serReturn == "remove" then
- rRemove = removeSel(1)
- if rRemove then table.remove(serList, toRemove)
- saveList(serList)
- end
- return serverList()
- elseif serReturn == "exit" then
- term.clear()
- welcome()
- else
- home = serReturn
- if main() == "error" then
- return "error"
- end
- end
- else
- term.setCursorPos(1, 1)
- term.clearLine()
- print("Server list is corrupted! [Ok]")
- os.pullEvent("key")
- return "error"
- end
- end
- function welcome()
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome!")
- print("=================================================")
- print("Use the arrow keys to navigate")
- print("Press enter to download/run files or open folders")
- print("Press \"Q\" to exit the browser")
- print("Press \"R\" to remove servers in the server list")
- print(" ")
- welRe = welcomeSelect(1)
- if welRe == 1 then
- if not fs.exists("/.previous") then
- f = io.open("/.previous", "w")
- f:write("http://previous")
- f:close()
- end
- f = io.open("/.previous", "r")
- home = f:read("*l")
- f:close()
- if main() == "error" then return
- welcome() end
- elseif welRe == 2 then
- term.setCursorPos(1, 8)
- term.clearLine()
- print("Server Address:")
- write("http://")
- local address = read()
- start = ("http://" .. address)
- if string.sub(string.len(start), string.len(start)) == "/" then
- home = start
- else
- home = (start .. "/")
- end
- f = io.open("/.previous", "w")
- f:write(home)
- f:close()
- if main() == "error" then return welcome() end
- elseif welRe == 3 then
- if serverList() == "error" then return welcome() end
- else
- term.clear()
- term.setCursorPos(1, 1)
- return "end"
- end
- end
- welcome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement