/** * Stores CSRF state and returns a URL to which the user should be sent to * in order to continue the login process with Facebook. The * provided redirectUrl should invoke the handleRedirect method. * If a previous request to certain permission(s) was declined * by the user, rerequest should be set to true or the permission(s) * will not be re-asked. * * @param string $redirectUrl The URL Facebook should redirect users to * after login. * @param array $scope List of permissions to request during login. * @param boolean $rerequest Toggle for this authentication to be a rerequest. * @param string $version Optional Graph API version if not default (v2.0). * @param string $separator The separator to use in http_build_query(). * * @return string */ public function getLoginUrl($redirectUrl, array $scope = [], $rerequest = false, $version = null, $separator = '&') { $version = FacebookRequest::getDefaultGraphApiVersion($version); $state = $this->generateState(); $this->storeState($state); $params = ['client_id' => $this->app->getId(), 'redirect_uri' => $redirectUrl, 'state' => $state, 'sdk' => 'php-sdk-' . FacebookRequest::VERSION, 'scope' => implode(',', $scope)]; if ($rerequest) { $params['auth_type'] = 'rerequest'; } return 'https://www.facebook.com/' . $version . '/dialog/oauth?' . http_build_query($params, null, $separator); }
private function defaultHeaders() { $headers = []; foreach (FacebookRequest::getDefaultHeaders() as $name => $value) { $headers[] = $name . ': ' . $value; } return $headers; }
/** * Converts a Request entity into an array that is batch-friendly. * * @param FacebookRequest $request The request entity to convert. * @param string|null $requestName The name of the request. * * @return array */ public static function requestEntityToBatchArray(FacebookRequest $request, $requestName = null) { $compiledHeaders = []; $headers = $request->getHeaders(); foreach ($headers as $name => $value) { $compiledHeaders[] = $name . ': ' . $value; } $batch = ['headers' => $compiledHeaders, 'method' => $request->getMethod(), 'relative_url' => $request->getUrl()]; $params = $request->getPostParams(); if ($params) { $batch['body'] = http_build_query($params, null, '&'); } if (isset($requestName)) { $batch['name'] = $requestName; } // @TODO Add support for "omit_response_on_success" // @TODO Add support for "depends_on" // @TODO Add support for "attached_files" // @TODO Add support for JSONP with "callback" return $batch; }
public function testSlashesAreProperlyPrepended() { $slashTestOne = FacebookRequest::forceSlashPrefix('foo'); $slashTestTwo = FacebookRequest::forceSlashPrefix('/foo'); $slashTestThree = FacebookRequest::forceSlashPrefix('foo/bar'); $slashTestFour = FacebookRequest::forceSlashPrefix('/foo/bar'); $slashTestFive = FacebookRequest::forceSlashPrefix(null); $slashTestSix = FacebookRequest::forceSlashPrefix(''); $this->assertEquals('/foo', $slashTestOne); $this->assertEquals('/foo', $slashTestTwo); $this->assertEquals('/foo/bar', $slashTestThree); $this->assertEquals('/foo/bar', $slashTestFour); $this->assertEquals(null, $slashTestFive); $this->assertEquals('', $slashTestSix); }
public function testPreppingABatchRequestProperlySetsThePostParams() { $app = new FacebookApp('123', 'foo_secret'); $batchRequest = new FacebookBatchRequest($app, 'foo_token'); $batchRequest->add(new FacebookRequest(null, 'bar_token', 'GET', '/foo'), 'foo_name'); $batchRequest->add(new FacebookRequest(null, null, 'POST', '/bar', ['foo' => 'bar'])); $batchRequest->prepareRequestsForBatch(); $params = $batchRequest->getParams(); $expectedHeaders = json_encode($this->requestHeaders); $version = FacebookRequest::getDefaultGraphApiVersion(); $expectedBatchParams = ['batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/' . $version . '\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},' . '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/' . $version . '\\/bar","body":"foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9"}]', 'include_headers' => true, 'access_token' => 'foo_token', 'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9']; $this->assertEquals($expectedBatchParams, $params); }