/chat/lib/class/AJAXChat.php
PHP | 3326 lines | 2597 code | 404 blank | 325 comment | 522 complexity | 77ee1f287493c2e592c1e962da1ec648 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0, GPL-2.0, Apache-2.0, AGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /*
- * @package AJAX_Chat
- * @author Sebastian Tschan
- * @copyright (c) Sebastian Tschan
- * @license GNU Affero General Public License
- * @link https://blueimp.net/ajax/
- */
- // Ajax Chat backend logic:
- class AJAXChat {
- var $db;
- var $_config;
- var $_requestVars;
- var $_infoMessages;
- var $_channels;
- var $_allChannels;
- var $_view;
- var $_lang;
- var $_invitations;
- var $_customVars;
- var $_sessionNew;
- var $_onlineUsersData;
- var $_bannedUsersData;
-
- function AJAXChat() {
- $this->initialize();
- }
- function initialize() {
- // Initialize configuration settings:
- $this->initConfig();
- // Initialize the DataBase connection:
- $this->initDataBaseConnection();
- // Initialize request variables:
- $this->initRequestVars();
-
- // Initialize the chat session:
- $this->initSession();
-
- // Handle the browser request and send the response content:
- $this->handleRequest();
- }
- function initConfig() {
- $config = null;
- if (!include(AJAX_CHAT_PATH.'lib/config.php')) {
- echo('<strong>Error:</strong> Could not find a config.php file in "'.AJAX_CHAT_PATH.'"lib/". Check to make sure the file exists.');
- die();
- }
- $this->_config = &$config;
- // Initialize custom configuration settings:
- $this->initCustomConfig();
- }
-
- function initRequestVars() {
- $this->_requestVars = array();
- $this->_requestVars['ajax'] = isset($_REQUEST['ajax']) ? true : false;
- $this->_requestVars['userID'] = isset($_REQUEST['userID']) ? (int)$_REQUEST['userID'] : null;
- $this->_requestVars['userName'] = isset($_REQUEST['userName']) ? $_REQUEST['userName'] : null;
- $this->_requestVars['channelID'] = isset($_REQUEST['channelID']) ? (int)$_REQUEST['channelID'] : null;
- $this->_requestVars['channelName'] = isset($_REQUEST['channelName']) ? $_REQUEST['channelName'] : null;
- $this->_requestVars['text'] = isset($_POST['text']) ? $_POST['text'] : null;
- $this->_requestVars['lastID'] = isset($_REQUEST['lastID']) ? (int)$_REQUEST['lastID'] : 0;
- $this->_requestVars['login'] = isset($_REQUEST['login']) ? true : false;
- $this->_requestVars['logout'] = isset($_POST['logout']) ? true : false;
- $this->_requestVars['password'] = isset($_REQUEST['password']) ? $_REQUEST['password'] : null;
- $this->_requestVars['view'] = isset($_REQUEST['view']) ? $_REQUEST['view'] : null;
- $this->_requestVars['year'] = isset($_REQUEST['year']) ? (int)$_REQUEST['year'] : null;
- $this->_requestVars['month'] = isset($_REQUEST['month']) ? (int)$_REQUEST['month'] : null;
- $this->_requestVars['day'] = isset($_REQUEST['day']) ? (int)$_REQUEST['day'] : null;
- $this->_requestVars['hour'] = isset($_REQUEST['hour']) ? (int)$_REQUEST['hour'] : null;
- $this->_requestVars['search'] = isset($_REQUEST['search']) ? $_REQUEST['search'] : null;
- $this->_requestVars['shoutbox'] = isset($_REQUEST['shoutbox']) ? true : false;
- $this->_requestVars['getInfos'] = isset($_REQUEST['getInfos']) ? $_REQUEST['getInfos'] : null;
- $this->_requestVars['lang'] = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : null;
- $this->_requestVars['delete'] = isset($_REQUEST['delete']) ? (int)$_REQUEST['delete'] : null;
-
- // Initialize custom request variables:
- $this->initCustomRequestVars();
-
- // Remove slashes which have been added to user input strings if magic_quotes_gpc is On:
- if(get_magic_quotes_gpc()) {
- // It is safe to remove the slashes as we escape user data ourself
- array_walk(
- $this->_requestVars,
- create_function(
- '&$value, $key',
- 'if(is_string($value)) $value = stripslashes($value);'
- )
- );
- }
- }
-
- function initDataBaseConnection() {
- // Create a new database object:
- $this->db = new AJAXChatDataBase(
- $this->_config['dbConnection']
- );
- // Use a new database connection if no existing is given:
- if(!$this->_config['dbConnection']['link']) {
- // Connect to the database server:
- $this->db->connect($this->_config['dbConnection']);
- if($this->db->error()) {
- echo $this->db->getError();
- die();
- }
- // Select the database:
- $this->db->select($this->_config['dbConnection']['name']);
- if($this->db->error()) {
- echo $this->db->getError();
- die();
- }
- }
- // Unset the dbConnection array for safety purposes:
- unset($this->_config['dbConnection']);
- }
-
- function getDataBaseTable($table) {
- return ($this->db->getName() ? '`'.$this->db->getName().'`.'.$this->getConfig('dbTableNames',$table) : $this->getConfig('dbTableNames',$table));
- }
- function initSession() {
- // Start the PHP session (if not already started):
- $this->startSession();
- if($this->isLoggedIn()) {
- // Logout if we receive a logout request, the chat has been closed or the userID could not be revalidated:
- if($this->getRequestVar('logout') || !$this->isChatOpen() || !$this->revalidateUserID()) {
- $this->logout();
- return;
- }
- // Logout if the Session IP is not the same when logged in and ipCheck is enabled:
- if($this->getConfig('ipCheck') && ($this->getSessionIP() === null || $this->getSessionIP() != $_SERVER['REMOTE_ADDR'])) {
- $this->logout('IP');
- return;
- }
- } else if(
- // Login if auto-login enabled or a login, userName or shoutbox parameter is given:
- $this->getConfig('forceAutoLogin') ||
- $this->getRequestVar('login') ||
- $this->getRequestVar('userName') ||
- $this->getRequestVar('shoutbox')
- ) {
- $this->login();
- }
- // Initialize the view:
- $this->initView();
- if($this->getView() == 'chat') {
- $this->initChatViewSession();
- } else if($this->getView() == 'logs') {
- $this->initLogsViewSession();
- }
- if(!$this->getRequestVar('ajax') && !headers_sent()) {
- // Set style cookie:
- $this->setStyle();
- // Set langCode cookie:
- $this->setLangCodeCookie();
- }
-
- $this->initCustomSession();
- }
- function initLogsViewSession() {
- if($this->getConfig('socketServerEnabled')) {
- if(!$this->getSessionVar('logsViewSocketAuthenticated')) {
- $this->updateLogsViewSocketAuthentication();
- $this->setSessionVar('logsViewSocketAuthenticated', true);
- }
- }
- }
- function updateLogsViewSocketAuthentication() {
- if($this->getUserRole() != AJAX_CHAT_ADMIN) {
- $channels = array();
- foreach($this->getChannels() as $channel) {
- if($this->getConfig('logsUserAccessChannelList') && !in_array($channel, $this->getConfig('logsUserAccessChannelList'))) {
- continue;
- }
- array_push($channels, $channel);
- }
- array_push($channels, $this->getPrivateMessageID());
- array_push($channels, $this->getPrivateChannelID());
- } else {
- // The channelID "ALL" authenticates for all channels:
- $channels = array('ALL');
- }
- $this->updateSocketAuthentication(
- $this->getUserID(),
- $this->getSocketRegistrationID(),
- $channels
- );
- }
- function initChatViewSession() {
- // If channel is not null we are logged in to the chat view:
- if($this->getChannel() !== null) {
- // Check if the current user has been logged out due to inactivity:
- if(!$this->isUserOnline()) {
- $this->logout();
- return;
- }
- if($this->getRequestVar('ajax')) {
- $this->initChannel();
- $this->updateOnlineStatus();
- $this->checkAndRemoveInactive();
- }
- } else {
- if($this->getRequestVar('ajax')) {
- // Set channel, insert login messages and add to online list on first ajax request in chat view:
- $this->chatViewLogin();
- }
- }
- }
- function isChatOpen() {
- if($this->getUserRole() == AJAX_CHAT_ADMIN)
- return true;
- if($this->getConfig('chatClosed'))
- return false;
- $time = time();
- if($this->getConfig('timeZoneOffset') !== null) {
- // Subtract the server timezone offset and add the config timezone offset:
- $time -= date('Z', $time);
- $time += $this->getConfig('timeZoneOffset');
- }
- // Check the opening hours:
- if($this->getConfig('openingHour') < $this->getConfig('closingHour'))
- {
- if(($this->getConfig('openingHour') > date('G', $time)) || ($this->getConfig('closingHour') <= date('G', $time)))
- return false;
- }
- else
- {
- if(($this->getConfig('openingHour') > date('G', $time)) && ($this->getConfig('closingHour') <= date('G', $time)))
- return false;
- }
- // Check the opening weekdays:
- if(!in_array(date('w', $time), $this->getConfig('openingWeekDays')))
- return false;
- return true;
- }
- function handleRequest() {
- if($this->getRequestVar('ajax')) {
- if($this->isLoggedIn()) {
- // Parse info requests (for current userName, etc.):
- $this->parseInfoRequests();
-
- // Parse command requests (e.g. message deletion):
- $this->parseCommandRequests();
-
- // Parse message requests:
- $this->initMessageHandling();
- }
- // Send chat messages and online user list in XML format:
- $this->sendXMLMessages();
- } else {
- // Display XHTML content for non-ajax requests:
- $this->sendXHTMLContent();
- }
- }
- function parseCommandRequests() {
- if($this->getRequestVar('delete') !== null) {
- $this->deleteMessage($this->getRequestVar('delete'));
- }
- }
- function parseInfoRequests() {
- if($this->getRequestVar('getInfos')) {
- $infoRequests = explode(',', $this->getRequestVar('getInfos'));
- foreach($infoRequests as $infoRequest) {
- $this->parseInfoRequest($infoRequest);
- }
- }
- }
-
- function parseInfoRequest($infoRequest) {
- switch($infoRequest) {
- case 'userID':
- $this->addInfoMessage($this->getUserID(), 'userID');
- break;
- case 'userName':
- $this->addInfoMessage($this->getUserName(), 'userName');
- break;
- case 'userRole':
- $this->addInfoMessage($this->getUserRole(), 'userRole');
- break;
- case 'channelID':
- $this->addInfoMessage($this->getChannel(), 'channelID');
- break;
- case 'channelName':
- $this->addInfoMessage($this->getChannelName(), 'channelName');
- break;
- case 'socketRegistrationID':
- $this->addInfoMessage($this->getSocketRegistrationID(), 'socketRegistrationID');
- break;
- default:
- $this->parseCustomInfoRequest($infoRequest);
- }
- }
- function sendXHTMLContent() {
- $httpHeader = new AJAXChatHTTPHeader($this->getConfig('contentEncoding'), $this->getConfig('contentType'));
- $template = new AJAXChatTemplate($this, $this->getTemplateFileName(), $httpHeader->getContentType());
- // Send HTTP header:
- $httpHeader->send();
- // Send parsed template content:
- echo $template->getParsedContent();
- }
- function getTemplateFileName() {
- switch($this->getView()) {
- case 'chat':
- return AJAX_CHAT_PATH.'lib/template/loggedIn.html';
- case 'logs':
- return AJAX_CHAT_PATH.'lib/template/logs.html';
- default:
- return AJAX_CHAT_PATH.'lib/template/loggedOut.html';
- }
- }
- function initView() {
- $this->_view = null;
- // "chat" is the default view:
- $view = ($this->getRequestVar('view') === null) ? 'chat' : $this->getRequestVar('view');
- if($this->hasAccessTo($view)) {
- $this->_view = $view;
- }
- }
- function getView() {
- return $this->_view;
- }
- function hasAccessTo($view) {
- switch($view) {
- case 'chat':
- case 'teaser':
- if($this->isLoggedIn()) {
- return true;
- }
- return false;
- case 'logs':
- if($this->isLoggedIn() && ($this->getUserRole() == AJAX_CHAT_ADMIN ||
- ($this->getConfig('logsUserAccess') &&
- ($this->getUserRole() == AJAX_CHAT_MODERATOR || $this->getUserRole() == AJAX_CHAT_USER))
- )) {
- return true;
- }
- return false;
- default:
- return false;
- }
- }
-
- function login() {
- // Retrieve valid login user data (from request variables or session data):
- $userData = $this->getValidLoginUserData();
-
- if(!$userData) {
- $this->addInfoMessage('errorInvalidUser');
- return false;
- }
- // If the chat is closed, only the admin may login:
- if(!$this->isChatOpen() && $userData['userRole'] != AJAX_CHAT_ADMIN) {
- $this->addInfoMessage('errorChatClosed');
- return false;
- }
-
- if(!$this->getConfig('allowGuestLogins') && $userData['userRole'] == AJAX_CHAT_GUEST) {
- return false;
- }
- // Check if userID or userName are already listed online:
- if($this->isUserOnline($userData['userID']) || $this->isUserNameInUse($userData['userName'])) {
- if($userData['userRole'] == AJAX_CHAT_USER || $userData['userRole'] == AJAX_CHAT_MODERATOR || $userData['userRole'] == AJAX_CHAT_ADMIN) {
- // Set the registered user inactive and remove the inactive users so the user can be logged in again:
- $this->setInactive($userData['userID'], $userData['userName']);
- $this->removeInactive();
- } else {
- $this->addInfoMessage('errorUserInUse');
- return false;
- }
- }
-
- // Check if user is banned:
- if($userData['userRole'] != AJAX_CHAT_ADMIN && $this->isUserBanned($userData['userName'], $userData['userID'], $_SERVER['REMOTE_ADDR'])) {
- $this->addInfoMessage('errorBanned');
- return false;
- }
-
- // Check if the max number of users is logged in (not affecting moderators or admins):
- if(!($userData['userRole'] == AJAX_CHAT_MODERATOR || $userData['userRole'] == AJAX_CHAT_ADMIN) && $this->isMaxUsersLoggedIn()) {
- $this->addInfoMessage('errorMaxUsersLoggedIn');
- return false;
- }
- // Use a new session id (if session has been started by the chat):
- $this->regenerateSessionID();
- // Log in:
- $this->setUserID($userData['userID']);
- $this->setUserName($userData['userName']);
- $this->setLoginUserName($userData['userName']);
- $this->setUserRole($userData['userRole']);
- $this->setLoggedIn(true);
- $this->setLoginTimeStamp(time());
- // IP Security check variable:
- $this->setSessionIP($_SERVER['REMOTE_ADDR']);
- // The client authenticates to the socket server using a socketRegistrationID:
- if($this->getConfig('socketServerEnabled')) {
- $this->setSocketRegistrationID(
- md5(uniqid(rand(), true))
- );
- }
- // Add userID, userName and userRole to info messages:
- $this->addInfoMessage($this->getUserID(), 'userID');
- $this->addInfoMessage($this->getUserName(), 'userName');
- $this->addInfoMessage($this->getUserRole(), 'userRole');
- // Purge logs:
- if($this->getConfig('logsPurgeLogs')) {
- $this->purgeLogs();
- }
- return true;
- }
-
- function chatViewLogin() {
- $this->setChannel($this->getValidRequestChannelID());
- $this->addToOnlineList();
-
- // Add channelID and channelName to info messages:
- $this->addInfoMessage($this->getChannel(), 'channelID');
- $this->addInfoMessage($this->getChannelName(), 'channelName');
-
- // Login message:
- $text = '/login '.$this->getUserName();
- $this->insertChatBotMessage(
- $this->getChannel(),
- $text,
- null,
- 1
- );
- }
- function getValidRequestChannelID() {
- $channelID = $this->getRequestVar('channelID');
- $channelName = $this->getRequestVar('channelName');
- // Check the given channelID, or get channelID from channelName:
- if($channelID === null) {
- if($channelName !== null) {
- $channelID = $this->getChannelIDFromChannelName($channelName);
- // channelName might need encoding conversion:
- if($channelID === null) {
- $channelID = $this->getChannelIDFromChannelName(
- $this->trimChannelName($channelName, $this->getConfig('contentEncoding'))
- );
- }
- }
- }
- // Validate the resulting channelID:
- if(!$this->validateChannel($channelID)) {
- if($this->getChannel() !== null) {
- return $this->getChannel();
- }
- return $this->getConfig('defaultChannelID');
- }
- return $channelID;
- }
- function initChannel() {
- $channelID = $this->getRequestVar('channelID');
- $channelName = $this->getRequestVar('channelName');
- if($channelID !== null) {
- $this->switchChannel($this->getChannelNameFromChannelID($channelID));
- } else if($channelName !== null) {
- if($this->getChannelIDFromChannelName($channelName) === null) {
- // channelName might need encoding conversion:
- $channelName = $this->trimChannelName($channelName, $this->getConfig('contentEncoding'));
- }
- $this->switchChannel($channelName);
- }
- }
-
- function logout($type=null) {
- // Update the socket server authentication for the user:
- if($this->getConfig('socketServerEnabled')) {
- $this->updateSocketAuthentication($this->getUserID());
- }
- if($this->isUserOnline()) {
- $this->chatViewLogout($type);
- }
- $this->setLoggedIn(false);
- $this->destroySession();
- // Re-initialize the view:
- $this->initView();
- }
-
- function chatViewLogout($type) {
- $this->removeFromOnlineList();
- if($type !== null) {
- $type = ' '.$type;
- }
- // Logout message
- $text = '/logout '.$this->getUserName().$type;
- $this->insertChatBotMessage(
- $this->getChannel(),
- $text,
- null,
- 1
- );
- }
-
- function switchChannel($channelName) {
- $channelID = $this->getChannelIDFromChannelName($channelName);
- if($channelID !== null && $this->getChannel() == $channelID) {
- // User is already in the given channel, return:
- return;
- }
- // Check if we have a valid channel:
- if(!$this->validateChannel($channelID)) {
- // Invalid channel:
- $text = '/error InvalidChannelName '.$channelName;
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- $text
- );
- return;
- }
- $oldChannel = $this->getChannel();
- $this->setChannel($channelID);
- $this->updateOnlineList();
-
- // Channel leave message
- $text = '/channelLeave '.$this->getUserName();
- $this->insertChatBotMessage(
- $oldChannel,
- $text,
- null,
- 1
- );
- // Channel enter message
- $text = '/channelEnter '.$this->getUserName();
- $this->insertChatBotMessage(
- $this->getChannel(),
- $text,
- null,
- 1
- );
- $this->addInfoMessage($channelName, 'channelSwitch');
- $this->addInfoMessage($channelID, 'channelID');
- $this->_requestVars['lastID'] = 0;
- }
-
- function addToOnlineList() {
- $sql = 'INSERT INTO '.$this->getDataBaseTable('online').'(
- userID,
- userName,
- userRole,
- channel,
- dateTime,
- ip
- )
- VALUES (
- '.$this->db->makeSafe($this->getUserID()).',
- '.$this->db->makeSafe($this->getUserName()).',
- '.$this->db->makeSafe($this->getUserRole()).',
- '.$this->db->makeSafe($this->getChannel()).',
- NOW(),
- '.$this->db->makeSafe($this->ipToStorageFormat($_SERVER['REMOTE_ADDR'])).'
- );';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- $this->resetOnlineUsersData();
- }
-
- function removeFromOnlineList() {
- $sql = 'DELETE FROM
- '.$this->getDataBaseTable('online').'
- WHERE
- userID = '.$this->db->makeSafe($this->getUserID()).';';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- $this->removeUserFromOnlineUsersData();
- }
-
- function updateOnlineList() {
- $sql = 'UPDATE
- '.$this->getDataBaseTable('online').'
- SET
- userName = '.$this->db->makeSafe($this->getUserName()).',
- channel = '.$this->db->makeSafe($this->getChannel()).',
- dateTime = NOW(),
- ip = '.$this->db->makeSafe($this->ipToStorageFormat($_SERVER['REMOTE_ADDR'])).'
- WHERE
- userID = '.$this->db->makeSafe($this->getUserID()).';';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- $this->resetOnlineUsersData();
- }
-
- function initMessageHandling() {
- // Don't handle messages if we are not in chat view:
- if($this->getView() != 'chat') {
- return;
- }
- // Check if we have been uninvited from a private or restricted channel:
- if(!$this->validateChannel($this->getChannel())) {
- // Switch to the default channel:
- $this->switchChannel($this->getChannelNameFromChannelID($this->getConfig('defaultChannelID')));
- return;
- }
-
- if($this->getRequestVar('text') !== null) {
- $this->insertMessage($this->getRequestVar('text'));
- }
- }
-
- function insertParsedMessage($text) {
- // If a queryUserName is set, sent all messages as private messages to this userName:
- if($this->getQueryUserName() !== null && strpos($text, '/') !== 0) {
- $text = '/msg '.$this->getQueryUserName().' '.$text;
- }
-
- // Parse IRC-style commands:
- if(strpos($text, '/') === 0) {
- $textParts = explode(' ', $text);
- switch($textParts[0]) {
-
- // Channel switch:
- case '/join':
- $this->insertParsedMessageJoin($textParts);
- break;
-
- // Logout:
- case '/quit':
- $this->logout();
- break;
-
- // Private message:
- case '/msg':
- case '/describe':
- $this->insertParsedMessagePrivMsg($textParts);
- break;
-
- // Invitation:
- case '/invite':
- $this->insertParsedMessageInvite($textParts);
- break;
- // Uninvitation:
- case '/uninvite':
- $this->insertParsedMessageUninvite($textParts);
- break;
- // Private messaging:
- case '/query':
- $this->insertParsedMessageQuery($textParts);
- break;
-
- // Kicking offending users from the chat:
- case '/kick':
- $this->insertParsedMessageKick($textParts);
- break;
-
- // Listing banned users:
- case '/bans':
- $this->insertParsedMessageBans($textParts);
- break;
-
- // Unban user (remove from ban list):
- case '/unban':
- $this->insertParsedMessageUnban($textParts);
- break;
-
- // Describing actions:
- case '/me':
- case '/action':
- $this->insertParsedMessageAction($textParts);
- break;
- // Listing online Users:
- case '/who':
- $this->insertParsedMessageWho($textParts);
- break;
-
- // Listing available channels:
- case '/list':
- $this->insertParsedMessageList($textParts);
- break;
- // Retrieving the channel of a User:
- case '/whereis':
- $this->insertParsedMessageWhereis($textParts);
- break;
-
- // Listing information about a User:
- case '/whois':
- $this->insertParsedMessageWhois($textParts);
- break;
-
- // Rolling dice:
- case '/roll':
- $this->insertParsedMessageRoll($textParts);
- break;
- // Switching userName:
- case '/nick':
- $this->insertParsedMessageNick($textParts);
- break;
-
- // Custom or unknown command:
- default:
- if(!$this->parseCustomCommands($text, $textParts)) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UnknownCommand '.$textParts[0]
- );
- }
- }
- } else {
- // No command found, just insert the plain message:
- $this->insertCustomMessage(
- $this->getUserID(),
- $this->getUserName(),
- $this->getUserRole(),
- $this->getChannel(),
- $text
- );
- }
- }
- function insertParsedMessageJoin($textParts) {
- if(count($textParts) == 1) {
- // join with no arguments is the own private channel, if allowed:
- if($this->isAllowedToCreatePrivateChannel()) {
- // Private channels are identified by square brackets:
- $this->switchChannel($this->getChannelNameFromChannelID($this->getPrivateChannelID()));
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingChannelName'
- );
- }
- } else {
- $this->switchChannel($textParts[1]);
- }
- }
-
- function insertParsedMessagePrivMsg($textParts) {
- if($this->isAllowedToSendPrivateMessage()) {
- if(count($textParts) < 3) {
- if(count($textParts) == 2) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingText'
- );
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- }
- } else {
- // Get UserID from UserName:
- $toUserID = $this->getIDFromName($textParts[1]);
- if($toUserID === null) {
- if($this->getQueryUserName() !== null) {
- // Close the current query:
- $this->insertMessage('/query');
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- }
- } else {
- // Insert /privaction command if /describe is used:
- $command = ($textParts[0] == '/describe') ? '/privaction' : '/privmsg';
- // Copy of private message to current User:
- $this->insertCustomMessage(
- $this->getUserID(),
- $this->getUserName(),
- $this->getUserRole(),
- $this->getPrivateMessageID(),
- $command.'to '.$textParts[1].' '.implode(' ', array_slice($textParts, 2))
- );
- // Private message to requested User:
- $this->insertCustomMessage(
- $this->getUserID(),
- $this->getUserName(),
- $this->getUserRole(),
- $this->getPrivateMessageID($toUserID),
- $command.' '.implode(' ', array_slice($textParts, 2))
- );
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error PrivateMessageNotAllowed'
- );
- }
- }
-
- function insertParsedMessageInvite($textParts) {
- if($this->getChannel() == $this->getPrivateChannelID() || in_array($this->getChannel(), $this->getChannels())) {
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- $toUserID = $this->getIDFromName($textParts[1]);
- if($toUserID === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- // Add the invitation to the database:
- $this->addInvitation($toUserID);
- $invitationChannelName = $this->getChannelNameFromChannelID($this->getChannel());
- // Copy of invitation to current User:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/inviteto '.$textParts[1].' '.$invitationChannelName
- );
- // Invitation to requested User:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID($toUserID),
- '/invite '.$this->getUserName().' '.$invitationChannelName
- );
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error InviteNotAllowed'
- );
- }
- }
-
- function insertParsedMessageUninvite($textParts) {
- if($this->getChannel() == $this->getPrivateChannelID() || in_array($this->getChannel(), $this->getChannels())) {
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- $toUserID = $this->getIDFromName($textParts[1]);
- if($toUserID === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- // Remove the invitation from the database:
- $this->removeInvitation($toUserID);
- $invitationChannelName = $this->getChannelNameFromChannelID($this->getChannel());
- // Copy of uninvitation to current User:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/uninviteto '.$textParts[1].' '.$invitationChannelName
- );
- // Uninvitation to requested User:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID($toUserID),
- '/uninvite '.$this->getUserName().' '.$invitationChannelName
- );
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UninviteNotAllowed'
- );
- }
- }
-
- function insertParsedMessageQuery($textParts) {
- if($this->isAllowedToSendPrivateMessage()) {
- if(count($textParts) == 1) {
- if($this->getQueryUserName() !== null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/queryClose '.$this->getQueryUserName()
- );
- // Close the current query:
- $this->setQueryUserName(null);
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error NoOpenQuery'
- );
- }
- } else {
- if($this->getIDFromName($textParts[1]) === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- if($this->getQueryUserName() !== null) {
- // Close the current query:
- $this->insertMessage('/query');
- }
- // Open a query to the requested user:
- $this->setQueryUserName($textParts[1]);
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/queryOpen '.$textParts[1]
- );
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error PrivateMessageNotAllowed'
- );
- }
- }
-
- function insertParsedMessageKick($textParts) {
- // Only moderators/admins may kick users:
- if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- // Get UserID from UserName:
- $kickUserID = $this->getIDFromName($textParts[1]);
- if($kickUserID === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- // Check the role of the user to kick:
- $kickUserRole = $this->getRoleFromID($kickUserID);
- if($kickUserRole == AJAX_CHAT_ADMIN || ($kickUserRole == AJAX_CHAT_MODERATOR && $this->getUserRole() != AJAX_CHAT_ADMIN)) {
- // Admins and moderators may not be kicked:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error KickNotAllowed '.$textParts[1]
- );
- } else {
- // Kick user and insert message:
- $channel = $this->getChannelFromID($kickUserID);
- $banMinutes = (count($textParts) > 2) ? $textParts[2] : null;
- $this->kickUser($textParts[1], $banMinutes, $kickUserID);
- // If no channel found, user logged out before he could be kicked
- if($channel !== null) {
- $this->insertChatBotMessage(
- $channel,
- '/kick '.$textParts[1],
- null,
- 1
- );
- // Send a copy of the message to the current user, if not in the channel:
- if($channel != $this->getChannel()) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/kick '.$textParts[1],
- null,
- 1
- );
- }
- }
- }
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error CommandNotAllowed '.$textParts[0]
- );
- }
- }
-
- function insertParsedMessageBans($textParts) {
- // Only moderators/admins may see the list of banned users:
- if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
- $this->removeExpiredBans();
- $bannedUsers = $this->getBannedUsers();
- if(count($bannedUsers) > 0) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/bans '.implode(' ', $bannedUsers)
- );
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/bansEmpty -'
- );
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error CommandNotAllowed '.$textParts[0]
- );
- }
- }
-
- function insertParsedMessageUnban($textParts) {
- // Only moderators/admins may unban users:
- if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
- $this->removeExpiredBans();
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- if(!in_array($textParts[1], $this->getBannedUsers())) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- // Unban user and insert message:
- $this->unbanUser($textParts[1]);
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/unban '.$textParts[1]
- );
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error CommandNotAllowed '.$textParts[0]
- );
- }
- }
-
- function insertParsedMessageAction($textParts) {
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingText'
- );
- } else {
- if($this->getQueryUserName() !== null) {
- // If we are in query mode, sent the action to the query user:
- $this->insertMessage('/describe '.$this->getQueryUserName().' '.implode(' ', array_slice($textParts, 1)));
- } else {
- $this->insertCustomMessage(
- $this->getUserID(),
- $this->getUserName(),
- $this->getUserRole(),
- $this->getChannel(),
- implode(' ', $textParts)
- );
- }
- }
- }
-
- function insertParsedMessageWho($textParts) {
- if(count($textParts) == 1) {
- if($this->isAllowedToListHiddenUsers()) {
- // List online users from any channel:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/who '.implode(' ', $this->getOnlineUsers())
- );
- } else {
- // Get online users for all accessible channels:
- $channels = $this->getChannels();
- // Add the own private channel if allowed:
- if($this->isAllowedToCreatePrivateChannel()) {
- array_push($channels, $this->getPrivateChannelID());
- }
- // Add the invitation channels:
- foreach($this->getInvitations() as $channelID) {
- if(!in_array($channelID, $channels)) {
- array_push($channels, $channelID);
- }
- }
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/who '.implode(' ', $this->getOnlineUsers($channels))
- );
- }
- } else {
- $channelName = $textParts[1];
- $channelID = $this->getChannelIDFromChannelName($channelName);
- if(!$this->validateChannel($channelID)) {
- // Invalid channel:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error InvalidChannelName '.$channelName
- );
- } else {
- // Get online users for the given channel:
- $onlineUsers = $this->getOnlineUsers(array($channelID));
- if(count($onlineUsers) > 0) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/whoChannel '.$channelName.' '.implode(' ', $onlineUsers)
- );
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/whoEmpty -'
- );
- }
- }
- }
- }
-
- function insertParsedMessageList($textParts) {
- // Get the names of all accessible channels:
- $channelNames = $this->getChannelNames();
- // Add the own private channel, if allowed:
- if($this->isAllowedToCreatePrivateChannel()) {
- array_push($channelNames, $this->getPrivateChannelName());
- }
- // Add the invitation channels:
- foreach($this->getInvitations() as $channelID) {
- $channelName = $this->getChannelNameFromChannelID($channelID);
- if($channelName !== null && !in_array($channelName, $channelNames)) {
- array_push($channelNames, $channelName);
- }
- }
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/list '.implode(' ', $channelNames)
- );
- }
- function insertParsedMessageWhereis($textParts) {
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- // Get UserID from UserName:
- $whereisUserID = $this->getIDFromName($textParts[1]);
- if($whereisUserID === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- $channelID = $this->getChannelFromID($whereisUserID);
- if($this->validateChannel($channelID)) {
- $channelName = $this->getChannelNameFromChannelID($channelID);
- } else {
- $channelName = null;
- }
- if($channelName === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- // List user information:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/whereis '.$textParts[1].' '.$channelName
- );
- }
- }
- }
- }
-
- function insertParsedMessageWhois($textParts) {
- // Only moderators/admins:
- if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
- if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- // Get UserID from UserName:
- $whoisUserID = $this->getIDFromName($textParts[1]);
- if($whoisUserID === null) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameNotFound '.$textParts[1]
- );
- } else {
- // List user information:
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/whois '.$textParts[1].' '.$this->getIPFromID($whoisUserID)
- );
- }
- }
- } else {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error CommandNotAllowed '.$textParts[0]
- );
- }
- }
-
- function insertParsedMessageRoll($textParts) {
- if(count($textParts) == 1) {
- // default is one d6:
- $text = '/roll '.$this->getUserName().' 1d6 '.$this->rollDice(6);
- } else {
- $diceParts = explode('d', $textParts[1]);
- if(count($diceParts) == 2) {
- $number = (int)$diceParts[0];
- $sides = (int)$diceParts[1];
-
- // Dice number must be an integer between 1 and 100, else roll only one:
- $number = ($number > 0 && $number <= 100) ? $number : 1;
-
- // Sides must be an integer between 1 and 100, else take 6:
- $sides = ($sides > 0 && $sides <= 100) ? $sides : 6;
-
- $text = '/roll '.$this->getUserName().' '.$number.'d'.$sides.' ';
- for($i=0; $i<$number; $i++) {
- if($i != 0)
- $text .= ',';
- $text .= $this->rollDice($sides);
- }
- } else {
- // if dice syntax is invalid, roll one d6:
- $text = '/roll '.$this->getUserName().' 1d6 '.$this->rollDice(6);
- }
- }
- $this->insertChatBotMessage(
- $this->getChannel(),
- $text
- );
- }
-
- function insertParsedMessageNick($textParts) {
- if(!$this->getConfig('allowNickChange') ||
- (!$this->getConfig('allowGuestUserName') && $this->getUserRole() == AJAX_CHAT_GUEST)) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error CommandNotAllowed '.$textParts[0]
- );
- } else if(count($textParts) == 1) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MissingUserName'
- );
- } else {
- $newUserName = implode(' ', array_slice($textParts, 1));
- if($newUserName == $this->getLoginUserName()) {
- // Allow the user to regain the original login userName:
- $prefix = '';
- $suffix = '';
- } else if($this->getUserRole() == AJAX_CHAT_GUEST) {
- $prefix = $this->getConfig('guestUserPrefix');
- $suffix = $this->getConfig('guestUserSuffix');
- } else {
- $prefix = $this->getConfig('changedNickPrefix');
- $suffix = $this->getConfig('changedNickSuffix');
- }
- $maxLength = $this->getConfig('userNameMaxLength')
- - $this->stringLength($prefix)
- - $this->stringLength($suffix);
- $newUserName = $this->trimString($newUserName, 'UTF-8', $maxLength, true);
- if(!$newUserName) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error InvalidUserName'
- );
- } else {
- $newUserName = $prefix.$newUserName.$suffix;
- if($this->isUserNameInUse($newUserName)) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error UserNameInUse'
- );
- } else {
- $oldUserName = $this->getUserName();
- $this->setUserName($newUserName);
- $this->updateOnlineList();
- // Add info message to update the client-side stored userName:
- $this->addInfoMessage($this->getUserName(), 'userName');
- $this->insertChatBotMessage(
- $this->getChannel(),
- '/nick '.$oldUserName.' '.$newUserName,
- null,
- 2
- );
- }
- }
- }
- }
-
- function insertMessage($text) {
- if(!$this->isAllowedToWriteMessage())
- return;
- if(!$this->floodControl())
- return;
- $text = $this->trimMessageText($text);
- if($text == '')
- return;
-
- if(!$this->onNewMessage($text))
- return;
-
- $text = $this->replaceCustomText($text);
-
- $this->insertParsedMessage($text);
- }
- function deleteMessage($messageID) {
- // Retrieve the channel of the given message:
- $sql = 'SELECT
- channel
- FROM
- '.$this->getDataBaseTable('messages').'
- WHERE
- id='.$this->db->makeSafe($messageID).';';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- $row = $result->fetch();
-
- if($row['channel'] !== null) {
- $channel = $row['channel'];
-
- if($this->getUserRole() == AJAX_CHAT_ADMIN) {
- $condition = '';
- } else if($this->getUserRole() == AJAX_CHAT_MODERATOR) {
- $condition = ' AND
- NOT (userRole='.$this->db->makeSafe(AJAX_CHAT_ADMIN).')
- AND
- NOT (userRole='.$this->db->makeSafe(AJAX_CHAT_CHATBOT).')';
- } else if($this->getUserRole() == AJAX_CHAT_USER && $this->getConfig('allowUserMessageDelete')) {
- $condition = 'AND
- (
- userID='.$this->db->makeSafe($this->getUserID()).'
- OR
- (
- channel = '.$this->db->makeSafe($this->getPrivateMessageID()).'
- OR
- channel = '.$this->db->makeSafe($this->getPrivateChannelID()).'
- )
- AND
- NOT (userRole='.$this->db->makeSafe(AJAX_CHAT_ADMIN).')
- AND
- NOT (userRole='.$this->db->makeSafe(AJAX_CHAT_CHATBOT).')
- )';
- } else {
- return false;
- }
-
- // Remove given message from the database:
- $sql = 'DELETE FROM
- '.$this->getDataBaseTable('messages').'
- WHERE
- id='.$this->db->makeSafe($messageID).'
- '.$condition.';';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- if($result->affectedRows() == 1) {
- // Insert a deletion command to remove the message from the clients chatlists:
- $this->insertChatBotMessage($channel, '/delete '.$messageID);
- return true;
- }
- }
- return false;
- }
-
- function floodControl() {
- // Moderators and Admins need no flood control:
- if($this->getUserRole() == AJAX_CHAT_MODERATOR || $this->getUserRole() == AJAX_CHAT_ADMIN) {
- return true;
- }
- $time = time();
- // Check the time of the last inserted message:
- if($this->getInsertedMessagesRateTimeStamp()+60 < $time) {
- $this->setInsertedMessagesRateTimeStamp($time);
- $this->setInsertedMessagesRate(1);
- } else {
- // Increase the inserted messages rate:
- $rate = $this->getInsertedMessagesRate()+1;
- $this->setInsertedMessagesRate($rate);
- // Check if message rate is too high:
- if($rate > $this->getConfig('maxMessageRate')) {
- $this->insertChatBotMessage(
- $this->getPrivateMessageID(),
- '/error MaxMessageRate'
- );
- // Return false so the message is not inserted:
- return false;
- }
- }
- return true;
- }
-
- function isAllowedToWriteMessage() {
- if($this->getUserRole() != AJAX_CHAT_GUEST)
- return true;
- if($this->getConfig('allowGuestWrite'))
- return true;
- return false;
- }
- function insertChatBotMessage($channelID, $messageText, $ip=null, $mode=0) {
- $this->insertCustomMessage(
- $this->getConfig('chatBotID'),
- $this->getConfig('chatBotName'),
- AJAX_CHAT_CHATBOT,
- $channelID,
- $messageText,
- $ip,
- $mode
- );
- }
-
- function insertCustomMessage($userID, $userName, $userRole, $channelID, $text, $ip=null, $mode=0) {
- // The $mode parameter is used for socket updates:
- // 0 = normal messages
- // 1 = channel messages (e.g. login/logout, channel enter/leave, kick)
- // 2 = messages with online user updates (nick)
-
- $ip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
-
- $sql = 'INSERT INTO '.$this->getDataBaseTable('messages').'(
- userID,
- userName,
- userRole,
- channel,
- dateTime,
- ip,
- text
- )
- VALUES (
- '.$this->db->makeSafe($userID).',
- '.$this->db->makeSafe($userName).',
- '.$this->db->makeSafe($userRole).',
- '.$this->db->makeSafe($channelID).',
- NOW(),
- '.$this->db->makeSafe($this->ipToStorageFormat($ip)).',
- '.$this->db->makeSafe($text).'
- );';
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- if($this->getConfig('socketServerEnabled')) {
- $this->sendSocketMessage(
- $this->getSocketBroadcastMessage(
- $this->db->getLastInsertedID(),
- time(),
- $userID,
- $userName,
- $userRole,
- $channelID,
- $text,
- $mode
- )
- );
- }
- }
- function getSocketBroadcastMessage(
- $messageID,
- $timeStamp,
- $userID,
- $userName,
- $userRole,
- $channelID,
- $text,
- $mode
- ) {
- // The $mode parameter:
- // 0 = normal messages
- // 1 = channel messages (e.g. login/logout, channel enter/leave, kick)
- // 2 = messages with online user updates (nick)
- // Get the message XML content:
- $xml = '<root chatID="'.$this->getConfig('socketServerChatID').'" channelID="'.$channelID.'" mode="'.$mode.'">';
- if($mode) {
- // Add the list of online users if the user list has been updated ($mode > 0):
- $xml .= $this->getChatViewOnlineUsersXML(array($channelID));
- }
- if($mode != 1 || $this->getConfig('showChannelMessages')) {
- $xml .= '<messages>';
- $xml .= $this->getChatViewMessageXML(
- $messageID,
- $timeStamp,
- $userID,
- $userName,
- $userRole,
- $channelID,
- $text
- );
- $xml .= '</messages>';
- }
- $xml .= '</root>';
- return $xml;
- }
- function sendSocketMessage($message) {
- // Open a TCP socket connection to the socket server:
- if($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) {
- if(@socket_connect($socket, $this->getConfig('socketServerIP'), $this->getConfig('socketServerPort'))) {
- // Append a null-byte to the string as EOL (End Of Line) character
- // which is required by Flash XML socket communication:
- $message .= "\0";
- @socket_write(
- $socket,
- $message,
- strlen($message) // Using strlen to count the bytes instead of the number of UTF-8 characters
- );
- }
- @socket_close($socket);
- }
- }
- function updateSocketAuthentication($userID, $socketRegistrationID=null, $channels=null) {
- // If no $socketRegistrationID or no $channels are given the authentication is removed for the given user:
- $authentication = '<authenticate chatID="'.$this->getConfig('socketServerChatID').'" userID="'.$userID.'" regID="'.$socketRegistrationID.'">';
- if($channels) {
- foreach($channels as $channelID) {
- $authentication .= '<channel id="'.$channelID.'"/>';
- }
- }
- $authentication .= '</authenticate>';
- $this->sendSocketMessage($authentication);
- }
- function setSocketRegistrationID($value) {
- $this->setSessionVar('SocketRegistrationID', $value);
- }
-
- function getSocketRegistrationID() {
- return $this->getSessionVar('SocketRegistrationID');
- }
-
- function rollDice($sides) {
- // seed with microseconds since last "whole" second:
- mt_srand((double)microtime()*1000000);
-
- return mt_rand(1, $sides);
- }
-
- function kickUser($userName, $banMinutes=null, $userID=null) {
- if($userID === null) {
- $userID = $this->getIDFromName($userName);
- }
- if($userID === null) {
- return;
- }
- $banMinutes = $banMinutes ? $banMinutes : $this->getConfig('defaultBanTime');
- if($banMinutes) {
- // Ban User for the given time in minutes:
- $this->banUser($userName, $banMinutes, $userID);
- }
- // Remove given User from online list:
- $sql = 'DELETE FROM
- '.$this->getDataBaseTable('online').'
- WHERE
- userID = '.$this->db->makeSafe($userID).';';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
- // Update the socket server authentication for the kicked user:
- if($this->getConfig('socketServerEnabled')) {
- $this->updateSocketAuthentication($userID);
- }
-
- $this->removeUserFromOnlineUsersData($userID);
- }
- function getBannedUsersData($key=null, $value=null) {
- if($this->_bannedUsersData === null) {
- $this->_bannedUsersData = array();
- $sql = 'SELECT
- userID,
- userName,
- ip
- FROM
- '.$this->getDataBaseTable('bans').'
- WHERE
- NOW() < dateTime;';
-
- // Create a new SQL query:
- $result = $this->db->sqlQuery($sql);
-
- // Stop if an error occurs:
- if($result->error()) {
- echo $result->getError();
- die();
- }
-
- while($row = $result->fetch()) {
- $row['ip'] = $this->ipFromStorageFormat($row['ip']);
- array_push($this->_bannedUsersData, $row);
- }
-
- $result->free();
- }
-
- if($key) {
- $bannedUsersData = array();
- foreach($this->_bannedUsersData as $bannedUserData) {
- if(!isset($bannedUserData[$key])) {
- return $bannedUsersData;
- }
- if($value) {
- if($bannedUserData[$key] == $value) {
- array_push($bannedUsersData, $bannedUserData);
- } else {
- continue;
- }
- } else {
- array_push($bannedUsersData, $bannedUserData[$key]);
- }
- }
- return $bannedUsersData;
- }
-
- return $this->_bannedUsersData;
- }
-
- function getBannedUsers() {
- return $this->getBannedUsersData('userName');
- }
-
- function banUser($userName, $banMinutes=null, $userID=null) {
- if($userID === null) {
- $userID = $this->getIDFromName($userName);
- }
- $ip = $this->getIPFromID($userID);
- if(!$ip || $userID === null) {
- return;
- }
- // Remove expired bans:
- $this->removeExpiredBans();
-
- $banMinutes = (int)$banMinutes;
- if(!$banMinutes) {
- // If banMinutes is not a valid integer, use the defaultBanTime:
- $banMinutes = $this->getConfig('defaultBanTime');
- }
-
- $sql = 'INSERT INTO '.$this->getDataBaseTable('bans').'(
- userID,
- userName,
- dateTime,
- ip
- )
- VALUES (
- '.$this->db->makeSafe($userID).',
- '.$this->db->makeSafe($userName).',
- DATE_ADD(NOW(), interval '.$this->db->makeSafe($banMinutes).' MINUTE),
- '.$this->db->makeSafe($this->ipToStorageFormat($ip)).'
- );';
-
- // Create a new SQL query:
- $r…
Large files files are truncated, but you can click here to view the full file