static.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package agi
  2. import (
  3. "net/url"
  4. "path/filepath"
  5. "strings"
  6. "github.com/robertkrimen/otto"
  7. "imuslab.com/arozos/mod/filesystem"
  8. "imuslab.com/arozos/mod/filesystem/arozfs"
  9. user "imuslab.com/arozos/mod/user"
  10. )
  11. //Get the full vpath if the passing value is a relative path
  12. //Return the original vpath if any error occured
  13. func relativeVpathRewrite(fsh *filesystem.FileSystemHandler, vpath string, vm *otto.Otto, u *user.User) string {
  14. //Check if the vpath contain a UUID
  15. if strings.Contains(vpath, ":/") || (len(vpath) > 0 && vpath[len(vpath)-1:] == ":") {
  16. //This vpath contain root uuid.
  17. return vpath
  18. }
  19. //We have no idea where the script is from. Trust its vpath is always full path
  20. if fsh == nil {
  21. return vpath
  22. }
  23. //Get the script execution root path
  24. rootPath, err := vm.Get("__FILE__")
  25. if err != nil {
  26. return vpath
  27. }
  28. rootPathString, err := rootPath.ToString()
  29. if err != nil {
  30. return vpath
  31. }
  32. //Convert the root path to vpath
  33. rootVpath, err := fsh.FileSystemAbstraction.RealPathToVirtualPath(rootPathString, u.Username)
  34. if err != nil {
  35. return vpath
  36. }
  37. rootScriptDir := filepath.Dir(rootVpath)
  38. return arozfs.ToSlash(filepath.Clean(filepath.Join(rootScriptDir, vpath)))
  39. }
  40. //Check if the user can access this script file
  41. func checkUserAccessToScript(thisuser *user.User, scriptFile string, scriptScope string) bool {
  42. moduleName := getScriptRoot(scriptFile, scriptScope)
  43. if !thisuser.GetModuleAccessPermission(moduleName) {
  44. return false
  45. }
  46. return true
  47. }
  48. //validate the given path is a script from webroot
  49. func isValidAGIScript(scriptPath string) bool {
  50. return fileExists(filepath.Join("./web", scriptPath)) && (filepath.Ext(scriptPath) == ".js" || filepath.Ext(scriptPath) == ".agi")
  51. }
  52. //Return the script root of the current executing script
  53. func getScriptRoot(scriptFile string, scriptScope string) string {
  54. //Get the script root from the script path
  55. webRootAbs, _ := filepath.Abs(scriptScope)
  56. webRootAbs = filepath.ToSlash(filepath.Clean(webRootAbs) + "/")
  57. scriptFileAbs, _ := filepath.Abs(scriptFile)
  58. scriptFileAbs = filepath.ToSlash(filepath.Clean(scriptFileAbs))
  59. scriptRoot := strings.Replace(scriptFileAbs, webRootAbs, "", 1)
  60. scriptRoot = strings.Split(scriptRoot, "/")[0]
  61. return scriptRoot
  62. }
  63. //For handling special url decode in the request
  64. func specialURIDecode(inputPath string) string {
  65. inputPath = strings.ReplaceAll(inputPath, "+", "{{plus_sign}}")
  66. inputPath, _ = url.QueryUnescape(inputPath)
  67. inputPath = strings.ReplaceAll(inputPath, "{{plus_sign}}", "+")
  68. return inputPath
  69. }