📄 connection.php
字号:
<?php
class Connection {
var $clientId = null;
var $id = null;
var $userid = null;
var $roomid = null;
var $color = null;
var $state = 1;
var $start = 0;
var $lang = 'en';
var $ip = '';
var $tzoffset = 0;
var $messageQueue;
function Connection($id = null, $args = array()) {
$this->messageQueue = new MessageQueue();
//clientId is id of client in socket Server
$this->clientId = $args['clientId'];
if( $id )
{
if( isset($GLOBALS['socket_server']) && ($args['ip'] != $GLOBALS['fc_config']['bot_ip']))
{
$this->id = $args['id'];
$this->userid = $args['userid'];
$this->roomid = $args['roomid'];
$this->color = $args['color'];
$this->state = $args['state'];
$this->start = $args['start'];
$this->lang = $args['lang'];
$this->ip = $args['ip'];
$this->tzoffset = $args['tzoffset'];
//Touch room
$this->updateSelfRoom();
return;
}
$stmt = new Statement("SELECT * FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE id=? LIMIT 1");
$rs = $stmt->process($id);
if($rec = $rs->next())
{
$this->id = $rec['id'];
$this->userid = $rec['userid'];
$this->roomid = $rec['roomid'];
$this->color = $rec['color'];
$this->state = $rec['state'];
$this->start = $rec['start'];
$this->lang = $rec['lang'];
$this->ip = $rec['ip'];
$this->tzoffset = $rec['tzoffset'];
//Touch connection
$stmt = new Statement("UPDATE {$GLOBALS['fc_config']['db']['pref']}connections SET updated=NOW() WHERE id=?");
$stmt->process($this->id);
//Touch room
$this->updateSelfRoom();
return;
}
}
$this->id = md5(uniqid(rand(), true));
$this->userid = ChatServer::isLoggedIn( $args );
$this->roomid = $this->getAvailableRoom($GLOBALS['fc_config']['defaultRoom']);
$color = $GLOBALS['fc_config']['themes'][$GLOBALS['fc_config']['defaultTheme']]['recommendedUserColor'];
if($color == null) $color = 0xFFFFFF;
$this->color = $color;
$this->state = 1;
$this->lang = $GLOBALS['fc_config']['defaultLanguage'];
$this->ip = ($args['ip'] == null)? $_SERVER['REMOTE_ADDR'] : $args['ip'];
//socketServer implementation save 'tzoffset'
if( isset($GLOBALS['socket_server'] ) && !($this->userid < 0) )
{
if(isset($args['tz'])) $this->tzoffset = $args['tz'];
$GLOBALS['socket_server']->saveClientConnection( $this->clientId, $this->getData() );
}
if($this->userid > 0 && $this->userid != null)
{
$this->start = $this->sendLoginInfo();
}
else if($args['c'] != 'lin')
{
$this->start = $this->sendBack(new Message('lout', null, null, 'login'));
}
//socketServer implementation save 'start'
if( isset($GLOBALS['socket_server']) && !($this->userid < 0) )
{
$GLOBALS['socket_server']->saveClientConnection( $this->clientId, $this->getData() );
}
$stmt = new Statement("INSERT INTO {$GLOBALS['fc_config']['db']['pref']}connections (id, updated, created, userid, roomid, color, state, start, lang, ip) VALUES (?, NOW(), NOW(), ?, ?, ?, ?, ?, ?, ?)");
$ret = $stmt->process($this->id, $this->userid, $this->roomid, $this->color, $this->state, $this->start, $this->lang, $this->ip);
}
function updateSelfRoom()
{
// next line fix for default room touch
if($this->roomid == $GLOBALS['fc_config']['defaultRoom']) return;
$stmt = new Statement("UPDATE {$GLOBALS['fc_config']['db']['pref']}rooms SET updated=NOW() WHERE id=?");
$stmt->process($this->roomid);
}
function getAvailableRoom($roomId)
{
$stmt = new Statement("SELECT id FROM {$GLOBALS['fc_config']['db']['pref']}rooms");
$rs = $stmt->process();
$retval = 0;
while($rec = $rs->next())
{
if($retval == 0) $retval = $rec['id'];
if($rec['id'] == $roomId)
{
$retval = $roomId;
break;
}
}
return ($retval);
}
function getData()
{
$data = array(
'clientId' => $this->clientId,
'id' => $this->id,
'userid' => $this->userid,
'roomid' => $this->roomid,
'color' => $this->color,
'state' => $this->state,
'start' => $this->start,
'lang' => $this->lang,
'ip' => $this->ip,
'tzoffset' => $this->tzoffset
);
return $data;
}
function save() {
//socketServer implementation
if( isset($GLOBALS['socket_server']) && !($this->userid < 0) )
{
$GLOBALS['socket_server']->saveClientConnection( $this->clientId, $this->getData() );
}
$stmt = new Statement("UPDATE {$GLOBALS['fc_config']['db']['pref']}connections SET updated=NOW(), userid=?, roomid=?, color=?, state=?, start=?, lang=?, ip=?, tzoffset=? WHERE id=?");
$stmt->process($this->userid, $this->roomid, $this->color, $this->state, $this->start, $this->lang, $this->ip, $this->tzoffset, $this->id);
}
function send($message) {
//Spy can send messages back to him self only
if(ChatServer::userInRole($this->userid, ROLE_SPY)) {
$message->toconnid = $this->id;
$message->touserid = null;
$message->toroomid = null;
}
//socketServer implementation
if( isset($GLOBALS['socket_server']) )
{
$GLOBALS['socket_server']->sendMessage( $message );
//if socket server then write only 'msg' messages
//if(!in_array($message->command, array('msg', 'adu', 'mvu', 'rmu', 'lout', 'glng', 'lng'))) return;
}
return $this->messageQueue->addMessage( $message );
}
function sendBack($message) {
$message->toconnid = $this->id;
return $this->send($message);
}
function sendToUser($userid, $message) {
$stmt = new Statement("SELECT * FROM {$GLOBALS['fc_config']['db']['pref']}ignors WHERE userid=? AND ignoreduserid=?");
if(
($rs = $stmt->process($userid, $this->userid)) && $rs->hasNext() &&
$message->command != 'ignu' && $message->command != 'nignu'
)
{
$this->sendBack(new Message('error', $userid, 0, 'ignored'));
} else {
switch($message->command) {
case 'nignu':
case 'ignu':
case 'msg':
$message->toconnid = $this->id;
break;
}
/*
$stmt = new Statement("SELECT id FROM {$GLOBALS['fc_config']['db']['pref']}users WHERE roles=?");
if(($rs = $stmt->process(ROLE_ADMIN)))
{
while($rs->hasNext())
{
$i = $rs->next();
$message->touserid = $i['id'];
if($message->touserid != $this->userid && $message->touserid != $userid ) $this->send($message);
}
}
*/
$message->touserid = $userid;
return $this->send($message);
}
}
function sendToAll($message) {
$message->toconnid = null;
$message->touserid = null;
$message->toroomid = null;
return $this->send($message);
}
function sendToRoom($roomid, $message) {
$message->toroomid = $roomid;
return $this->send($message);
}
function process($req) {
//Set default values for missed request params
if(!isset($req['c'])) $req['c'] = 'msgl'; //command
if(!isset($req['u'])) $req['u'] = null; //userId or UserName
if(!isset($req['r'])) $req['r'] = null; //roomId
if(!isset($req['b'])) $req['b'] = 0; //backtime
if(!isset($req['t'])) $req['t'] = ''; //text
if(!isset($req['l'])) $req['l'] = null; //language
if(!isset($req['p'])) $req['p'] = 0; //is public room
if(!isset($req['lg'])) $req['lg'] = ''; //login
if(!isset($req['ps'])) $req['ps'] = ''; //password
if(!isset($req['n'])) $req['n'] = 0; //???
if(!isset($req['a'])) $req['a'] = ''; // additional arguments
if(!isset($req['s'])) $req['s'] = 0; //???
if(!isset($req['tz'])) $req['tz'] = 0; //timezone
if(get_magic_quotes_gpc())
{
foreach($req as $k => $v) $req[$k] = stripslashes($v);
}
if($req['c'] == 'lin')
{
//Try to login
$this->doLogin($req['lg'], $req['ps'], $req['l'], $req['tz'], $req['r'], $req['bot_ip']);
}
else if($req['c'] == 'tzset')
{
$this->doTimeZoneSet($req['tz']);
}
else if($this->userid)
{
//Process request
switch($req['c']) {
case 'msgl' : $this->doLoadMessages(); break;
case 'lout' : $this->doLogout(); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -