tool.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package tool
  5. import (
  6. "crypto/md5"
  7. "crypto/rand"
  8. "crypto/sha1"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "fmt"
  12. "html/template"
  13. "math/big"
  14. "strings"
  15. "time"
  16. "unicode"
  17. "unicode/utf8"
  18. "github.com/Unknwon/com"
  19. "github.com/Unknwon/i18n"
  20. log "gopkg.in/clog.v1"
  21. "github.com/gogs/chardet"
  22. "github.com/gogs/gogs/pkg/setting"
  23. )
  24. // MD5Bytes encodes string to MD5 bytes.
  25. func MD5Bytes(str string) []byte {
  26. m := md5.New()
  27. m.Write([]byte(str))
  28. return m.Sum(nil)
  29. }
  30. // MD5 encodes string to MD5 hex value.
  31. func MD5(str string) string {
  32. return hex.EncodeToString(MD5Bytes(str))
  33. }
  34. // SHA1 encodes string to SHA1 hex value.
  35. func SHA1(str string) string {
  36. h := sha1.New()
  37. h.Write([]byte(str))
  38. return hex.EncodeToString(h.Sum(nil))
  39. }
  40. // ShortSHA1 truncates SHA1 string length to at most 10.
  41. func ShortSHA1(sha1 string) string {
  42. if len(sha1) > 10 {
  43. return sha1[:10]
  44. }
  45. return sha1
  46. }
  47. // DetectEncoding returns best guess of encoding of given content.
  48. func DetectEncoding(content []byte) (string, error) {
  49. if utf8.Valid(content) {
  50. log.Trace("Detected encoding: UTF-8 (fast)")
  51. return "UTF-8", nil
  52. }
  53. result, err := chardet.NewTextDetector().DetectBest(content)
  54. if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 {
  55. log.Trace("Using default AnsiCharset: %s", setting.Repository.AnsiCharset)
  56. return setting.Repository.AnsiCharset, err
  57. }
  58. log.Trace("Detected encoding: %s", result.Charset)
  59. return result.Charset, err
  60. }
  61. // BasicAuthDecode decodes username and password portions of HTTP Basic Authentication
  62. // from encoded content.
  63. func BasicAuthDecode(encoded string) (string, string, error) {
  64. s, err := base64.StdEncoding.DecodeString(encoded)
  65. if err != nil {
  66. return "", "", err
  67. }
  68. auth := strings.SplitN(string(s), ":", 2)
  69. return auth[0], auth[1], nil
  70. }
  71. // BasicAuthEncode encodes username and password in HTTP Basic Authentication format.
  72. func BasicAuthEncode(username, password string) string {
  73. return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  74. }
  75. const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  76. // RandomString returns generated random string in given length of characters.
  77. // It also returns possible error during generation.
  78. func RandomString(n int) (string, error) {
  79. buffer := make([]byte, n)
  80. max := big.NewInt(int64(len(alphanum)))
  81. for i := 0; i < n; i++ {
  82. index, err := randomInt(max)
  83. if err != nil {
  84. return "", err
  85. }
  86. buffer[i] = alphanum[index]
  87. }
  88. return string(buffer), nil
  89. }
  90. func randomInt(max *big.Int) (int, error) {
  91. rand, err := rand.Int(rand.Reader, max)
  92. if err != nil {
  93. return 0, err
  94. }
  95. return int(rand.Int64()), nil
  96. }
  97. // verify time limit code
  98. func VerifyTimeLimitCode(data string, minutes int, code string) bool {
  99. if len(code) <= 18 {
  100. return false
  101. }
  102. // split code
  103. start := code[:12]
  104. lives := code[12:18]
  105. if d, err := com.StrTo(lives).Int(); err == nil {
  106. minutes = d
  107. }
  108. // right active code
  109. retCode := CreateTimeLimitCode(data, minutes, start)
  110. if retCode == code && minutes > 0 {
  111. // check time is expired or not
  112. before, _ := time.ParseInLocation("200601021504", start, time.Local)
  113. now := time.Now()
  114. if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
  115. return true
  116. }
  117. }
  118. return false
  119. }
  120. const TIME_LIMIT_CODE_LENGTH = 12 + 6 + 40
  121. // CreateTimeLimitCode generates a time limit code based on given input data.
  122. // Format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
  123. func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
  124. format := "200601021504"
  125. var start, end time.Time
  126. var startStr, endStr string
  127. if startInf == nil {
  128. // Use now time create code
  129. start = time.Now()
  130. startStr = start.Format(format)
  131. } else {
  132. // use start string create code
  133. startStr = startInf.(string)
  134. start, _ = time.ParseInLocation(format, startStr, time.Local)
  135. startStr = start.Format(format)
  136. }
  137. end = start.Add(time.Minute * time.Duration(minutes))
  138. endStr = end.Format(format)
  139. // create sha1 encode string
  140. sh := sha1.New()
  141. sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
  142. encoded := hex.EncodeToString(sh.Sum(nil))
  143. code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
  144. return code
  145. }
  146. // HashEmail hashes email address to MD5 string.
  147. // https://en.gravatar.com/site/implement/hash/
  148. func HashEmail(email string) string {
  149. email = strings.ToLower(strings.TrimSpace(email))
  150. h := md5.New()
  151. h.Write([]byte(email))
  152. return hex.EncodeToString(h.Sum(nil))
  153. }
  154. // AvatarLink returns relative avatar link to the site domain by given email,
  155. // which includes app sub-url as prefix. However, it is possible
  156. // to return full URL if user enables Gravatar-like service.
  157. func AvatarLink(email string) (url string) {
  158. if setting.EnableFederatedAvatar && setting.LibravatarService != nil &&
  159. strings.Contains(email, "@") {
  160. var err error
  161. url, err = setting.LibravatarService.FromEmail(email)
  162. if err != nil {
  163. log.Warn("AvatarLink.LibravatarService.FromEmail [%s]: %v", email, err)
  164. }
  165. }
  166. if len(url) == 0 && !setting.DisableGravatar {
  167. url = setting.GravatarSource + HashEmail(email) + "?d=identicon"
  168. }
  169. if len(url) == 0 {
  170. url = setting.AppSubURL + "/img/avatar_default.png"
  171. }
  172. return url
  173. }
  174. // AppendAvatarSize appends avatar size query parameter to the URL in the correct format.
  175. func AppendAvatarSize(url string, size int) string {
  176. if strings.Contains(url, "?") {
  177. return url + "&s=" + com.ToStr(size)
  178. }
  179. return url + "?s=" + com.ToStr(size)
  180. }
  181. // Seconds-based time units
  182. const (
  183. Minute = 60
  184. Hour = 60 * Minute
  185. Day = 24 * Hour
  186. Week = 7 * Day
  187. Month = 30 * Day
  188. Year = 12 * Month
  189. )
  190. func computeTimeDiff(diff int64) (int64, string) {
  191. diffStr := ""
  192. switch {
  193. case diff <= 0:
  194. diff = 0
  195. diffStr = "now"
  196. case diff < 2:
  197. diff = 0
  198. diffStr = "1 second"
  199. case diff < 1*Minute:
  200. diffStr = fmt.Sprintf("%d seconds", diff)
  201. diff = 0
  202. case diff < 2*Minute:
  203. diff -= 1 * Minute
  204. diffStr = "1 minute"
  205. case diff < 1*Hour:
  206. diffStr = fmt.Sprintf("%d minutes", diff/Minute)
  207. diff -= diff / Minute * Minute
  208. case diff < 2*Hour:
  209. diff -= 1 * Hour
  210. diffStr = "1 hour"
  211. case diff < 1*Day:
  212. diffStr = fmt.Sprintf("%d hours", diff/Hour)
  213. diff -= diff / Hour * Hour
  214. case diff < 2*Day:
  215. diff -= 1 * Day
  216. diffStr = "1 day"
  217. case diff < 1*Week:
  218. diffStr = fmt.Sprintf("%d days", diff/Day)
  219. diff -= diff / Day * Day
  220. case diff < 2*Week:
  221. diff -= 1 * Week
  222. diffStr = "1 week"
  223. case diff < 1*Month:
  224. diffStr = fmt.Sprintf("%d weeks", diff/Week)
  225. diff -= diff / Week * Week
  226. case diff < 2*Month:
  227. diff -= 1 * Month
  228. diffStr = "1 month"
  229. case diff < 1*Year:
  230. diffStr = fmt.Sprintf("%d months", diff/Month)
  231. diff -= diff / Month * Month
  232. case diff < 2*Year:
  233. diff -= 1 * Year
  234. diffStr = "1 year"
  235. default:
  236. diffStr = fmt.Sprintf("%d years", diff/Year)
  237. diff = 0
  238. }
  239. return diff, diffStr
  240. }
  241. // TimeSincePro calculates the time interval and generate full user-friendly string.
  242. func TimeSincePro(then time.Time) string {
  243. now := time.Now()
  244. diff := now.Unix() - then.Unix()
  245. if then.After(now) {
  246. return "future"
  247. }
  248. var timeStr, diffStr string
  249. for {
  250. if diff == 0 {
  251. break
  252. }
  253. diff, diffStr = computeTimeDiff(diff)
  254. timeStr += ", " + diffStr
  255. }
  256. return strings.TrimPrefix(timeStr, ", ")
  257. }
  258. func timeSince(then time.Time, lang string) string {
  259. now := time.Now()
  260. lbl := i18n.Tr(lang, "tool.ago")
  261. diff := now.Unix() - then.Unix()
  262. if then.After(now) {
  263. lbl = i18n.Tr(lang, "tool.from_now")
  264. diff = then.Unix() - now.Unix()
  265. }
  266. switch {
  267. case diff <= 0:
  268. return i18n.Tr(lang, "tool.now")
  269. case diff <= 2:
  270. return i18n.Tr(lang, "tool.1s", lbl)
  271. case diff < 1*Minute:
  272. return i18n.Tr(lang, "tool.seconds", diff, lbl)
  273. case diff < 2*Minute:
  274. return i18n.Tr(lang, "tool.1m", lbl)
  275. case diff < 1*Hour:
  276. return i18n.Tr(lang, "tool.minutes", diff/Minute, lbl)
  277. case diff < 2*Hour:
  278. return i18n.Tr(lang, "tool.1h", lbl)
  279. case diff < 1*Day:
  280. return i18n.Tr(lang, "tool.hours", diff/Hour, lbl)
  281. case diff < 2*Day:
  282. return i18n.Tr(lang, "tool.1d", lbl)
  283. case diff < 1*Week:
  284. return i18n.Tr(lang, "tool.days", diff/Day, lbl)
  285. case diff < 2*Week:
  286. return i18n.Tr(lang, "tool.1w", lbl)
  287. case diff < 1*Month:
  288. return i18n.Tr(lang, "tool.weeks", diff/Week, lbl)
  289. case diff < 2*Month:
  290. return i18n.Tr(lang, "tool.1mon", lbl)
  291. case diff < 1*Year:
  292. return i18n.Tr(lang, "tool.months", diff/Month, lbl)
  293. case diff < 2*Year:
  294. return i18n.Tr(lang, "tool.1y", lbl)
  295. default:
  296. return i18n.Tr(lang, "tool.years", diff/Year, lbl)
  297. }
  298. }
  299. func RawTimeSince(t time.Time, lang string) string {
  300. return timeSince(t, lang)
  301. }
  302. // TimeSince calculates the time interval and generate user-friendly string.
  303. func TimeSince(t time.Time, lang string) template.HTML {
  304. return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
  305. }
  306. // Subtract deals with subtraction of all types of number.
  307. func Subtract(left interface{}, right interface{}) interface{} {
  308. var rleft, rright int64
  309. var fleft, fright float64
  310. var isInt bool = true
  311. switch left.(type) {
  312. case int:
  313. rleft = int64(left.(int))
  314. case int8:
  315. rleft = int64(left.(int8))
  316. case int16:
  317. rleft = int64(left.(int16))
  318. case int32:
  319. rleft = int64(left.(int32))
  320. case int64:
  321. rleft = left.(int64)
  322. case float32:
  323. fleft = float64(left.(float32))
  324. isInt = false
  325. case float64:
  326. fleft = left.(float64)
  327. isInt = false
  328. }
  329. switch right.(type) {
  330. case int:
  331. rright = int64(right.(int))
  332. case int8:
  333. rright = int64(right.(int8))
  334. case int16:
  335. rright = int64(right.(int16))
  336. case int32:
  337. rright = int64(right.(int32))
  338. case int64:
  339. rright = right.(int64)
  340. case float32:
  341. fright = float64(left.(float32))
  342. isInt = false
  343. case float64:
  344. fleft = left.(float64)
  345. isInt = false
  346. }
  347. if isInt {
  348. return rleft - rright
  349. } else {
  350. return fleft + float64(rleft) - (fright + float64(rright))
  351. }
  352. }
  353. // EllipsisString returns a truncated short string,
  354. // it appends '...' in the end of the length of string is too large.
  355. func EllipsisString(str string, length int) string {
  356. if len(str) < length {
  357. return str
  358. }
  359. return str[:length-3] + "..."
  360. }
  361. // TruncateString returns a truncated string with given limit,
  362. // it returns input string if length is not reached limit.
  363. func TruncateString(str string, limit int) string {
  364. if len(str) < limit {
  365. return str
  366. }
  367. return str[:limit]
  368. }
  369. // StringsToInt64s converts a slice of string to a slice of int64.
  370. func StringsToInt64s(strs []string) []int64 {
  371. ints := make([]int64, len(strs))
  372. for i := range strs {
  373. ints[i] = com.StrTo(strs[i]).MustInt64()
  374. }
  375. return ints
  376. }
  377. // Int64sToStrings converts a slice of int64 to a slice of string.
  378. func Int64sToStrings(ints []int64) []string {
  379. strs := make([]string, len(ints))
  380. for i := range ints {
  381. strs[i] = com.ToStr(ints[i])
  382. }
  383. return strs
  384. }
  385. // Int64sToMap converts a slice of int64 to a int64 map.
  386. func Int64sToMap(ints []int64) map[int64]bool {
  387. m := make(map[int64]bool)
  388. for _, i := range ints {
  389. m[i] = true
  390. }
  391. return m
  392. }
  393. // IsLetter reports whether the rune is a letter (category L).
  394. // https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L257
  395. func IsLetter(ch rune) bool {
  396. return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
  397. }