idleirc/server.php

153 lines
6.0 KiB
PHP
Executable File

<?php
///////
// IdleIRC - 2020
// (C) Chris Dorman, GPLv3
// https://notabug.org/Pentium44/idleirc
///////
// server.php - used to communicate between web frontend and irc client
// Grabs from IRC client output
// Pushes to IRC client input
// Include PHP config file with server, title, and channel settings
include_once("config.php");
session_start();
if(isset($_GET['msg'])) {
doLog("Msg information: " . trim($_GET['msg']));
}
$acctpass = $_SESSION['idleirc-pass'];
$channel = $_SESSION['idleirc-channel'];
$servaddr = $_SESSION['idleirc-servaddr'];
$servport = $_SESSION['idleirc-servport'];
// If we have a message; grab user and content and push to IRC client
if (isset($_GET['msg']) && $_GET['msg']!="" && isset($_GET['nick']) && $_GET['nick']!=""){
$nick = stripslashes(htmlentities($_GET['nick'])); // Usernick
$msg = urldecode(stripslashes(trim($_GET['msg']))); // User message content
$line = ""; // start with nothing
$logline = ""; // start with nothing
// Seperate message input via space
$cmd = explode(" ", $msg);
if($cmd[0]=="/msg") { // If using /msg, push private message
$prvmsg = array_splice($cmd, 2); // grab private message from string
$line .= "PRIVMSG" . " " . $cmd[1] . " "; // set for push
$x = 0;
$logline .= "<tr><td class='userinfo'><b>$nick</b> -> " . $cmd[1] . ": </td><td>";
foreach($prvmsg as $word) {
// Grab each word in the array after the privmsg username
if($x == 0) {
$line .= ":" . $word;
$logline .= $word;
} else {
$line .= " " . $word;
$logline .= " " . $word;
}
$x++;
}
$line .= "\n";
$logline .= "</td></tr>\n";
} else if($cmd[0]=="/me") { // If using /msg, push private message
$prvmsg = array_splice($cmd, 1); // grab private message from string
$line .= "PRIVMSG" . " " . $channel . " :\x01ACTION "; // set for push
$x = 0;
$logline .= "<tr><td class='userinfo'><b>$channel:</b></td><td> $nick ";
foreach($prvmsg as $word) {
// Grab each word in the array after the privmsg username
if($x == 0) {
$line .= $word;
$logline .= $word;
} else {
$line .= " " . $word;
$logline .= " " . $word;
}
$x++;
}
$logline .= "</td></tr>\n";
$line .= "\x01\n";
} else if ($cmd[0]=="/join") {
doLog("$username: joining " . $cmd[1]);
$line .= "JOIN" . " " . $cmd[1] . "\n"; // set for push
$logline .= "<tr><td class='userinfo'><b>$nick</b>:</td><td>Joining " . $cmd[1] . "</td></tr>\n"; // push to client
$_SESSION['idleirc-channel'] = trim($cmd[1]);
} else if ($cmd[0] == "/part") {
doLog("$username: leaving " . $cmd[1]);
$line .= "PART $channel :$username leaving...\n"; // push close command to IRC
$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
} else if ($cmd[0] == "/focus") {
if(trim($cmd[1]) != $channel) {
$_SESSION['idleirc-channel'] = trim($cmd[1]);
$logline .= "<tr><td class='userinfo'><span style='color:$ipcolor;'>$servaddr</b>: </td><td>$channel is focused, all messages will be sent to $channel</td></tr>\n"; // push to client
}
} else {
// @@ This is a work in progress
// Sends every channel message to each channel :[
$line .= "PRIVMSG $channel :$msg\n";
$logline .= "<tr><td class='userinfo'><b>$nick</b> in $channel:</td><td> $msg</td></tr>\n";
}
// Get original content
$content = file_get_contents("$nick.log");
echo "<table>" . nl2br(stripslashes($content)) . "</table>";
// Grab all contents, and push to socket output file.
file_put_contents("$nick.log", $content . $logline);
// Grab user message and push to IRC client
file_put_contents(".$nick.push", $line);
// Throw out your user message
//echo nl2br(stripslashes($line));
// DONE
} else if (isset($_GET['get']) && isset($_GET['nick']) && $_GET['nick']!="") {
$nick = stripslashes(htmlentities($_GET['nick'])); // Username
// Grab IRC client output
$content = file_get_contents("$nick.log");
// Push content to the web frontend
echo "<table>" . nl2br(stripslashes($content)) . "</table";
// DONE
} else if (isset($_GET['do']) && isset($_GET['nick']) && $_GET['nick']!="") {
$nick = stripslashes(htmlentities($_GET['nick']));
include("users/" . $nick . ".php");
if ($_GET['do']=="clearlog") {
if(file_exists($nick . ".log") && ($acctpass == $userpass)) {
unlink($nick . ".log");
}
} else if($_GET['do']=="login" && !file_exists(".$nick.pingfile") && ($acctpass == $userpass)) { // Is user asking for login?
// Join channel
if(!isset($_SESSION['idleirc-channel'])) {
file_put_contents(".$nick.push", "JOIN " . $default_channel . "\n");
} else {
file_put_contents(".$nick.push", "JOIN " . $channel . "\n");
}
// Make sure users DB is clean, put nothing into socket read file
if(!file_exists("$nick.log")) {
file_put_contents("$nick.log", "");
chmod("$username.log", 0755); // file permissions for read / write
}
// start pingfile - determines if webclient is active via write timestamp
file_put_contents(".$nick.pingfile", "pong");
chmod(".$username.pingfile", 0755); // file permissions for read / write
// Execute IRC client in background
// IRC server will die when either 1) pingfile is deleted, or
// 2) if pingfile is older than 10 seconds of current sys time
$realpath = realpath("./irc.php"); // get full file path
// Execute IRC client
shell_exec("/usr/bin/php $realpath $nick $servaddr $servport > /dev/null 2>/dev/null &");
} else if($_GET['do']=="logout" && ($acctpass == $userpass)) { // Is user asking for logout?
// Remove ping file if user logs out. IRC server will close
$content = file_get_contents("$nick.log");
file_put_contents("$nick.log", $content . "<tr><td class='userinfo'><b>$nick</b> ~ </td><td> left the server...</td></tr>\n");
unlink(".$nick.pingfile");
} else if($_GET['do']=="keepup") { // Client asking for keepup ping.
// PONG to server.
//file_put_contents(".$nick.pingfile", "ping");
}
}
?>