2020-12-09 18:32:06 -08:00
< ? 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 ();
2020-12-09 22:17:19 -08:00
$username = $_SESSION [ 'idleirc-user' ];
$usernick = $_SESSION [ 'idleirc-nick' ];
2020-12-09 18:32:06 -08:00
$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' ] != " " ){
2020-12-09 22:17:19 -08:00
$nick = $usernick ; // Usernick
2020-12-10 21:15:12 -08:00
$msg = rawurldecode ( stripslashes ( trim ( $_GET [ 'msg' ]))); // User message content
2020-12-09 18:32:06 -08:00
$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
2020-12-10 21:15:12 -08:00
$line .= " PRIVMSG " . " " . trim ( $cmd [ 1 ]) . " : " ; // set for push
2020-12-09 18:32:06 -08:00
$x = 0 ;
2021-01-03 18:16:13 -08:00
$logline .= " <tr><td class='userinfo'><b> $nick </b>: " . trim ( $cmd [ 1 ]) . " : </td><td> " ;
2020-12-09 18:32:06 -08:00
foreach ( $prvmsg as $word ) {
// Grab each word in the array after the privmsg username
if ( $x == 0 ) {
2020-12-10 21:15:12 -08:00
$line .= $word ;
2020-12-09 18:32:06 -08:00
$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 . " : \x01 ACTION " ; // 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 " ) {
2020-12-09 18:50:56 -08:00
doLog ( " $username : joining " . $cmd [ 1 ]);
2020-12-09 22:17:19 -08:00
$line .= " JOIN " . trim ( $cmd [ 1 ]) . " \n " ; // set for push
2020-12-09 21:25:20 -08:00
//$logline .= "<tr><td class='userinfo'><b>$nick</b>:</td><td>Joining " . $cmd[1] . "</td></tr>\n"; // push to client
2020-12-09 18:50:56 -08:00
$_SESSION [ 'idleirc-channel' ] = trim ( $cmd [ 1 ]);
2020-12-09 23:26:34 -08:00
} else if ( $cmd [ 0 ] == " /channel " || $cmd [ 0 ] == " /focused " ) {
2021-01-03 18:16:13 -08:00
doLog ( " $username : checking focused channel: " . $channel );
$logline .= " <tr><td class='userinfo'><span style='color: $ipcolor ;'> $title $version </span> ~</td><td>Focused on $channel </td></tr> \n " ; // push to client
} else if ( $cmd [ 0 ] == " /nick " ) {
2020-12-09 22:17:19 -08:00
if ( $cmd [ 1 ] != " " ) {
2021-01-03 18:16:13 -08:00
doLog ( " $username : setting nick to " . $cmd [ 1 ]);
$line .= " NICK " . trim ( $cmd [ 1 ]) . " \n " ; // set for push
2020-12-09 22:17:19 -08:00
$_SESSION [ 'idleirc-nick' ] = trim ( $cmd [ 1 ]);
2020-12-10 21:15:12 -08:00
} else {
$logline .= " <tr><td class='userinfo'><span style='color: $ipcolor ;'> $title $version </span> ~</td><td>Joining " . $cmd [ 1 ] . " </td></tr> \n " ;
}
2021-01-03 18:16:13 -08:00
} else if ( $cmd [ 0 ] == " /usermode " ) {
if ( $cmd [ 1 ] != " " ) {
doLog ( " $username : setting usermode to " . $cmd [ 1 ]);
$line .= " MODE $nick +x " . trim ( $cmd [ 1 ]) . " \n " ; // set for push
} else {
$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 " ;
}
} else if ( $cmd [ 0 ] == " /archive " ) {
if ( file_exists ( " users/ $username .logcount " )) {
$archivedLogCount = file_get_contents ( " users/ $username .logcount " );
$archivedLogCount ++ ;
file_put_contents ( " users/ $username .logcount " , $archivedLogCount );
rename ( " users/ $username .log " , " users/ $username . $archivedLogCount .log " );
$logline .= " <tr><td class='userinfo'><span style='color: $ipcolor ;'> $title $version </span> ~</td><td>Logs are archived! View under 'IRC logs'</td></tr> \n " ;
} else {
file_put_contents ( " users/ $username .logcount " , " 1 " );
rename ( " users/ $username .log " , " users/ $username .1.log " );
$logline .= " <tr><td class='userinfo'><span style='color: $ipcolor ;'> $title $version </span> ~</td><td>Logs are archived! View under 'IRC logs'</td></tr> \n " ;
}
} else if ( $cmd [ 0 ] == " /mode " ) {
if ( $cmd [ 1 ] != " " ) {
2020-12-10 21:15:12 -08:00
if ( $cmd [ 2 ] != " " ) {
doLog ( " $username : setting nick to " . $cmd [ 1 ]);
$line .= " MODE " . trim ( $cmd [ 1 ]) . " " . trim ( $cmd [ 2 ]) . " \n " ; // set for push
} else {
$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 " ;
}
} else {
2021-01-03 18:16:13 -08:00
$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 " ;
2020-12-09 22:17:19 -08:00
}
2021-01-03 18:16:13 -08:00
} else if ( $cmd [ 0 ] == " /list " ) {
2020-12-09 22:17:19 -08:00
if ( $cmd [ 1 ] != " " ) {
doLog ( " $username : listing users for " . $cmd [ 1 ]);
$line .= " NAMES " . trim ( $cmd [ 1 ]) . " \n " ; // set for push
2021-01-03 18:16:13 -08:00
} else {
2020-12-09 22:44:04 -08:00
doLog ( " $username : listing users for $channel " );
2021-01-03 18:16:13 -08:00
$line .= " NAMES $channel\n " ; // set for push
2020-12-09 22:44:04 -08:00
}
2021-01-03 18:16:13 -08:00
} else if ( $cmd [ 0 ] == " /rejoin " ) {
2020-12-09 21:36:55 -08:00
doLog ( " $username : rejoining channel " );
2021-01-03 18:16:13 -08:00
if ( $cmd [ 1 ] != " " ) {
$line .= " PART " . trim ( $cmd [ 1 ]) . " : $username leaving... \n " ; // push close command to IRC
2020-12-09 21:36:55 -08:00
$line .= " JOIN " . trim ( $cmd [ 1 ]) . " \n " ; // set for push
2021-01-03 18:16:13 -08:00
//$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving " . trim($cmd[1]) . "</td></tr>\n"; // push to client
} else {
$line .= " PART $channel : $username leaving... \n " ; // push close command to IRC
2020-12-09 21:36:55 -08:00
$line .= " JOIN $channel\n " ; // set for push
2021-01-03 18:16:13 -08:00
//$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
}
2020-12-09 18:50:56 -08:00
} else if ( $cmd [ 0 ] == " /part " ) {
2020-12-09 21:36:55 -08:00
doLog ( " $username : leaving " . trim ( $cmd [ 1 ]));
2020-12-09 21:25:20 -08:00
if ( $cmd [ 1 ] != " " ) {
2021-01-03 18:16:13 -08:00
$line .= " PART " . trim ( $cmd [ 1 ]) . " : $nick leaving... \n " ; // push close command to IRC
//$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving " . trim($cmd[1]) . "</td></tr>\n"; // push to client
2020-12-09 21:25:20 -08:00
} else {
$line .= " PART $channel : $username leaving... \n " ; // push close command to IRC
2021-01-03 18:16:13 -08:00
//$logline .= "<tr><td class='userinfo'><b>$nick</b>: </td><td>Leaving $channel</td></tr>\n"; // push to client
2020-12-09 21:25:20 -08:00
}
} else if ( $cmd [ 0 ] == " /focus " || $cmd [ 0 ] == " /switch " || $cmd [ 0 ] == " /query " ) {
2020-12-09 18:50:56 -08:00
if ( trim ( $cmd [ 1 ]) != $channel ) {
$_SESSION [ 'idleirc-channel' ] = trim ( $cmd [ 1 ]);
2020-12-10 21:15:12 -08:00
$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
2020-12-09 21:25:20 -08:00
} else {
2020-12-10 21:15:12 -08:00
$logline .= " <tr><td class='userinfo'><span style='color: $ipcolor ;'> $title $version </span> ~</td><td>You're already focused on $channel </td></tr> \n " ;
2020-12-09 18:50:56 -08:00
}
2020-12-09 21:52:22 -08:00
} else if ( $cmd [ 0 ] == " /help " || $cmd [ 0 ] == " /? " ) {
2020-12-10 21:15:12 -08:00
$logline .= " <tr><td class='userinfo'><span style='color: $ipcolor ;'> $title $version ~</span></td> " ;
2020-12-09 22:17:19 -08:00
$logline .= " <td>HELP: Information about $title <br />/join [channel] : Join IRC channel [channel]<br /> " ;
2021-01-03 18:27:47 -08:00
$logline .= " /archive: Archive current log file, and clear current chat history.<br /> " ;
2020-12-09 22:17:19 -08:00
$logline .= " /focus [channel|user]: Funnel messages to specific channel or user (Alias: /switch; /query)<br /> " ;
$logline .= " /part (channel): Part channel, part focused channel if none specified<br /> " ;
$logline .= " /rejoin (channel): Part, and join a channel; focused channel if none is specified<br /> " ;
$logline .= " /nick [nickname]: Change your nickname<br /> " ;
2020-12-09 23:26:34 -08:00
$logline .= " /list [channel]: List users in a channel<br /> " ;
2021-01-03 14:48:05 -08:00
$logline .= " /msg [channel|user] [msg]: Message channel, or PM 'user'<br /> " ;
2020-12-10 21:15:12 -08:00
$logline .= " /usermode [flag(s): Sets MODE flag(s) to user<br /> " ;
$logfile .= " /mode [channel] [flag(s)]: Sets MODE flag(s) for [channel]<br /> " ;
2020-12-09 23:26:34 -08:00
$logline .= " /channel: Prints focused channel / user (Alias: /focused)</td></tr> \n " ;
2020-12-09 18:32:06 -08:00
} else {
// @@ This is a work in progress
// Sends every channel message to each channel :[
$line .= " PRIVMSG $channel : $msg\n " ;
2020-12-09 21:52:22 -08:00
$logline .= " <tr><td class='userinfo'><b> $nick </b> to $channel :</td><td> $msg </td></tr> \n " ;
2020-12-09 18:32:06 -08:00
}
// Get original content
2020-12-11 20:55:13 -08:00
$content = file_get_contents ( " users/ $username .log " );
2020-12-09 18:32:06 -08:00
echo " <table> " . nl2br ( stripslashes ( $content )) . " </table> " ;
// Grab all contents, and push to socket output file.
2020-12-11 20:55:13 -08:00
file_put_contents ( " users/ $username .log " , $content . $logline );
2020-12-09 18:32:06 -08:00
// Grab user message and push to IRC client
2020-12-11 20:55:13 -08:00
file_put_contents ( " users/. $username .push " , $line );
2020-12-09 18:32:06 -08:00
// Throw out your user message
//echo nl2br(stripslashes($line));
// DONE
2021-01-03 20:38:53 -08:00
} else if ( isset ( $_GET [ 'userlist' ])) {
// Generate userlist
if ( file_exists ( " users/ $username .userlist " )) {
$userListContent = file_get_contents ( " users/ $username .userlist " );
echo $userListContent ;
} else {
echo " " ;
}
2020-12-09 18:32:06 -08:00
} else if ( isset ( $_GET [ 'get' ]) && isset ( $_GET [ 'nick' ]) && $_GET [ 'nick' ] != " " ) {
2020-12-12 21:39:40 -08:00
$nick = stripslashes ( htmlentities ( $_GET [ 'nick' ])); // Username
// Grab IRC client output
$content = file_get_contents ( " users/ $nick .log " );
if ( $_GET [ 'get' ] == " " ) {
// Push content to the web frontend
echo " <table> " . nl2br ( stripslashes ( $content )) . " </table> " ;
// DONE
} else if ( $_GET [ 'get' ] == " notificationmentionexists " ) {
if ( file_exists ( " users/. $nick .mentioned " )) {
echo " true " ;
} else {
echo " false " ;
}
} else if ( $_GET [ 'get' ] == " notificationpmedexists " ) {
if ( file_exists ( " users/. $nick .pmed " )) {
echo " true " ;
} else {
echo " false " ;
}
} else if ( $_GET [ 'get' ] == " notificationmention " ) {
$mentionuser = file_get_contents ( " users/. $nick .mentioned " );
unlink ( " users/. $nick .mentioned " );
echo $mentionuser ;
} else if ( $_GET [ 'get' ] == " notificationpmed " ) {
$pmuser = file_get_contents ( " users/. $nick .pmed " );
unlink ( " users/. $nick .pmed " );
echo $pmuser ;
}
2020-12-09 18:32:06 -08:00
} else if ( isset ( $_GET [ 'do' ]) && isset ( $_GET [ 'nick' ]) && $_GET [ 'nick' ] != " " ) {
$nick = stripslashes ( htmlentities ( $_GET [ 'nick' ]));
include ( " users/ " . $nick . " .php " );
if ( $_GET [ 'do' ] == " clearlog " ) {
2020-12-11 20:55:13 -08:00
if ( file_exists ( " users/ " . $nick . " .log " ) && ( $acctpass == $userpass )) {
2021-01-03 18:23:29 -08:00
unlink ( " users/ $nick .log " );
2020-12-09 18:32:06 -08:00
}
2021-01-03 18:23:29 -08:00
if ( file_exists ( " users/ " . $nick . " .logcount " ) && ( $acctpass == $userpass )) {
$archived = file_get_contents ( " users/ $nick .logcount " );
for ( $x = 1 ; $x <= $archived ; $x ++ ) {
if ( file_exists ( " users/ $nick . $x .log " )) {
unlink ( " users/ $nick . $x .log " );
}
}
unlink ( " users/ $nick .logcount " );
}
2020-12-11 20:55:13 -08:00
} else if ( $_GET [ 'do' ] == " login " && ! file_exists ( " users/. $username .pingfile " ) && ( $acctpass == $userpass )) { // Is user asking for login?
2020-12-09 18:32:06 -08:00
// Join channel
2020-12-09 23:04:03 -08:00
$isachannel = substr_count ( $_SESSION [ 'idleirc-channel' ], '#' ) > 1 ? TRUE : FALSE ;
if ( ! isset ( $_SESSION [ 'idleirc-channel' ]) || $isachannel == FALSE ) {
2020-12-12 21:39:40 -08:00
file_put_contents ( " users/. $username .push " , " JOIN " . $userchannel . " \n " );
2020-12-09 18:32:06 -08:00
} else {
2020-12-12 21:39:40 -08:00
file_put_contents ( " users/. $username .push " , " JOIN " . $userchannel . " \n " );
2020-12-09 18:32:06 -08:00
}
// Make sure users DB is clean, put nothing into socket read file
2020-12-11 20:55:13 -08:00
if ( ! file_exists ( " users/ $username .log " )) {
file_put_contents ( " users/ $username .log " , " " );
chmod ( " users/ $username .log " , 0755 ); // file permissions for read / write
2020-12-09 18:32:06 -08:00
}
// start pingfile - determines if webclient is active via write timestamp
2020-12-11 20:55:13 -08:00
file_put_contents ( " users/. $username .pingfile " , " pong " );
chmod ( " users/. $username .pingfile " , 0755 ); // file permissions for read / write
2020-12-09 18:32:06 -08:00
// 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
2020-12-10 23:07:25 -08:00
shell_exec ( " /usr/bin/php $realpath $username $servaddr $servport > /dev/null 2>/dev/null & " );
2020-12-09 18:32:06 -08:00
} else if ( $_GET [ 'do' ] == " logout " && ( $acctpass == $userpass )) { // Is user asking for logout?
// Remove ping file if user logs out. IRC server will close
2020-12-11 20:55:13 -08:00
$content = file_get_contents ( " users/ $nick .log " );
file_put_contents ( " users/ $nick .log " , $content . " <tr><td class='userinfo'><b> $usernick </b> ~ </td><td> left the server...</td></tr> \n " );
unlink ( " users/. $nick .pingfile " );
2020-12-09 18:32:06 -08:00
}
}
?>