You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool.go 11 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 base
  5. import (
  6. "crypto/hmac"
  7. "crypto/md5"
  8. "crypto/rand"
  9. "crypto/sha1"
  10. "encoding/base64"
  11. "encoding/hex"
  12. "fmt"
  13. "hash"
  14. "html/template"
  15. "math"
  16. "regexp"
  17. "strings"
  18. "time"
  19. "unicode/utf8"
  20. "github.com/Unknwon/com"
  21. "github.com/Unknwon/i18n"
  22. "github.com/microcosm-cc/bluemonday"
  23. "github.com/gogits/chardet"
  24. "github.com/gogits/gogs/modules/avatar"
  25. "github.com/gogits/gogs/modules/log"
  26. "github.com/gogits/gogs/modules/setting"
  27. )
  28. var Sanitizer = bluemonday.UGCPolicy().AllowAttrs("class").Matching(regexp.MustCompile(`[\p{L}\p{N}\s\-_',:\[\]!\./\\\(\)&]*`)).OnElements("code")
  29. // EncodeMD5 encodes string to md5 hex value.
  30. func EncodeMD5(str string) string {
  31. m := md5.New()
  32. m.Write([]byte(str))
  33. return hex.EncodeToString(m.Sum(nil))
  34. }
  35. // Encode string to sha1 hex value.
  36. func EncodeSha1(str string) string {
  37. h := sha1.New()
  38. h.Write([]byte(str))
  39. return hex.EncodeToString(h.Sum(nil))
  40. }
  41. func ShortSha(sha1 string) string {
  42. if len(sha1) == 40 {
  43. return sha1[:10]
  44. }
  45. return sha1
  46. }
  47. func DetectEncoding(content []byte) (string, error) {
  48. if utf8.Valid(content) {
  49. log.Debug("Detected encoding: utf-8 (fast)")
  50. return "UTF-8", nil
  51. }
  52. result, err := chardet.NewTextDetector().DetectBest(content)
  53. if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 {
  54. log.Debug("Using default AnsiCharset: %s", setting.Repository.AnsiCharset)
  55. return setting.Repository.AnsiCharset, err
  56. }
  57. log.Debug("Detected encoding: %s", result.Charset)
  58. return result.Charset, err
  59. }
  60. func BasicAuthDecode(encoded string) (string, string, error) {
  61. s, err := base64.StdEncoding.DecodeString(encoded)
  62. if err != nil {
  63. return "", "", err
  64. }
  65. auth := strings.SplitN(string(s), ":", 2)
  66. return auth[0], auth[1], nil
  67. }
  68. func BasicAuthEncode(username, password string) string {
  69. return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  70. }
  71. // GetRandomString generate random string by specify chars.
  72. func GetRandomString(n int, alphabets ...byte) string {
  73. const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  74. var bytes = make([]byte, n)
  75. rand.Read(bytes)
  76. for i, b := range bytes {
  77. if len(alphabets) == 0 {
  78. bytes[i] = alphanum[b%byte(len(alphanum))]
  79. } else {
  80. bytes[i] = alphabets[b%byte(len(alphabets))]
  81. }
  82. }
  83. return string(bytes)
  84. }
  85. // http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto
  86. func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
  87. prf := hmac.New(h, password)
  88. hashLen := prf.Size()
  89. numBlocks := (keyLen + hashLen - 1) / hashLen
  90. var buf [4]byte
  91. dk := make([]byte, 0, numBlocks*hashLen)
  92. U := make([]byte, hashLen)
  93. for block := 1; block <= numBlocks; block++ {
  94. // N.B.: || means concatenation, ^ means XOR
  95. // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
  96. // U_1 = PRF(password, salt || uint(i))
  97. prf.Reset()
  98. prf.Write(salt)
  99. buf[0] = byte(block >> 24)
  100. buf[1] = byte(block >> 16)
  101. buf[2] = byte(block >> 8)
  102. buf[3] = byte(block)
  103. prf.Write(buf[:4])
  104. dk = prf.Sum(dk)
  105. T := dk[len(dk)-hashLen:]
  106. copy(U, T)
  107. // U_n = PRF(password, U_(n-1))
  108. for n := 2; n <= iter; n++ {
  109. prf.Reset()
  110. prf.Write(U)
  111. U = U[:0]
  112. U = prf.Sum(U)
  113. for x := range U {
  114. T[x] ^= U[x]
  115. }
  116. }
  117. }
  118. return dk[:keyLen]
  119. }
  120. // verify time limit code
  121. func VerifyTimeLimitCode(data string, minutes int, code string) bool {
  122. if len(code) <= 18 {
  123. return false
  124. }
  125. // split code
  126. start := code[:12]
  127. lives := code[12:18]
  128. if d, err := com.StrTo(lives).Int(); err == nil {
  129. minutes = d
  130. }
  131. // right active code
  132. retCode := CreateTimeLimitCode(data, minutes, start)
  133. if retCode == code && minutes > 0 {
  134. // check time is expired or not
  135. before, _ := time.ParseInLocation("200601021504", start, time.Local)
  136. now := time.Now()
  137. if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
  138. return true
  139. }
  140. }
  141. return false
  142. }
  143. const TimeLimitCodeLength = 12 + 6 + 40
  144. // create a time limit code
  145. // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
  146. func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
  147. format := "200601021504"
  148. var start, end time.Time
  149. var startStr, endStr string
  150. if startInf == nil {
  151. // Use now time create code
  152. start = time.Now()
  153. startStr = start.Format(format)
  154. } else {
  155. // use start string create code
  156. startStr = startInf.(string)
  157. start, _ = time.ParseInLocation(format, startStr, time.Local)
  158. startStr = start.Format(format)
  159. }
  160. end = start.Add(time.Minute * time.Duration(minutes))
  161. endStr = end.Format(format)
  162. // create sha1 encode string
  163. sh := sha1.New()
  164. sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
  165. encoded := hex.EncodeToString(sh.Sum(nil))
  166. code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
  167. return code
  168. }
  169. // AvatarLink returns avatar link by given e-mail.
  170. func AvatarLink(email string) string {
  171. if setting.DisableGravatar || setting.OfflineMode {
  172. return setting.AppSubUrl + "/img/avatar_default.jpg"
  173. }
  174. gravatarHash := avatar.HashEmail(email)
  175. if setting.Service.EnableCacheAvatar {
  176. return setting.AppSubUrl + "/avatar/" + gravatarHash
  177. }
  178. return setting.GravatarSource + gravatarHash
  179. }
  180. // Seconds-based time units
  181. const (
  182. Minute = 60
  183. Hour = 60 * Minute
  184. Day = 24 * Hour
  185. Week = 7 * Day
  186. Month = 30 * Day
  187. Year = 12 * Month
  188. )
  189. func computeTimeDiff(diff int64) (int64, string) {
  190. diffStr := ""
  191. switch {
  192. case diff <= 0:
  193. diff = 0
  194. diffStr = "now"
  195. case diff < 2:
  196. diff = 0
  197. diffStr = "1 second"
  198. case diff < 1*Minute:
  199. diffStr = fmt.Sprintf("%d seconds", diff)
  200. diff = 0
  201. case diff < 2*Minute:
  202. diff -= 1 * Minute
  203. diffStr = "1 minute"
  204. case diff < 1*Hour:
  205. diffStr = fmt.Sprintf("%d minutes", diff/Minute)
  206. diff -= diff / Minute * Minute
  207. case diff < 2*Hour:
  208. diff -= 1 * Hour
  209. diffStr = "1 hour"
  210. case diff < 1*Day:
  211. diffStr = fmt.Sprintf("%d hours", diff/Hour)
  212. diff -= diff / Hour * Hour
  213. case diff < 2*Day:
  214. diff -= 1 * Day
  215. diffStr = "1 day"
  216. case diff < 1*Week:
  217. diffStr = fmt.Sprintf("%d days", diff/Day)
  218. diff -= diff / Day * Day
  219. case diff < 2*Week:
  220. diff -= 1 * Week
  221. diffStr = "1 week"
  222. case diff < 1*Month:
  223. diffStr = fmt.Sprintf("%d weeks", diff/Week)
  224. diff -= diff / Week * Week
  225. case diff < 2*Month:
  226. diff -= 1 * Month
  227. diffStr = "1 month"
  228. case diff < 1*Year:
  229. diffStr = fmt.Sprintf("%d months", diff/Month)
  230. diff -= diff / Month * Month
  231. case diff < 2*Year:
  232. diff -= 1 * Year
  233. diffStr = "1 year"
  234. default:
  235. diffStr = fmt.Sprintf("%d years", diff/Year)
  236. diff = 0
  237. }
  238. return diff, diffStr
  239. }
  240. // TimeSincePro calculates the time interval and generate full user-friendly string.
  241. func TimeSincePro(then time.Time) string {
  242. now := time.Now()
  243. diff := now.Unix() - then.Unix()
  244. if then.After(now) {
  245. return "future"
  246. }
  247. var timeStr, diffStr string
  248. for {
  249. if diff == 0 {
  250. break
  251. }
  252. diff, diffStr = computeTimeDiff(diff)
  253. timeStr += ", " + diffStr
  254. }
  255. return strings.TrimPrefix(timeStr, ", ")
  256. }
  257. func timeSince(then time.Time, lang string) string {
  258. now := time.Now()
  259. lbl := i18n.Tr(lang, "tool.ago")
  260. diff := now.Unix() - then.Unix()
  261. if then.After(now) {
  262. lbl = i18n.Tr(lang, "tool.from_now")
  263. diff = then.Unix() - now.Unix()
  264. }
  265. switch {
  266. case diff <= 0:
  267. return i18n.Tr(lang, "tool.now")
  268. case diff <= 2:
  269. return i18n.Tr(lang, "tool.1s", lbl)
  270. case diff < 1*Minute:
  271. return i18n.Tr(lang, "tool.seconds", diff, lbl)
  272. case diff < 2*Minute:
  273. return i18n.Tr(lang, "tool.1m", lbl)
  274. case diff < 1*Hour:
  275. return i18n.Tr(lang, "tool.minutes", diff/Minute, lbl)
  276. case diff < 2*Hour:
  277. return i18n.Tr(lang, "tool.1h", lbl)
  278. case diff < 1*Day:
  279. return i18n.Tr(lang, "tool.hours", diff/Hour, lbl)
  280. case diff < 2*Day:
  281. return i18n.Tr(lang, "tool.1d", lbl)
  282. case diff < 1*Week:
  283. return i18n.Tr(lang, "tool.days", diff/Day, lbl)
  284. case diff < 2*Week:
  285. return i18n.Tr(lang, "tool.1w", lbl)
  286. case diff < 1*Month:
  287. return i18n.Tr(lang, "tool.weeks", diff/Week, lbl)
  288. case diff < 2*Month:
  289. return i18n.Tr(lang, "tool.1mon", lbl)
  290. case diff < 1*Year:
  291. return i18n.Tr(lang, "tool.months", diff/Month, lbl)
  292. case diff < 2*Year:
  293. return i18n.Tr(lang, "tool.1y", lbl)
  294. default:
  295. return i18n.Tr(lang, "tool.years", diff/Year, lbl)
  296. }
  297. }
  298. func RawTimeSince(t time.Time, lang string) string {
  299. return timeSince(t, lang)
  300. }
  301. // TimeSince calculates the time interval and generate user-friendly string.
  302. func TimeSince(t time.Time, lang string) template.HTML {
  303. return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang)))
  304. }
  305. const (
  306. Byte = 1
  307. KByte = Byte * 1024
  308. MByte = KByte * 1024
  309. GByte = MByte * 1024
  310. TByte = GByte * 1024
  311. PByte = TByte * 1024
  312. EByte = PByte * 1024
  313. )
  314. var bytesSizeTable = map[string]uint64{
  315. "b": Byte,
  316. "kb": KByte,
  317. "mb": MByte,
  318. "gb": GByte,
  319. "tb": TByte,
  320. "pb": PByte,
  321. "eb": EByte,
  322. }
  323. func logn(n, b float64) float64 {
  324. return math.Log(n) / math.Log(b)
  325. }
  326. func humanateBytes(s uint64, base float64, sizes []string) string {
  327. if s < 10 {
  328. return fmt.Sprintf("%dB", s)
  329. }
  330. e := math.Floor(logn(float64(s), base))
  331. suffix := sizes[int(e)]
  332. val := float64(s) / math.Pow(base, math.Floor(e))
  333. f := "%.0f"
  334. if val < 10 {
  335. f = "%.1f"
  336. }
  337. return fmt.Sprintf(f+"%s", val, suffix)
  338. }
  339. // FileSize calculates the file size and generate user-friendly string.
  340. func FileSize(s int64) string {
  341. sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
  342. return humanateBytes(uint64(s), 1024, sizes)
  343. }
  344. // Subtract deals with subtraction of all types of number.
  345. func Subtract(left interface{}, right interface{}) interface{} {
  346. var rleft, rright int64
  347. var fleft, fright float64
  348. var isInt bool = true
  349. switch left.(type) {
  350. case int:
  351. rleft = int64(left.(int))
  352. case int8:
  353. rleft = int64(left.(int8))
  354. case int16:
  355. rleft = int64(left.(int16))
  356. case int32:
  357. rleft = int64(left.(int32))
  358. case int64:
  359. rleft = left.(int64)
  360. case float32:
  361. fleft = float64(left.(float32))
  362. isInt = false
  363. case float64:
  364. fleft = left.(float64)
  365. isInt = false
  366. }
  367. switch right.(type) {
  368. case int:
  369. rright = int64(right.(int))
  370. case int8:
  371. rright = int64(right.(int8))
  372. case int16:
  373. rright = int64(right.(int16))
  374. case int32:
  375. rright = int64(right.(int32))
  376. case int64:
  377. rright = right.(int64)
  378. case float32:
  379. fright = float64(left.(float32))
  380. isInt = false
  381. case float64:
  382. fleft = left.(float64)
  383. isInt = false
  384. }
  385. if isInt {
  386. return rleft - rright
  387. } else {
  388. return fleft + float64(rleft) - (fright + float64(rright))
  389. }
  390. }
  391. // StringsToInt64s converts a slice of string to a slice of int64.
  392. func StringsToInt64s(strs []string) []int64 {
  393. ints := make([]int64, len(strs))
  394. for i := range strs {
  395. ints[i] = com.StrTo(strs[i]).MustInt64()
  396. }
  397. return ints
  398. }
  399. // Int64sToStrings converts a slice of int64 to a slice of string.
  400. func Int64sToStrings(ints []int64) []string {
  401. strs := make([]string, len(ints))
  402. for i := range ints {
  403. strs[i] = com.ToStr(ints[i])
  404. }
  405. return strs
  406. }
  407. // Int64sToMap converts a slice of int64 to a int64 map.
  408. func Int64sToMap(ints []int64) map[int64]bool {
  409. m := make(map[int64]bool)
  410. for _, i := range ints {
  411. m[i] = true
  412. }
  413. return m
  414. }