static.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. //Return the script root of the current executing script
  17. func getScriptRoot(scriptFile string, scriptScope string) string {
  18. //Get the script root from the script path
  19. webRootAbs, _ := filepath.Abs(scriptScope)
  20. webRootAbs = filepath.ToSlash(filepath.Clean(webRootAbs) + "/")
  21. scriptFileAbs, _ := filepath.Abs(scriptFile)
  22. scriptFileAbs = filepath.ToSlash(filepath.Clean(scriptFileAbs))
  23. scriptRoot := strings.Replace(scriptFileAbs, webRootAbs, "", 1)
  24. scriptRoot = strings.Split(scriptRoot, "/")[0]
  25. return scriptRoot
  26. }
  27. //For handling special url decode in the request
  28. func specialURIDecode(inputPath string) string {
  29. inputPath = strings.ReplaceAll(inputPath, "+", "{{plus_sign}}")
  30. inputPath, _ = url.QueryUnescape(inputPath)
  31. inputPath = strings.ReplaceAll(inputPath, "{{plus_sign}}", "+")
  32. return inputPath
  33. }
  34. func specialGlob(path string) ([]string, error) {
  35. files, err := filepath.Glob(path)
  36. if err != nil {
  37. return []string{}, err
  38. }
  39. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  40. if len(files) == 0 {
  41. //Handle reverse check. Replace all [ and ] with *
  42. newSearchPath := strings.ReplaceAll(path, "[", "?")
  43. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  44. newSearchPath = strings.ReplaceAll(newSearchPath, ":", "?")
  45. //Scan with all the similar structure except [ and ]
  46. tmpFilelist, _ := filepath.Glob(newSearchPath)
  47. for _, file := range tmpFilelist {
  48. file = filepath.ToSlash(file)
  49. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  50. files = append(files, file)
  51. }
  52. }
  53. }
  54. }
  55. //Convert all filepaths to slash
  56. for i := 0; i < len(files); i++ {
  57. files[i] = filepath.ToSlash(files[i])
  58. }
  59. return files, nil
  60. }