public static function refreshAPIData() { /* * We need the Teamspeak 3 and GW2 API libraries */ require_once "library/Moturdrn/TS3GW2Auth/Includes/GW2_API_Tools/GW2_API_Tools.php"; require_once "library/Moturdrn/TS3GW2Auth/Includes/TeamSpeak3/ts3admin.class.php"; $tsAdmin = new ts3admin(XenForo_Application::getOptions()->ts3gw2auth_ts3server, XenForo_Application::getOptions()->ts3gw2auth_ts3serverquery); if ($tsAdmin->getElement('success', $tsAdmin->connect())) { $tsAdmin->login(XenForo_Application::getOptions()->ts3gw2auth_ts3username, XenForo_Application::getOptions()->ts3gw2auth_ts3password); $tsAdmin->selectServer(XenForo_Application::getOptions()->ts3gw2auth_ts3port); } $logArray = array(); /* * Create the Auth model for getting keys etc */ $authModel = XenForo_Model::create('Moturdrn_TS3GW2Auth_Model_Auth'); $checkSchedule = self::CHECK_EVERY_HOURS; $lastChecked_date = date('Y-m-d H:i:s', strtotime("-{$checkSchedule} hours")); $lastChecked = strtotime($lastChecked_date); $auths = $authModel->getAuthOlderThan($lastChecked); $checkDate = strtotime("now"); foreach ($auths as $auth) { $APIData = gw2_api_request('/v2/account', $auth['gw2_apikey']); $response = json_decode($APIData[count($APIData) - 1], true); /* API Returned OK - Key Valid */ if ($APIData[0] == 'HTTP/1.1 200 OK') { /** @var $authWriter Moturdrn_TS3GW2Auth_DataWriter_Auth * */ $authWriter = XenForo_DataWriter::create('Moturdrn_TS3GW2Auth_DataWriter_Auth'); $authWriter->setExistingData($auth['ts3_uniqueid']); $authWriter->set('gw2_name', $response['name']); $authWriter->set('gw2_account_guid', $response['id']); $authWriter->set('gw2_world', $response['world']); $authWriter->set('last_check', $checkDate); $authWriter->save(); /* API Returned Bad Request - Key Revoked */ } elseif ($APIData[0] == 'HTTP/1.1 400 Bad Request' && $response['text'] == 'invalid key') { /** @var $authWriter Moturdrn_TS3GW2Auth_DataWriter_Auth * */ $authWriter = XenForo_DataWriter::create('Moturdrn_TS3GW2Auth_DataWriter_Auth'); $authWriter->setExistingData($auth['ts3_uniqueid']); $authWriter->set('gw2_world', 0); $authWriter->save(); $logWriter = XenForo_DataWriter::create('Moturdrn_TS3GW2Auth_DataWriter_Log'); $logWriter->set('date', $checkDate); $logWriter->set('ts3_uniqueid', $auth['ts3_uniqueid']); $logWriter->set('ts3_dbid', $auth['ts3_dbid']); $logWriter->set('message', 'API Key Not Valid'); $logWriter->set('apikey', $auth['gw2_apikey']); $logWriter->set('apidata', serialize($APIData)); $logWriter->set('extradata', ''); $logWriter->save(); } } /* * Get Verified TS3 IDs and Unverified TS3 IDs */ $verified = $authModel->getAuthVerified(XenForo_Application::getOptions()->ts3gw2auth_worldid); $verifiedMembers = $currentMembers = $newMembers = $oldMembers = array(); foreach ($verified as $key => $addToGroup) { $verifiedMembers[] = $addToGroup['ts3_uniqueid']; } $group_members = $tsAdmin->serverGroupClientList(XenForo_Application::getOptions()->ts3gw2auth_verified_group, true); foreach ($group_members['data'] as $member) { $currentMembers[] = $member['client_unique_identifier']; } $newMembers = array_diff($verifiedMembers, $currentMembers); $oldMembers = array_diff($currentMembers, $verifiedMembers); foreach ($newMembers as $newMember) { $ts3_dbid = $tsAdmin->clientGetDbIdFromUid($newMember); if ($ts3_dbid['success'] == 1) { $tsAdmin->serverGroupAddClient(XenForo_Application::getOptions()->ts3gw2auth_verified_group, $ts3_dbid['data']['cldbid']); } } foreach ($oldMembers as $oldMember) { $ts3_dbid = $tsAdmin->clientGetDbIdFromUid($oldMember); if ($ts3_dbid['success'] == 1) { $tsAdmin->serverGroupDeleteClient(XenForo_Application::getOptions()->ts3gw2auth_verified_group, $ts3_dbid['data']['cldbid']); } } }
public function doAuthUnAuth($action, $ts3_dbid) { $this->_ts3group = XenForo_Application::getOptions()->ts3gw2auth_verified_group; $this->_ts3server = XenForo_Application::getOptions()->ts3gw2auth_ts3server; $this->_ts3port = XenForo_Application::getOptions()->ts3gw2auth_ts3port; $this->_ts3sqport = XenForo_Application::getOptions()->ts3gw2auth_ts3serverquery; $this->_ts3user = XenForo_Application::getOptions()->ts3gw2auth_ts3username; $this->_ts3pass = XenForo_Application::getOptions()->ts3gw2auth_ts3password; $this->_gw2world = XenForo_Application::getOptions()->ts3gw2auth_worldid; $this->_ts3prefix = XenForo_Application::getOptions()->ts3gw2auth_keyprefix; require_once "library/Moturdrn/TS3GW2Auth/Includes/TeamSpeak3/ts3admin.class.php"; $tsAdmin = new ts3admin($this->_ts3server, $this->_ts3sqport); if ($tsAdmin->getElement('success', $tsAdmin->connect())) { $tsAdmin->login($this->_ts3user, $this->_ts3pass); $tsAdmin->selectServer($this->_ts3port); } if ($action == 'Auth') { $tsAdmin->serverGroupAddClient($this->_ts3group, $ts3_dbid); } elseif ($action == 'UnAuth') { $tsAdmin->serverGroupDeleteClient($this->_ts3group, $ts3_dbid); } }
function tslink_update_groups($mybb_uid) { if (!isset($messages)) { $messages = []; } $messages[] = 'In the following part we gonna update the user his groups on the TS server.'; require __DIR__ . '/config.php'; // Connect to the database. $ConnectDB = new mysqli($hostname, $username, $password, $database); // check connection if ($ConnectDB->connect_errno) { die($ConnectDB->connect_error); } // Get the member from the mybb database. $getit = "SELECT * FROM {$table} WHERE uid = '{$mybb_uid}' LIMIT 1"; $messages['getit_query'] = $getit; $rows = $ConnectDB->query($getit); $row = $rows->fetch_array(MYSQLI_ASSOC); $messages['found_member'] = 'User ID: ' . $row['uid'] . ' Username: '******'username']; // Get the memberstatus from the user. $memberstatus = $row['memberstatus']; $messages['memberstatus'] = $memberstatus; // Let's determine which servergroup to use according to the status of the user. if ($memberstatus == '2') { $ServerGroupID_ToAdd = $ts3_sgid_vip_member; } elseif ($memberstatus == '1') { $ServerGroupID_ToAdd = $ts3_sgid_don_member; } else { $ServerGroupID_ToAdd = $ts3_sgid_member; } $messages['ServerGroupID_ToAdd'] = $ServerGroupID_ToAdd; // Get the user's unique id's from the mybb database $get_ts_uids = 'SELECT * FROM ' . TABLE_PREFIX . "tslink_uids WHERE uid = '{$mybb_uid}' "; $messages['get_ts_uids_query'] = $get_ts_uids; $ts_unique_ids = $ConnectDB->query($get_ts_uids); foreach ($ts_unique_ids as $ts_unique_id) { $messages['registered_ts_db_entries'][] = 'TS UID: ' . $ts_unique_id['ts_uid'] . ' TS CLDBID: ' . $ts_unique_id['ts_cldbid']; } $messages['groups_wont_be_removed'] = implode(',', $ts3_sgid_dont_remove); // Build a new ts3admin object. $ts3 = new ts3admin($ts3_server, $ts3_query_port); // Connect to the TS server. if ($ts3->getElement('success', $ts3->connect())) { $messages['ts_connection'] = 'Successful'; // Login to the TS server. if ($ts3->getElement('success', $ts3->login($ts3_username, $ts3_password))) { $messages['ts3_login'] = '******'; // Select virtual server. if ($ts3->getElement('success', $ts3->selectServer($ts3_server_port))) { $messages['ts3_virtual_server_select'] = 'Successful'; // Set displayed name in TS to given $ts3->setName($ts3_nickname); foreach ($ts_unique_ids as $ts_unique_id) { // First lets remove all groups the user is member of. // First get all servergroups the user is member of. $ClientServerGroups = $ts3->servergroupsbyclientid($ts_unique_id['ts_cldbid']); $csg = []; $c = 0; if (is_array($ClientServerGroups['data'])) { foreach ($ClientServerGroups['data'] as $ClientServerGroup) { $messages['found_groups']['CLDBID_' . $ts_unique_id['ts_cldbid']][] = 'sgid: ' . $ClientServerGroup['sgid'] . ' Name: ' . $ClientServerGroup['name']; } } // For every servergroup found, remove it. if (is_array($ClientServerGroups['data'])) { foreach ($ClientServerGroups['data'] as $Client_ServerGroup) { $csg[] = $Client_ServerGroup['sgid']; } } foreach ($csg as $ClientServerGroupID) { // Except for the servergroups we don't want to have removed. if (!in_array($ClientServerGroupID, $ts3_sgid_dont_remove)) { try { $messages['SGID_' . $ClientServerGroupID]['removing_from'][] = 'CLDBID: ' . $ts_unique_id['ts_cldbid']; $removeResults = $ts3->serverGroupDeleteClient($ClientServerGroupID, $ts_unique_id['ts_cldbid']); if ($removeResults['success']) { $messages['SGID_' . $ClientServerGroupID]['removing_from_result']['CLDBID_' . $ts_unique_id['ts_cldbid']] = 'Succes.'; } else { $messages['SGID_' . $ClientServerGroupID]['removing_from_result']['CLDBID_' . $ts_unique_id['ts_cldbid']] = $removeResults['errors']; } } catch (Exception $e) { // Catches the error(s) if any. But don't do anything with it. } } } $c++; try { // Add the user to the servergroup. $messages['SGID_' . $ServerGroupID_ToAdd]['adding_to'][] = 'CLDBID: ' . $ts_unique_id['ts_cldbid']; $serverGroupAddClientResults = $ts3->serverGroupAddClient($ServerGroupID_ToAdd, $ts_unique_id['ts_cldbid']); if ($serverGroupAddClientResults['success']) { $messages['SGID_' . $ServerGroupID_ToAdd]['adding_to_result']['CLDBID_' . $ts_unique_id['ts_cldbid']] = 'Succes.'; } else { $messages['SGID_' . $ServerGroupID_ToAdd]['adding_to_result']['CLDBID_' . $ts_unique_id['ts_cldbid']] = $serverGroupAddClientResults['errors']; } //$messages[$ServerGroupID_ToAdd]['adding_to_result'][] = $serverGroupAddClientResults; } catch (Exception $e) { // Catches the error(s) if any. But don't do anything with it. } } } else { echo '<p>Could not select the virtual server.</p> <p>Please check the TS server port in the config!</p> <p>Also make sure this (UDP) port is open in the outgoing firewall!</p>'; $messages['ts3_virtual_server_select'] = 'Could not select the virtual server.'; } } else { echo '<p>Could not login to the TS server.</p> <p>Please check the username and password in the config!</p>'; $messages['ts3_login'] = '******'; } } else { echo '<p>Connection to the TS server could not be established.</p> <p>Please check the TS server and TS server query port in the config!</p> <p>Also make sure this (TCP) port is open in the outgoing firewall!</p>'; $messages['ts3_connect'] = 'Connection to the TS server could not be established.'; } if (count($ts3->getDebugLog()) > 0) { $messages['ts3_add_remove_debuglog'] = $ts3->getDebugLog(); } // Close connection $ConnectDB->close(); return $messages; }