tool.go 12 KB

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