#!/usr/bin/lua local os = require "os" local fs = require "nixio.fs" local json = require "json" local uci = require("uci") local function is_in_dir(file, dir) if file == dir then return true else return file:sub(1, #dir) == dir end end local function rm(item) local ftype = fs.stat(item,"type") if ftype == "reg" then return fs.remove(item) elseif ftype == "dir" then local dir = fs.dir(item) for file in dir do if not rm(item..'/'..file) then return false end end return fs.rmdir(item) end end local function chmod(item,mode,recursive) local result = fs.chmod(item,mode) if result and recursive then local dir = fs.dir(item) if dir then for file in dir do local ftype = fs.stat(item..'/'..file,"type") if ftype == "dir" then result = chmod(item..'/'..file,mode,recursive) elseif ftype == "reg" then result = chmod(item..'/'..file,string.gsub(mode,"x","-"),false) end if not result then break end end end end return result end print("Content-type: text/html; charset=utf-8") print("Cache-control: no-cache") print("Pragma: no-cache") -- prepare the browser for content: print("\r") local result local http_headers = nixio.getenv() local request=io.read("*all") local params=json.decode(request) local action=params["action"] local u_c = uci.cursor() local basepath = u_c.get("filemanager","config","basedir") local path local items local item local newItemPath local newPath local singleFilename local content local mode local destination local compressedFilename local recursive local folderName local files local fstat local ftype local basename local realitem local command if action == "list" then path = fs.realpath(basepath..'/'..params["path"]) if is_in_dir(path,basepath) then files = {} local rec for name in fs.dir(path) do fstat=fs.stat(fs.realpath(path..'/'..name)) if fstat["type"]=="reg" then ftype="file" elseif fstat["type"]=="dir" then ftype="dir" else ftype="" end if ftype then rec={name=name,rights=fstat["modestr"],size=fstat["size"],type=ftype,date=os.date('%Y-%m-%d %H:%M:%S',fstat["mtime"])} table.insert(files,rec) end end result = { result = files } else result = { result = {} } end elseif action == "rename" then item = fs.realpath(basepath..'/'..params["item"]) newItemPath = fs.realpath(basepath..'/'..fs.dirname(params["newItemPath"]))..'/'..fs.basename(params["newItemPath"]) if is_in_dir(item,basepath) and is_in_dir(newItemPath,basepath) and item ~= basepath and newItemPath ~= basepath then result = { result = { success=fs.rename(item,newItemPath), error="" } } else result = { result = { success=false, error="Invalid path request" } } end elseif action == "move" then items = params["items"] newPath = fs.realpath(basepath..'/'..params["newPath"]) if is_in_dir(newPath,basepath) then result = true for key,item in pairs(items) do item = fs.realpath(basepath..'/'..item) basename = fs.basename(item) if is_in_dir(item,basepath) and item ~= basepath then result = fs.move(item,newPath.."/"..basename) if not result then break end else result = false break end end result = { result = { success=result, error="" } } else result = { result = { success=false, error="Invalid destination request" } } end elseif action == "copy" then items = params["items"] newPath = fs.realpath(basepath..'/'..params["newPath"]) singleFilename = params["singleFilename"] if singleFilename then singleFilename = fs.basename(singleFilename) end if is_in_dir(newPath,basepath) then result = true for key,item in pairs(items) do item = fs.realpath(basepath..'/'..item) if singleFilename then basename = singleFilename else basename = fs.basename(item) end if is_in_dir(item,basepath) and item ~= basepath then result = fs.copy(item,newPath.."/"..basename) if not result then break end else result = false break end end result = { result = { success=result, error="" } } else result = { result = { success=false, error="Invalid destination request" } } end elseif action == "remove" then items = params["items"] result = true for key,item in pairs(items) do item = fs.realpath(basepath..'/'..item) if is_in_dir(item,basepath) and item ~= basepath then result = rm(item) if not result then break end else result = false break end end result = { result = { success=result, error="" } } elseif action == "getContent" then item = fs.realpath(basepath..'/'..params["item"]) if is_in_dir(item,basepath) and item ~= basepath then content = fs.readfile(item) result = { result = content } else result = { result = { success=false, error="Invalid path request" } } end elseif action == "edit" then item = fs.realpath(basepath..'/'..params["item"]) content = params["content"] if is_in_dir(item,basepath) and item ~= basepath then result = { result = { success=fs.writefile(item,content), error="" } } else result = { result = { success=false, error="Invalid path request" } } end elseif action == "createFolder" then newPath = fs.realpath(basepath..'/'..fs.dirname(params["newPath"]))..'/'..fs.basename(params["newPath"]) if is_in_dir(newPath,basepath) and newPath ~= basepath then result = { result = { success=fs.mkdir(newPath), error="" } } else result = { result = { success=false, error="Invalid path request" } } end elseif action == "changePermissions" then items = params["items"] if params["perms"] then mode = params["perms"] else mode = params["permsCode"] end if mode then recursive = params["recursive"] result = true for key,item in pairs(items) do item = fs.realpath(basepath..'/'..item) if is_in_dir(item,basepath) and item ~= basepath then result = chmod(item,mode,recursive) if not result then break end else result = false break end end result = { result = { success=result, error="" } } else result = { result = { success=false, error="No permission requested" } } end elseif action == "compress" then items = params["items"] destination = params["destination"] compressedFilename = params["compressedFilename"] newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["compressedFilename"]) result = true files = "" for key,item in pairs(items) do realitem = fs.realpath(basepath..'/'..item) if is_in_dir(realitem,basepath) and realitem ~= basepath then item = item:match("/(.*)") files = files.." "..item else result = false break end end if files then command = "cd "..basepath.."; zip -r "..newPath..files os.execute(command) end result = { result = { success=result, error="" } } elseif action == "extract" then item = fs.realpath(basepath..'/'..params["item"]) destination = params["destination"] folderName = params["folderName"] newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["folderName"]) result = true files = "" if is_in_dir(item,basepath) and is_in_dir(newPath,basepath) and item ~= basepath then command = "unzip "..item.." -d "..newPath os.execute(command) result = { result = { success=true, error="" } } else result = { result = { success=false, error="Invalid path request" } } end else result = { result = { success=false, error="Operation not impolemented yet" } } end print(json.encode(result))