Ejemplo n.º 1
0
 * This example differs from others in that it will reattempt a request if
 * the timestamp is detected to be off from the Twitter servers.
 *
 * Instructions:
 * 1) If you don't have one already, create a Twitter application on
 *      https://dev.twitter.com/apps
 * 2) From the application details page copy the consumer key and consumer
 *      secret into the place in this code marked with (YOUR_CONSUMER_KEY
 *      and YOUR_CONSUMER_SECRET)
 * 3) From the application details page copy the access token and access token
 *      secret into the place in this code marked with (A_USER_TOKEN
 *      and A_USER_SECRET)
 * 4) Visit this page using your web browser.
 *
 * @author themattharris
 */
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array('consumer_key' => 'YOUR_CONSUMER_KEY', 'consumer_secret' => 'YOUR_CONSUMER_SECRET', 'user_token' => 'A_USER_TOKEN', 'user_secret' => 'A_USER_SECRET'));
// for the demo set the timestamp to yesterday
$tmhOAuth->config['force_timestamp'] = true;
$tmhOAuth->config['timestamp'] = strtotime('yesterday');
$code = tmhUtilities::auto_fix_time_request($tmhOAuth, 'GET', $tmhOAuth->url('1/account/verify_credentials'));
if ($code == 200) {
    if ($tmhOAuth->auto_fixed_time) {
        echo 'Had to auto adjust the time. Please check the date and time is correct on your device/server';
    }
    tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
    tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));
}
function new_twitter_login_action()
{
    global $wp, $wpdb, $new_twitter_settings;
    if (isset($_GET['action']) && $_GET['action'] == 'unlink') {
        $user_info = wp_get_current_user();
        if ($user_info->ID) {
            $wpdb->query($wpdb->prepare('DELETE FROM ' . $wpdb->prefix . 'social_users
        WHERE ID = %d
        AND type = \'twitter\'', $user_info->ID));
            set_site_transient($user_info->ID . '_new_twitter_admin_notice', __('Your Twitter profile is successfully unlinked from your account.', 'nextend-twitter-connect'), 3600);
        }
        new_twitter_redirect();
    }
    require dirname(__FILE__) . '/sdk/init.php';
    $here = new_twitter_login_url();
    $access_token = get_site_transient(nextend_uniqid() . '_twitter_at');
    $oauth = get_site_transient(nextend_uniqid() . '_twitter_o');
    if ($access_token !== false) {
        $tmhOAuth->config['user_token'] = $access_token['oauth_token'];
        $tmhOAuth->config['user_secret'] = $access_token['oauth_token_secret'];
        $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/account/verify_credentials'));
        if ($code == 401) {
            $code = tmhUtilities::auto_fix_time_request($tmhOAuth, 'GET', $tmhOAuth->url('1.1/account/verify_credentials'));
        }
        if ($code == 200) {
            $resp = json_decode($tmhOAuth->response['response']);
            $ID = $wpdb->get_var($wpdb->prepare('
        SELECT ID FROM ' . $wpdb->prefix . 'social_users WHERE type = "twitter" AND identifier = "%d"
      ', $resp->id));
            if (!get_user_by('id', $ID)) {
                $wpdb->query($wpdb->prepare('
          DELETE FROM ' . $wpdb->prefix . 'social_users WHERE ID = "%d"
        ', $ID));
                $ID = null;
            }
            if (!is_user_logged_in()) {
                if ($ID == NULL) {
                    // Register
                    $email = new_twitter_request_email();
                    if ($ID == false) {
                        // Real register
                        require_once ABSPATH . WPINC . '/registration.php';
                        $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
                        if (!isset($new_twitter_settings['twitter_user_prefix'])) {
                            $new_twitter_settings['twitter_user_prefix'] = 'Twitter - ';
                        }
                        $sanitized_user_login = sanitize_user($new_twitter_settings['twitter_user_prefix'] . $resp->screen_name);
                        if (!validate_username($sanitized_user_login)) {
                            $sanitized_user_login = sanitize_user('twitter' . $user_profile['id']);
                        }
                        $defaul_user_name = $sanitized_user_login;
                        $i = 1;
                        while (username_exists($sanitized_user_login)) {
                            $sanitized_user_login = $defaul_user_name . $i;
                            $i++;
                        }
                        $ID = wp_create_user($sanitized_user_login, $random_password, $email);
                        if (!is_wp_error($ID)) {
                            wp_new_user_notification($ID, $random_password);
                            $user_info = get_userdata($ID);
                            wp_update_user(array('ID' => $ID, 'display_name' => $resp->name, 'twitter' => $resp->screen_name));
                            do_action('nextend_twitter_user_registered', $ID, $resp, $tmhOAuth);
                        } else {
                            return;
                        }
                    }
                    if ($ID) {
                        $wpdb->insert($wpdb->prefix . 'social_users', array('ID' => $ID, 'type' => 'twitter', 'identifier' => $resp->id), array('%d', '%s', '%s'));
                    }
                    if (isset($new_twitter_settings['twitter_redirect_reg']) && $new_twitter_settings['twitter_redirect_reg'] != '' && $new_twitter_settings['twitter_redirect_reg'] != 'auto') {
                        set_site_transient(nextend_uniqid() . '_twitter_r', $new_twitter_settings['twitter_redirect_reg'], 3600);
                    }
                }
                if ($ID) {
                    // Login
                    $secure_cookie = is_ssl();
                    $secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
                    global $auth_secure_cookie;
                    // XXX ugly hack to pass this to wp_authenticate_cookie
                    $auth_secure_cookie = $secure_cookie;
                    wp_set_auth_cookie($ID, true, $secure_cookie);
                    $user_info = get_userdata($ID);
                    do_action('wp_login', $user_info->user_login, $user_info);
                    update_user_meta($ID, 'twitter_profile_picture', $resp->profile_image_url);
                    do_action('nextend_twitter_user_logged_in', $ID, $resp, $tmhOAuth);
                }
            } else {
                if (new_twitter_is_user_connected()) {
                    // It was a simple login
                } elseif ($ID === NULL) {
                    // Let's connect the account to the current user!
                    $current_user = wp_get_current_user();
                    $wpdb->insert($wpdb->prefix . 'social_users', array('ID' => $current_user->ID, 'type' => 'twitter', 'identifier' => $resp->id), array('%d', '%s', '%s'));
                    do_action('nextend_twitter_user_account_linked', $ID, $resp, $tmhOAuth);
                    $user_info = wp_get_current_user();
                    set_site_transient($user_info->ID . '_new_twitter_admin_notice', __('Your Twitter profile is successfully linked with your account. Now you can sign in with Twitter easily.', 'nextend-twitter-connect'), 3600);
                } else {
                    $user_info = wp_get_current_user();
                    set_site_transient($user_info->ID . '_new_twitter_admin_notice', __('This Twitter profile is already linked with other account. Linking process failed!', 'nextend-twitter-connect'), 3600);
                }
            }
            new_twitter_redirect();
        } else {
            echo "Twitter Error 3";
            exit;
        }
        // we're being called back by Twitter
    } elseif ($oauth !== false && isset($_REQUEST['oauth_verifier'])) {
        $tmhOAuth->config['user_token'] = $oauth['oauth_token'];
        $tmhOAuth->config['user_secret'] = $oauth['oauth_token_secret'];
        $params = array('oauth_verifier' => $_REQUEST['oauth_verifier']);
        $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), $params);
        if ($code == 401) {
            $code = tmhUtilities::auto_fix_time_request($tmhOAuth, 'POST', $tmhOAuth->url('oauth/access_token', ''), $params);
        }
        if ($code == 200) {
            $access_token = $tmhOAuth->extract_params($tmhOAuth->response['response']);
            set_site_transient(nextend_uniqid() . '_twitter_at', $access_token, 3600);
            delete_site_transient(nextend_uniqid() . '_twitter_o');
            header("Location: " . $here);
            exit;
        } else {
            echo "Twitter Error 2";
            exit;
        }
        // start the OAuth dance
    } else {
        if (isset($new_twitter_settings['twitter_redirect']) && $new_twitter_settings['twitter_redirect'] != '' && $new_twitter_settings['twitter_redirect'] != 'auto') {
            $_GET['redirect'] = $new_twitter_settings['twitter_redirect'];
        }
        if (isset($_GET['redirect'])) {
            set_site_transient(nextend_uniqid() . '_twitter_r', $_GET['redirect'], 3600);
        }
        $redirect = get_site_transient(nextend_uniqid() . '_twitter_r');
        if ($redirect == '' || $redirect == new_twitter_login_url()) {
            $redirect = site_url();
            set_site_transient(nextend_uniqid() . '_twitter_r', $redirect, 3600);
        }
        $callback = $here;
        $params = array('oauth_callback' => $callback);
        if (isset($_REQUEST['force_read'])) {
            $params['x_auth_access_type'] = 'read';
        }
        $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), $params);
        if ($code == 401) {
            $code = tmhUtilities::auto_fix_time_request($tmhOAuth, 'POST', $tmhOAuth->url('oauth/request_token', ''), $params);
        }
        if ($code == 200) {
            $oauth = $tmhOAuth->extract_params($tmhOAuth->response['response']);
            set_site_transient(nextend_uniqid() . '_twitter_o', $oauth, 3600);
            $method = 'authenticate';
            $force = isset($_REQUEST['force']) ? '&force_login=1' : '';
            $authurl = $tmhOAuth->url("oauth/{$method}", '') . "?oauth_token={$oauth['oauth_token']}{$force}";
            header('Location: ' . $authurl);
            exit;
        } else {
            //print_r($tmhOAuth);
            echo "Twitter Error 1";
            exit;
        }
    }
}