server.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. ///////
  3. // IdleIRC - 2020
  4. // (C) Chris Dorman, GPLv3
  5. // https://notabug.org/Pentium44/idleirc
  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. // Include PHP config file with server, title, and channel settings
  11. include_once("config.php");
  12. session_start();
  13. $username = $_SESSION['idleirc-user'];
  14. $usernick = $_SESSION['idleirc-nick'];
  15. $acctpass = $_SESSION['idleirc-pass'];
  16. $channel = $_SESSION['idleirc-channel'];
  17. $servaddr = $_SESSION['idleirc-servaddr'];
  18. $servport = $_SESSION['idleirc-servport'];
  19. // If we have a message; grab user and content and push to IRC client
  20. if (isset($_GET['msg']) && $_GET['msg']!="" && isset($_GET['nick']) && $_GET['nick']!=""){
  21. $nick = $usernick; // Usernick
  22. $msg = rawurldecode(stripslashes(trim($_GET['msg']))); // User message content
  23. $line = ""; // start with nothing
  24. $logline = ""; // start with nothing
  25. // Seperate message input via space
  26. $cmd = explode(" ", $msg);
  27. if($cmd[0]=="/msg") { // If using /msg, push private message
  28. $prvmsg = array_splice($cmd, 2); // grab private message from string
  29. $line .= "PRIVMSG" . " " . trim($cmd[1]) . " :"; // set for push
  30. $x = 0;
  31. $logline .= "<tr><td class='userinfo'><b>$nick</b> -> " . trim($cmd[1]) . ": </td><td>";
  32. foreach($prvmsg as $word) {
  33. // Grab each word in the array after the privmsg username
  34. if($x == 0) {
  35. $line .= $word;
  36. $logline .= $word;
  37. } else {
  38. $line .= " " . $word;
  39. $logline .= " " . $word;
  40. }
  41. $x++;
  42. }
  43. $line .= "\n";
  44. $logline .= "</td></tr>\n";
  45. } else if($cmd[0]=="/me") { // If using /msg, push private message
  46. $prvmsg = array_splice($cmd, 1); // grab private message from string
  47. $line .= "PRIVMSG" . " " . $channel . " :\x01ACTION "; // set for push
  48. $x = 0;
  49. $logline .= "<tr><td class='userinfo'><b>$channel:</b></td><td> $nick ";
  50. foreach($prvmsg as $word) {
  51. // Grab each word in the array after the privmsg username
  52. if($x == 0) {
  53. $line .= $word;
  54. $logline .= $word;
  55. } else {
  56. $line .= " " . $word;
  57. $logline .= " " . $word;
  58. }
  59. $x++;
  60. }
  61. $logline .= "</td></tr>\n";
  62. $line .= "\x01\n";
  63. } else if ($cmd[0]=="/join") {
  64. doLog("$username: joining " . $cmd[1]);
  65. $line .= "JOIN " . trim($cmd[1]) . "\n"; // set for push
  66. //$logline .= "<tr><td class='userinfo'><b>$nick</b>:</td><td>Joining " . $cmd[1] . "</td></tr>\n"; // push to client
  67. $_SESSION['idleirc-channel'] = trim($cmd[1]);
  68. } else if ($cmd[0]=="/channel" || $cmd[0]=="/focused") {
  69. doLog("$username: checking focused channel: " . $channel);
  70. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Focused on $channel</td></tr>\n"; // push to client
  71. } else if ($cmd[0]=="/nick") {
  72. if($cmd[1]!="") {
  73. doLog("$username: setting nick to " . $cmd[1]);
  74. $line .= "NICK " . trim($cmd[1]) . "\n"; // set for push
  75. $_SESSION['idleirc-nick'] = trim($cmd[1]);
  76. } else {
  77. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Joining " . $cmd[1] . "</td></tr>\n";
  78. }
  79. } else if ($cmd[0]=="/usermode") {
  80. if($cmd[1]!="") {
  81. doLog("$username: setting usermode to " . $cmd[1]);
  82. $line .= "MODE $nick +x" . trim($cmd[1]) . "\n"; // set for push
  83. } else {
  84. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Missing User MODE flags<br /> /usermode [flag(s)]</td></tr>\n";
  85. }
  86. } else if ($cmd[0]=="/mode") {
  87. if($cmd[1]!="") {
  88. if($cmd[2]!="") {
  89. doLog("$username: setting nick to " . $cmd[1]);
  90. $line .= "MODE " . trim($cmd[1]) . " " . trim($cmd[2]) . "\n"; // set for push
  91. } else {
  92. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Missing MODE flags:<br /> /mode [channel] [flag(s)]</td></tr>\n";
  93. }
  94. } else {
  95. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Missing channel and flags:<br /> /mode [channel] [flag(s)]</td></tr>\n";
  96. }
  97. } else if ($cmd[0]=="/list") {
  98. if($cmd[1]!="") {
  99. doLog("$username: listing users for " . $cmd[1]);
  100. $line .= "NAMES " . trim($cmd[1]) . "\n"; // set for push
  101. } else {
  102. doLog("$username: listing users for $channel");
  103. $line .= "NAMES $channel\n"; // set for push
  104. }
  105. } else if ($cmd[0] == "/rejoin") {
  106. doLog("$username: rejoining channel");
  107. if ($cmd[1] != "") {
  108. $line .= "PART " . trim($cmd[1]) . " :$username leaving...\n"; // push close command to IRC
  109. $line .= "JOIN " . trim($cmd[1]) . "\n"; // set for push
  110. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving " . trim($cmd[1]) . "</td></tr>\n"; // push to client
  111. } else {
  112. $line .= "PART $channel :$username leaving...\n"; // push close command to IRC
  113. $line .= "JOIN $channel\n"; // set for push
  114. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
  115. }
  116. } else if ($cmd[0] == "/part") {
  117. doLog("$username: leaving " . trim($cmd[1]));
  118. if ($cmd[1] != "") {
  119. $line .= "PART " . trim($cmd[1]) . " :$nick leaving...\n"; // push close command to IRC
  120. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving " . trim($cmd[1]) . "</td></tr>\n"; // push to client
  121. } else {
  122. $line .= "PART $channel :$username leaving...\n"; // push close command to IRC
  123. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
  124. }
  125. } else if ($cmd[0] == "/focus" || $cmd[0] == "/switch" || $cmd[0] == "/query") {
  126. if(trim($cmd[1]) != $channel) {
  127. $_SESSION['idleirc-channel'] = trim($cmd[1]);
  128. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>" . trim($cmd[1]) . " is focused, all messages will be sent to " . trim($cmd[1]) . "</td></tr>\n"; // push to client
  129. } else {
  130. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>You're already focused on $channel</td></tr>\n";
  131. }
  132. } else if ($cmd[0] == "/help" || $cmd[0] == "/?") {
  133. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version ~</span></td>";
  134. $logline .= "<td>HELP: Information about $title<br />/join [channel] : Join IRC channel [channel]<br />";
  135. $logline .= "/focus [channel|user]: Funnel messages to specific channel or user (Alias: /switch; /query)<br />";
  136. $logline .= "/part (channel): Part channel, part focused channel if none specified<br />";
  137. $logline .= "/rejoin (channel): Part, and join a channel; focused channel if none is specified<br />";
  138. $logline .= "/nick [nickname]: Change your nickname<br />";
  139. $logline .= "/list [channel]: List users in a channel<br />";
  140. $logline .= "/usermode [flag(s): Sets MODE flag(s) to user<br />";
  141. $logfile .= "/mode [channel] [flag(s)]: Sets MODE flag(s) for [channel]<br />";
  142. $logline .= "/channel: Prints focused channel / user (Alias: /focused)</td></tr>\n";
  143. } else {
  144. // @@ This is a work in progress
  145. // Sends every channel message to each channel :[
  146. $line .= "PRIVMSG $channel :$msg\n";
  147. $logline .= "<tr><td class='userinfo'><b>$nick</b> to $channel:</td><td> $msg</td></tr>\n";
  148. }
  149. // Get original content
  150. $content = file_get_contents("users/$username.log");
  151. echo "<table>" . nl2br(stripslashes($content)) . "</table>";
  152. // Grab all contents, and push to socket output file.
  153. file_put_contents("users/$username.log", $content . $logline);
  154. // Grab user message and push to IRC client
  155. file_put_contents("users/.$username.push", $line);
  156. // Throw out your user message
  157. //echo nl2br(stripslashes($line));
  158. // DONE
  159. } else if (isset($_GET['get']) && isset($_GET['nick']) && $_GET['nick']!="") {
  160. $nick = stripslashes(htmlentities($_GET['nick'])); // Username
  161. // Grab IRC client output
  162. $content = file_get_contents("users/$nick.log");
  163. if($_GET['get']=="") {
  164. // Push content to the web frontend
  165. echo "<table>" . nl2br(stripslashes($content)) . "</table>";
  166. // DONE
  167. } else if($_GET['get']=="notificationmentionexists") {
  168. if(file_exists("users/.$nick.mentioned")) {
  169. echo "true";
  170. } else {
  171. echo "false";
  172. }
  173. } else if($_GET['get']=="notificationpmedexists") {
  174. if(file_exists("users/.$nick.pmed")) {
  175. echo "true";
  176. } else {
  177. echo "false";
  178. }
  179. } else if($_GET['get']=="notificationmention") {
  180. $mentionuser = file_get_contents("users/.$nick.mentioned");
  181. unlink("users/.$nick.mentioned");
  182. echo $mentionuser;
  183. } else if($_GET['get']=="notificationpmed") {
  184. $pmuser = file_get_contents("users/.$nick.pmed");
  185. unlink("users/.$nick.pmed");
  186. echo $pmuser;
  187. }
  188. } else if (isset($_GET['do']) && isset($_GET['nick']) && $_GET['nick']!="") {
  189. $nick = stripslashes(htmlentities($_GET['nick']));
  190. include("users/" . $nick . ".php");
  191. if ($_GET['do']=="clearlog") {
  192. if(file_exists("users/" . $nick . ".log") && ($acctpass == $userpass)) {
  193. unlink("users/" . $nick . ".log");
  194. }
  195. } else if($_GET['do']=="login" && !file_exists("users/.$username.pingfile") && ($acctpass == $userpass)) { // Is user asking for login?
  196. // Join channel
  197. $isachannel = substr_count($_SESSION['idleirc-channel'],'#') > 1 ? TRUE : FALSE ;
  198. if(!isset($_SESSION['idleirc-channel']) || $isachannel == FALSE) {
  199. file_put_contents("users/.$username.push", "JOIN " . $userchannel . "\n");
  200. } else {
  201. file_put_contents("users/.$username.push", "JOIN " . $userchannel . "\n");
  202. }
  203. // Make sure users DB is clean, put nothing into socket read file
  204. if(!file_exists("users/$username.log")) {
  205. file_put_contents("users/$username.log", "");
  206. chmod("users/$username.log", 0755); // file permissions for read / write
  207. }
  208. // start pingfile - determines if webclient is active via write timestamp
  209. file_put_contents("users/.$username.pingfile", "pong");
  210. chmod("users/.$username.pingfile", 0755); // file permissions for read / write
  211. // Execute IRC client in background
  212. // IRC server will die when either 1) pingfile is deleted, or
  213. // 2) if pingfile is older than 10 seconds of current sys time
  214. $realpath = realpath("./irc.php"); // get full file path
  215. // Execute IRC client
  216. shell_exec("/usr/bin/php $realpath $username $servaddr $servport > /dev/null 2>/dev/null &");
  217. } else if($_GET['do']=="logout" && ($acctpass == $userpass)) { // Is user asking for logout?
  218. // Remove ping file if user logs out. IRC server will close
  219. $content = file_get_contents("users/$nick.log");
  220. file_put_contents("users/$nick.log", $content . "<tr><td class='userinfo'><b>$usernick</b> ~ </td><td> left the server...</td></tr>\n");
  221. unlink("users/.$nick.pingfile");
  222. }
  223. }
  224. ?>