index.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. $("#usermsg").val(clientmsg);
  181. },
  182. });
  183. return false;
  184. });
  185. $('#current_saloon').text(saloon_id);
  186. var login = '<?php echo str_replace("'","''", $_SESSION['name']); ?>';
  187. var session_id = '<?php echo session_id(); ?>';
  188. var last_message = 0;
  189. var audio = new Audio('./front-desk-bell.mp3');
  190. audio.volume = 0.05;
  191. $("#submit_new_saloon").click(function(){
  192. change_saloon($("#text_new_saloon").val());
  193. return false;
  194. });
  195. //If user submits the form
  196. $("#submitmsg").click(function() {
  197. var clientmsg = $("#usermsg").val();
  198. $("#usermsg").val('');
  199. $("#usermsg").focus();
  200. $.ajax({
  201. type: 'POST',
  202. url: 'post.php',
  203. data: {msg:{msg:clientmsg, login: login, session_id: session_id}, saloon: saloon_id },
  204. //cache: false,
  205. async: true,
  206. success: function(data) {
  207. loadLog();
  208. },
  209. error: function(request, status, error) {
  210. $("#usermsg").val(clientmsg);
  211. },
  212. });
  213. return false;
  214. });
  215. function beacon() {
  216. $.ajax({
  217. type: 'POST',
  218. url: 'users.php',
  219. data: {saloon: saloon_id},
  220. dataType: 'json',
  221. //cache: false,
  222. async: true,
  223. success: function(data) {
  224. var html = '';
  225. var html_others = '';
  226. $("#usrs").html('');
  227. $("#usrs_others").html('');
  228. for (var usr in data) {
  229. if(data[usr].saloon == saloon_id)
  230. {
  231. $("#usrs").append("<div class='msgln' style='color:" + color_from_login(data[usr].login) + "'><em>"+data[usr].login+"</em></div>");
  232. }
  233. else
  234. {
  235. var link = $("<a><div class='msgln' style='color:" + color_from_login(data[usr].login) + "'><em>"+data[usr].login + " (" + data[usr].saloon + ")</em></div></a>");
  236. var saloon = data[usr].saloon;
  237. link.click(function(){change_saloon(saloon);});
  238. $("#usrs_others").append(link);
  239. }
  240. }
  241. },
  242. });
  243. }
  244. function update_session() {
  245. $.ajax({
  246. type: 'POST',
  247. url: 'update_session.php',
  248. data: {saloon: saloon_id, session:{login: login, session_id: session_id, saloon: saloon_id}},
  249. dataType: 'json',
  250. //cache: false,
  251. async: true,
  252. success: function(data) {
  253. },
  254. });
  255. }
  256. //Load the file containing the chat log
  257. function loadLog() {
  258. var oldscrollHeight = $("#chatbox")[0].scrollHeight;
  259. $.ajax({
  260. type: 'POST',
  261. url: 'server.php',
  262. data: {from: last_message, saloon: saloon_id},
  263. dataType: 'json',
  264. //cache: false,
  265. async: true,
  266. success: function(data) {
  267. last_message = data.last_message;
  268. if(data['reset'])
  269. {
  270. $("#chatbox").html('');
  271. }
  272. $(window).focus(function() {
  273. document.title = "Chat FAME";
  274. });
  275. var html = '';
  276. var date;
  277. var msg;
  278. var login;
  279. for (var k in data.msgs) {
  280. msg = linkify(escapeHtml(data.msgs[k].msg));
  281. date = unix_time_to_locale_string(data.msgs[k].time);
  282. login = data.msgs[k].login
  283. html = html
  284. +"<div class='msgln'>("+date+") <span style='color:" + color_from_login(login) + "'><b>"
  285. +login+"</b></span>: "+msg+"<br></div>";
  286. }
  287. $("#chatbox").append(html); //Insert chat messages into the #chatbox div
  288. var newscrollHeight = $("#chatbox")[0].scrollHeight;
  289. if (newscrollHeight > oldscrollHeight) {
  290. $("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
  291. }
  292. document.title = "(1) Chat FAME";
  293. if (!$("#mute").prop('checked')) {
  294. if (!document.hasFocus()) audio.play();
  295. }
  296. },
  297. });
  298. }
  299. function change_saloon(new_saloon)
  300. {
  301. saloon_id = new_saloon;
  302. $('#current_saloon').text(saloon_id);
  303. last_message = 0;
  304. $("#chatbox").html('');
  305. update_session();
  306. loadLog();
  307. beacon();
  308. }
  309. update_session();
  310. loadLog();
  311. beacon();
  312. setInterval(loadLog, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
  313. setInterval(beacon, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
  314. setInterval(update_session, 1000*<?php echo getSetup('update_session_interval_seconds') ?>);
  315. //If user wants to end session
  316. $("#exit").click(function() {
  317. var exit = confirm("Voulez-vous vraiment quitter la session ?");
  318. if (exit == true) {
  319. window.location = 'index.php?logout=true';
  320. }
  321. });
  322. });
  323. </script>
  324. <?php
  325. }
  326. ?>
  327. </body>
  328. </html>