Simple PHP IRC web client using Ajax calls.

index.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. ///////
  3. // webirc-client - 2013-2020
  4. // (C) Chris Dorman, GPL v3 - (C) Microchat devs
  5. // https://github.com/Pentium44/cwchat
  6. // https://git.minetest.org/Pentium44/webirc-client
  7. ///////
  8. session_start();
  9. include "config.php";
  10. function loginForm(){
  11. ?>
  12. <br>
  13. <div class="login">
  14. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=login" method="post">
  15. Username: <input style="padding: 2px;" class="text" type="text" name="username"><br>
  16. <input style="padding: 2px;" class="text" type="submit" name="submitBtn" value="Login">
  17. </form>
  18. </div>
  19. <?php
  20. }
  21. //Logout
  22. if (isset($_GET['do']) && $_GET['do']=="logout") {
  23. $_SESSION['cwchat-user'] = null;
  24. }
  25. if (isset($_GET['do']) && $_GET['do']=="login" && isset($_POST['submitBtn'])){
  26. $name = isset($_POST['username']) && ($_POST['username'] !== "") ? htmlentities(stripslashes($_POST['username'])) : "Unnamed";
  27. $_SESSION['cwchat-user'] = $name;
  28. }
  29. //if(!isset($_SESSION['cwchat-user'])) { header("Location: ?do=login"); }
  30. ?>
  31. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
  32. <html>
  33. <head>
  34. <title>CWChat</title>
  35. <link href="style.css" rel="stylesheet" type="text/css" />
  36. <script language="javascript" type="text/javascript">
  37. <!--
  38. var httpObject = null;
  39. var link = "";
  40. var pinglink = "";
  41. var timerID = 0;
  42. var nickName = "<?php echo $_SESSION['cwchat-user']; ?>";
  43. // Get the HTTP Object
  44. function getHTTPObject() {
  45. if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  46. else if (window.XMLHttpRequest) return new XMLHttpRequest();
  47. else {
  48. alert("Your browser does not support AJAX.");
  49. return null;
  50. }
  51. }
  52. // Change the value of the outputText field
  53. function setHtml() {
  54. if(ajaxVar.readyState == 4){
  55. var response = ajaxVar.responseText;
  56. var msgBox = document.getElementById("msgs");
  57. msgBox.innerHTML += response;
  58. msgBox.scrollTop = msgBox.scrollHeight;
  59. }
  60. }
  61. // Change the value of the outputText field
  62. function setAll() {
  63. if(ajaxVar.readyState == 4){
  64. var response = ajaxVar.responseText;
  65. var msgBox = document.getElementById("msgs");
  66. msgBox.innerHTML = response;
  67. msgBox.scrollTop = msgBox.scrollHeight;
  68. }
  69. }
  70. // Implement business logic
  71. function serverWrite() {
  72. ajaxVar = getHTTPObject();
  73. if (ajaxVar != null) {
  74. link = "server.php?nick="+nickName+"&msg="+document.getElementById('msg').value;
  75. ajaxVar.open("GET", link , true);
  76. ajaxVar.send(null);
  77. }
  78. }
  79. function getInput() {
  80. // Send the server function the input
  81. var userInput = document.getElementById('msg');
  82. serverWrite(userInput.value);
  83. // Clean out the input values
  84. var msgBar = document.getElementById("msg");
  85. msgBar.value = "";
  86. msgBar.focus();
  87. }
  88. // Implement business logic
  89. function serverReload() {
  90. ajaxVar = getHTTPObject();
  91. //var randomnumber=Math.floor(Math.random()*10000);
  92. if (ajaxVar != null) {
  93. link = "server.php?get=all&nick="+nickName;
  94. ajaxVar.open("GET", link , true);
  95. ajaxVar.onreadystatechange = setAll;
  96. ajaxVar.send(null);
  97. }
  98. }
  99. // Implement business logic
  100. function serverPing() {
  101. ajaxPing = getHTTPObject();
  102. if (ajaxPing != null) {
  103. pinglink = "server.php?do=keepup&nick="+nickName;
  104. ajaxPing.open("GET", pinglink , true);
  105. ajaxPing.send(null);
  106. }
  107. }
  108. function UpdateTimer() {
  109. serverReload();
  110. serverPing();
  111. setTimeout(UpdateTimer, 1000);
  112. }
  113. function keypressed(e) {
  114. if(e.keyCode=='13'){
  115. getInput();
  116. }
  117. }
  118. function doLogin() {
  119. ajaxVar = getHTTPObject();
  120. if(ajaxVar != null) {
  121. link = "server.php?do=login&nick="+nickName;
  122. ajaxVar.open("GET", link, true);
  123. ajaxVar.onreadystatechange = setHtml;
  124. ajaxVar.send(null);
  125. }
  126. }
  127. function doLogout() {
  128. ajaxVar = getHTTPObject();
  129. if(ajaxVar != null) {
  130. link = "server.php?do=logout&nick="+nickName;
  131. ajaxVar.open("GET", link, true);
  132. ajaxVar.onreadystatechange = setHtml;
  133. ajaxVar.send(null);
  134. }
  135. }
  136. window.onbeforeunload = function (e) {
  137. doLogout();
  138. };
  139. function wrapBBCode(tag) {
  140. var msgInput = document.getElementById('msg');
  141. var content = msgInput.value;
  142. var selectedContent = content.substring(msgInput.selectionStart, msgInput.selectionEnd);
  143. var beforeContent = content.substring(0, msgInput.selectionStart);
  144. var afterContent = content.substring(msgInput.selectionEnd, content.length);
  145. msgInput.value = beforeContent + '[' + tag + ']' + selectedContent + '[/' + tag + ']' + afterContent;
  146. }
  147. //-->
  148. </script>
  149. </head>
  150. <body onload="UpdateTimer();">
  151. <div class="info"><?php echo "CWChat " . $version . " ~ Connected to: " . $server . ":" . $port . " on " . $channels[0]; ?></div>
  152. <?php
  153. if (!isset($_SESSION['cwchat-user'])){
  154. loginForm();
  155. } else {
  156. ?>
  157. <div class="logout"><a onclick="doLogout();" href="index.php?do=logout">Logout</a></div>
  158. <div id="msgs">
  159. <?php
  160. echo "<div class=\"text\">";
  161. $get = file_get_contents("." .$_SESSION['cwchat-user'] . ".socket");
  162. echo $get;
  163. echo "</div>";
  164. ?>
  165. </div>
  166. <div id="msgbox" onkeyup="keypressed(event);">
  167. <table>
  168. <tr>
  169. <td>
  170. <textarea name="msg" style="width: 600px; height: 22px;" id="msg"></textarea>
  171. </td>
  172. <td>
  173. <button style="width: 80px;" onclick="getInput();">Send</button>
  174. </td>
  175. </tr>
  176. </table>
  177. </div>
  178. <script type="text/javascript">
  179. doLogin();
  180. </script>
  181. <?php
  182. }
  183. ?>
  184. </div>
  185. </body>
  186. </html>