common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package agi
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. uuid "github.com/satori/go.uuid"
  14. )
  15. //Send text response with given w and message as string
  16. func sendTextResponse(w http.ResponseWriter, msg string) {
  17. w.Write([]byte(msg))
  18. }
  19. //Send JSON response, with an extra json header
  20. func sendJSONResponse(w http.ResponseWriter, json string) {
  21. w.Header().Set("Content-Type", "application/json")
  22. w.Write([]byte(json))
  23. }
  24. func sendErrorResponse(w http.ResponseWriter, errMsg string) {
  25. w.Header().Set("Content-Type", "application/json")
  26. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  27. }
  28. func sendOK(w http.ResponseWriter) {
  29. w.Header().Set("Content-Type", "application/json")
  30. w.Write([]byte("\"OK\""))
  31. }
  32. /*
  33. The paramter move function (mv)
  34. You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in
  35. r (HTTP Request Object)
  36. getParamter (string, aka $_GET['This string])
  37. Will return
  38. Paramter string (if any)
  39. Error (if error)
  40. */
  41. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  42. if postMode == false {
  43. //Access the paramter via GET
  44. keys, ok := r.URL.Query()[getParamter]
  45. if !ok || len(keys[0]) < 1 {
  46. //log.Println("Url Param " + getParamter +" is missing")
  47. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  48. }
  49. // Query()["key"] will return an array of items,
  50. // we only want the single item.
  51. key := keys[0]
  52. return string(key), nil
  53. } else {
  54. //Access the parameter via POST
  55. r.ParseForm()
  56. x := r.Form.Get(getParamter)
  57. if len(x) == 0 || x == "" {
  58. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  59. }
  60. return string(x), nil
  61. }
  62. }
  63. func stringInSlice(a string, list []string) bool {
  64. for _, b := range list {
  65. if b == a {
  66. return true
  67. }
  68. }
  69. return false
  70. }
  71. func fileExists(filename string) bool {
  72. _, err := os.Stat(filename)
  73. if os.IsNotExist(err) {
  74. return false
  75. }
  76. return true
  77. }
  78. func IsDir(path string) bool {
  79. if fileExists(path) == false {
  80. return false
  81. }
  82. fi, err := os.Stat(path)
  83. if err != nil {
  84. log.Fatal(err)
  85. return false
  86. }
  87. switch mode := fi.Mode(); {
  88. case mode.IsDir():
  89. return true
  90. case mode.IsRegular():
  91. return false
  92. }
  93. return false
  94. }
  95. func inArray(arr []string, str string) bool {
  96. for _, a := range arr {
  97. if a == str {
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. func timeToString(targetTime time.Time) string {
  104. return targetTime.Format("2006-01-02 15:04:05")
  105. }
  106. func IntToString(number int) string {
  107. return strconv.Itoa(number)
  108. }
  109. func StringToInt(number string) (int, error) {
  110. return strconv.Atoi(number)
  111. }
  112. func StringToInt64(number string) (int64, error) {
  113. i, err := strconv.ParseInt(number, 10, 64)
  114. if err != nil {
  115. return -1, err
  116. }
  117. return i, nil
  118. }
  119. func Int64ToString(number int64) string {
  120. convedNumber := strconv.FormatInt(number, 10)
  121. return convedNumber
  122. }
  123. func GetUnixTime() int64 {
  124. return time.Now().Unix()
  125. }
  126. func LoadImageAsBase64(filepath string) (string, error) {
  127. if !fileExists(filepath) {
  128. return "", errors.New("File not exists")
  129. }
  130. f, _ := os.Open(filepath)
  131. reader := bufio.NewReader(f)
  132. content, _ := ioutil.ReadAll(reader)
  133. encoded := base64.StdEncoding.EncodeToString(content)
  134. return string(encoded), nil
  135. }
  136. func newUUIDv4() string {
  137. thisuuid := uuid.NewV4().String()
  138. return thisuuid
  139. }
  140. //Check if the target path is escaping the rootpath, accept relative and absolute path
  141. func (g *Gateway) checkRootEscape(rootPath string, targetPath string) (bool, error) {
  142. rootAbs, err := filepath.Abs(rootPath)
  143. if err != nil {
  144. return true, err
  145. }
  146. targetAbs, err := filepath.Abs(targetPath)
  147. if err != nil {
  148. return true, err
  149. }
  150. if len(targetAbs) < len(rootAbs) || targetAbs[:len(rootAbs)] != rootAbs {
  151. //Potential path escape. Return true
  152. return true, nil
  153. }
  154. return false, nil
  155. }