Brak opisu

facebook-client.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. require_once NSL_PATH . '/includes/oauth2.php';
  3. class NextendSocialProviderFacebookClient extends NextendSocialOauth2 {
  4. const DEFAULT_GRAPH_VERSION = 'v7.0';
  5. private $isTest = false;
  6. protected $access_token_data = array(
  7. 'access_token' => '',
  8. 'expires_in' => -1,
  9. 'created' => -1
  10. );
  11. protected $scopes = array(
  12. 'public_profile',
  13. 'email'
  14. );
  15. public function __construct($providerID, $isTest) {
  16. $this->isTest = $isTest;
  17. parent::__construct($providerID);
  18. $this->endpointAccessToken = 'https://graph.facebook.com/' . self::DEFAULT_GRAPH_VERSION . '/oauth/access_token';
  19. $this->endpointRestAPI = 'https://graph.facebook.com/' . self::DEFAULT_GRAPH_VERSION . '/';
  20. }
  21. public function getEndpointAuthorization() {
  22. if (preg_match('/Android|iPhone|iP[ao]d|Mobile/', $_SERVER['HTTP_USER_AGENT'])) {
  23. $endpointAuthorization = 'https://m.facebook.com/';
  24. } else {
  25. $endpointAuthorization = 'https://www.facebook.com/';
  26. }
  27. $endpointAuthorization .= self::DEFAULT_GRAPH_VERSION . '/dialog/oauth';
  28. if ((isset($_GET['display']) && $_GET['display'] == 'popup') || $this->isTest) {
  29. $endpointAuthorization .= '?display=popup';
  30. }
  31. return $endpointAuthorization;
  32. }
  33. protected function formatScopes($scopes) {
  34. return implode(',', $scopes);
  35. }
  36. public function isAccessTokenLongLived() {
  37. return $this->access_token_data['created'] + $this->access_token_data['expires_in'] > time() + (60 * 60 * 2);
  38. }
  39. /**
  40. * @return false|string
  41. * @throws Exception
  42. */
  43. public function requestLongLivedAccessToken() {
  44. $http_args = array(
  45. 'timeout' => 15,
  46. 'user-agent' => 'WordPress',
  47. 'body' => array(
  48. 'grant_type' => 'fb_exchange_token',
  49. 'client_id' => $this->client_id,
  50. 'client_secret' => $this->client_secret,
  51. 'fb_exchange_token' => $this->access_token_data['access_token']
  52. )
  53. );
  54. $request = wp_remote_get($this->endpointAccessToken, $this->extendAllHttpArgs($http_args));
  55. if (is_wp_error($request)) {
  56. throw new Exception($request->get_error_message());
  57. } else if (wp_remote_retrieve_response_code($request) !== 200) {
  58. $this->errorFromResponse(json_decode(wp_remote_retrieve_body($request), true));
  59. }
  60. $accessTokenData = json_decode(wp_remote_retrieve_body($request), true);
  61. if (!is_array($accessTokenData)) {
  62. throw new Exception(sprintf(__('Unexpected response: %s', 'nextend-facebook-connect'), wp_remote_retrieve_body($request)));
  63. }
  64. $accessTokenData['created'] = time();
  65. $this->access_token_data = $accessTokenData;
  66. return wp_json_encode($accessTokenData);
  67. }
  68. protected function errorFromResponse($response) {
  69. if (isset($response['error'])) {
  70. throw new Exception($response['error']['message']);
  71. }
  72. }
  73. protected function extendAllHttpArgs($http_args) {
  74. $http_args['body']['appsecret_proof'] = hash_hmac('sha256', $this->getAccessToken(), $this->client_secret);
  75. return $http_args;
  76. }
  77. protected function getAccessToken() {
  78. if (!empty($this->access_token_data['access_token'])) {
  79. return $this->access_token_data['access_token'];
  80. }
  81. return $this->client_id;
  82. }
  83. }