FacebookStrategy.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Facebook strategy for Opauth
  4. * based on https://developers.facebook.com/docs/authentication/server-side/
  5. *
  6. * More information on Opauth: http://opauth.org
  7. *
  8. * @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
  9. * @link http://opauth.org
  10. * @package Opauth.FacebookStrategy
  11. * @license MIT License
  12. */
  13. class FacebookStrategy extends OpauthStrategy{
  14. /**
  15. * Compulsory config keys, listed as unassociative arrays
  16. * eg. array('app_id', 'app_secret');
  17. */
  18. public $expects = array('app_id', 'app_secret');
  19. /**
  20. * Optional config keys with respective default values, listed as associative arrays
  21. * eg. array('scope' => 'email');
  22. */
  23. public $defaults = array(
  24. 'redirect_uri' => '{complete_url_to_strategy}int_callback'
  25. );
  26. /**
  27. * Auth request
  28. */
  29. public function request(){
  30. $url = 'https://www.facebook.com/dialog/oauth';
  31. $params = array(
  32. 'client_id' => $this->strategy['app_id'],
  33. 'redirect_uri' => $this->strategy['redirect_uri']
  34. );
  35. if (!empty($this->strategy['scope'])) $params['scope'] = $this->strategy['scope'];
  36. if (!empty($this->strategy['state'])) $params['state'] = $this->strategy['state'];
  37. if (!empty($this->strategy['response_type'])) $params['response_type'] = $this->strategy['response_type'];
  38. if (!empty($this->strategy['display'])) $params['display'] = $this->strategy['display'];
  39. if (!empty($this->strategy['auth_type'])) $params['auth_type'] = $this->strategy['auth_type'];
  40. $this->clientGet($url, $params);
  41. }
  42. /**
  43. * Internal callback, after Facebook's OAuth
  44. */
  45. public function int_callback(){
  46. if (array_key_exists('code', $_GET) && !empty($_GET['code'])){
  47. $url = 'https://graph.facebook.com/oauth/access_token';
  48. $params = array(
  49. 'client_id' =>$this->strategy['app_id'],
  50. 'client_secret' => $this->strategy['app_secret'],
  51. 'redirect_uri'=> $this->strategy['redirect_uri'],
  52. 'code' => trim($_GET['code'])
  53. );
  54. $response = $this->serverGet($url, $params, null, $headers);
  55. parse_str($response, $results);
  56. if (!empty($results) && !empty($results['access_token'])){
  57. $me = $this->me($results['access_token']);
  58. $this->auth = array(
  59. 'provider' => 'Facebook',
  60. 'uid' => $me->id,
  61. 'info' => array(
  62. 'name' => $me->name,
  63. 'image' => 'https://graph.facebook.com/'.$me->id.'/picture?type=square'
  64. ),
  65. 'credentials' => array(
  66. 'token' => $results['access_token'],
  67. 'expires' => date('c', time() + $results['expires'])
  68. ),
  69. 'raw' => $me
  70. );
  71. if (!empty($me->email)) $this->auth['info']['email'] = $me->email;
  72. if (!empty($me->username)) $this->auth['info']['nickname'] = $me->username;
  73. if (!empty($me->first_name)) $this->auth['info']['first_name'] = $me->first_name;
  74. if (!empty($me->last_name)) $this->auth['info']['last_name'] = $me->last_name;
  75. if (!empty($me->location)) $this->auth['info']['location'] = $me->location->name;
  76. if (!empty($me->link)) $this->auth['info']['urls']['facebook'] = $me->link;
  77. if (!empty($me->website)) $this->auth['info']['urls']['website'] = $me->website;
  78. /**
  79. * Missing optional info values
  80. * - description
  81. * - phone: not accessible via Facebook Graph API
  82. */
  83. $this->callback();
  84. }
  85. else{
  86. $error = array(
  87. 'provider' => 'Facebook',
  88. 'code' => 'access_token_error',
  89. 'message' => 'Failed when attempting to obtain access token',
  90. 'raw' => $headers
  91. );
  92. $this->errorCallback($error);
  93. }
  94. }
  95. else{
  96. $error = array(
  97. 'provider' => 'Facebook',
  98. 'code' => $_GET['error'],
  99. 'message' => $_GET['error_description'],
  100. 'raw' => $_GET
  101. );
  102. $this->errorCallback($error);
  103. }
  104. }
  105. /**
  106. * Queries Facebook Graph API for user info
  107. *
  108. * @param string $access_token
  109. * @return array Parsed JSON results
  110. */
  111. private function me($access_token){
  112. $me = $this->serverGet('https://graph.facebook.com/me', array('access_token' => $access_token), null, $headers);
  113. if (!empty($me)){
  114. return json_decode($me);
  115. }
  116. else{
  117. $error = array(
  118. 'provider' => 'Facebook',
  119. 'code' => 'me_error',
  120. 'message' => 'Failed when attempting to query for user information',
  121. 'raw' => array(
  122. 'response' => $me,
  123. 'headers' => $headers
  124. )
  125. );
  126. $this->errorCallback($error);
  127. }
  128. }
  129. }