Ejemplo n.º 1
1
function tslink_update_uids($givenip)
{
    require __DIR__ . '/config.php';
    $messages = [];
    $messages[] = 'First we gonna update the user his TS uids in the database.';
    // 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.
    $mybb_user_query = "SELECT * FROM {$table} WHERE (HEX(lastip) = '{$mybb_ip}' OR HEX(regip) = '{$mybb_ip}') LIMIT 1";
    $messages['mybb_user_query'] = $mybb_user_query;
    $mybb_users = $ConnectDB->query($mybb_user_query) or trigger_error($ConnectDB->error . "[{$mybb_user_query}]");
    $mybb_user = $mybb_users->fetch_array(MYSQLI_ASSOC);
    $messages['found_user'] = '******' . $mybb_user['uid'] . ' Username: '******'username'];
    // Get the memberstatus from the user.
    $mybb_uid = $ConnectDB->real_escape_string($mybb_user['uid']);
    // 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['ts3_connect'] = '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';
                // Get the users from the teamspeak database.
                // Define how many records we want to query at once.
                // The maximum amount of records TeamSpeak will reply is 200.
                $maxaantalperque = 200;
                // Get the total amount of entries in the database.
                $DBClientEntriescount = $ts3->clientDbList($start = 0, $duration = 1, $count = true);
                foreach ($DBClientEntriescount['data'] as $clientindb) {
                    $DBClientEntries = $clientindb['count'];
                }
                $messages['ts3_DBClientEntries'] = $DBClientEntries;
                // Calculate how many times we have to do a query until we have all entries from the teamspeak database.
                $aantalqueries = $DBClientEntries / $maxaantalperque;
                $aantalqueries = ceil($aantalqueries);
                // Query the teamspeak database as many times as needed.
                $i = 1;
                while ($i <= $aantalqueries) {
                    if ($i == 1) {
                        $maxaantalvorige = 0;
                    }
                    $maxaantaldezeque = $i * $maxaantalperque;
                    try {
                        $ClientArrays[$i] = $ts3->clientDbList($start = $maxaantalvorige, $duration = $maxaantaldezeque, $count = false);
                    } catch (Exception $e) {
                        // Catches the error(s) if any. But don't do anything with it.
                    }
                    $maxaantalvorige = $maxaantaldezeque + 1;
                    $i++;
                }
                $messages['looking_for_ip'] = 'Start search in TS DB for entries with ip: ' . $givenip;
                // Lets see if we can find the user in the teamspeak database.
                foreach ($ClientArrays as $ClientArray) {
                    foreach ($ClientArray as $Clients) {
                        if (is_array($Clients) && count($Clients) > 0) {
                            foreach ($Clients as $ts3_Client) {
                                // Check if the user's ip address is known in the teamspeak database.
                                if (is_array($ts3_Client) && $ts3_Client['client_lastip'] == $givenip) {
                                    $ts3_client_found_on_ip = true;
                                    $messages['ts3_client_found_on_ip'][$ts3_Client['cldbid']] = $ts3_Client['client_unique_identifier'];
                                    try {
                                        // Put the user's client unique identifier and database id into the database for later usage.
                                        $ts_uid = $ConnectDB->real_escape_string($ts3_Client['client_unique_identifier']);
                                        $ts_cldbid = $ConnectDB->real_escape_string($ts3_Client['cldbid']);
                                        mysqli_query($ConnectDB, 'INSERT INTO ' . TABLE_PREFIX . "tslink_uids (`uid`, `ts_uid`, `ts_cldbid`) VALUES ('" . $mybb_uid . "', '" . $ts_uid . "', '" . $ts_cldbid . "')");
                                    } catch (Exception $e) {
                                        $messages['ts3_client_found_on_ip'][$ts3_Client['cldbid']]['error_saving_uid_to_db'] = $e;
                                        // Catches the error(s) if any. But don't do anything with it.
                                    }
                                }
                            }
                        }
                    }
                }
                if (!$ts3_client_found_on_ip) {
                    $messages['ts3_client_found_on_ip'] = 'No clients found in the TS database with the same IP.';
                }
            } 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_uids_update_debuglog'] = $ts3->getDebugLog();
    }
    // Close connection
    $ConnectDB->close();
    // Now we finally have all unique id's for this user's ip, let's update his groups
    $messages[] = tslink_update_groups($mybb_uid);
    return $messages;
}