chatjs.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. require_once("user.php");
  3. $USER = new User("registration_callback");
  4. $userpic = md5( strtolower( trim( $USER->email ) ) );
  5. header("Content-type: application/javascript");
  6. ?>
  7. /*
  8. * JavaScript MD5
  9. * https://github.com/blueimp/JavaScript-MD5
  10. *
  11. * Copyright 2011, Sebastian Tschan
  12. * https://blueimp.net
  13. *
  14. * Licensed under the MIT license:
  15. * https://opensource.org/licenses/MIT
  16. *
  17. * Based on
  18. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  19. * Digest Algorithm, as defined in RFC 1321.
  20. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  21. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  22. * Distributed under the BSD License
  23. * See http://pajhome.org.uk/crypt/md5 for more info.
  24. */
  25. /* global define */
  26. ;(function ($) {
  27. 'use strict'
  28. /*
  29. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  30. * to work around bugs in some JS interpreters.
  31. */
  32. function safeAdd (x, y) {
  33. var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  34. var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  35. return (msw << 16) | (lsw & 0xFFFF)
  36. }
  37. /*
  38. * Bitwise rotate a 32-bit number to the left.
  39. */
  40. function bitRotateLeft (num, cnt) {
  41. return (num << cnt) | (num >>> (32 - cnt))
  42. }
  43. /*
  44. * These functions implement the four basic operations the algorithm uses.
  45. */
  46. function md5cmn (q, a, b, x, s, t) {
  47. return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
  48. }
  49. function md5ff (a, b, c, d, x, s, t) {
  50. return md5cmn((b & c) | ((~b) & d), a, b, x, s, t)
  51. }
  52. function md5gg (a, b, c, d, x, s, t) {
  53. return md5cmn((b & d) | (c & (~d)), a, b, x, s, t)
  54. }
  55. function md5hh (a, b, c, d, x, s, t) {
  56. return md5cmn(b ^ c ^ d, a, b, x, s, t)
  57. }
  58. function md5ii (a, b, c, d, x, s, t) {
  59. return md5cmn(c ^ (b | (~d)), a, b, x, s, t)
  60. }
  61. /*
  62. * Calculate the MD5 of an array of little-endian words, and a bit length.
  63. */
  64. function binlMD5 (x, len) {
  65. /* append padding */
  66. x[len >> 5] |= 0x80 << (len % 32)
  67. x[(((len + 64) >>> 9) << 4) + 14] = len
  68. var i
  69. var olda
  70. var oldb
  71. var oldc
  72. var oldd
  73. var a = 1732584193
  74. var b = -271733879
  75. var c = -1732584194
  76. var d = 271733878
  77. for (i = 0; i < x.length; i += 16) {
  78. olda = a
  79. oldb = b
  80. oldc = c
  81. oldd = d
  82. a = md5ff(a, b, c, d, x[i], 7, -680876936)
  83. d = md5ff(d, a, b, c, x[i + 1], 12, -389564586)
  84. c = md5ff(c, d, a, b, x[i + 2], 17, 606105819)
  85. b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330)
  86. a = md5ff(a, b, c, d, x[i + 4], 7, -176418897)
  87. d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426)
  88. c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341)
  89. b = md5ff(b, c, d, a, x[i + 7], 22, -45705983)
  90. a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416)
  91. d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417)
  92. c = md5ff(c, d, a, b, x[i + 10], 17, -42063)
  93. b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162)
  94. a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682)
  95. d = md5ff(d, a, b, c, x[i + 13], 12, -40341101)
  96. c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290)
  97. b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329)
  98. a = md5gg(a, b, c, d, x[i + 1], 5, -165796510)
  99. d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632)
  100. c = md5gg(c, d, a, b, x[i + 11], 14, 643717713)
  101. b = md5gg(b, c, d, a, x[i], 20, -373897302)
  102. a = md5gg(a, b, c, d, x[i + 5], 5, -701558691)
  103. d = md5gg(d, a, b, c, x[i + 10], 9, 38016083)
  104. c = md5gg(c, d, a, b, x[i + 15], 14, -660478335)
  105. b = md5gg(b, c, d, a, x[i + 4], 20, -405537848)
  106. a = md5gg(a, b, c, d, x[i + 9], 5, 568446438)
  107. d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690)
  108. c = md5gg(c, d, a, b, x[i + 3], 14, -187363961)
  109. b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501)
  110. a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467)
  111. d = md5gg(d, a, b, c, x[i + 2], 9, -51403784)
  112. c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473)
  113. b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734)
  114. a = md5hh(a, b, c, d, x[i + 5], 4, -378558)
  115. d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463)
  116. c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562)
  117. b = md5hh(b, c, d, a, x[i + 14], 23, -35309556)
  118. a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060)
  119. d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353)
  120. c = md5hh(c, d, a, b, x[i + 7], 16, -155497632)
  121. b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640)
  122. a = md5hh(a, b, c, d, x[i + 13], 4, 681279174)
  123. d = md5hh(d, a, b, c, x[i], 11, -358537222)
  124. c = md5hh(c, d, a, b, x[i + 3], 16, -722521979)
  125. b = md5hh(b, c, d, a, x[i + 6], 23, 76029189)
  126. a = md5hh(a, b, c, d, x[i + 9], 4, -640364487)
  127. d = md5hh(d, a, b, c, x[i + 12], 11, -421815835)
  128. c = md5hh(c, d, a, b, x[i + 15], 16, 530742520)
  129. b = md5hh(b, c, d, a, x[i + 2], 23, -995338651)
  130. a = md5ii(a, b, c, d, x[i], 6, -198630844)
  131. d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415)
  132. c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905)
  133. b = md5ii(b, c, d, a, x[i + 5], 21, -57434055)
  134. a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571)
  135. d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606)
  136. c = md5ii(c, d, a, b, x[i + 10], 15, -1051523)
  137. b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799)
  138. a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359)
  139. d = md5ii(d, a, b, c, x[i + 15], 10, -30611744)
  140. c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380)
  141. b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649)
  142. a = md5ii(a, b, c, d, x[i + 4], 6, -145523070)
  143. d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379)
  144. c = md5ii(c, d, a, b, x[i + 2], 15, 718787259)
  145. b = md5ii(b, c, d, a, x[i + 9], 21, -343485551)
  146. a = safeAdd(a, olda)
  147. b = safeAdd(b, oldb)
  148. c = safeAdd(c, oldc)
  149. d = safeAdd(d, oldd)
  150. }
  151. return [a, b, c, d]
  152. }
  153. /*
  154. * Convert an array of little-endian words to a string
  155. */
  156. function binl2rstr (input) {
  157. var i
  158. var output = ''
  159. var length32 = input.length * 32
  160. for (i = 0; i < length32; i += 8) {
  161. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF)
  162. }
  163. return output
  164. }
  165. /*
  166. * Convert a raw string to an array of little-endian words
  167. * Characters >255 have their high-byte silently ignored.
  168. */
  169. function rstr2binl (input) {
  170. var i
  171. var output = []
  172. output[(input.length >> 2) - 1] = undefined
  173. for (i = 0; i < output.length; i += 1) {
  174. output[i] = 0
  175. }
  176. var length8 = input.length * 8
  177. for (i = 0; i < length8; i += 8) {
  178. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32)
  179. }
  180. return output
  181. }
  182. /*
  183. * Calculate the MD5 of a raw string
  184. */
  185. function rstrMD5 (s) {
  186. return binl2rstr(binlMD5(rstr2binl(s), s.length * 8))
  187. }
  188. /*
  189. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  190. */
  191. function rstrHMACMD5 (key, data) {
  192. var i
  193. var bkey = rstr2binl(key)
  194. var ipad = []
  195. var opad = []
  196. var hash
  197. ipad[15] = opad[15] = undefined
  198. if (bkey.length > 16) {
  199. bkey = binlMD5(bkey, key.length * 8)
  200. }
  201. for (i = 0; i < 16; i += 1) {
  202. ipad[i] = bkey[i] ^ 0x36363636
  203. opad[i] = bkey[i] ^ 0x5C5C5C5C
  204. }
  205. hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8)
  206. return binl2rstr(binlMD5(opad.concat(hash), 512 + 128))
  207. }
  208. /*
  209. * Convert a raw string to a hex string
  210. */
  211. function rstr2hex (input) {
  212. var hexTab = '0123456789abcdef'
  213. var output = ''
  214. var x
  215. var i
  216. for (i = 0; i < input.length; i += 1) {
  217. x = input.charCodeAt(i)
  218. output += hexTab.charAt((x >>> 4) & 0x0F) +
  219. hexTab.charAt(x & 0x0F)
  220. }
  221. return output
  222. }
  223. /*
  224. * Encode a string as utf-8
  225. */
  226. function str2rstrUTF8 (input) {
  227. return unescape(encodeURIComponent(input))
  228. }
  229. /*
  230. * Take string arguments and return either raw or hex encoded strings
  231. */
  232. function rawMD5 (s) {
  233. return rstrMD5(str2rstrUTF8(s))
  234. }
  235. function hexMD5 (s) {
  236. return rstr2hex(rawMD5(s))
  237. }
  238. function rawHMACMD5 (k, d) {
  239. return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d))
  240. }
  241. function hexHMACMD5 (k, d) {
  242. return rstr2hex(rawHMACMD5(k, d))
  243. }
  244. function md5 (string, key, raw) {
  245. if (!key) {
  246. if (!raw) {
  247. return hexMD5(string)
  248. }
  249. return rawMD5(string)
  250. }
  251. if (!raw) {
  252. return hexHMACMD5(key, string)
  253. }
  254. return rawHMACMD5(key, string)
  255. }
  256. if (typeof define === 'function' && define.amd) {
  257. define(function () {
  258. return md5
  259. })
  260. } else if (typeof module === 'object' && module.exports) {
  261. module.exports = md5
  262. } else {
  263. $.md5 = md5
  264. }
  265. }(this));
  266. var last_time=0;
  267. function sendText(){
  268. $('#writehere')[0].onkeydown=function(e){
  269. if (e.keyCode==13){
  270. e.preventDefault();
  271. if (this.value){
  272. var datatosend={"text":this.value,"user":"<?=$USER->username;?>","email":"<?=$USER->email;?>"};
  273. $.ajax({
  274. type:"POST",
  275. url:"chatAJAX.php",
  276. data:datatosend,
  277. datatype:"json",
  278. success:function(data){}
  279. })
  280. this.value='';
  281. $("#writehere").attr('placeholder', "Sending");
  282. $("#writehere").prop('disabled', true);
  283. }
  284. }
  285. }
  286. }
  287. function checkText(){
  288. $.ajax({
  289. type:"POST",
  290. url:"chatAJAX.php",
  291. data:{time:last_time},
  292. datatype:"json",
  293. success:function(data){
  294. if (data){
  295. data=JSON.parse(data);
  296. for (var i=0; i<data.length; i++){
  297. if(data[i].USER === "<?=$USER->username;?>"){
  298. $('#messages').append(
  299. '<li><img src="https://www.gravatar.com/avatar/'+ md5(data[i].EMAIL) +'?s=100&d=mm" class="img-circle user-avatar" alt="user"><div class="chat-panel blue-bg"><div class="chat-heading clearfix"><h4 class="pull-left zero-m">' + data[i].USER + '</h4><p class="pull-right"><i class="fa fa-clock-o"></i>'+ (setDateTime(data[i].TIME)) + '</p></div><div class="chat-body">' + data[i].MESSAGE + '</div></div></li>'
  300. );
  301. $(".box").animate({ scrollTop: $('.box').prop("scrollHeight")}, 0);
  302. }else{
  303. $('#messages').append(
  304. '<li class="chat-inverted"><img src="https://www.gravatar.com/avatar/'+ md5(data[i].EMAIL) +'?s=100&d=mm" class="img-circle user-avatar" alt="user"><div class="chat-panel red-bg"><div class="chat-heading clearfix"><h4 class="pull-left zero-m">' + data[i].USER + '</h4><p class="pull-right"><i class="fa fa-clock-o"></i>'+ (setDateTime(data[i].TIME)) + '</p></div><div class="chat-body">' + data[i].MESSAGE + '</div></div></li>'
  305. );
  306. $(".box").animate({ scrollTop: $('.box').prop("scrollHeight")}, 0);
  307. }
  308. }
  309. last_time=Date.now()/1000;
  310. $('#messages').scrollTop($('#messages')[0].scrollHeight);
  311. }
  312. checkText();
  313. $("#writehere").attr('placeholder', "Enter your text");
  314. $("#writehere").prop('disabled', false);
  315. $("#writehere").focus();
  316. }
  317. });
  318. }
  319. function datecompare(date1, sign, date2) {
  320. var day1 = date1.getDate();
  321. var mon1 = date1.getMonth();
  322. var year1 = date1.getFullYear();
  323. var day2 = date2.getDate();
  324. var mon2 = date2.getMonth();
  325. var year2 = date2.getFullYear();
  326. if (sign === '===') {
  327. if (day1 === day2 && mon1 === mon2 && year1 === year2) return true;
  328. else return false;
  329. }
  330. else if (sign === '>') {
  331. if (year1 > year2) return true;
  332. else if (year1 === year2 && mon1 > mon2) return true;
  333. else if (year1 === year2 && mon1 === mon2 && day1 > day2) return true;
  334. else return false;
  335. }
  336. }
  337. function setDateTime(timedate){
  338. var tempdate=new Date(timedate*1000),
  339. date=TwoDigits(tempdate.getDate()),
  340. month=TwoDigits(tempdate.getMonth()+1),
  341. year=tempdate.getFullYear(),
  342. hours=TwoDigits(tempdate.getHours()),
  343. mins=TwoDigits(tempdate.getMinutes()),
  344. sec=TwoDigits(tempdate.getSeconds());
  345. var Today = new Date();
  346. if(datecompare(Today, ">", tempdate)){
  347. return (month+'-'+date+' '+hours+':'+mins );
  348. }
  349. if(datecompare(Today, "===", tempdate)){
  350. return (hours+':'+mins );
  351. }
  352. }
  353. function TwoDigits(number){
  354. return (number<10 ? '0' : '') + number;
  355. }
  356. sendText();
  357. checkText();