diff options
Diffstat (limited to 'chat/lib/class')
-rw-r--r-- | chat/lib/class/CustomAJAXChat.php | 186 |
1 files changed, 129 insertions, 57 deletions
diff --git a/chat/lib/class/CustomAJAXChat.php b/chat/lib/class/CustomAJAXChat.php index a18e640..5fad81f 100644 --- a/chat/lib/class/CustomAJAXChat.php +++ b/chat/lib/class/CustomAJAXChat.php @@ -5,36 +5,89 @@ * @copyright (c) Sebastian Tschan * @license Modified MIT License * @link https://blueimp.net/ajax/ + * + * MyBB integration: + * http://www.mybboard.net/ */ class CustomAJAXChat extends AJAXChat { + // Initialize custom configuration settings + function initCustomConfig() { + global $db; + + // Use the existing MyBB database connection: + $this->setConfig('dbConnection', 'link', $db->current_link); + } + + // Initialize custom request variables: + function initCustomRequestVars() { + global $mybb; + + // Auto-login MyBB users: + if(!$this->getRequestVar('logout') && $mybb->user['uid']) { + $this->setRequestVar('login', true); + } + } + + // Replace custom template tags: + function replaceCustomTemplateTags($tag, $tagContent) { + global $mybb; + + switch($tag) { + + case 'FORUM_LOGIN_URL': + if($mybb->user['uid']) { + return ($this->getRequestVar('view') == 'logs') ? './?view=logs' : './'; + } else { + return $mybb->settings['bburl'].'/member.php'; + } + + case 'REDIRECT_URL': + if($mybb->user['uid']) { + return ''; + } else { + return $this->htmlEncode($this->getRequestVar('view') == 'logs' ? $this->getChatURL().'?view=logs' : $this->getChatURL()); + } + + default: + return null; + } + } + + // Returns true if the userID of the logged in user is identical to the userID of the authentication system + // or the user is authenticated as guest in the chat and the authentication system + function revalidateUserID() { + global $mybb; + + if($this->getUserRole() === AJAX_CHAT_GUEST && !$mybb->user['uid'] || ($this->getUserID() === $mybb->user['uid'])) { + return true; + } + return false; + } + // Returns an associative array containing userName, userID and userRole // Returns null if login is invalid function getValidLoginUserData() { + global $mybb; - $customUsers = $this->getCustomUsers(); - - if($this->getRequestVar('password')) { - // Check if we have a valid registered user: - - $userName = $this->getRequestVar('userName'); - $userName = $this->convertEncoding($userName, $this->getConfig('contentEncoding'), $this->getConfig('sourceEncoding')); - - $password = $this->getRequestVar('password'); - $password = $this->convertEncoding($password, $this->getConfig('contentEncoding'), $this->getConfig('sourceEncoding')); - - foreach($customUsers as $key=>$value) { - if(($value['userName'] == $userName) && ($value['password'] == $password)) { - $userData = array(); - $userData['userID'] = $key; - $userData['userName'] = $this->trimUserName($value['userName']); - $userData['userRole'] = $value['userRole']; - return $userData; - } - } + // Check if we have a valid registered user: + if($mybb->user['uid']) { + $userData = array(); + $userData['userID'] = $mybb->user['uid']; + + $userData['userName'] = $this->trimUserName($mybb->user['username']); + + // Take the userrole from the MyBB users primary group: + if($mybb->user['usergroup'] == 4) + $userData['userRole'] = AJAX_CHAT_ADMIN; + else if($mybb->user['usergroup'] == 3) + $userData['userRole'] = AJAX_CHAT_MODERATOR; + else + $userData['userRole'] = AJAX_CHAT_USER; + + return $userData; - return null; } else { // Guest users: return $this->getGuestUser(); @@ -46,18 +99,13 @@ class CustomAJAXChat extends AJAXChat { function &getChannels() { if($this->_channels === null) { $this->_channels = array(); - - $customUsers = $this->getCustomUsers(); - - // Get the channels, the user has access to: - if($this->getUserRole() == AJAX_CHAT_GUEST) { - $validChannels = $customUsers[0]['channels']; - } else { - $validChannels = $customUsers[$this->getUserID()]['channels']; - } - - // Add the valid channels to the channel list (the defaultChannelID is always valid): - foreach($this->getAllChannels() as $key=>$value) { + + $allChannels = $this->getAllChannels(); + + // Build the forum permissions for all forums: + $forumPermissions = forum_permissions(); + + foreach($allChannels as $key=>$value) { if ($value == $this->getConfig('defaultChannelID')) { $this->_channels[$key] = $value; continue; @@ -66,7 +114,9 @@ class CustomAJAXChat extends AJAXChat { if($this->getConfig('limitChannelList') && !in_array($value, $this->getConfig('limitChannelList'))) { continue; } - if(in_array($value, $validChannels)) { + + // Add the valid channels to the channel list (the defaultChannelID is always valid): + if($forumPermissions[$value]['canview'] == '1' || $value == $this->getConfig('defaultChannelID')) { $this->_channels[$key] = $value; } } @@ -78,18 +128,32 @@ class CustomAJAXChat extends AJAXChat { // Make sure channel names don't contain any whitespace function &getAllChannels() { if($this->_allChannels === null) { - // Get all existing channels: - $customChannels = $this->getCustomChannels(); - + global $db; + + $this->_allChannels = array(); + + // Get all MyBB forums: + $sql = 'SELECT + fid, + name + FROM + '.TABLE_PREFIX.'forums + WHERE + type=\'f\';'; + $result = $db->query($sql); + $defaultChannelFound = false; - - foreach($customChannels as $name=>$id) { - $this->_allChannels[$this->trimChannelName($name)] = $id; - if($id == $this->getConfig('defaultChannelID')) { + + while ($row = $db->fetch_array($result)) { + $forumName = $this->trimChannelName($row['name']); + + $this->_allChannels[$forumName] = $row['fid']; + + if(!$defaultChannelFound && $row['fid'] == $this->getConfig('defaultChannelID')) { $defaultChannelFound = true; } } - + if(!$defaultChannelFound) { // Add the default channel as first array element to the channel list // First remove it in case it appeard under a different ID @@ -105,20 +169,28 @@ class CustomAJAXChat extends AJAXChat { return $this->_allChannels; } - function &getCustomUsers() { - // List containing the registered chat users: - $users = null; - require(AJAX_CHAT_PATH.'lib/data/users.php'); - return $users; - } - - function getCustomChannels() { - // List containing the custom channels: - $channels = null; - require(AJAX_CHAT_PATH.'lib/data/channels.php'); - // Channel array structure should be: - // ChannelName => ChannelID - return array_flip($channels); + // Method to set the style cookie depending on the MyBB user style + function setStyle() { + global $theme; + + if(isset($_COOKIE[$this->getConfig('sessionName').'_style']) && in_array($_COOKIE[$this->getConfig('sessionName').'_style'], $this->getConfig('styleAvailable'))) + return; + + $styleName = $theme['name']; + + if(!in_array($styleName, $this->getConfig('styleAvailable'))) { + $styleName = $this->getConfig('styleDefault'); + } + + setcookie( + $this->getConfig('sessionName').'_style', + $styleName, + time()+60*60*24*$this->getConfig('sessionCookieLifeTime'), + $this->getConfig('sessionCookiePath'), + $this->getConfig('sessionCookieDomain'), + $this->getConfig('sessionCookieSecure') + ); + return; } -}
\ No newline at end of file +} |