PageRenderTime 69ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/document_root/texyla/plugins/gravatar/gravatar.js

https://github.com/jakubkinst/Course-Manager
JavaScript | 305 lines | 262 code | 37 blank | 6 comment | 45 complexity | 32554b120e6501b326209086b20651be MD5 | raw file
  1. /**
  2. * @author Lopo <lopo@lohini.net>
  3. */
  4. jQuery.texyla.setDefaults({
  5. gravatarMakro: '[* gravatar:%var% *]'
  6. });
  7. jQuery.texyla.addWindow('gravatar', {
  8. createContent: function () {
  9. var el = jQuery(
  10. '<div><form><div>' +
  11. '<label>' + this.lng.gravatarUrl + '<br>' +
  12. '<input type="text" size="20" class="key">' +
  13. "</label><br><br>" +
  14. this.lng.gravatarPreview + '</div>' +
  15. '<div class="thumb"></div>' +
  16. "</form></div>"
  17. );
  18. el.find('.key').bind('keyup change', function () {
  19. var val = this.value;
  20. if (!(/^[^@\s]+@[^@\s]+\.[a-z]{2,10}$/i).test(val)) {
  21. return;
  22. }
  23. jQuery(this).data('key', val);
  24. el.find('.thumb').html(
  25. '<img src="http://www.gravatar.com/avatar/' + faultylabs.MD5(val.trim().toLowerCase()).toLowerCase() + '?d=mm&s=32" width="32" height="32">'
  26. );
  27. });
  28. return el;
  29. },
  30. action: function (el) {
  31. var txt = this.expand(this.options.gravatarMakro, el.find('.key').data('key'));
  32. this.texy.update().replace(txt);
  33. },
  34. dimensions: [320, 'auto']
  35. });
  36. jQuery.texyla.addStrings('cs', {
  37. });
  38. //http://blog.faultylabs.com/?d=md5
  39. if (typeof faultylabs == 'undefined') {
  40. faultylabs = {}
  41. }
  42. faultylabs.MD5 = function(data) {
  43. function to_zerofilled_hex(n) {
  44. var t1 = (n >>> 0).toString(16)
  45. return "00000000".substr(0, 8 - t1.length) + t1
  46. }
  47. function chars_to_bytes(ac) {
  48. var retval = []
  49. for (var i = 0; i < ac.length; i++) {
  50. retval = retval.concat(str_to_bytes(ac[i]))
  51. }
  52. return retval
  53. }
  54. function int64_to_bytes(num) {
  55. var retval = []
  56. for (var i = 0; i < 8; i++) {
  57. retval.push(num & 0xFF)
  58. num = num >>> 8
  59. }
  60. return retval
  61. }
  62. function rol(num, places) {
  63. return ((num << places) & 0xFFFFFFFF) | (num >>> (32 - places))
  64. }
  65. function fF(b, c, d) {
  66. return (b & c) | (~b & d)
  67. }
  68. function fG(b, c, d) {
  69. return (d & b) | (~d & c)
  70. }
  71. function fH(b, c, d) {
  72. return b ^ c ^ d
  73. }
  74. function fI(b, c, d) {
  75. return c ^ (b | ~d)
  76. }
  77. function bytes_to_int32(arr, off) {
  78. return (arr[off + 3] << 24) | (arr[off + 2] << 16) | (arr[off + 1] << 8) | (arr[off])
  79. }
  80. function str_to_bytes(str) {
  81. var retval = [ ]
  82. for (var i = 0; i < str.length; i++)
  83. if (str.charCodeAt(i) <= 0x7F) {
  84. retval.push(str.charCodeAt(i))
  85. } else {
  86. var tmp = encodeURIComponent(str.charAt(i)).substr(1).split('%')
  87. for (var j = 0; j < tmp.length; j++) {
  88. retval.push(parseInt(tmp[j], 0x10))
  89. }
  90. }
  91. return retval
  92. }
  93. function int128le_to_hex(a, b, c, d) {
  94. var ra = ""
  95. var t = 0
  96. var ta = 0
  97. for (var i = 3; i >= 0; i--) {
  98. ta = arguments[i]
  99. t = (ta & 0xFF)
  100. ta = ta >>> 8
  101. t = t << 8
  102. t = t | (ta & 0xFF)
  103. ta = ta >>> 8
  104. t = t << 8
  105. t = t | (ta & 0xFF)
  106. ta = ta >>> 8
  107. t = t << 8
  108. t = t | ta
  109. ra = ra + to_zerofilled_hex(t)
  110. }
  111. return ra
  112. }
  113. function typed_to_plain(tarr) {
  114. var retval = new Array(tarr.length)
  115. for (var i = 0; i < tarr.length; i++) {
  116. retval[i] = tarr[i]
  117. }
  118. return retval
  119. }
  120. var databytes = null
  121. // String
  122. var type_mismatch = null
  123. if (typeof data == 'string') {
  124. databytes = str_to_bytes(data)
  125. } else if (data.constructor == Array) {
  126. if (data.length === 0) {
  127. databytes = data
  128. } else if (typeof data[0] == 'string') {
  129. databytes = chars_to_bytes(data)
  130. } else if (typeof data[0] == 'number') {
  131. databytes = data
  132. } else {
  133. type_mismatch = typeof data[0]
  134. }
  135. } else if (typeof ArrayBuffer != 'undefined') {
  136. if (data instanceof ArrayBuffer) {
  137. databytes = typed_to_plain(new Uint8Array(data))
  138. } else if ((data instanceof Uint8Array) || (data instanceof Int8Array)) {
  139. databytes = typed_to_plain(data)
  140. } else if ((data instanceof Uint32Array) || (data instanceof Int32Array) ||
  141. (data instanceof Uint16Array) || (data instanceof Int16Array) ||
  142. (data instanceof Float32Array) || (data instanceof Float64Array)
  143. ) {
  144. databytes = typed_to_plain(new Uint8Array(data.buffer))
  145. } else {
  146. type_mismatch = typeof data
  147. }
  148. } else {
  149. type_mismatch = typeof data
  150. }
  151. if (type_mismatch) {
  152. alert('MD5 type mismatch, cannot process ' + type_mismatch)
  153. }
  154. function _add(n1, n2) {
  155. return 0x0FFFFFFFF & (n1 + n2)
  156. }
  157. return do_digest()
  158. function do_digest() {
  159. function updateRun(nf, sin32, dw32, b32) {
  160. var temp = d
  161. d = c
  162. c = b
  163. //b = b + rol(a + (nf + (sin32 + dw32)), b32)
  164. b = _add(b,
  165. rol(
  166. _add(a,
  167. _add(nf, _add(sin32, dw32))
  168. ), b32
  169. )
  170. )
  171. a = temp
  172. }
  173. var org_len = databytes.length
  174. databytes.push(0x80)
  175. var tail = databytes.length % 64
  176. if (tail > 56) {
  177. for (var i = 0; i < (64 - tail); i++) {
  178. databytes.push(0x0)
  179. }
  180. tail = databytes.length % 64
  181. }
  182. for (i = 0; i < (56 - tail); i++) {
  183. databytes.push(0x0)
  184. }
  185. databytes = databytes.concat(int64_to_bytes(org_len * 8))
  186. var h0 = 0x67452301
  187. var h1 = 0xEFCDAB89
  188. var h2 = 0x98BADCFE
  189. var h3 = 0x10325476
  190. var a = 0, b = 0, c = 0, d = 0
  191. for (i = 0; i < databytes.length / 64; i++) {
  192. a = h0
  193. b = h1
  194. c = h2
  195. d = h3
  196. var ptr = i * 64
  197. updateRun(fF(b, c, d), 0xd76aa478, bytes_to_int32(databytes, ptr), 7)
  198. updateRun(fF(b, c, d), 0xe8c7b756, bytes_to_int32(databytes, ptr + 4), 12)
  199. updateRun(fF(b, c, d), 0x242070db, bytes_to_int32(databytes, ptr + 8), 17)
  200. updateRun(fF(b, c, d), 0xc1bdceee, bytes_to_int32(databytes, ptr + 12), 22)
  201. updateRun(fF(b, c, d), 0xf57c0faf, bytes_to_int32(databytes, ptr + 16), 7)
  202. updateRun(fF(b, c, d), 0x4787c62a, bytes_to_int32(databytes, ptr + 20), 12)
  203. updateRun(fF(b, c, d), 0xa8304613, bytes_to_int32(databytes, ptr + 24), 17)
  204. updateRun(fF(b, c, d), 0xfd469501, bytes_to_int32(databytes, ptr + 28), 22)
  205. updateRun(fF(b, c, d), 0x698098d8, bytes_to_int32(databytes, ptr + 32), 7)
  206. updateRun(fF(b, c, d), 0x8b44f7af, bytes_to_int32(databytes, ptr + 36), 12)
  207. updateRun(fF(b, c, d), 0xffff5bb1, bytes_to_int32(databytes, ptr + 40), 17)
  208. updateRun(fF(b, c, d), 0x895cd7be, bytes_to_int32(databytes, ptr + 44), 22)
  209. updateRun(fF(b, c, d), 0x6b901122, bytes_to_int32(databytes, ptr + 48), 7)
  210. updateRun(fF(b, c, d), 0xfd987193, bytes_to_int32(databytes, ptr + 52), 12)
  211. updateRun(fF(b, c, d), 0xa679438e, bytes_to_int32(databytes, ptr + 56), 17)
  212. updateRun(fF(b, c, d), 0x49b40821, bytes_to_int32(databytes, ptr + 60), 22)
  213. updateRun(fG(b, c, d), 0xf61e2562, bytes_to_int32(databytes, ptr + 4), 5)
  214. updateRun(fG(b, c, d), 0xc040b340, bytes_to_int32(databytes, ptr + 24), 9)
  215. updateRun(fG(b, c, d), 0x265e5a51, bytes_to_int32(databytes, ptr + 44), 14)
  216. updateRun(fG(b, c, d), 0xe9b6c7aa, bytes_to_int32(databytes, ptr), 20)
  217. updateRun(fG(b, c, d), 0xd62f105d, bytes_to_int32(databytes, ptr + 20), 5)
  218. updateRun(fG(b, c, d), 0x2441453, bytes_to_int32(databytes, ptr + 40), 9)
  219. updateRun(fG(b, c, d), 0xd8a1e681, bytes_to_int32(databytes, ptr + 60), 14)
  220. updateRun(fG(b, c, d), 0xe7d3fbc8, bytes_to_int32(databytes, ptr + 16), 20)
  221. updateRun(fG(b, c, d), 0x21e1cde6, bytes_to_int32(databytes, ptr + 36), 5)
  222. updateRun(fG(b, c, d), 0xc33707d6, bytes_to_int32(databytes, ptr + 56), 9)
  223. updateRun(fG(b, c, d), 0xf4d50d87, bytes_to_int32(databytes, ptr + 12), 14)
  224. updateRun(fG(b, c, d), 0x455a14ed, bytes_to_int32(databytes, ptr + 32), 20)
  225. updateRun(fG(b, c, d), 0xa9e3e905, bytes_to_int32(databytes, ptr + 52), 5)
  226. updateRun(fG(b, c, d), 0xfcefa3f8, bytes_to_int32(databytes, ptr + 8), 9)
  227. updateRun(fG(b, c, d), 0x676f02d9, bytes_to_int32(databytes, ptr + 28), 14)
  228. updateRun(fG(b, c, d), 0x8d2a4c8a, bytes_to_int32(databytes, ptr + 48), 20)
  229. updateRun(fH(b, c, d), 0xfffa3942, bytes_to_int32(databytes, ptr + 20), 4)
  230. updateRun(fH(b, c, d), 0x8771f681, bytes_to_int32(databytes, ptr + 32), 11)
  231. updateRun(fH(b, c, d), 0x6d9d6122, bytes_to_int32(databytes, ptr + 44), 16)
  232. updateRun(fH(b, c, d), 0xfde5380c, bytes_to_int32(databytes, ptr + 56), 23)
  233. updateRun(fH(b, c, d), 0xa4beea44, bytes_to_int32(databytes, ptr + 4), 4)
  234. updateRun(fH(b, c, d), 0x4bdecfa9, bytes_to_int32(databytes, ptr + 16), 11)
  235. updateRun(fH(b, c, d), 0xf6bb4b60, bytes_to_int32(databytes, ptr + 28), 16)
  236. updateRun(fH(b, c, d), 0xbebfbc70, bytes_to_int32(databytes, ptr + 40), 23)
  237. updateRun(fH(b, c, d), 0x289b7ec6, bytes_to_int32(databytes, ptr + 52), 4)
  238. updateRun(fH(b, c, d), 0xeaa127fa, bytes_to_int32(databytes, ptr), 11)
  239. updateRun(fH(b, c, d), 0xd4ef3085, bytes_to_int32(databytes, ptr + 12), 16)
  240. updateRun(fH(b, c, d), 0x4881d05, bytes_to_int32(databytes, ptr + 24), 23)
  241. updateRun(fH(b, c, d), 0xd9d4d039, bytes_to_int32(databytes, ptr + 36), 4)
  242. updateRun(fH(b, c, d), 0xe6db99e5, bytes_to_int32(databytes, ptr + 48), 11)
  243. updateRun(fH(b, c, d), 0x1fa27cf8, bytes_to_int32(databytes, ptr + 60), 16)
  244. updateRun(fH(b, c, d), 0xc4ac5665, bytes_to_int32(databytes, ptr + 8), 23)
  245. updateRun(fI(b, c, d), 0xf4292244, bytes_to_int32(databytes, ptr), 6)
  246. updateRun(fI(b, c, d), 0x432aff97, bytes_to_int32(databytes, ptr + 28), 10)
  247. updateRun(fI(b, c, d), 0xab9423a7, bytes_to_int32(databytes, ptr + 56), 15)
  248. updateRun(fI(b, c, d), 0xfc93a039, bytes_to_int32(databytes, ptr + 20), 21)
  249. updateRun(fI(b, c, d), 0x655b59c3, bytes_to_int32(databytes, ptr + 48), 6)
  250. updateRun(fI(b, c, d), 0x8f0ccc92, bytes_to_int32(databytes, ptr + 12), 10)
  251. updateRun(fI(b, c, d), 0xffeff47d, bytes_to_int32(databytes, ptr + 40), 15)
  252. updateRun(fI(b, c, d), 0x85845dd1, bytes_to_int32(databytes, ptr + 4), 21)
  253. updateRun(fI(b, c, d), 0x6fa87e4f, bytes_to_int32(databytes, ptr + 32), 6)
  254. updateRun(fI(b, c, d), 0xfe2ce6e0, bytes_to_int32(databytes, ptr + 60), 10)
  255. updateRun(fI(b, c, d), 0xa3014314, bytes_to_int32(databytes, ptr + 24), 15)
  256. updateRun(fI(b, c, d), 0x4e0811a1, bytes_to_int32(databytes, ptr + 52), 21)
  257. updateRun(fI(b, c, d), 0xf7537e82, bytes_to_int32(databytes, ptr + 16), 6)
  258. updateRun(fI(b, c, d), 0xbd3af235, bytes_to_int32(databytes, ptr + 44), 10)
  259. updateRun(fI(b, c, d), 0x2ad7d2bb, bytes_to_int32(databytes, ptr + 8), 15)
  260. updateRun(fI(b, c, d), 0xeb86d391, bytes_to_int32(databytes, ptr + 36), 21)
  261. h0 = _add(h0, a)
  262. h1 = _add(h1, b)
  263. h2 = _add(h2, c)
  264. h3 = _add(h3, d)
  265. }
  266. return int128le_to_hex(h3, h2, h1, h0).toUpperCase()
  267. }
  268. }