index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. function deleteOldHistory() {
  19. $expireHistory = getSetup('expire_history');
  20. $expireDate = date('Y-m-d', strtotime("-$expireHistory day"));
  21. foreach (glob('./history/*') as $f) {
  22. if (basename($f) < $expireDate) {
  23. unlink($f);
  24. }
  25. }
  26. }
  27. //-------------------------
  28. session_start();
  29. if (isset($_GET['logout'])) {
  30. session_destroy();
  31. header("Location: ./"); //Redirect the user
  32. }
  33. if (isset($_POST['enter'])) {
  34. if ($_POST['name'] != "") {
  35. $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
  36. } else {
  37. echo '<span class="error">Please type in a name</span>';
  38. }
  39. }
  40. ?>
  41. <!DOCTYPE html>
  42. <html lang="en">
  43. <head>
  44. <title>Chat FAME</title>
  45. <meta charset="UTF-8" />
  46. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  47. <link type="text/css" rel="stylesheet" href="style.css" />
  48. </head>
  49. <?php
  50. if (!isset($_SESSION['name'])) {
  51. loginForm();
  52. deleteOldHistory();
  53. } else {
  54. ?>
  55. <div id="wrapper">
  56. <div id="menu">
  57. <p class="welcome">Ceci est le chat de la FAME. Bienvenue à toi <b><?php echo $_SESSION['name']; ?></b> !</p>
  58. <p class="logout"><a id="exit" href="#">Quitter</a></p>
  59. <p class="logout" style="margin-right: 1em;"><a target="_blank" href="history.php">Historique</a></p>
  60. <p class="logout" style="margin-right: 2em;"><a target="_blank" href="https://cloud.nry.pw/index.php/s/5E2Y2wfi3QRpFKd">Fichiers</a></p>
  61. <div style="clear:both"></div>
  62. </div>
  63. <div id="datbox">
  64. <div id="chatbox"></div>
  65. <div id="usrlist"><b>Fameux connectés :</b><br><br><div id="usrs"></div></div>
  66. </div>
  67. <form name="message" action=".">
  68. <input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" autofocus />
  69. <input name="submitmsg" type="submit" id="submitmsg" value="Envoyer" />
  70. </form>
  71. <br><input id="mute" name="mode muet" type="checkbox"/> mode muet (sans le "diiing" des nouveaux messages quoi)
  72. </div>
  73. <script type="text/javascript" src="jquery.min.js"></script>
  74. <script type="text/javascript">
  75. /*
  76. * URL Linkification
  77. *
  78. * @see http://jmrware.com/articles/2010/linkifyurl/linkify.html
  79. *
  80. */
  81. function linkify(text)
  82. {
  83. 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;
  84. 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';
  85. return text.replace(url_pattern, url_replace);
  86. }
  87. // jQuery Document
  88. $(document).ready(function() {
  89. var id = 'undefined';
  90. var oldId = null;
  91. var isRunLoadLog = false;
  92. var audio = new Audio('./front-desk-bell.mp3');
  93. audio.volume = 0.05;
  94. //If user submits the form
  95. $("#submitmsg").click(function() {
  96. var clientmsg = $("#usermsg").val();
  97. $("#usermsg").val('');
  98. $("#usermsg").focus();
  99. $.ajax({
  100. type: 'POST',
  101. url: 'post.php',
  102. data: {text: clientmsg},
  103. //cache: false,
  104. async: true,
  105. success: function(data) {
  106. if (!isRunLoadLog) {
  107. loadLog();
  108. }
  109. },
  110. error: function(request, status, error) {
  111. $("#usermsg").val(clientmsg);
  112. },
  113. });
  114. return false;
  115. });
  116. function beacon() {
  117. $.ajax({
  118. type: 'POST',
  119. url: 'users.php',
  120. data: {},
  121. dataType: 'json',
  122. //cache: false,
  123. async: true,
  124. success: function(data) {
  125. var html = '';
  126. for (var usr in data.data) {
  127. html = html+"<div class='msgln'><em>"+data.data[usr]+"</em></div>";
  128. $("#usrs").html(html);
  129. }
  130. },
  131. });
  132. }
  133. //Load the file containing the chat log
  134. function loadLog() {
  135. isRunLoadLog = true;
  136. var oldscrollHeight = $("#chatbox")[0].scrollHeight;
  137. $.ajax({
  138. type: 'POST',
  139. url: 'server.php',
  140. data: {id: id},
  141. dataType: 'json',
  142. //cache: false,
  143. async: true,
  144. success: function(data) {
  145. id = data.id;
  146. $(window).focus(function() {
  147. document.title = "Chat FAME";
  148. });
  149. if (oldId !== id) {
  150. oldId = id;
  151. var html = '';
  152. var date;
  153. for (var k in data.data.reverse()) {
  154. date = new Date(parseInt(data.data[k][0])*1000);
  155. date = date.toLocaleTimeString('en-GB');
  156. date = date.replace(/([\d]+\D+[\d]{2})\D+[\d]{2}(.*)/, '$1$2');
  157. html = html
  158. +"<div class='msgln'>("+date+") <b>"
  159. +data.data[k][1]+"</b>: "+linkify(data.data[k][2])+"<br></div>";
  160. }
  161. $("#chatbox").append(html); //Insert chat messages into the #chatbox div
  162. var newscrollHeight = $("#chatbox")[0].scrollHeight;
  163. if (newscrollHeight > oldscrollHeight) {
  164. $("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
  165. }
  166. document.title = "(1) Chat FAME";
  167. if (!$("#mute").prop('checked')) {
  168. if (!document.hasFocus()) audio.play();
  169. }
  170. }
  171. isRunLoadLog = false;
  172. },
  173. });
  174. }
  175. loadLog();
  176. beacon();
  177. setInterval(loadLog, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
  178. setInterval(beacon, <?php echo getSetup('interval') ?>); //Reload file every 2.5 seconds
  179. //If user wants to end session
  180. $("#exit").click(function() {
  181. var exit = confirm("Voulez-vous vraiment quitter la session ?");
  182. if (exit == true) {
  183. window.location = 'index.php?logout=true';
  184. }
  185. });
  186. });
  187. </script>
  188. <?php
  189. }
  190. ?>
  191. </body>
  192. </html>