Simple PHP IRC web client using Ajax calls.

server.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. ///////
  3. // webirc-client - 2013-2020
  4. // (C) Chris Dorman, GPL v3
  5. // https://notabug.org/Pentium44/ircchat
  6. ///////
  7. // server.php - used to communicate between web frontend and irc client
  8. // Grabs from IRC client output
  9. // Pushes to IRC client input
  10. // Keeps IRC client informed on web frontend connection
  11. // Include PHP config file with server, title, and channel settings
  12. include_once("config.php");
  13. session_start();
  14. $channel = $_SESSION['cwchat-channel'];
  15. // If we have a message; grab user and content and push to IRC client
  16. if (isset($_GET['msg']) && $_GET['msg']!="" && isset($_GET['nick']) && $_GET['nick']!=""){
  17. $nick = stripslashes(htmlentities($_GET['nick'])); // Usernick
  18. $msg = urldecode(stripslashes(trim($_GET['msg']))); // User message content
  19. $line = ""; // start with nothing
  20. // Seperate message input via space
  21. $cmd = explode(" ", $msg);
  22. if($cmd[0]=="/msg") { // If using /msg, push private message
  23. $prvmsg = array_splice($cmd, 2); // grab private message from string
  24. $line .= "PRIVMSG" . " " . $cmd[1] . " :"; // set for push
  25. foreach($prvmsg as $word) {
  26. // Grab each word in the array after the privmsg username
  27. $line .= $word . " ";
  28. }
  29. $line .= "\n";
  30. } else if ($cmd[0]=="/join") {
  31. doLog("$username: channel switch from $channel to" . $cmd[1] . "($msg)");
  32. $line .= "PART $channel\n"; // push close command to IRC
  33. $line .= "JOIN" . " " . $cmd[1] . "\n"; // set for push
  34. $_SESSION['cwchat-channel'] = trim($cmd[1]);
  35. } else {
  36. // @@ This is a work in progress
  37. // Sends every channel message to each channel :[
  38. $line .= "PRIVMSG $channel :$msg\n";
  39. }
  40. // Get original content
  41. $content = file_get_contents(".$nick.socket");
  42. echo nl2br(stripslashes($content));
  43. // Grab all contents, and push to socket output file.
  44. file_put_contents(".$nick.socket", $content . $line);
  45. // Grab user message and push to IRC client
  46. file_put_contents(".$nick.push", $line);
  47. // Throw out your user message
  48. echo nl2br(stripslashes($line));
  49. // DONE
  50. } else if (isset($_GET['get']) && isset($_GET['nick']) && $_GET['nick']!="") {
  51. $nick = stripslashes(htmlentities($_GET['nick'])); // Username
  52. // Grab IRC client output
  53. $content = file_get_contents(".$nick.socket");
  54. // Push content to the web frontend
  55. echo nl2br(htmlentities(stripslashes($content)));
  56. // DONE
  57. } else if (isset($_GET['do']) && isset($_GET['nick']) && $_GET['nick']!="") {
  58. $nick = stripslashes(htmlentities($_GET['nick']));
  59. if($_GET['do']=="login") { // Is user asking for login?
  60. // Join channel
  61. if(!isset($_SESSION['cwchat-channel'])) {
  62. file_put_contents(".$nick.push", "JOIN " . $default_channel . "\n");
  63. } else {
  64. file_put_contents(".$nick.push", "JOIN " . $channel . "\n");
  65. }
  66. // Make sure users DB is clean, put nothing into socket read file
  67. file_put_contents(".$nick.socket", "");
  68. chmod(".$username.socket", 0755); // file permissions for read / write
  69. // start pingfile - determines if webclient is active via write timestamp
  70. file_put_contents(".$nick.pingfile", "pong");
  71. chmod(".$username.pingfile", 0755); // file permissions for read / write
  72. // Execute IRC client in background
  73. // IRC server will die when either 1) pingfile is deleted, or
  74. // 2) if pingfile is older than 10 seconds of current sys time
  75. $realpath = realpath("./irc.php"); // get full file path
  76. // Execute IRC client
  77. shell_exec("/usr/bin/php $realpath $nick > /dev/null 2>/dev/null &");
  78. } else if($_GET['do']=="logout") { // Is user asking for logout?
  79. // Remove ping file if user logs out. IRC server will close
  80. unlink(".$nick.pingfile");
  81. } else if($_GET['do']=="keepup") { // Client asking for keepup ping.
  82. // PONG to server.
  83. file_put_contents(".$nick.pingfile", "ping");
  84. }
  85. }
  86. ?>