static.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package agi
  2. import (
  3. "net/url"
  4. "path/filepath"
  5. "strings"
  6. user "imuslab.com/arozos/mod/user"
  7. )
  8. //Check if the user can access this script file
  9. func checkUserAccessToScript(thisuser *user.User, scriptFile string, scriptScope string) bool {
  10. moduleName := getScriptRoot(scriptFile, scriptScope)
  11. if !thisuser.GetModuleAccessPermission(moduleName) {
  12. return false
  13. }
  14. return true
  15. }
  16. //validate the given path is a script from webroot
  17. func isValidAGIScript(scriptPath string) bool {
  18. return fileExists(filepath.Join("./web", scriptPath)) && (filepath.Ext(scriptPath) == ".js" || filepath.Ext(scriptPath) == ".agi")
  19. }
  20. //Return the script root of the current executing script
  21. func getScriptRoot(scriptFile string, scriptScope string) string {
  22. //Get the script root from the script path
  23. webRootAbs, _ := filepath.Abs(scriptScope)
  24. webRootAbs = filepath.ToSlash(filepath.Clean(webRootAbs) + "/")
  25. scriptFileAbs, _ := filepath.Abs(scriptFile)
  26. scriptFileAbs = filepath.ToSlash(filepath.Clean(scriptFileAbs))
  27. scriptRoot := strings.Replace(scriptFileAbs, webRootAbs, "", 1)
  28. scriptRoot = strings.Split(scriptRoot, "/")[0]
  29. return scriptRoot
  30. }
  31. //For handling special url decode in the request
  32. func specialURIDecode(inputPath string) string {
  33. inputPath = strings.ReplaceAll(inputPath, "+", "{{plus_sign}}")
  34. inputPath, _ = url.QueryUnescape(inputPath)
  35. inputPath = strings.ReplaceAll(inputPath, "{{plus_sign}}", "+")
  36. return inputPath
  37. }
  38. func specialGlob(path string) ([]string, error) {
  39. files, err := filepath.Glob(path)
  40. if err != nil {
  41. return []string{}, err
  42. }
  43. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  44. if len(files) == 0 {
  45. //Handle reverse check. Replace all [ and ] with *
  46. newSearchPath := strings.ReplaceAll(path, "[", "?")
  47. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  48. newSearchPath = strings.ReplaceAll(newSearchPath, ":", "?")
  49. //Scan with all the similar structure except [ and ]
  50. tmpFilelist, _ := filepath.Glob(newSearchPath)
  51. for _, file := range tmpFilelist {
  52. file = filepath.ToSlash(file)
  53. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  54. files = append(files, file)
  55. }
  56. }
  57. }
  58. }
  59. //Convert all filepaths to slash
  60. for i := 0; i < len(files); i++ {
  61. files[i] = filepath.ToSlash(files[i])
  62. }
  63. return files, nil
  64. }