/** * This method is used to process the second part of authentication workflow, after redirect * * @return array Array with status and user details */ public function processAuth() { $ngConnectINI = eZINI::instance('ngconnect.ini'); $http = eZHTTPTool::instance(); $appID = trim($ngConnectINI->variable('LoginMethod_facebook', 'FacebookAppID')); $appSecret = trim($ngConnectINI->variable('LoginMethod_facebook', 'FacebookAppSecret')); if (empty($appID) || empty($appSecret)) { return array('status' => 'error', 'message' => 'Facebook app ID or Facebook app secret undefined.'); } $code = trim($http->getVariable('code', '')); $state = trim($http->getVariable('state', '')); if (empty($code) || empty($state)) { return array('status' => 'error', 'message' => 'code or state GET parameters undefined.'); } if (!$http->hasSessionVariable('NGConnectOAuthState') || $state != $http->sessionVariable('NGConnectOAuthState')) { $http->removeSessionVariable('NGConnectOAuthState'); return array('status' => 'error', 'message' => 'State parameter does not match stored value.'); } else { $http->removeSessionVariable('NGConnectOAuthState'); } $callbackUri = self::CALLBACK_URI_PART; $loginWindowType = trim($ngConnectINI->variable('ngconnect', 'LoginWindowType')); if ($loginWindowType == 'popup') { $callbackUri = '/layout/set/ngconnect' . self::CALLBACK_URI_PART; } eZURI::transformURI($callbackUri, false, 'full'); $tokenUri = str_replace(array('%app_id%', '%site_url%', '%app_secret%', '%code%'), array(urlencode($appID), urlencode($callbackUri), urlencode($appSecret), urlencode($code)), self::TOKEN_URI); $accessToken = ngConnectFunctions::fetchDataFromUrl($tokenUri); if (!$accessToken) { return array('status' => 'error', 'message' => 'Error while retrieving access token.'); } $accessTokenJson = json_decode($accessToken, true); if ($accessTokenJson !== null) { return array('status' => 'error', 'message' => $accessTokenJson['error']['message']); } $graphUri = str_replace(array('%access_token%'), array(trim($accessToken)), self::GRAPH_URI); $graphResponse = ngConnectFunctions::fetchDataFromUrl($graphUri); if (!$graphResponse) { return array('status' => 'error', 'message' => 'Error while retrieving graph response.'); } $user = json_decode($graphResponse, true); if ($user === null) { return array('status' => 'error', 'message' => 'Invalid JSON data returned.'); } if (!isset($user['id'])) { return array('status' => 'error', 'message' => 'Invalid Facebook user.'); } $pictureUri = self::PICTURE_URI; $imageSize = trim($ngConnectINI->variable('LoginMethod_facebook', 'ImageSize')); if ($imageSize == 'original') { $pictureUri = $pictureUri . '?type=large'; } $result = array('status' => 'success', 'login_method' => 'facebook', 'id' => $user['id'], 'first_name' => isset($user['first_name']) ? $user['first_name'] : '', 'last_name' => isset($user['last_name']) ? $user['last_name'] : '', 'email' => isset($user['email']) ? $user['email'] : '', 'picture' => str_replace('%user_id%', $user['id'], $pictureUri)); return $result; }
/** * Fills the user object data map with auth data * * @param array $dataMap * @param array $authResult */ private static function fillUserObject($dataMap, $authResult) { if (isset($dataMap['first_name'])) { $dataMap['first_name']->fromString($authResult['first_name']); $dataMap['first_name']->store(); } if (isset($dataMap['last_name'])) { $dataMap['last_name']->fromString($authResult['last_name']); $dataMap['last_name']->store(); } if (isset($dataMap['image']) && !empty($authResult['picture'])) { $storageDir = eZSys::storageDirectory() . '/ngconnect'; if (!file_exists($storageDir)) { mkdir($storageDir); } $fileName = $storageDir . '/' . $authResult['login_method'] . '_' . $authResult['id']; $image = ngConnectFunctions::fetchDataFromUrl($authResult['picture'], true, $fileName); if ($image) { $dataMap['image']->fromString($fileName); $dataMap['image']->store(); unlink($fileName); } } }