Simple PHP IRC web client using Ajax calls.

index.php 6.1KB

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