I implemented PHP Free Chat as a replacement for Meebo on my page here:
http://yellowstone.paulthompson.net/camchat
And then I coded up a MySQL Log Proxy..... I must give credit to Moonbase as I learned from his example here:
http://www.phpfreechat.net/forum/viewtopic.php?id=2192.
Anyhow, here's the code:
- Code: Select all
<?php
/**
* mysqllog.class.php
*
* Copyright © 2010 Paul Thompson <captbunzo@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
require_once dirname(__FILE__)."/../pfci18n.class.php";
require_once dirname(__FILE__)."/../pfcuserconfig.class.php";
require_once dirname(__FILE__)."/../pfcproxycommand.class.php";
/**
* pfcProxyCommand_mysqllog
* this proxy will log channels and NO private messages to a mysql database.
* @author Paul Thompson <captbunzo@gmail.com>
*/
class pfcProxyCommand_mysqllog extends pfcProxyCommand
{
// Required config - no defaults
private $mysql_database;
private $mysql_username;
private $mysql_password;
// Optional config - provide defaults
private $mysql_host = 'localhost';
private $mysql_port = '3306';
private $mysql_prefix = '';
// Out database connection
private $db;
function run(&$xml_reponse, $p)
{
$cmdtocheck = array('send', 'me', 'notice');
if ( in_array($this->name, $cmdtocheck) )
{
$clientid = $p['clientid'];
$param = $p['param'];
$sender = $p['sender'];
$recipient = $p['recipient'];
$recipientid = $p['recipientid'];
$c =& pfcGlobalConfig::Instance();
$u =& pfcUserConfig::Instance();
// only log channels, no private communication (denoted by 'pv_...')
$channel = (substr($recipient,0,3) == 'ch_' ? substr($recipient,3) : '');
if ($channel != '') {
$this->init();
$this->log($channel, $sender, $param);
}
}
// forward the command to the next proxy or to the final command
return $this->next->run($xml_reponse, $p);
}
function init() {
$c =& pfcGlobalConfig::Instance();
// Directly assign required config
$this->mysql_database = $c->proxies_cfg[$this->proxyname]['database'];
$this->mysql_username = $c->proxies_cfg[$this->proxyname]['username'];
$this->mysql_password = $c->proxies_cfg[$this->proxyname]['password'];
// Check optional values
$mysql_host = $c->proxies_cfg[$this->proxyname]['host'];
$mysql_port = $c->proxies_cfg[$this->proxyname]['port'];
$mysql_prefix = $c->proxies_cfg[$this->proxyname]['prefix'];
if (!empty($mysql_host)) $this->mysql_host = $mysql_host;
if (!empty($mysql_port)) $this->mysql_port = $mysql_port;
if (!empty($mysql_prefix)) $this->mysql_prefix = $mysql_prefix;
// Attempt to setup the database connection
$db_connect_string = 'mysql:host=' . $this->mysql_host . ';'
. 'port=' . $this->mysql_port . ';'
. 'dbname=' . $this->mysql_database;
try {
$this->db = new PDO($db_connect_string, $this->mysql_username, $this->mysql_password);
} catch (PDOException $e) {
die( 'DB Connection Error: '. $e->getMessage() );
}
}
function log($channel, $nickname, $text) {
$insert_sql = "INSERT INTO " . $this->mysql_prefix . "chatlog
SET log_date = :log_date
, channel = :channel
, command = :command
, text = :text
, timestamp = :timestamp";
$input_parameters = array( 'log_date' => date('Y-m-d')
, 'channel' => $channel
, 'command' => $this->name
, 'text' => $text
, 'timestamp' => date('Y-m-d H:i:s') );
if (! (($this->name == 'notice') && preg_match('/quit (timeout)$/', $text)) ) {
$insert_sql .= ' , nickname = :nickname';
$input_parameters['nickname'] = $nickname;
}
$statement = $this->db->prepare($insert_sql);
$statement->execute($input_parameters);
}
}
/**
* SQL for building the table
CREATE TABLE IF NOT EXISTS chatlog (
log_id int(10) unsigned NOT NULL auto_increment,
log_date date NOT NULL,
channel varchar(31) character set utf8 NOT NULL,
nickname varchar(15) character set utf8 default NULL,
command enum('me','send','notice') character set utf8 NOT NULL,
`text` varchar(400) character set utf8 NOT NULL,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (log_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
*/
?>
This is configured as follows:
- Code: Select all
/* Required */
$params['proxies_cfg']['mysqllog'] = array();
$params['proxies_cfg']['mysqllog']['database'] = DATABASE_NAME;
$params['proxies_cfg']['mysqllog']['username'] = DATABASE_USERNAME;
$params['proxies_cfg']['mysqllog']['password'] = DATABASE_PASSWORD;
/* Optional */
$params['proxies_cfg']['mysqllog']['host'] = DATABASE_HOST_OR_IP; // default = localhost
$params['proxies_cfg']['mysqllog']['port'] = DATABASE_PORT; // default = 3306
$params['proxies_cfg']['mysqllog']['prefix'] = DATABASE_TABLE_PREFIX; // default = none
This could also be configured (which is how I prefer):
- Code: Select all
$params['proxies_cfg']['mysqllog'] = array(
'database' => DATABASE_NAME
, 'username' => DATABASE_USERNAME
, 'password' => DATABASE_PASSWORD
, 'host' => DATABASE_HOST_OR_IP // default = localhost
, 'port' => DATABASE_PORT // default = 3306
, 'prefix' => DATABASE_TABLE_PREFIX // default = none
);