function connect($url)
        {
            // Establish an Blogger_OAuth consumer
            $base_url = admin_url();
            $request_token_endpoint = 'https://www.google.com/accounts/OAuthGetRequestToken';
            $authorize_endpoint = 'https://www.google.com/accounts/OAuthAuthorizeToken';

            $test_consumer = new Blogger_OAuthConsumer('anonymous', 'anonymous', null); // anonymous is a google thing to allow non-registered apps to work

            if (!Blogger_OAuthUtil::sslinstalled())
            {
                return new WP_Error('OpenSSL', __('OpenSSL is not installed check your PHP.INI or ask your server provider to enable the OpenSSL module.','blogger-importer'));
            }

            //prepare to get request token
            //https://developers.google.com/accounts/docs/OAuth_ref#RequestToken
            $sig_method = new Blogger_OAuthSignatureMethod_HMAC_SHA1();
            $parsed = parse_url($request_token_endpoint);
            $params = array('callback' => $base_url, 'scope' => 'http://www.blogger.com/feeds/', 'xoauth_displayname' => 'WordPress');

            $req_req = Blogger_OAuthRequest::from_consumer_and_token($test_consumer, null, "GET", $request_token_endpoint, $params);
            $req_req->sign_request($sig_method, $test_consumer, null);

            // go get the request tokens from Google
            $req_response = wp_remote_get($req_req->to_url(), array('sslverify' => false, 'timeout' => Blogger_Import::REMOTE_TIMEOUT));
            if (is_wp_error($req_response))
            {
                return $req_response;
            }
            $req_token = wp_remote_retrieve_body($req_response);
            if (is_wp_error($req_token))
            {
                return $req_token;
            }

            // parse the tokens
            parse_str($req_token, $tokens);

            $oauth_token = $tokens['oauth_token'];
            $oauth_token_secret = $tokens['oauth_token_secret'];

            //AGC:10/10/2012 Added error handling here based on http://wordpress.org/support/topic/plugin-blogger-importer-invalid-token/page/2?replies=31#post-3243710
            if ($oauth_token == '')
            {
                return new WP_Error('Invalid Token', __('Invalid Token: Check server date, firewall settings and transports (curl, streams and fsockopen)','blogger-importer'));
            } else
            {
                $this->callback_url = add_query_arg(array( 'token' => $oauth_token, 'secret' => $oauth_token_secret ),$url);
                $this->endpoint = $authorize_endpoint;
                $this->oauth_token = $oauth_token;
                return true;
            }
        }
Ejemplo n.º 2
0
  public static function build_http_query($params) {
    if (!$params) return '';

    // Urlencode both keys and values
    $keys = Blogger_OAuthUtil::urlencode_rfc3986(array_keys($params));
    $values = Blogger_OAuthUtil::urlencode_rfc3986(array_values($params));
    $params = array_combine($keys, $values);

    // Parameters are sorted by name, using lexicographical byte value ordering.
    // Ref: Spec: 9.1.1 (1)
    uksort($params, 'strcmp');

    $pairs = array();
    foreach ($params as $parameter => $value) {
      if (is_array($value)) {
        // If two or more parameters share the same name, they are sorted by their value
        // Ref: Spec: 9.1.1 (1)
        // June 12th, 2010 - changed to sort because of issue 164 by hidetaka
        sort($value, SORT_STRING);
        foreach ($value as $duplicate_value) {
          $pairs[] = $parameter . '=' . $duplicate_value;
        }
      } else {
        $pairs[] = $parameter . '=' . $value;
      }
    }
    // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
    // Each name-value pair is separated by an '&' character (ASCII code 38)
    return implode('&', $pairs);
  }