
<?php
date_default_timezone_set('Europe/Bucharest');
set_time_limit(0);
$conf = array(
	'serv' => 'irc.freenode.net',
	'port' => 6667,
	'nick' => 'greenb0x22',
	'real' => 'IRC.php :: greenb0x',
	'chan' => '#test123',
	'logs' => true,
);
class IRC
{
	var $socket;
	var $ex = array();
	function __construct($conf)
	{
		$this->socket = fsockopen($conf['serv'], 
$conf['port']);
		$this->login($conf);
		$this->main($conf);
	}
	function login($conf)
	{
		$this->send_data('USER', $conf['nick'] . ' greenb0x ' . 
$conf['nick'] . ' :' . $conf['real']);
		$this->send_data('NICK', $conf['nick']);
		$this->join_channel($conf['chan']);
		if($conf['logs']) {
			$date = date("Y-m-d");
			$time = date('h:i:s A');
			$logfile = fopen("$date-logs.html","a");
			fwrite($logfile,"<h2>Logs started at 
$time</h2><br>");
			fclose($logfile);
		}
	}
	function main($conf)
	{
		$data = fgets($this->socket, 256);
		echo nl2br($data);
		flush();
		$this->ex = explode(' ', $data);
		if ($this->ex[0] == 'PING')
		{
			$this->send_data('PONG', $this->ex[1]);
		}
		if($conf['logs'])
		{
			$logtxt = $this->filter_log($this->ex[1], 
$this->ex[2], $this->ex[0], $this->get_msg($this->ex));
			if($logtxt != null) {
				$date = date("Y-m-d");
				$logfile = 
fopen("$date-logs.html","a");
				fwrite($logfile,"$logtxt<br>");
				fclose($logfile);
			}
		}
		$command = str_replace(array(
			chr(10),
			chr(13)
		), '', $this->ex[3]);
		switch ($command)
		{
		case ':!j':
			$this->join_channel($this->ex[4]);
			break;
		case ':!p':
			$this->send_data('PART ' . $this->ex[4] . ' :', 
'Save earth to save life.');
			break;
		case ':!w':
			$message = "";
			for ($i = 5; $i <= (count($this->ex)); $i++)
			{
			$message.= $this->ex[$i] . " ";
			}
			$this->send_data('PRIVMSG ' . $this->ex[4] . ' 
:', $message);
			break;
		case ':!r':
			echo "<meta content=\"5\" 
http-equiv=\"refresh\" />";
			exit;
		case ':!q':
			if($conf['logs']) {
				$date = date("Y-m-d");
				$time = date('h:i:s A');
				$logfile = 
fopen("$date-logs.html","a");
				fwrite($logfile,"<h2>Logs ended at 
$time</h2><br>");
				fclose($logfile);
			}
			exit;
		}
		$this->main($conf);
	}
	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:s A]")." 
&lt;<strong>".$nick."</strong>&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)
	{
		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($chan)
	{
		if (is_array($chan))
		{
			foreach($chan as $channel)
			{
				$this->send_data('JOIN', $channel);
			}
		}
		else
		{
			$this->send_data('JOIN', $chan);
		}
	}
}
$irc = new IRC($conf);
?>
