Advertisement
dacman9

DacStorageTemp

Sep 3rd, 2017
2,508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. --Variables
  2. local env = getfenv()
  3. local width,height = term.getSize()
  4. local username
  5. local password
  6. local token
  7. local SKIPLOGIN = false
  8. local selected = false
  9. local scroll = 0
  10. local refresh = false
  11. local baseOptions = {"Logout","Upload","New"}
  12. local fileOptions = {"Logout","Upload","New"," -----","Download","Delete","Share","Rename","Edit","Run"}
  13. local fileOptions2 = {"Logout","Upload","New"," -----","Download","Delete","Unshare","Rename","Edit","Run"}
  14. local shared = {}
  15. --Functions
  16. local drawBanner = function()
  17. paintutils.drawLine(1,1,width,1,colors.green)
  18. term.setCursorPos(math.ceil((width-10)/2),1)
  19. term.setBackgroundColor(colors.green)
  20. term.setTextColor(colors.white)
  21. term.write("DacStorage")
  22. end
  23. local info = function(txt,t)
  24. paintutils.drawLine(1,height,width,height,colors.lightGray)
  25. term.setTextColor(colors.gray)
  26. term.setBackgroundColor(colors.lightGray)
  27. term.setCursorPos(2,height)
  28. term.write(txt)
  29. refresh = true
  30. if not t then
  31. while true do
  32. local e = os.pullEvent()
  33. if e == "mouse_click" or e == "char" then
  34. break
  35. end
  36. end
  37. end
  38. end
  39. local prompt = function(txt)
  40. info(txt..": ",true)
  41. return read()
  42. end
  43. local runprogram = function(selection)
  44. local p = http.post("https://dacpages.tk/dpserver.php","f=filedownload&filename="..selection.."&username="..username.."&token="..token)
  45. local msg = p.readAll()
  46. if msg == "File Does Not Exist!" then
  47. info(msg)
  48. return
  49. end
  50. loadstring(msg)()
  51. end
  52. local rclick = function(x,y,tbl,selection)
  53. if y == 1 then
  54. return
  55. end
  56. local bw,bh,value
  57. bh = #tbl
  58. bw = 0
  59. for i = 1,#tbl do
  60. if #tbl[i]+2 > bw then
  61. bw = #tbl[i]+2
  62. end
  63. end
  64. if x+bw > width then
  65. x = width-bw+1
  66. end
  67. if y+bh > height then
  68. y = height-bh+1
  69. end
  70. paintutils.drawFilledBox(x+1,y+1,x+bw,y+bh,colors.black)
  71. paintutils.drawFilledBox(x,y,x+bw-1,y+bh-1,colors.green)
  72. term.setTextColor(colors.white)
  73. for i = 1,#tbl do
  74. term.setCursorPos(x+1,y+i-1)
  75. term.write(tbl[i])
  76. end
  77. local e,a,b,c = os.pullEvent("mouse_click")
  78. if b > x+bw-1 or b < x or c > y+bh-1 or c < y then
  79. return
  80. else
  81. value = tbl[c-y+1]
  82. end
  83. if value == "Download" then
  84. local p = http.post("https://dacpages.tk/dpserver.php","f=filedownload&filename="..selection.."&username="..username.."&token="..token)
  85. local msg = p.readAll()
  86. if msg == "File Does Not Exist!" then
  87. info(msg)
  88. else
  89. local f = fs.open(selection,"w")
  90. f.write(msg)
  91. f.close()
  92. end
  93. p.close()
  94. elseif value == "Upload" then
  95. local file = prompt("Enter file location")
  96. if not fs.exists(file) then
  97. info("File Does Not Exist!")
  98. return
  99. end
  100. local f = fs.open(file,"r")
  101. local data = textutils.urlEncode(f.readAll())
  102. f.close()
  103. local p = http.post("https://dacpages.tk/dpserver.php","f=rawupload&filename="..fs.getName(file).."&username="..username.."&token="..token.."&filedata="..data)
  104. local msg = p.readAll()
  105. if msg == "success" then
  106. refresh = true
  107. else
  108. info(msg)
  109. end
  110. p.close()
  111. elseif value == "Delete" then
  112. if string.lower(string.sub(prompt("Are you sure? (y/n)"),1,1)) ~= "y" then
  113. return
  114. end
  115. local p = http.post("https://dacpages.tk/dpserver.php","f=filedelete&filename="..selection.."&username="..username.."&token="..token)
  116. local msg = p.readAll()
  117. if msg == "success" then
  118. refresh = true
  119. else
  120. info(msg)
  121. end
  122. p.close()
  123. elseif value == "Rename" then
  124. local newfilename = prompt("Enter New Filename")
  125. local p = http.post("https://dacpages.tk/dpserver.php","f=filerename&filename="..selection.."&username="..username.."&token="..token.."&newfilename="..newfilename)
  126. local msg = p.readAll()
  127. if msg == "success" then
  128. refresh = true
  129. else
  130. info(msg)
  131. end
  132. p.close()
  133. elseif value == "Edit" then
  134. local p = http.post("https://dacpages.tk/dpserver.php","f=filedownload&filename="..selection.."&username="..username.."&token="..token)
  135. local msg = p.readAll()
  136. if msg == "File Does Not Exist!" then
  137. info(msg)
  138. p.close()
  139. return
  140. end
  141. local f = fs.open(".temp","w")
  142. f.write(msg)
  143. f.close()
  144. p.close()
  145. shell.run("edit",".temp")
  146. f = fs.open(".temp","r")
  147. local data = textutils.urlEncode(f.readAll())
  148. f.close()
  149. local p = http.post("https://dacpages.tk/dpserver.php","f=filedelete&filename="..selection.."&username="..username.."&token="..token)
  150. local msg = p.readAll()
  151. if msg ~= "success" then
  152. info(msg)
  153. p.close()
  154. return
  155. end
  156. p.close()
  157. local p = http.post("https://dacpages.tk/dpserver.php","f=rawupload&filename="..selection.."&username="..username.."&token="..token.."&filedata="..data)
  158. local msg = p.readAll()
  159. if msg == "success" then
  160. refresh = true
  161. else
  162. info(msg)
  163. refresh = true
  164. end
  165. p.close()
  166. elseif value == "New" then
  167. local filename = prompt("Enter filename")
  168. local p = http.post("https://dacpages.tk/dpserver.php","f=rawupload&filename="..filename.."&username="..username.."&token="..token.."&filedata=")
  169. local msg = p.readAll()
  170. if msg == "success" then
  171. refresh = true
  172. else
  173. info(msg)
  174. end
  175. p.close()
  176. elseif value == "Run" then
  177. runprogram(selection)
  178. elseif value == "Share" or value == "Unshare" then
  179. local p = http.post("https://dacpages.tk/dpserver.php","f=sharefiletoggle&filename="..selection.."&username="..username.."&token="..token)
  180. local msg = p.readAll()
  181. if msg == "t" then
  182. info("File shared")
  183. elseif msg == "f" then
  184. info("File unshared")
  185. else
  186. info(msg)
  187. end
  188. p.close()
  189. elseif value == "Logout" then
  190. fs.delete(".dacstorageLogin")
  191. error("")
  192. end
  193. end
  194. --Code
  195. if fs.exists(".dacstorageLogin") then
  196. local f = fs.open(".dacstorageLogin","r")
  197. local data = textutils.unserialize(f.readAll())
  198. f.close()
  199. token = data.token
  200. username = data.username
  201. local page = http.post("https://dacpages.tk/dpserver.php","f=signin&token="..token.."&username="..username)
  202. local out = page.readAll()
  203. if out == "success" then
  204. SKIPLOGIN = true
  205. else
  206. fs.delete(".dacstorageLogin")
  207. end
  208. end
  209. while not SKIPLOGIN do
  210. term.setBackgroundColor(colors.white)
  211. term.clear()
  212. drawBanner()
  213. term.setTextColor(colors.black)
  214. term.setBackgroundColor(colors.white)
  215. term.setCursorPos(3,4)
  216. term.write("Enter Username for DacPages Account:")
  217. term.setCursorPos(5,6)
  218. term.setTextColor(colors.gray)
  219. username = textutils.urlEncode(read())
  220. term.setCursorPos(3,8)
  221. term.setTextColor(colors.black)
  222. term.write("Enter DacPages Account Password:")
  223. term.setCursorPos(5,10)
  224. term.setTextColor(colors.gray)
  225. password = textutils.urlEncode(read("*"))
  226. local page = http.post("https://dacpages.tk/dpserver.php","f=signin&password="..password.."&username="..username)
  227. local out = page.readAll()
  228. if string.sub(out,1,7) == "token: " then
  229. token = string.sub(out,8,#out)
  230. local f = fs.open(".dacstorageLogin","w")
  231. f.write(textutils.serialize({token=token,username=username}))
  232. f.close()
  233. break
  234. else
  235. term.setCursorPos(3,height)
  236. term.setTextColor(colors.red)
  237. term.write(out)
  238. sleep(3)
  239. end
  240. end
  241. while true do
  242. term.setBackgroundColor(colors.white)
  243. term.clear()
  244. drawBanner()
  245. local page = http.post("https://dacpages.tk/dpserver.php","f=listfiles&username="..username.."&token="..token)
  246. local list = {}
  247. for i in string.gmatch(page.readAll(),"[^,]+") do
  248. if not (i == "." or i == "..") then
  249. list[#list+1] = i
  250. end
  251. end
  252. page = http.post("https://dacpages.tk/dpserver.php","f=listfiledata&data=shared&username="..username.."&token="..token)
  253. shared = {}
  254. for i in string.gmatch(page.readAll(),"[^,]+") do
  255. shared[i] = true;
  256. end
  257. while true do
  258. for i = 2,height do
  259. term.setCursorPos(2,i)
  260. if list[i-1+scroll] then
  261. if i-1+scroll == selected then
  262. term.setBackgroundColor(colors.lightBlue)
  263. paintutils.drawLine(1,i,width,i,colors.lightBlue)
  264. term.setTextColor(colors.white)
  265. else
  266. paintutils.drawLine(1,i,width,i,colors.white)
  267. term.setTextColor(colors.black)
  268. term.setBackgroundColor(colors.white)
  269. end
  270. term.setCursorPos(2,i)
  271. term.write(list[i-1+scroll])
  272. else
  273. paintutils.drawLine(1,i,width,i,colors.white)
  274. end
  275. end
  276. while true do
  277. local e,a,b,c = os.pullEvent()
  278. if e == "mouse_click" then
  279. if list[c-1] and a == 1 then
  280. selected = c-1+scroll
  281. break
  282. elseif a == 2 then
  283. if list[c-1+scroll] then
  284. if shared[list[c-1+scroll]] then
  285. rclick(b,c,fileOptions2,list[c-1+scroll])
  286. else
  287. rclick(b,c,fileOptions,list[c-1+scroll])
  288. end
  289. else
  290. rclick(b,c,baseOptions)
  291. end
  292. break
  293. end
  294. elseif e == "mouse_scroll" then
  295. scroll = scroll + a
  296. if scroll > #list-(height-1) then
  297. scroll = #list-(height-1)
  298. end
  299. if scroll < 0 then
  300. scroll = 0
  301. end
  302. break
  303. elseif e == "key" then
  304. if a == 200 and selected and selected > 1 then
  305. selected = selected - 1
  306. if selected <= scroll then
  307. scroll = selected - 1
  308. end
  309. break
  310. elseif a == 208 and ((not selected) or selected < #list) then
  311. if not selected then
  312. selected = 0
  313. end
  314. selected = selected + 1
  315. if selected > height-1+scroll then
  316. scroll = selected-height+1
  317. end
  318. break
  319. elseif a == 28 and selected and list[selected] then
  320. runprogram(list[selected])
  321. end
  322. end
  323. end
  324. if refresh then
  325. refresh = false
  326. break
  327. end
  328. end
  329. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement