📄 chatserver.php
字号:
<?php
$GLOBALS['curruserid'] = array();
class ChatServer {
//User handlers
function isLoggedIn( $args = array() ) {
if( $GLOBALS['curruserid'][$GLOBALS['clientId']] == SPY_USERID )
{
return SPY_USERID;
}
else if( $args['ip'] == $GLOBALS['fc_config']['bot_ip'] && $GLOBALS['fc_config']['enableBots'])
{
return $GLOBALS['fc_config']['bot']->getBotId( $args['login'] );
}
else
{
if( isset($GLOBALS['socket_server']) )
{
$uid = $GLOBALS['socket_server']->clientInfo[$GLOBALS['clientId']]['connection']['userid'];
if(isset($uid)) return $uid;
}
return $GLOBALS['fc_config']['cms']->isLoggedIn();
}
}
function login($login, $password, $args = array()) {
if($password == $GLOBALS['fc_config']['spyPassword'])
{
$GLOBALS['curruserid'][$GLOBALS['clientId']] = SPY_USERID;
}
else if( $args['ip'] == $GLOBALS['fc_config']['bot_ip'] && $GLOBALS['fc_config']['enableBots'])
{
$GLOBALS['curruserid'][$GLOBALS['clientId']] = $GLOBALS['fc_config']['bot']->getBotId( $login );
}
else
{
//$classname = get_class($GLOBALS['fc_config']['cms']);
//toLog('class', $classname);
//if( $GLOBALS['fc_config']['enableSocketServer'] ) $GLOBALS['fc_config']['cms'] = new PhpBB2CMS();
$GLOBALS['curruserid'][$GLOBALS['clientId']] = $GLOBALS['fc_config']['cms']->login($login, $password);
}
return $GLOBALS['curruserid'][$GLOBALS['clientId']];
}
function logout() {
$GLOBALS['curruserid'][$GLOBALS['clientId']] = 0;
$GLOBALS['fc_config']['cms']->logout();
}
function getUser( $userid ) {
if( $userid == SPY_USERID )
{
return array('id' => SPY_USERID, 'login' => 'spy', 'roles' => ROLE_SPY);
}
else if( $userid != null && $userid < SPY_USERID && $GLOBALS['fc_config']['enableBots'])
{
return( $GLOBALS['fc_config']['bot']->getUser($userid) );
}
else
{
return $GLOBALS['fc_config']['cms']->getUser($userid);
}
}
function getUsers( ) {
return $GLOBALS['fc_config']['cms']->getUsers();
}
//temporary function
function getGender($userid)
{
if($userid <= SPY_USERID)
{
return '';
}
else
{
$ret = '';
if( method_exists($GLOBALS['fc_config']['cms'], 'getGender') )
{
$ret = $GLOBALS['fc_config']['cms']->getGender($userid);
}
else
{
$ret = 'U';//Note: if metod not exists default user is undefined
}
$ret = (trim($ret) == '') ? 'U' : trim($ret);
return $ret;
}
}
function getUserProfile($userid) {
return $GLOBALS['fc_config']['cms']->getUserProfile($userid);
}
function userInRole($userid, $role) {
if($userid == SPY_USERID)
{
return (ROLE_SPY & $role) != 0;
}
else if( $userid != null && $userid < SPY_USERID && $GLOBALS['fc_config']['enableBots'])
{
$user = $GLOBALS['fc_config']['bot']->getUser($userid);
return( $role == $user['role'] );
}
else
{
return $GLOBALS['fc_config']['cms']->userInRole($userid, $role);
}
}
//Connecton handlers
function &getConnection($req = array(), $clientId = null) {
$args = array();
if(!isset($req['id']) || !$req['id'])
$req['id'] = null;
if( isset($GLOBALS['socket_server']) && $clientId !== null)
$args = array_merge($req, $GLOBALS['socket_server']->clientInfo[$clientId]['connection']);
if( isset($req['bot_ip']) )
$args = array_merge($args, array( 'ip' => $req['bot_ip'], 'login' => $req['login']));
return new Connection( $req['id'], $args );
}
function addUser($login, $password, $roles){
return $GLOBALS['fc_config']['cms']->addUser($login, $password, $roles);
}
function deleteUser($login){
$GLOBALS['fc_config']['cms']->deleteUser($login);
}
function writeToFile($arr, $path)
{
$data = serialize($arr);
$file = @fopen( $path, "wb" );
if ( ! $file ) return;
if ( ! flock($file, LOCK_EX) ) return;
$res = fwrite($file, $data);
fflush($file);
fclose($file);
}
function readFromFile($path)
{
$file = @fopen( $path, "rb" );
if ( ! $file ) return;
$data = fread( $file, filesize( $path ) ) ;
fclose($file);
return (unserialize($data));
}
function purgeExpired() {
$file_path = $GLOBALS['fc_config']['appdata_path'];
$arr = array();
if( file_exists( $file_path ) && filesize( $file_path ) > 0 )
{
$arr = ChatServer::readFromFile($file_path);
}
else
{
$arr['time'] = time();
ChatServer::writeToFile($arr, $file_path);
}
if((time() - $arr['time']) > $GLOBALS['fc_config']['msgRequestInterval'])
{
$arr['time'] = time() + 3600;
ChatServer::writeToFile($arr, $file_path);
ChatServer::purge();
//write time to file
$arr['time'] = time();
ChatServer::writeToFile($arr, $file_path);
}
}
function purge()
{
$bot_ip = $GLOBALS['fc_config']['bot_ip'];
if($GLOBALS['fc_config']['enableBots'] == false)
{
$stmt = new Statement("DELETE FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE ip = ?");
$stmt->process($bot_ip);
}
else
{
$stmt = new Statement("UPDATE {$GLOBALS['fc_config']['db']['pref']}connections SET updated=NOW() WHERE userid IS NOT NULL AND ip=?");
$stmt->process($bot_ip);
$GLOBALS['fc_config']['bot']->processOptions();
if( $GLOBALS['fc_config']['enableSocketServer'] ) $GLOBALS['fc_config']['bot']->processMessages();
}
//Do all we need
if( !$GLOBALS['fc_config']['enableSocketServer'] )
{
//Close expired connection
$stmt = new Statement("DELETE FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE updated < DATE_SUB(NOW(),INTERVAL ? SECOND)");
$stmt->process($GLOBALS['fc_config']['autocloseAfter']);
//Logout expired users
$stmt = new Statement("SELECT id FROM {$GLOBALS['fc_config']['db']['pref']}connections WHERE userid IS NOT NULL AND updated < DATE_SUB(NOW(),INTERVAL ? SECOND) AND ip <> ?");
if($rs = $stmt->process($GLOBALS['fc_config']['autologoutAfter'], $bot_ip)) {
while($rec = $rs->next()) {
$conn = new Connection($rec['id']);
$conn->doLogout('expiredlogin');
}
}
}
//Remove expired rooms
$stmt = new Statement("SELECT id FROM {$GLOBALS['fc_config']['db']['pref']}rooms WHERE ispermanent IS NULL AND updated < DATE_SUB(NOW(),INTERVAL ? SECOND)");
$rmst = new Statement("DELETE FROM {$GLOBALS['fc_config']['db']['pref']}rooms WHERE ispermanent IS NULL AND id=?");
if($rs = $stmt->process($GLOBALS['fc_config']['autoremoveAfter'])) {
$messageQueue = new MessageQueue();
while($room = $rs->next()) {
$msg = new Message('rmr', null, $room['id']);
$messageQueue->addMessage( $msg );
if( isset($GLOBALS['socket_server']) ) $GLOBALS['socket_server']->sendMessage( $msg );
$rmst->process($room['id']);
}
}
//Remove expired messages
$rmst = new Statement("DELETE FROM {$GLOBALS['fc_config']['db']['pref']}messages WHERE created < DATE_SUB(NOW(),INTERVAL ? SECOND)");
$rmst->process($GLOBALS['fc_config']['msgRemoveAfter']);
//Remove expired bans
$rmst = new Statement("DELETE FROM {$GLOBALS['fc_config']['db']['pref']}bans WHERE created < DATE_SUB(NOW(),INTERVAL ? SECOND)");
$rmst->process($GLOBALS['fc_config']['autounbanAfter']);
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -