server.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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]=="/archive") {
  87. if(file_exists("users/$username.logcount")) {
  88. $archivedLogCount = file_get_contents("users/$username.logcount");
  89. $archivedLogCount++;
  90. file_put_contents("users/$username.logcount", $archivedLogCount);
  91. rename("users/$username.log", "users/$username.$archivedLogCount.log");
  92. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Logs are archived! View under 'IRC logs'</td></tr>\n";
  93. } else {
  94. file_put_contents("users/$username.logcount", "1");
  95. rename("users/$username.log", "users/$username.1.log");
  96. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>Logs are archived! View under 'IRC logs'</td></tr>\n";
  97. }
  98. } else if ($cmd[0]=="/mode") {
  99. if($cmd[1]!="") {
  100. if($cmd[2]!="") {
  101. doLog("$username: setting nick to " . $cmd[1]);
  102. $line .= "MODE " . trim($cmd[1]) . " " . trim($cmd[2]) . "\n"; // set for push
  103. } else {
  104. $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";
  105. }
  106. } else {
  107. $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";
  108. }
  109. } else if ($cmd[0]=="/list") {
  110. if($cmd[1]!="") {
  111. doLog("$username: listing users for " . $cmd[1]);
  112. $line .= "NAMES " . trim($cmd[1]) . "\n"; // set for push
  113. } else {
  114. doLog("$username: listing users for $channel");
  115. $line .= "NAMES $channel\n"; // set for push
  116. }
  117. } else if ($cmd[0] == "/rejoin") {
  118. doLog("$username: rejoining channel");
  119. if ($cmd[1] != "") {
  120. $line .= "PART " . trim($cmd[1]) . " :$username leaving...\n"; // push close command to IRC
  121. $line .= "JOIN " . trim($cmd[1]) . "\n"; // set for push
  122. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving " . trim($cmd[1]) . "</td></tr>\n"; // push to client
  123. } else {
  124. $line .= "PART $channel :$username leaving...\n"; // push close command to IRC
  125. $line .= "JOIN $channel\n"; // set for push
  126. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
  127. }
  128. } else if ($cmd[0] == "/part") {
  129. doLog("$username: leaving " . trim($cmd[1]));
  130. if ($cmd[1] != "") {
  131. $line .= "PART " . trim($cmd[1]) . " :$nick leaving...\n"; // push close command to IRC
  132. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving " . trim($cmd[1]) . "</td></tr>\n"; // push to client
  133. } else {
  134. $line .= "PART $channel :$username leaving...\n"; // push close command to IRC
  135. //$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
  136. }
  137. } else if ($cmd[0] == "/focus" || $cmd[0] == "/switch" || $cmd[0] == "/query") {
  138. if(trim($cmd[1]) != $channel) {
  139. $_SESSION['idleirc-channel'] = trim($cmd[1]);
  140. $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
  141. } else {
  142. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version</span> ~</td><td>You're already focused on $channel</td></tr>\n";
  143. }
  144. } else if ($cmd[0] == "/help" || $cmd[0] == "/?") {
  145. $logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$title $version ~</span></td>";
  146. $logline .= "<td>HELP: Information about $title<br />/join [channel] : Join IRC channel [channel]<br />";
  147. $logline .= "/archive: Archive current log file, and clear current chat history.<br />";
  148. $logline .= "/focus [channel|user]: Funnel messages to specific channel or user (Alias: /switch; /query)<br />";
  149. $logline .= "/part (channel): Part channel, part focused channel if none specified<br />";
  150. $logline .= "/rejoin (channel): Part, and join a channel; focused channel if none is specified<br />";
  151. $logline .= "/nick [nickname]: Change your nickname<br />";
  152. $logline .= "/list [channel]: List users in a channel<br />";
  153. $logline .= "/msg [channel|user] [msg]: Message channel, or PM 'user'<br />";
  154. $logline .= "/usermode [flag(s): Sets MODE flag(s) to user<br />";
  155. $logfile .= "/mode [channel] [flag(s)]: Sets MODE flag(s) for [channel]<br />";
  156. $logline .= "/channel: Prints focused channel / user (Alias: /focused)</td></tr>\n";
  157. } else {
  158. // @@ This is a work in progress
  159. // Sends every channel message to each channel :[
  160. $line .= "PRIVMSG $channel :$msg\n";
  161. $logline .= "<tr><td class='userinfo'><b>$nick</b> to $channel:</td><td> $msg</td></tr>\n";
  162. }
  163. // Get original content
  164. $content = file_get_contents("users/$username.log");
  165. echo "<table>" . nl2br(stripslashes($content)) . "</table>";
  166. // Grab all contents, and push to socket output file.
  167. file_put_contents("users/$username.log", $content . $logline);
  168. // Grab user message and push to IRC client
  169. file_put_contents("users/.$username.push", $line);
  170. // Throw out your user message
  171. //echo nl2br(stripslashes($line));
  172. // DONE
  173. } else if (isset($_GET['userlist'])) {
  174. // Generate userlist
  175. if(file_exists("users/$username.userlist")) {
  176. $userListContent = file_get_contents("users/$username.userlist");
  177. echo $userListContent;
  178. } else {
  179. echo " ";
  180. }
  181. } else if (isset($_GET['get']) && isset($_GET['nick']) && $_GET['nick']!="") {
  182. $nick = stripslashes(htmlentities($_GET['nick'])); // Username
  183. // Grab IRC client output
  184. $content = file_get_contents("users/$nick.log");
  185. if($_GET['get']=="") {
  186. // Push content to the web frontend
  187. echo "<table>" . nl2br(stripslashes($content)) . "</table>";
  188. // DONE
  189. } else if($_GET['get']=="notificationmentionexists") {
  190. if(file_exists("users/.$nick.mentioned")) {
  191. echo "true";
  192. } else {
  193. echo "false";
  194. }
  195. } else if($_GET['get']=="notificationpmedexists") {
  196. if(file_exists("users/.$nick.pmed")) {
  197. echo "true";
  198. } else {
  199. echo "false";
  200. }
  201. } else if($_GET['get']=="notificationmention") {
  202. $mentionuser = file_get_contents("users/.$nick.mentioned");
  203. unlink("users/.$nick.mentioned");
  204. echo $mentionuser;
  205. } else if($_GET['get']=="notificationpmed") {
  206. $pmuser = file_get_contents("users/.$nick.pmed");
  207. unlink("users/.$nick.pmed");
  208. echo $pmuser;
  209. }
  210. } else if (isset($_GET['do']) && isset($_GET['nick']) && $_GET['nick']!="") {
  211. $nick = stripslashes(htmlentities($_GET['nick']));
  212. include("users/" . $nick . ".php");
  213. if ($_GET['do']=="clearlog") {
  214. if(file_exists("users/" . $nick . ".log") && ($acctpass == $userpass)) {
  215. unlink("users/$nick.log");
  216. }
  217. if(file_exists("users/" . $nick . ".logcount") && ($acctpass == $userpass)) {
  218. $archived = file_get_contents("users/$nick.logcount");
  219. for($x = 1; $x <= $archived; $x++) {
  220. if(file_exists("users/$nick.$x.log")) {
  221. unlink("users/$nick.$x.log");
  222. }
  223. }
  224. unlink("users/$nick.logcount");
  225. }
  226. } else if($_GET['do']=="login" && !file_exists("users/.$username.pingfile") && ($acctpass == $userpass)) { // Is user asking for login?
  227. // Join channel
  228. $isachannel = substr_count($_SESSION['idleirc-channel'],'#') > 1 ? TRUE : FALSE ;
  229. if(!isset($_SESSION['idleirc-channel']) || $isachannel == FALSE) {
  230. file_put_contents("users/.$username.push", "JOIN " . $userchannel . "\n");
  231. } else {
  232. file_put_contents("users/.$username.push", "JOIN " . $userchannel . "\n");
  233. }
  234. // Make sure users DB is clean, put nothing into socket read file
  235. if(!file_exists("users/$username.log")) {
  236. file_put_contents("users/$username.log", "");
  237. chmod("users/$username.log", 0755); // file permissions for read / write
  238. }
  239. // start pingfile - determines if webclient is active via write timestamp
  240. file_put_contents("users/.$username.pingfile", "pong");
  241. chmod("users/.$username.pingfile", 0755); // file permissions for read / write
  242. // Execute IRC client in background
  243. // IRC server will die when either 1) pingfile is deleted, or
  244. // 2) if pingfile is older than 10 seconds of current sys time
  245. $realpath = realpath("./irc.php"); // get full file path
  246. // Execute IRC client
  247. shell_exec("/usr/bin/php $realpath $username $servaddr $servport > /dev/null 2>/dev/null &");
  248. } else if($_GET['do']=="logout" && ($acctpass == $userpass)) { // Is user asking for logout?
  249. // Remove ping file if user logs out. IRC server will close
  250. $content = file_get_contents("users/$nick.log");
  251. file_put_contents("users/$nick.log", $content . "<tr><td class='userinfo'><b>$usernick</b> ~ </td><td> left the server...</td></tr>\n");
  252. unlink("users/.$nick.pingfile");
  253. }
  254. }
  255. ?>