index.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. function loginForm() {
  3. echo'
  4. <div id="loginform">
  5. <form action="." method="post">
  6. <p>Please enter your name to continue:</p>
  7. <label for="name">Name:</label>
  8. <input type="text" name="name" id="name" autofocus />
  9. <input type="submit" name="enter" id="enter" value="Enter" />
  10. </form>
  11. </div>
  12. ';
  13. }
  14. function getSetup($key = null) {
  15. $arr = parse_ini_file('setup.ini');
  16. return isset($key) ? $arr[$key] : $arr;
  17. }
  18. //-------------------------
  19. session_start();
  20. if (isset($_GET['logout'])) {
  21. session_destroy();
  22. header("Location: ./"); //Redirect the user
  23. }
  24. if (isset($_POST['enter'])) {
  25. if ($_POST['name'] != "") {
  26. $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
  27. } else {
  28. echo '<span class="error">Please type in a name</span>';
  29. }
  30. }
  31. ?>
  32. <!DOCTYPE html>
  33. <html lang="en">
  34. <head>
  35. <title>Chat FAME</title>
  36. <meta charset="UTF-8" />
  37. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  38. <link type="text/css" rel="stylesheet" href="style.css" />
  39. </head>
  40. <?php
  41. if (!isset($_SESSION['name'])) {
  42. loginForm();
  43. } else {
  44. ?>
  45. <div id="wrapper">
  46. <div id="menu">
  47. <p class="welcome">Ceci est le chat de la FAME. Bienvenue à toi <b><?php echo $_SESSION['name']; ?></b> !</p>
  48. <p class="logout"><a id="exit" href="#">Quitter</a></p>
  49. <p class="logout" style="margin-right: 1em;"><a target="_blank" href="history.php">Historique</a></p>
  50. <p class="logout" style="margin-right: 2em;"><a target="_blank" href="https://cloud.nry.pw/index.php/s/5E2Y2wfi3QRpFKd">Fichiers</a></p>
  51. <div style="clear:both"></div>
  52. </div>
  53. <div id="datbox">
  54. <div id="chatbox"></div>
  55. <div id="usrlist">
  56. <b>Fameux dans ce salon (<span id='current_saloon' ></span>):</b>
  57. <div id="usrs"></div>
  58. <input type="submit" id="delete_saloon" value="Supprimer tous les messages" />
  59. <br><br>
  60. <b>Autres Fameux</b>
  61. <div id="usrs_others"></div>
  62. <br><br>
  63. <b>Nouveau saloon</b>
  64. <div id="div_new_saloon">
  65. <input type="text" id="text_new_saloon" />
  66. <input type="submit" id="submit_new_saloon" value="Créer" />
  67. </div>
  68. </div>
  69. </div>
  70. <form name="message" action=".">
  71. <input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" autofocus />
  72. <input name="submitmsg" type="submit" id="submitmsg" value="Envoyer" />
  73. </form>
  74. <br><input id="mute" name="mode muet" type="checkbox"/> mode muet (sans le "diiing" des nouveaux messages quoi)
  75. </div>
  76. <script type="text/javascript" src="jquery.min.js"></script>
  77. <script type="text/javascript">
  78. /*
  79. * URL Linkification
  80. *
  81. * @see http://jmrware.com/articles/2010/linkifyurl/linkify.html
  82. *
  83. */
  84. function linkify(text)
  85. {
  86. var url_pattern = /(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img;
  87. var url_replace = '$1$4$7$10$13<a href="$2$5$8$11$14" target="_blank">$2$5$8$11$14</a>$3$6$9$12';
  88. return text.replace(url_pattern, url_replace);
  89. }
  90. function small_hash(str)
  91. {
  92. var result = 0;
  93. if(!str)
  94. {
  95. return result;
  96. }
  97. for (i=0;i < str.length; i++) {
  98. result += str.charCodeAt(i) * Math.pow(i,2);
  99. }
  100. return result;
  101. }
  102. function color_from_login(login)
  103. {
  104. var thisdivcolor = small_hash(login) % (360);
  105. return 'hsl('+thisdivcolor+',80%, 50%)';
  106. }
  107. //https://www.w3resource.com/javascript-exercises/javascript-date-exercise-46.php
  108. function get_diff_days(dt2, dt1)
  109. {
  110. var diff =(dt2.getTime() - dt1.getTime()) / 1000;
  111. diff /= (60 * 60 * 24);
  112. return Math.abs(Math.round(diff));
  113. }
  114. function pad00(n){
  115. return n > 9 ? "" + n: "0" + n;
  116. }
  117. function unix_time_to_locale_string(t)
  118. {
  119. var now = new Date();
  120. var evt_date = new Date(t * 1000);
  121. var date_string = '';
  122. var diff_days = get_diff_days(now, evt_date);
  123. if(diff_days == 0)
  124. {
  125. }
  126. else if (diff_days == 1)
  127. {
  128. date_string = 'Hier ';
  129. }
  130. else if (diff_days == 2)
  131. {
  132. date_string = 'Avant-hier ';
  133. }
  134. else
  135. {
  136. date_string = evt_date.toLocaleDateString() + ' ';
  137. }
  138. date_string += pad00(evt_date.getHours()) + ':' + pad00(evt_date.getMinutes());
  139. return date_string;
  140. }
  141. /*
  142. * echapper un texte html
  143. *
  144. * un debat houleux
  145. * @see http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  146. *
  147. */
  148. function escapeHtml(html_text)
  149. {
  150. return $('<div/>').text(html_text).html();
  151. }
  152. // jQuery Document
  153. $(document).ready(function() {
  154. var saloon_id = '<?php echo isset($_POST['saloon'])?str_replace("'","''", $_SESSION['saloon']):''; ?>';
  155. if(!saloon_id)
  156. {
  157. // do we have a hash ?
  158. saloon_id = $(location).attr('hash');
  159. if(!saloon_id)
  160. {
  161. saloon_id = 'Bienvenue';
  162. }
  163. }
  164. $('#delete_saloon').click(function()
  165. {
  166. if (!confirm("Tous les messages seront définitivement supprimés à vie. OK ?"))
  167. {
  168. return false;
  169. }
  170. $.ajax({
  171. type: 'POST',
  172. url: 'delete_saloon.php',
  173. data: {saloon: saloon_id },
  174. //cache: false,
  175. async: true,
  176. success: function(data) {
  177. loadLog();
  178. },
  179. error: function(request, status, error) {
  180. },
  181. });
  182. return false;
  183. });
  184. $('#current_saloon').text(saloon_id);
  185. var login = '<?php echo str_replace("'","''", $_SESSION['name']); ?>';
  186. var session_id = '<?php echo session_id(); ?>';
  187. var last_message = 0;
  188. var audio = new Audio('./front-desk-bell.mp3');
  189. audio.volume = 0.05;
  190. $("#submit_new_saloon").click(function(){
  191. change_saloon($("#text_new_saloon").val());
  192. return false;
  193. });
  194. function sendMessage(clientmsg)
  195. {
  196. $.ajax({
  197. type: 'POST',
  198. url: 'post.php',
  199. data: {msg:{msg:clientmsg, login: login, session_id: session_id}, saloon: saloon_id },
  200. //cache: false,
  201. async: true,
  202. success: function(data) {
  203. loadLog();
  204. },
  205. error: function(request, status, error) {
  206. // try again
  207. setTimeout(function(){sendMessage(msg);}, 500);
  208. },
  209. });
  210. }
  211. //If user submits the form
  212. $("#submitmsg").click(function() {
  213. var clientmsg = $("#usermsg").val();
  214. if(clientmsg == '') return false;
  215. $("#usermsg").val('');
  216. $("#usermsg").focus();
  217. sendMessage(clientmsg);
  218. return false;
  219. });
  220. function beacon() {
  221. $.ajax({
  222. type: 'POST',
  223. url: 'users.php',
  224. data: {saloon: saloon_id},
  225. dataType: 'json',
  226. //cache: false,
  227. async: true,
  228. success: function(data) {
  229. var html = '';
  230. var html_others = '';
  231. $("#usrs").html('');
  232. $("#usrs_others").html('');
  233. for (var usr in data) {
  234. if(data[usr].saloon == saloon_id)
  235. {
  236. $("#usrs").append("<div class='msgln' style='color:" + color_from_login(data[usr].login) + "'><em>"+data[usr].login+"</em></div>");
  237. }
  238. else
  239. {
  240. var link = $("<a><div class='msgln' style='color:" + color_from_login(data[usr].login) + "'><em>"+data[usr].login + " (" + data[usr].saloon + ")</em></div></a>");
  241. var saloon = data[usr].saloon;
  242. link.click(function(){change_saloon(saloon);});
  243. $("#usrs_others").append(link);
  244. }
  245. }
  246. },
  247. });
  248. }
  249. function update_session() {
  250. $.ajax({
  251. type: 'POST',
  252. url: 'update_session.php',
  253. data: {saloon: saloon_id, session:{login: login, session_id: session_id, saloon: saloon_id}},
  254. dataType: 'json',
  255. //cache: false,
  256. async: true,
  257. success: function(data) {
  258. },
  259. });
  260. }
  261. var loading_log = false;
  262. //Load the file containing the chat log
  263. function loadLog() {
  264. if(loading_log) return;
  265. loading_log = true;
  266. var oldscrollHeight = $("#chatbox")[0].scrollHeight;
  267. $.ajax({
  268. type: 'POST',
  269. url: 'server.php',
  270. data: {from: last_message, saloon: saloon_id},
  271. dataType: 'json',
  272. //cache: false,
  273. async: true,
  274. success: function(data) {
  275. loading_log = false;
  276. last_message = data.last_message;
  277. if(data['reset'])
  278. {
  279. $("#chatbox").html('');
  280. }
  281. $(window).focus(function() {
  282. document.title = "Chat FAME";
  283. });
  284. var html = '';
  285. var date;
  286. var msg;
  287. var login;
  288. for (var k in data.msgs) {
  289. msg = linkify(escapeHtml(data.msgs[k].msg));
  290. date = unix_time_to_locale_string(data.msgs[k].time);
  291. login = data.msgs[k].login
  292. html = html
  293. +"<div class='msgln'>("+date+") <span style='color:" + color_from_login(login) + "'><b>"
  294. +login+"</b></span>: "+msg+"<br></div>";
  295. }
  296. $("#chatbox").append(html); //Insert chat messages into the #chatbox div
  297. var newscrollHeight = $("#chatbox")[0].scrollHeight;
  298. if (newscrollHeight > oldscrollHeight) {
  299. $("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
  300. }
  301. document.title = "(1) Chat FAME";
  302. if (!$("#mute").prop('checked')) {
  303. if (!document.hasFocus()) audio.play();
  304. }
  305. },
  306. error: function(request, status, error) {
  307. // perhaps next time...
  308. loading_log = false;
  309. }
  310. });
  311. }
  312. function change_saloon(new_saloon)
  313. {
  314. saloon_id = new_saloon;
  315. $('#current_saloon').text(saloon_id);
  316. last_message = 0;
  317. $("#chatbox").html('');
  318. update_session();
  319. loadLog();
  320. beacon();
  321. }
  322. update_session();
  323. loadLog();
  324. beacon();
  325. setInterval(loadLog, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
  326. setInterval(beacon, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
  327. setInterval(update_session, 1000*<?php echo getSetup('update_session_interval_seconds') ?>);
  328. //If user wants to end session
  329. $("#exit").click(function() {
  330. var exit = confirm("Voulez-vous vraiment quitter la session ?");
  331. if (exit == true) {
  332. window.location = 'index.php?logout=true';
  333. }
  334. });
  335. });
  336. </script>
  337. <?php
  338. }
  339. ?>
  340. </body>
  341. </html>