Example #1
0
function xfac_edit_user_profile_update($wpUserId)
{
    $config = xfac_option_getConfig();
    if (empty($config)) {
        return;
    }
    if (!empty($_POST['xfac_disconnect'])) {
        foreach ($_POST['xfac_disconnect'] as $recordId => $confirmed) {
            if ($confirmed) {
                $record = xfac_user_getRecordById($recordId);
                if ($record->user_id == $wpUserId) {
                    xfac_user_deleteRecord($record);
                }
            }
        }
    }
    if (!empty($_POST['xfac_connect'])) {
        $xfUserId = intval($_POST['xfac_connect']);
        if ($xfUserId > 0) {
            $adminAccessToken = xfac_user_getAdminAccessToken($config);
            if (!empty($adminAccessToken)) {
                $userAccessToken = xfac_api_postOauthTokenAdmin($config, $adminAccessToken, $xfUserId);
                if (!empty($userAccessToken)) {
                    $result = xfac_api_getUsersMe($config, $userAccessToken['access_token']);
                    if (!empty($result['user']['user_id'])) {
                        xfac_syncLogin_syncRole($config, get_user_by('id', $wpUserId), $result['user']);
                        if (isset($_POST['role'])) {
                            // because we have already sync'd role, ignore role submitted via POST
                            unset($_POST['role']);
                        }
                        xfac_user_updateRecord($wpUserId, $config['root'], $xfUserId, $result['user'], $userAccessToken);
                    }
                }
            }
        }
    }
}
Example #2
0
function xfac_option_getMeta($config)
{
    static $rebuiltCount = 0;
    if (empty($config)) {
        return array();
    }
    $meta = get_option('xfac_meta');
    $rebuild = false;
    if (empty($meta) or empty($meta['linkIndex'])) {
        $rebuild = true;
    } else {
        foreach ($config as $configKey => $configValue) {
            if (empty($meta[$configKey]) or $meta[$configKey] !== $configValue) {
                $rebuild = true;
                break;
            }
        }
    }
    $xfAdminAccountOption = intval(get_option('xfac_xf_admin_account'));
    $xfAdminAccountMeta = empty($meta['xfac_xf_admin_account']) ? 0 : intval($meta['xfac_xf_admin_account']);
    if ($xfAdminAccountMeta !== $xfAdminAccountOption) {
        $rebuild = true;
    }
    if ($rebuild and !empty($_REQUEST['oauth_token'])) {
        // looks like admin enter WordPress url as the root, abort rebuilding
        $rebuild = false;
    }
    if ($rebuild and $rebuiltCount > 0) {
        // we rebuild once, only retry if $meta is empty
        if (!empty($meta)) {
            $rebuild = false;
        }
    }
    if ($rebuild) {
        xfac_updateNotice('xf_guest_account');
        xfac_updateNotice('xf_admin_account');
        $meta = $config;
        $meta['linkIndex'] = xfac_api_getPublicLink($config, 'index');
        $meta['modules'] = array();
        $meta['forums'] = array();
        if (!empty($meta['linkIndex'])) {
            if ($xfAdminAccountOption) {
                $adminAccessToken = xfac_user_getAdminAccessToken($config);
                if (empty($adminAccessToken)) {
                    // unable to obtain admin access token
                    // probably a missing record or expired refresh token
                    // reset the option
                    update_option('xfac_xf_admin_account', 0);
                    $xfAdminAccountOption = 0;
                }
            }
            $xfGuestAccountOption = intval(get_option('xfac_xf_guest_account'));
            if ($xfGuestAccountOption) {
                $guestAccessToken = xfac_user_getSystemAccessToken($config);
                if (empty($guestAccessToken)) {
                    // unable to obtain guest access token
                    // probably an expired refresh token
                    // reset the option
                    update_option('xfac_xf_guest_account', 0);
                } else {
                    $mappedTags = xfac_syncPost_getMappedTags();
                    if (!empty($mappedTags)) {
                        // make sures the guest account follows required forums
                        // and have the needed notification subscription
                        xfac_syncPost_followForums($config, $guestAccessToken, array_keys($mappedTags));
                    }
                }
            }
            $meta['modules'] = xfac_api_getModules($config);
            $meta['linkAlerts'] = xfac_api_getPublicLink($config, 'account/alerts');
            $meta['linkConversations'] = xfac_api_getPublicLink($config, 'conversations');
            $meta['linkLogin'] = xfac_api_getPublicLink($config, 'login');
            $meta['linkLoginLogin'] = xfac_api_getPublicLink($config, 'login/login');
            $meta['linkRegister'] = xfac_api_getPublicLink($config, 'register');
            $forums = xfac_api_getForums($config);
            if (!empty($forums['forums'])) {
                $meta['forums'] = $forums['forums'];
            }
            $meta['xfac_xf_admin_account'] = $xfAdminAccountOption;
            if (!empty($meta['xfac_xf_admin_account'])) {
                $userGroups = xfac_api_getUserGroups($config, 0, xfac_user_getAdminAccessToken($config));
                if (!empty($userGroups['user_groups'])) {
                    $meta['userGroups'] = $userGroups['user_groups'];
                }
            }
        }
        $rebuiltCount++;
        update_option('xfac_meta', $meta);
        xfac_log('xfac_option_getMeta rebuilt $meta=%s', $meta);
    }
    return $meta;
}
Example #3
0
function xfac_profile_update_user_pass($wpUserId)
{
    if (empty($GLOBALS['_xfac_syncLogin_pending_user_pass'])) {
        // no data
        return;
    }
    $pending = $GLOBALS['_xfac_syncLogin_pending_user_pass'];
    if (!is_array($pending) || count($pending) != 2) {
        // data unrecognized
        return;
    }
    if ($pending[0] != $wpUserId) {
        // user_id not matched
        return;
    }
    $config = xfac_option_getConfig();
    if (empty($config)) {
        // no config
        return;
    }
    $adminAccessToken = xfac_user_getAdminAccessToken($config);
    if (empty($adminAccessToken)) {
        // no admin access token
        return;
    }
    $records = xfac_user_getRecordsByUserId($wpUserId);
    if (empty($records)) {
        // no user record
        return null;
    }
    $record = reset($records);
    xfac_api_postUserPassword($config, $adminAccessToken, $record->identifier, $pending[1]);
    xfac_log('xfac_profile_update_user_pass pushed password for $wpUser (#%d)', $wpUserId);
}
Example #4
0
function xfac_tools_connect()
{
    /** @var wpdb $wpdb */
    global $wpdb;
    $config = xfac_option_getConfig();
    if (empty($config)) {
        wp_die(__('XenForo API configuration is missing.', 'xenforo-api-consumer'));
    }
    $adminAccessToken = xfac_user_getAdminAccessToken($config);
    if (empty($adminAccessToken)) {
        wp_die(__('Admin Account\'s access token cannot be obtained.', 'xenforo-api-consumer'));
    }
    if (!xfac_api_hasModuleVersion($config, 'forum', 2015030901) || !xfac_api_hasModuleVersion($config, 'oauth2', 2015030902)) {
        wp_die(__('Please update XenForo API to run this tool.', 'xenforo-api-consumer'));
    }
    $optionFilters = array('position' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0), 'limit' => array('filter' => FILTER_VALIDATE_INT, 'default' => 10), 'associate' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0), 'push' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0));
    $options = array();
    foreach ($optionFilters as $optionKey => $optionFilter) {
        $optionValue = filter_input(INPUT_GET, $optionKey, $optionFilter['filter']);
        if (!empty($optionValue)) {
            $options[$optionKey] = $optionValue;
        } else {
            $options[$optionKey] = $optionFilter['default'];
        }
    }
    if (empty($options['associate']) && empty($options['push'])) {
        wp_die(__('At least one action must be selected: either associate or push', 'xenforo-api-consumer'));
    }
    $maxWpUserIds = $wpdb->get_var('SELECT MAX(ID) FROM ' . $wpdb->prefix . 'users');
    if ($options['position'] >= $maxWpUserIds) {
        die(__('Done.', 'xenforo-api-consumer'));
    }
    $dbUsers = $wpdb->get_results('
        SELECT *
        FROM ' . $wpdb->prefix . 'users
        WHERE ID > ' . $options['position'] . '
        LIMIT ' . $options['limit']);
    foreach ($dbUsers as $dbUser) {
        $user = new WP_User($dbUser);
        $options['position'] = max($options['position'], $user->ID);
        $records = xfac_user_getRecordsByUserId($user->ID);
        if (!empty($records)) {
            // this user has connected
            continue;
        }
        printf(__('Processing user #%d (%s)', 'xenforo-api-consumer'), $user->ID, $user->user_login);
        echo "<br />\n";
        $candidates = array();
        $userLoginUsers = xfac_api_getUsersFind($config, $user->user_login);
        if (!empty($userLoginUsers['users'])) {
            foreach ($userLoginUsers['users'] as $userLoginUser) {
                // similar logic with includes/dashboard/profile.php
                if (strlen($userLoginUser['username']) == strlen($user->user_login)) {
                    $candidates[$userLoginUser['user_id']] = $userLoginUser;
                }
            }
        }
        $emailUsers = xfac_api_getUsersFind($config, '', $user->user_email, $adminAccessToken);
        if (!empty($emailUsers['users'])) {
            foreach ($emailUsers['users'] as $emailUser) {
                $candidates[$emailUser['user_id']] = $emailUser;
            }
        }
        if (!empty($candidates) && !empty($options['associate'])) {
            foreach ($candidates as $candidate) {
                $userAccessToken = xfac_api_postOauthTokenAdmin($config, $adminAccessToken, $candidate['user_id']);
                if (!empty($userAccessToken)) {
                    xfac_syncLogin_syncRole($config, $user, $candidate, false);
                    xfac_user_updateRecord($user->ID, $config['root'], $candidate['user_id'], $candidate, $userAccessToken);
                    xfac_log('xfac_tools_connect associated $wpUser (#%d) vs. $xfUser (#%d)', $user->ID, $candidate['user_id']);
                } else {
                    $errors = xfac_api_getLastErrors();
                    if (!is_array($errors)) {
                        $errors = array(__('Unknown error', 'xenforo-api-consumer'));
                    }
                    xfac_log('xfac_tools_connect failed to associate $wpUser (#%d) vs. $xfUser (#%d): %s', $user->ID, $candidate['user_id'], implode(', ', $errors));
                }
            }
        }
        if (empty($candidates) && !empty($options['push'])) {
            $result = xfac_api_postUser($config, $user->user_email, $user->user_login, '', array('oauth_token' => $adminAccessToken));
            if (!empty($result)) {
                $xfUser = $result['user'];
                $token = $result['token'];
                xfac_syncLogin_syncRole($config, $user, $xfUser, false);
                xfac_user_updateRecord($user->ID, $config['root'], $xfUser['user_id'], $xfUser, $token);
                xfac_log('xfac_tools_connect pushed $wpUser (#%d)', $user->ID);
            } else {
                $errors = xfac_api_getLastErrors();
                if (!is_array($errors)) {
                    $errors = array(__('Unknown error', 'xenforo-api-consumer'));
                }
                xfac_log('xfac_tools_connect failed to push $wpUser (#%d): %s', $user->ID, implode(', ', $errors));
            }
        }
    }
    $optionsStr = '';
    foreach ($options as $optionKey => $optionValue) {
        if ($optionValue !== $optionFilters[$optionKey]['default']) {
            $optionsStr .= sprintf('&%s=%s', $optionKey, rawurlencode($optionValue));
        }
    }
    die(sprintf('<script>window.location = "%s";</script>', admin_url(sprintf('tools.php?action=xfac_tools_connect%s', $optionsStr))));
}
Example #5
0
function xfac_options_init()
{
    if (!empty($_REQUEST['do'])) {
        switch ($_REQUEST['do']) {
            case 'xfac_xf_guest_account':
                require xfac_template_locateTemplate('dashboard_xfac_xf_guest_account.php');
                return;
        }
    }
    // prepare common data
    $config = xfac_option_getConfig();
    $meta = array();
    if (!empty($config)) {
        $meta = xfac_option_getMeta($config);
    }
    $currentWpUser = wp_get_current_user();
    $currentWpUserRecords = xfac_user_getRecordsByUserId($currentWpUser->ID);
    $adminAccessToken = xfac_user_getAdminAccessToken($config);
    // setup sections
    $sections = array(array('id' => 'xfac_api', 'title' => __('API Configuration', 'xenforo-api-consumer')));
    if (!empty($meta['linkIndex'])) {
        $sections = array_merge($sections, array(array('id' => 'xfac_post_comment', 'title' => __('Post & Comment', 'xenforo-api-consumer')), array('id' => 'xfac_user_role', 'title' => __('User & Role', 'xenforo-api-consumer')), array('id' => 'xfac_ui', 'title' => __('Appearances', 'xenforo-api-consumer'))));
    }
    // always show advanced sections
    $sections[] = array('id' => 'xfac_advanced', 'title' => __('Advanced', 'xenforo-api-consumer'));
    // setup tabs
    $tab = 'xfac_api';
    if (!empty($_REQUEST['tab'])) {
        $tab = 'xfac_' . $_GET['tab'];
    }
    $sectionFound = false;
    foreach ($sections as $section) {
        if ($section['id'] === $tab) {
            $sectionFound = true;
        }
    }
    if (!$sectionFound) {
        $firstSection = reset($sections);
        $tab = $firstSection['id'];
    }
    // prepare section's data
    switch ($tab) {
        case 'xfac_api':
            $xfGuestRecords = xfac_user_getRecordsByUserId(0);
            $xfAdminRecords = $currentWpUserRecords;
            $configuredAdminRecord = null;
            $xfAdminAccountOption = intval(get_option('xfac_xf_admin_account'));
            if ($xfAdminAccountOption > 0) {
                $configuredAdminRecord = xfac_user_getRecordById($xfAdminAccountOption);
                if (!empty($configuredAdminRecord)) {
                    $found = false;
                    foreach ($xfAdminRecords as $xfAdminRecord) {
                        if ($xfAdminRecord->id == $configuredAdminRecord->id) {
                            $found = true;
                        }
                    }
                    if (!$found) {
                        $xfAdminRecords[] = $configuredAdminRecord;
                    }
                }
            }
            break;
        case 'xfac_post_comment':
            $hourlyNext = wp_next_scheduled('xfac_cron_hourly');
            $tagForumMappings = get_option('xfac_tag_forum_mappings');
            if (!is_array($tagForumMappings)) {
                $tagForumMappings = array();
            }
            $tags = get_terms('post_tag', array('hide_empty' => false));
            break;
        case 'xfac_user_role':
            $syncRoleOption = get_option('xfac_sync_role');
            if (!is_array($syncRoleOption)) {
                $syncRoleOption = array();
            }
            break;
        case 'xfac_ui':
            $optionTopBarForums = get_option('xfac_top_bar_forums');
            if (!is_array($optionTopBarForums)) {
                $optionTopBarForums = array();
            }
            break;
    }
    require xfac_template_locateTemplate('dashboard_options.php');
}
Example #6
0
function xfac_user_getAccessTokenForRecord($record)
{
    $token = $record->token;
    if (!empty($token['expire_date']) && $token['expire_date'] > time()) {
        return $token['access_token'];
    }
    $config = xfac_option_getConfig();
    if (empty($config)) {
        return null;
    }
    if (empty($newToken)) {
        // try to refresh and get new token
        if (!empty($token['refresh_token']) && (!isset($token['refresh_token_expire_date']) || $token['refresh_token_expire_date'] > time())) {
            $newToken = xfac_api_getAccessTokenFromRefreshToken($config, $token['refresh_token']);
        }
    }
    if (empty($newToken)) {
        // try to get new token with admin token
        // of course do not attempt that if the current record IS the admin record
        $xfAdminAccountOption = intval(get_option('xfac_xf_admin_account'));
        if ($xfAdminAccountOption > 0 && $record->id != $xfAdminAccountOption) {
            $adminAccessToken = xfac_user_getAdminAccessToken($config);
            if (!empty($adminAccessToken)) {
                $newToken = xfac_api_postOauthTokenAdmin($config, $adminAccessToken, $record->identifier);
            }
        }
    }
    if (empty($newToken)) {
        return null;
    }
    xfac_user_updateRecord($record->user_id, $config['root'], $record->identifier, $record->profile, $newToken);
    return $newToken['access_token'];
}