Simple PHP IRC web client using Ajax calls.

server.php 3.8KB

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