chatjs.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. function dblDigit(d) {
  8. if (d < 10) { d = "0" + d;}
  9. return d;
  10. }
  11. function formatAMPM(date) {
  12. var hours = date.getHours();
  13. var minutes = date.getMinutes();
  14. var month = dblDigit(date.getMonth() + 1);
  15. var day = dblDigit(date.getDate());
  16. var ampm = hours >= 12 ? 'P' : 'A';
  17. hours = hours % 12;
  18. hours = hours ? hours : 12; // the hour '0' should be '12'
  19. hours = dblDigit(hours);
  20. minutes = minutes < 10 ? '0'+minutes : minutes;
  21. var strTime = month + '-' + day + ' ' + hours + ':' + minutes + '' + ampm;
  22. return strTime;
  23. }
  24. var isMobile = false; //initiate as false
  25. var d = new Date();
  26. var timezone = d.getTimezoneOffset();
  27. console.log(timezone)
  28. // device detection
  29. if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
  30. || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(navigator.userAgent.substr(0,4))) isMobile = true;
  31. /*
  32. * JavaScript MD5
  33. * https://github.com/blueimp/JavaScript-MD5
  34. *
  35. * Copyright 2011, Sebastian Tschan
  36. * https://blueimp.net
  37. *
  38. * Licensed under the MIT license:
  39. * https://opensource.org/licenses/MIT
  40. *
  41. * Based on
  42. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  43. * Digest Algorithm, as defined in RFC 1321.
  44. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  45. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  46. * Distributed under the BSD License
  47. * See http://pajhome.org.uk/crypt/md5 for more info.
  48. */
  49. /* global define */
  50. ;(function ($) {
  51. 'use strict'
  52. /*
  53. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  54. * to work around bugs in some JS interpreters.
  55. */
  56. function safeAdd (x, y) {
  57. var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  58. var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  59. return (msw << 16) | (lsw & 0xFFFF)
  60. }
  61. /*
  62. * Bitwise rotate a 32-bit number to the left.
  63. */
  64. function bitRotateLeft (num, cnt) {
  65. return (num << cnt) | (num >>> (32 - cnt))
  66. }
  67. /*
  68. * These functions implement the four basic operations the algorithm uses.
  69. */
  70. function md5cmn (q, a, b, x, s, t) {
  71. return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
  72. }
  73. function md5ff (a, b, c, d, x, s, t) {
  74. return md5cmn((b & c) | ((~b) & d), a, b, x, s, t)
  75. }
  76. function md5gg (a, b, c, d, x, s, t) {
  77. return md5cmn((b & d) | (c & (~d)), a, b, x, s, t)
  78. }
  79. function md5hh (a, b, c, d, x, s, t) {
  80. return md5cmn(b ^ c ^ d, a, b, x, s, t)
  81. }
  82. function md5ii (a, b, c, d, x, s, t) {
  83. return md5cmn(c ^ (b | (~d)), a, b, x, s, t)
  84. }
  85. /*
  86. * Calculate the MD5 of an array of little-endian words, and a bit length.
  87. */
  88. function binlMD5 (x, len) {
  89. /* append padding */
  90. x[len >> 5] |= 0x80 << (len % 32)
  91. x[(((len + 64) >>> 9) << 4) + 14] = len
  92. var i
  93. var olda
  94. var oldb
  95. var oldc
  96. var oldd
  97. var a = 1732584193
  98. var b = -271733879
  99. var c = -1732584194
  100. var d = 271733878
  101. for (i = 0; i < x.length; i += 16) {
  102. olda = a
  103. oldb = b
  104. oldc = c
  105. oldd = d
  106. a = md5ff(a, b, c, d, x[i], 7, -680876936)
  107. d = md5ff(d, a, b, c, x[i + 1], 12, -389564586)
  108. c = md5ff(c, d, a, b, x[i + 2], 17, 606105819)
  109. b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330)
  110. a = md5ff(a, b, c, d, x[i + 4], 7, -176418897)
  111. d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426)
  112. c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341)
  113. b = md5ff(b, c, d, a, x[i + 7], 22, -45705983)
  114. a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416)
  115. d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417)
  116. c = md5ff(c, d, a, b, x[i + 10], 17, -42063)
  117. b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162)
  118. a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682)
  119. d = md5ff(d, a, b, c, x[i + 13], 12, -40341101)
  120. c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290)
  121. b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329)
  122. a = md5gg(a, b, c, d, x[i + 1], 5, -165796510)
  123. d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632)
  124. c = md5gg(c, d, a, b, x[i + 11], 14, 643717713)
  125. b = md5gg(b, c, d, a, x[i], 20, -373897302)
  126. a = md5gg(a, b, c, d, x[i + 5], 5, -701558691)
  127. d = md5gg(d, a, b, c, x[i + 10], 9, 38016083)
  128. c = md5gg(c, d, a, b, x[i + 15], 14, -660478335)
  129. b = md5gg(b, c, d, a, x[i + 4], 20, -405537848)
  130. a = md5gg(a, b, c, d, x[i + 9], 5, 568446438)
  131. d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690)
  132. c = md5gg(c, d, a, b, x[i + 3], 14, -187363961)
  133. b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501)
  134. a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467)
  135. d = md5gg(d, a, b, c, x[i + 2], 9, -51403784)
  136. c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473)
  137. b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734)
  138. a = md5hh(a, b, c, d, x[i + 5], 4, -378558)
  139. d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463)
  140. c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562)
  141. b = md5hh(b, c, d, a, x[i + 14], 23, -35309556)
  142. a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060)
  143. d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353)
  144. c = md5hh(c, d, a, b, x[i + 7], 16, -155497632)
  145. b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640)
  146. a = md5hh(a, b, c, d, x[i + 13], 4, 681279174)
  147. d = md5hh(d, a, b, c, x[i], 11, -358537222)
  148. c = md5hh(c, d, a, b, x[i + 3], 16, -722521979)
  149. b = md5hh(b, c, d, a, x[i + 6], 23, 76029189)
  150. a = md5hh(a, b, c, d, x[i + 9], 4, -640364487)
  151. d = md5hh(d, a, b, c, x[i + 12], 11, -421815835)
  152. c = md5hh(c, d, a, b, x[i + 15], 16, 530742520)
  153. b = md5hh(b, c, d, a, x[i + 2], 23, -995338651)
  154. a = md5ii(a, b, c, d, x[i], 6, -198630844)
  155. d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415)
  156. c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905)
  157. b = md5ii(b, c, d, a, x[i + 5], 21, -57434055)
  158. a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571)
  159. d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606)
  160. c = md5ii(c, d, a, b, x[i + 10], 15, -1051523)
  161. b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799)
  162. a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359)
  163. d = md5ii(d, a, b, c, x[i + 15], 10, -30611744)
  164. c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380)
  165. b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649)
  166. a = md5ii(a, b, c, d, x[i + 4], 6, -145523070)
  167. d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379)
  168. c = md5ii(c, d, a, b, x[i + 2], 15, 718787259)
  169. b = md5ii(b, c, d, a, x[i + 9], 21, -343485551)
  170. a = safeAdd(a, olda)
  171. b = safeAdd(b, oldb)
  172. c = safeAdd(c, oldc)
  173. d = safeAdd(d, oldd)
  174. }
  175. return [a, b, c, d]
  176. }
  177. /*
  178. * Convert an array of little-endian words to a string
  179. */
  180. function binl2rstr (input) {
  181. var i
  182. var output = ''
  183. var length32 = input.length * 32
  184. for (i = 0; i < length32; i += 8) {
  185. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF)
  186. }
  187. return output
  188. }
  189. /*
  190. * Convert a raw string to an array of little-endian words
  191. * Characters >255 have their high-byte silently ignored.
  192. */
  193. function rstr2binl (input) {
  194. var i
  195. var output = []
  196. output[(input.length >> 2) - 1] = undefined
  197. for (i = 0; i < output.length; i += 1) {
  198. output[i] = 0
  199. }
  200. var length8 = input.length * 8
  201. for (i = 0; i < length8; i += 8) {
  202. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32)
  203. }
  204. return output
  205. }
  206. /*
  207. * Calculate the MD5 of a raw string
  208. */
  209. function rstrMD5 (s) {
  210. return binl2rstr(binlMD5(rstr2binl(s), s.length * 8))
  211. }
  212. /*
  213. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  214. */
  215. function rstrHMACMD5 (key, data) {
  216. var i
  217. var bkey = rstr2binl(key)
  218. var ipad = []
  219. var opad = []
  220. var hash
  221. ipad[15] = opad[15] = undefined
  222. if (bkey.length > 16) {
  223. bkey = binlMD5(bkey, key.length * 8)
  224. }
  225. for (i = 0; i < 16; i += 1) {
  226. ipad[i] = bkey[i] ^ 0x36363636
  227. opad[i] = bkey[i] ^ 0x5C5C5C5C
  228. }
  229. hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8)
  230. return binl2rstr(binlMD5(opad.concat(hash), 512 + 128))
  231. }
  232. /*
  233. * Convert a raw string to a hex string
  234. */
  235. function rstr2hex (input) {
  236. var hexTab = '0123456789abcdef'
  237. var output = ''
  238. var x
  239. var i
  240. for (i = 0; i < input.length; i += 1) {
  241. x = input.charCodeAt(i)
  242. output += hexTab.charAt((x >>> 4) & 0x0F) +
  243. hexTab.charAt(x & 0x0F)
  244. }
  245. return output
  246. }
  247. /*
  248. * Encode a string as utf-8
  249. */
  250. function str2rstrUTF8 (input) {
  251. return unescape(encodeURIComponent(input))
  252. }
  253. /*
  254. * Take string arguments and return either raw or hex encoded strings
  255. */
  256. function rawMD5 (s) {
  257. return rstrMD5(str2rstrUTF8(s))
  258. }
  259. function hexMD5 (s) {
  260. return rstr2hex(rawMD5(s))
  261. }
  262. function rawHMACMD5 (k, d) {
  263. return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d))
  264. }
  265. function hexHMACMD5 (k, d) {
  266. return rstr2hex(rawHMACMD5(k, d))
  267. }
  268. function md5 (string, key, raw) {
  269. if (!key) {
  270. if (!raw) {
  271. return hexMD5(string)
  272. }
  273. return rawMD5(string)
  274. }
  275. if (!raw) {
  276. return hexHMACMD5(key, string)
  277. }
  278. return rawHMACMD5(key, string)
  279. }
  280. if (typeof define === 'function' && define.amd) {
  281. define(function () {
  282. return md5
  283. })
  284. } else if (typeof module === 'object' && module.exports) {
  285. module.exports = md5
  286. } else {
  287. $.md5 = md5
  288. }
  289. }(this));
  290. $(document).ready(function()
  291. {
  292. // init
  293. $(window).focus();
  294. var tabinfocus = true;
  295. $("#chat").hide();
  296. // allowed characters in username
  297. $("#username").keyup(function()
  298. {
  299. var text = $(this).val();
  300. $(this).val(text.replace(/[^a-zA-Z0-9 ]/g, ""));
  301. });
  302. // enter username
  303. var user = "";
  304. user = "<?php echo $USER->username; ?>";
  305. // choose avatar, check username, start chat
  306. var avatar = "";
  307. avatar = "https://www.gravatar.com/avatar/" + md5('<?php echo $USER->email; ?>') + "?d=mm";
  308. startchat();
  309. // start chat
  310. function startchat()
  311. {
  312. // zoom chat to fit viewport
  313. if( $(window).innerWidth() > 630 ) // if not mobile
  314. {
  315. var ratio = $(window).innerHeight() / 660; // browser viewport by chat height
  316. //$("#chat").css("zoom", ratio);
  317. //$("#chat").css("-moz-transform-origin", "0 0");
  318. //$("#chat").css("-moz-transform", "scale(" + ratio + ")");
  319. }
  320. // update favicon to user avatar
  321. //$("#favicon").remove();
  322. //var userfavicon = "<link id=\"favicon\" type=\"image/x-icon\"" +
  323. " rel=\"shortcut icon\" href=\"img/" + avatar + ".ico\">";
  324. //$(userfavicon).appendTo("head");
  325. // start chat
  326. $("#chat").show();
  327. $("#message").focus();
  328. refresh();
  329. }
  330. // allowed characters in message
  331. // log message
  332. $("#message").focus().keypress(function(event)
  333. {
  334. if( event.keyCode === 13 )
  335. {
  336. event.preventDefault();
  337. if( $("#content").is(":visible") )
  338. {
  339. var message = $("#message").val();
  340. message = encodeURIComponent(message);
  341. var data = "messagedata=" + message + "###" + user + "###" + avatar;
  342. $(this).val("");
  343. $.ajax
  344. ({
  345. type: "POST",
  346. url: "chat/logmessage.php",
  347. data: data,
  348. cache: false
  349. });
  350. }
  351. }
  352. });
  353. // log emoticon
  354. $(".emoticonimg").click(function()
  355. {
  356. var emoticonselected = $(this);
  357. emoticonselected.hide();
  358. var emoticonid = $(this).attr("id");
  359. var data = "messagedata=" + "specialchar" + emoticonid + "###" + user + "###" + avatar;
  360. $.ajax
  361. ({
  362. type: "POST",
  363. url: "chat/logmessage.php",
  364. data: data,
  365. cache: false,
  366. success: function(result)
  367. {
  368. emoticonselected.show();
  369. }
  370. });
  371. });
  372. // show upload form
  373. $("#showuploadform").click(function()
  374. {
  375. $("#showuploadform").hide();
  376. $("#showemoticons").show();
  377. $("#uploadform").show();
  378. $("#emoticons").hide();
  379. $("#loadingwrapper").hide();
  380. var r = Math.floor((Math.random() * 14) + 1);
  381. var randomemoticon = "chat/img/emoticon" + r + ".png";
  382. $("#showemoticonsimg").attr("src", randomemoticon);
  383. refresh(); // also use as manual content refresh
  384. });
  385. // upload image
  386. $("#form").submit(function(event)
  387. {
  388. event.preventDefault();
  389. $("#uploadform").hide();
  390. $("#showemoticons").hide();
  391. $("#loadingwrapper").show();
  392. var filedata = $("#uploadimage").prop("files")[0];
  393. var formdata = new FormData();
  394. formdata.append("image", filedata);
  395. var datavars = user + "###" + avatar;
  396. formdata.append("datavars", datavars);
  397. $.ajax
  398. ({
  399. type: "POST",
  400. url: "chat/uploadimage.php",
  401. cache: false,
  402. contentType: false,
  403. processData: false,
  404. data: formdata,
  405. success: function(result)
  406. {
  407. $("#uploadimage").val("");
  408. $("#showuploadform").show();
  409. $("#showemoticons").hide();
  410. $("#uploadform").hide();
  411. $("#loadingwrapper").hide();
  412. $("#content").show();
  413. $("#emoticons").show();
  414. }
  415. });
  416. });
  417. // show emoticons
  418. $("#showemoticons").click(function()
  419. {
  420. $("#showuploadform").show();
  421. $("#showemoticons").hide();
  422. $("#uploadform").hide();
  423. $("#loadingwrapper").hide();
  424. $("#content").show();
  425. $("#emoticons").show();
  426. });
  427. // refresh content
  428. var content = $("#messages");
  429. var newcontent = content.html();
  430. function refresh()
  431. {
  432. setTimeout(function()
  433. {
  434. var data = "user=" + user;
  435. $.ajax
  436. ({
  437. type: "POST",
  438. url: "chat/refreshmessages.php",
  439. data: data,
  440. cache: false,
  441. success: function(result)
  442. {
  443. // check who is still online
  444. var datetoday = new Date();
  445. var timenow = datetoday.getTime() / 1000;
  446. $.ajax
  447. ({
  448. url: "chat/getonline.php",
  449. cache: false,
  450. success: function(result)
  451. {
  452. var onlineusers = JSON.parse(result);
  453. var oldonlineusers = $("#onlineusers").html();
  454. var newonlineusers = '';
  455. var onlinecount = 0;
  456. if( onlineusers.length <= 0 ) // no user typing
  457. {
  458. newonlineusers += "No Users Online";
  459. onlinecount = 0;
  460. }
  461. else
  462. {
  463. if( onlineusers.length >= 1 ) // one user typing
  464. {
  465. jQuery.each( onlineusers, function( i, val ) {
  466. var timecheck = val[1];
  467. var status = "";
  468. var color = "";
  469. if( timecheck < timenow - 1800 )
  470. {
  471. status = '<span style="min-height: 14px;margin-top: 10px;" class="pull-right badge badge-danger"> </span>';
  472. color = "red";
  473. }else{
  474. status = '<span style="min-height: 14px;margin-top: 10px;" class="pull-right badge badge-success"> </span>';
  475. color = "blue";
  476. }
  477. if( timecheck < timenow - 3600 )
  478. {
  479. newonlineusers += '';
  480. }else{
  481. newonlineusers += '<div class="member-info"><img style="height:40px" src="'+val[2]+'" alt="admin" class="img-circle"><span class="member-name" style="position: absolute;margin-top: 10px;">'+val[0]+'</span>'+status+'</div>';
  482. i++;
  483. onlinecount++;
  484. }
  485. $("img[alt^='"+val[0]+"']").each(function()
  486. {
  487. var timestamp = val[1];
  488. // set user offline avatar
  489. if( timestamp < timenow - 1800 )
  490. {
  491. $(this).addClass("offline");
  492. $(this).removeClass("online");
  493. }
  494. else // set user online avatar
  495. {
  496. $(this).addClass("online");
  497. $(this).removeClass("offline");
  498. }
  499. });
  500. });
  501. }
  502. }
  503. if(onlinecount > 9 ){ onlinecount = "9-plus"; }
  504. if(newonlineusers === ''){ newonlineusers = "No Users Online"; onlinecount = 0;}
  505. if( newonlineusers != oldonlineusers )
  506. {
  507. $("#onlineusers").html(newonlineusers);
  508. $("#online-count").attr("class", "mdi mdi-numeric-"+onlinecount+"-box");
  509. }
  510. }
  511. });
  512. // new messages
  513. var newmessages = result.split("###endofmessage###");
  514. for( var i=0; i<newmessages.length; i++ )
  515. {
  516. var message = newmessages[i];
  517. if( message != "" )
  518. {
  519. var messagekeypos = message.indexOf("avatarandtext");
  520. var messagekey = message.substr(messagekeypos + 19, 32);
  521. var contentdom = document.getElementById("messages");
  522. var messagedom = document.getElementById(messagekey);
  523. if( $.contains(contentdom, messagedom) == false )
  524. {
  525. // append new message
  526. content.append(message).promise().done(function()
  527. {
  528. // scroll to bottom
  529. if( newcontent != content.html() )
  530. {
  531. var toscroll = document.getElementById("messages");
  532. toscroll.scrollTop = toscroll.scrollHeight;
  533. $(".box").animate({ scrollTop: $('.box').prop("scrollHeight")}, 0);
  534. newcontent = content.html();
  535. $(function(){
  536. $('.chat-timestamp').each(function(){
  537. var $this = $(this).attr('time');
  538. if(isMobile === true){
  539. newdate = moment($this+'Z').format('lll');
  540. }else {
  541. newdate = moment(new Date($this+'Z')).format('lll');
  542. }
  543. $(this).text(newdate);
  544. });
  545. });
  546. }
  547. // new message tab alert
  548. var userwriting = user + "writing";
  549. if( message.lastIndexOf(userwriting) == -1 )
  550. {
  551. if ($('.chat-box').hasClass('email-active')){
  552. console.log("supress message");
  553. }else{
  554. newmessagealert(message);
  555. }
  556. }
  557. // refresh eventlisteners of messages to set likes
  558. $(".messagelike").unbind().click(function()
  559. {
  560. if( this.innerHTML.indexOf("target=") == -1 &&
  561. this.innerHTML.indexOf("_blank") == -1 )
  562. {
  563. likemessage(this.id);
  564. }
  565. });
  566. });
  567. }
  568. }
  569. }
  570. // get and show likes
  571. $.ajax
  572. ({
  573. url: "chat/getlikes.php",
  574. cache: false,
  575. success: function(result)
  576. {
  577. var likesandunlikes = result.split("#");
  578. var likedmessages = likesandunlikes[0];
  579. var unlikedmessages = likesandunlikes[1];
  580. var likes = JSON.parse(likedmessages);
  581. var unlikes = JSON.parse(unlikedmessages);
  582. for( var i=0; i<likes.length; i++ )
  583. {
  584. var likeid = "#like" + likes[i];
  585. $(likeid).show();
  586. }
  587. for( var i=0; i<unlikes.length; i++ )
  588. {
  589. var unlikeid = "#like" + unlikes[i];
  590. $(unlikeid).hide();
  591. }
  592. }
  593. });
  594. // hide intro
  595. if( result != "" )
  596. {
  597. $("#intro").hide();
  598. }
  599. // loop
  600. refresh();
  601. }
  602. });
  603. // update if current user is typing
  604. if( $("#message").val() != "" )
  605. {
  606. istyping(user, 1);
  607. }
  608. else if( $("#message").val() == "" )
  609. {
  610. istyping(user, 0);
  611. }
  612. }, 1250);
  613. }
  614. // tab focus
  615. window.onload = function()
  616. {
  617. tabinfocus = true;
  618. window.chatLoaded = false;
  619. console.log("loading chat");
  620. setTimeout(function() {
  621. if ($('.chat-box').hasClass('email-active')){
  622. tabinfocus = true;
  623. $(".mdi-forum").removeClass("tada loop-animation new-message");//SET MESSAGE TO ZERO
  624. console.log("in focus");
  625. }else{
  626. tabinfocus = false;
  627. console.log("not in focus");
  628. }
  629. window.chatLoaded = true;
  630. console.log("chat started");
  631. }, 5000);
  632. };
  633. window.onfocus = function()
  634. {
  635. if(window.chatLoaded === true){
  636. if ($('.chat-box').hasClass('email-active')){
  637. tabinfocus = true;
  638. $(".mdi-forum").removeClass("tada loop-animation new-message");//SET MESSAGE TO ZERO
  639. console.log("in focus");
  640. }else{
  641. tabinfocus = false;
  642. console.log("not in focus");
  643. }
  644. }
  645. };
  646. window.onblur = function()
  647. {
  648. if(window.chatLoaded === true){
  649. tabinfocus = false;
  650. console.log("not in focus");
  651. }
  652. };
  653. // new message tab alert
  654. function newmessagealert(message)
  655. {
  656. if( !tabinfocus )
  657. {
  658. i = parseInt(parent.document.title);
  659. if(isNaN(i)){
  660. i = 1;
  661. }else{
  662. i++
  663. }
  664. console.log("new message");
  665. $(".mdi-forum").addClass("tada loop-animation new-message");
  666. var $jQueryObject = $($.parseHTML(message));
  667. var alertMessage = $jQueryObject.find(".chat-body").html();
  668. var alertUsername = $jQueryObject.find("h4[class^='pull-left zero-m']").html();
  669. var alertIcon = $jQueryObject.find("img").attr("src");;
  670. if(isMobile === false){
  671. parent.Push.create(alertUsername, {
  672. body: alertMessage,
  673. icon: alertIcon,
  674. timeout: 4000,
  675. onClick: function () {
  676. window.parent.focus();
  677. this.close();
  678. }
  679. });
  680. }
  681. // sound
  682. var audio = $("#tabalert")[0];
  683. audio.play();
  684. }
  685. }
  686. // like message
  687. function likemessage(messageid)
  688. {
  689. var data = "messageid=" + messageid;
  690. $.ajax
  691. ({
  692. type: "POST",
  693. url: "chat/setlike.php",
  694. data: data,
  695. cache: false
  696. });
  697. }
  698. // update which users are typing
  699. function istyping(u, t)
  700. {
  701. // set typing user
  702. var data = "datavars=" + u + "###" + t;
  703. $.ajax
  704. ({
  705. type: "POST",
  706. url: "chat/settyping.php",
  707. data: data,
  708. cache: false,
  709. success: function(result)
  710. {
  711. // get typing users
  712. $.ajax
  713. ({
  714. url: "chat/gettyping.php",
  715. cache: false,
  716. success: function(result)
  717. {
  718. var typingusers = JSON.parse(result);
  719. if( typingusers.length == 0 ) // no user typing
  720. {
  721. $("#istyping").html("");
  722. }
  723. else
  724. { //$("#istyping").html("<li class=\"chat-inverted\"><img src=\"images/ellipsis.png\" class=\"dont img-circle user-avatar online\" alt=\"user\"><div class=\"chat-panel red-bg\"><div class=\"chat-body\">" + typingusers[0] + "...</div></div></li>");
  725. if( typingusers.length == 1 ) // one user typing
  726. {
  727. $("#istyping").html(typingusers[0] + " is typing...");
  728. //$(".box").animate({ scrollTop: $('.box').prop("scrollHeight")}, 0);
  729. }
  730. else if( typingusers.length == 2 ) // two users typing
  731. {
  732. var whoistyping = typingusers[0] +
  733. " and " + typingusers[1] +
  734. " are typing...";
  735. $("#istyping").html(whoistyping);
  736. //$(".box").animate({ scrollTop: $('.box').prop("scrollHeight")}, 0);
  737. }
  738. else if( typingusers.length > 2 ) // more than two users typing
  739. {
  740. var whoistyping = typingusers[0] +
  741. " and " + typingusers[1] +
  742. " and others are typing...";
  743. $("#istyping").html(whoistyping);
  744. //$(".box").animate({ scrollTop: $('.box').prop("scrollHeight")}, 0);
  745. }
  746. }
  747. }
  748. });
  749. }
  750. });
  751. }
  752. });