Description
Simple PHP IRC Logger is a simple PHP IRC Bot class designed to connect to an IRC server, and generate HTML log files. It is based on a modified version of Simple PHP IRC Bot. This script is designed and intended to be built upon for your needs, so if you have any problems please ask in our forum. Please also follow some of the notes below for guidance. For future updates and scripts, don’t forget to subscribe.
Features and Functions
- !join #channel – Joins the specified IRC channel
- !part #channel – Leaves the specified IRC channel
- !say #channel Message – Says message in the specified IRC channel
- !restart – Quits and restarts the script. (Helpful while testing the script)
- !shutdown – Quits and stops the script.
Usage and output:
<random-user> !say #nystic_chat hello there
<wildphp-bot> hello there
- Enable/Disable Logging (PHP Script Variable)
- Enable/Disable Logging Warning Message (PHP Script Variable)
Note the script in the current version of the script, all joined channels will be added to the single log file for the day.
Installation
- Download or copy paste the script from this page
- (Optional) If downloaded rename the file to bot.php ( Sorry, the blog will only allow me to upload it as log-bot.txt)
- Edit the $config array in the script to the server settings, should be self explanatory
- Download and install XAMPP
- Place bot.php in the htdocs folder of your XAMPP installation
- Don’t forget to start the Apache server by pressing the start button in the XAMPP Control Panel
- Go to http://localhost/log-bot.php, and the bot should connect and join the channel you specified in about 30 seconds
- If you have warning enabled the bot should say “Chat Logging has been [Enabled]” when entering the default room
- If you have logging enabled an HTML file should appear in the same directory, it will be updated when anyone says something in the room
Code
Download log-bot.txt (Right click, Save As…)
or
<?php
/**
* Simple PHP IRC Logger
*
* PHP Version 5
*
* LICENSE: This source file is subject to Creative Commons Attribution
* 3.0 License that is available through the world-wide-web at the following URI:
* http://creativecommons.org/licenses/by/3.0/. Basically you are free to adapt
* and use this script commercially/non-commercially. My only requirement is that
* you keep this header as an attribution to my work. Enjoy!
*
* @category Chat Room Scipt
* @package Simple PHP IRC Logger
* @author Super3boy <admin@wildphp.com>
* @copyright 2010, The Nystic Network
* @license http://creativecommons.org/licenses/by/3.0/
* @link http://wildphp.com (Visit for updated versions and more free scripts!)
* @version 1.0.0 (Last updated 04-04-2010)
*
*/
//So the bot doesn't stop.
set_time_limit(0);
ini_set('display_errors', 'on');
/* --- Varibles and Config Info --- */
//Sample connection data.
$config = array(
//General Config Info
'server' => 'chat.freenode.net',
'port' => 6667,
'name' => 'wildphp-bot',
'nick' => 'wildphp-bot',
'pass' => '',
//Logging Config Info
'channel' => '#nystic_chat',
'logging' => true,
'warning' => true,
);
/*
//Set your connection data.
$config = array(
//General Config Info
'server' => 'irc.example.com',
'port' => 6667,
'name' => 'real name',
'nick' => 'user',
'pass' => 'pass',
//Logging Config Info
'channel' => '#channel',
'logging' => true,
'warning' => true,
);
*/
/* --- IRCBot Class --- */
class IRCBot {
//This is going to hold our TCP/IP connection
var $socket;
//This is going to hold all of the messages both server and client
var $ex = array();
//var $logging = true;
/*
Construct item, opens the server connection, logs the bot in
@param array
*/
function __construct($config)
{
$this->socket = fsockopen($config['server'], $config['port']);
$this->login($config);
$this->main($config);
}
/*
Logs the bot in on the server
@param array
*/
function login($config)
{
$this->send_data('USER', $config['nick'].' wildphp.com '.$config['nick'].' :'.$config['name']);
$this->send_data('NICK', $config['nick']);
$this->join_channel($config['channel']);
if($config['logging']) {
$date = date("n-j-y");
$time = date('h:i:s A');
$logfile = fopen("$date-log.html","a");
fwrite($logfile,"<br/>**************** Logging Started at $time ****************<br/>");
fclose($logfile);
//Warn that logging has been enabled
if($config['warning']) {
$this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Enabled]");
}
}
}
/*
This is the workhorse function, grabs the data from the server and displays on the browser
*/
function main($config)
{
$data = fgets($this->socket, 256);
echo nl2br($data);
flush();
$this->ex = explode(' ', $data);
if($this->ex[0] == 'PING')
{
$this->send_data('PONG', $this->ex[0]); //Plays ping-pong with the server to stay connected.
}
//Logs the chat
if($config['logging'])
{
$logtxt = $this->filter_log($this->ex[1], $this->ex[2], $this->ex[0], $this->get_msg($this->ex)); //Gets human readable text from irc data
if($logtxt != null) { //Writes to log if it is a message
$date = date("n-j-y");
$logfile = fopen("$date-log.html","a");
fwrite($logfile,"$logtxt<br />");
fclose($logfile);
}
}
$command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
switch($command) //List of commands the bot responds to from a user.
{
case ':!join':
$this->join_channel($this->ex[4]);
break;
case ':!quit':
$this->send_data('QUIT', 'Wildphp.com Made Bot');
break;
case ':!op':
$this->op_user();
break;
case ':!deop':
$this->op_user('','', false);
break;
case ':!protect':
$this->protect_user();
break;
case ':!say':
$message = "";
for($i=4; $i <= (count($this->ex)); $i++)
{
$message .= $this->ex[$i]." ";
}
$this->send_data('PRIVMSG '.$config['channel'].' :', $message);
break;
case ':!restart':
//Warn that logging has been disabled
if($config['warning']) {
$this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Disabled]");
}
echo "<meta http-equiv=\"refresh\" content=\"3\">";
if($config['logging']) {
$date = date("n-j-y");
$time = date('h:i:s A');
$logfile = fopen("$date-log.html","a");
fwrite($logfile,"<br/>**************** Logging Ended at $time ****************<br/>");
fclose($logfile);
}
exit;
case ':!shutdown':
//Warn that logging has been disabled
if($config['warning']) {
$this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Disabled]");
}
if($config['logging']) {
$date = date("n-j-y");
$time = date('h:i:s A');
$logfile = fopen("$date-log.html","a");
fwrite($logfile,"<br/>**************** Logging Ended at $time ****************<br/>");
fclose($logfile);
}
exit;
}
$this->main($config);
}
/* --- IRCBot Class's Functions --- */
function filter_log($type, $chan, $nick, $msg)
{
$nick = ltrim($nick, ":");
$nick = substr($nick, 0, strpos($nick, "!"));
$msg = ltrim($msg, ":");
if($type == "PRIVMSG")
{
return date("[H:i]")." &amp;lt;".$nick."&amp;gt; ".$msg;
}
return null ;
}
function get_msg($arr)
{
$message = "";
for($i=3; $i <= (count($this->ex)); $i++)
{
$message .= $this->ex[$i]." ";
}
return $message;
}
function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
{
if($msg == null)
{
fputs($this->socket, $cmd."\r\n");
echo '<strong>'.$cmd.'</strong><br />';
} else {
fputs($this->socket, $cmd.' '.$msg."\r\n");
echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
}
}
function join_channel($channel) //Joins a channel, used in the join function.
{
if(is_array($channel))
{
foreach($channel as $chan)
{
$this->send_data('JOIN', $chan);
}
} else {
$this->send_data('JOIN', $channel);
}
}
function protect_user($user = '')
{
if($user == '')
{
if(php_version() >= '5.3.0')
{
$user = strstr($this->ex[0], '!', true);
} else {
$length = strstr($this->ex[0], '!');
$user = substr($this->ex[0], 0, $length);
}
}
$this->send_data('MODE', $this->ex[2] . ' +a ' . $user);
}
function op_user($channel = '', $user = '', $op = true) {
if($channel == '' || $user == '')
{
if($channel == '')
{
$channel = $this->ex[2];
}
if($user == '')
{
if(php_version() >= '5.3.0')
{
$user = strstr($this->ex[0], '!', true);
} else {
$length = strstr($this->ex[0], '!');
$user = substr($this->ex[0], 0, $length);
}
}
}
if($op)
{
$this->send_data('MODE', $channel . ' +o ' . $user);
} else {
$this->send_data('MODE', $channel . ' -o ' . $user);
}
}
}
//Start the bot
$bot = new IRCBot($config);
?>
Warning
This script uses PHP sockets, and most shared hosting and some servers have it disabled by default. This script runs continuously so it is not recommended to run on shared hosting accounts. We recommend you run it locally using the instructions below.
Conclusion
Thanks for trying out our Simple PHP IRC Logger. You can check out the base class here. If their are any bugs in the code please let us know right away. Also thanks to KillerAuzzie for the for tutorial/basecode.



