Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. <?php
  2. /*!
  3. * HybridAuth
  4. * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
  5. * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
  6. */
  7.  
  8. /**
  9. * Hybrid_Providers_Facebook provider adapter based on OAuth2 protocol
  10. *
  11. * Hybrid_Providers_Facebook use the Facebook PHP SDK created by Facebook
  12. *
  13. * http://hybridauth.sourceforge.net/userguide/IDProvider_info_Facebook.html
  14. */
  15. class Hybrid_Providers_Facebook extends Hybrid_Provider_Model
  16. {
  17. // default permissions, and alot of them. You can change them from the configuration by setting the scope to what you want/need
  18. public $scope = "email, user_about_me, user_birthday, user_hometown, user_website, read_stream, offline_access, publish_stream, read_friendlists";
  19.  
  20. /**
  21. * IDp wrappers initializer
  22. */
  23. function initialize()
  24. {
  25. if ( ! $this->config["keys"]["id"] || ! $this->config["keys"]["secret"] ){
  26. throw new Exception( "Your application id and secret are required in order to connect to {$this->providerId}.", 4 );
  27. }
  28.  
  29. if ( ! class_exists('FacebookApiException', false) ) {
  30. require_once Hybrid_Auth::$config["path_libraries"] . "Facebook/base_facebook.php";
  31. require_once Hybrid_Auth::$config["path_libraries"] . "Facebook/facebook.php";
  32. }
  33.  
  34. $this->api = new Facebook( ARRAY( 'appId' => $this->config["keys"]["id"], 'secret' => $this->config["keys"]["secret"] ) );
  35.  
  36. if ( $this->token("access_token") ) {
  37. $this->api->setAccessToken( $this->token("access_token") );
  38. $this->api->setExtendedAccessToken();
  39. $access_token = $this->api->getAccessToken();
  40.  
  41. if( $access_token ){
  42. $this->token("access_token", $access_token );
  43. $this->api->setAccessToken( $access_token );
  44. }
  45.  
  46. $this->api->setAccessToken( $this->token("access_token") );
  47. }
  48.  
  49. $this->api->getUser();
  50. }
  51.  
  52. /**
  53. * begin login step
  54. *
  55. * simply call Facebook::require_login().
  56. */
  57. function loginBegin()
  58. {
  59. $parameters = array("scope" => $this->scope, "redirect_uri" => $this->endpoint, "display" => "page");
  60. $optionals = array("scope", "redirect_uri", "display");
  61.  
  62. foreach ($optionals as $parameter){
  63. if( isset( $this->config[$parameter] ) && ! empty( $this->config[$parameter] ) ){
  64. $parameters[$parameter] = $this->config[$parameter];
  65. }
  66. }
  67.  
  68. // get the login url
  69. $url = $this->api->getLoginUrl( $parameters );
  70.  
  71. // redirect to facebook
  72. Hybrid_Auth::redirect( $url );
  73. }
  74.  
  75. /**
  76. * finish login step
  77. */
  78. function loginFinish()
  79. {
  80. // in case we get error_reason=user_denied&error=access_denied
  81. if ( isset( $_REQUEST['error'] ) && $_REQUEST['error'] == "access_denied" ){
  82. throw new Exception( "Authentication failed! The user denied your request.", 5 );
  83. }
  84.  
  85. // try to get the UID of the connected user from fb, should be > 0
  86. if ( ! $this->api->getUser() ){
  87. throw new Exception( "Authentication failed! {$this->providerId} returned an invalid user id.", 5 );
  88. }
  89.  
  90. // set user as logged in
  91. $this->setUserConnected();
  92.  
  93. // store facebook access token
  94. $this->token( "access_token", $this->api->getAccessToken() );
  95. }
  96.  
  97. /**
  98. * logout
  99. */
  100. function logout()
  101. {
  102. $this->api->destroySession();
  103.  
  104. parent::logout();
  105. }
  106.  
  107. /**
  108. * load the user profile from the IDp api client
  109. */
  110. function getUserProfile()
  111. {
  112. // request user profile from fb api
  113. try{
  114. $data = $this->api->api('/me');
  115. }
  116. catch( FacebookApiException $e ){
  117. throw new Exception( "User profile request failed! {$this->providerId} returned an error: $e", 6 );
  118. }
  119.  
  120. // if the provider identifier is not recived, we assume the auth has failed
  121. if ( ! isset( $data["id"] ) ){
  122. throw new Exception( "User profile request failed! {$this->providerId} api returned an invalid response.", 6 );
  123. }
  124.  
  125. # store the user profile.
  126. $this->user->profile->identifier = (array_key_exists('id',$data))?$data['id']:"";
  127. $this->user->profile->displayName = (array_key_exists('name',$data))?$data['name']:"";
  128. $this->user->profile->firstName = (array_key_exists('first_name',$data))?$data['first_name']:"";
  129. $this->user->profile->lastName = (array_key_exists('last_name',$data))?$data['last_name']:"";
  130. $this->user->profile->photoURL = "https://graph.facebook.com/" . $this->user->profile->identifier . "/picture?type=square";
  131. $this->user->profile->profileURL = (array_key_exists('link',$data))?$data['link']:"";
  132. $this->user->profile->webSiteURL = (array_key_exists('website',$data))?$data['website']:"";
  133. $this->user->profile->gender = (array_key_exists('gender',$data))?$data['gender']:"";
  134. $this->user->profile->description = (array_key_exists('bio',$data))?$data['bio']:"";
  135. $this->user->profile->email = (array_key_exists('email',$data))?$data['email']:"";
  136. $this->user->profile->emailVerified = (array_key_exists('email',$data))?$data['email']:"";
  137. $this->user->profile->region = (array_key_exists("hometown",$data)&&array_key_exists("name",$data['hometown']))?$data['hometown']["name"]:"";
  138.  
  139. if( array_key_exists('birthday',$data) ) {
  140. list($birthday_month, $birthday_day, $birthday_year) = explode( "/", $data['birthday'] );
  141.  
  142. $this->user->profile->birthDay = (int) $birthday_day;
  143. $this->user->profile->birthMonth = (int) $birthday_month;
  144. $this->user->profile->birthYear = (int) $birthday_year;
  145. }
  146.  
  147. return $this->user->profile;
  148. }
  149.  
  150. /**
  151. * load the user contacts
  152. */
  153. function getUserContacts()
  154. {
  155. try{
  156. $response = $this->api->api('/me/friends');
  157. }
  158. catch( FacebookApiException $e ){
  159. throw new Exception( "User contacts request failed! {$this->providerId} returned an error: $e" );
  160. }
  161.  
  162. if( ! $response || ! count( $response["data"] ) ){
  163. return ARRAY();
  164. }
  165.  
  166. $contacts = ARRAY();
  167.  
  168. foreach( $response["data"] as $item ){
  169. $uc = new Hybrid_User_Contact();
  170.  
  171. $uc->identifier = (array_key_exists("id",$item))?$item["id"]:"";
  172. $uc->displayName = (array_key_exists("name",$item))?$item["name"]:"";
  173. $uc->profileURL = "https://www.facebook.com/profile.php?id=" . $uc->identifier;
  174. $uc->photoURL = "https://graph.facebook.com/" . $uc->identifier . "/picture?type=square";
  175.  
  176. $contacts[] = $uc;
  177. }
  178.  
  179. return $contacts;
  180. }
  181.  
  182. /**
  183. * update user status
  184. */
  185. function setUserStatus( $status )
  186. {
  187. $parameters = array();
  188.  
  189. if( is_array( $status ) ){
  190. $parameters = $status;
  191. }
  192. else{
  193. $parameters["message"] = $status;
  194. }
  195.  
  196. try{
  197. $response = $this->api->api( "/me/feed", "post", $parameters );
  198. }
  199. catch( FacebookApiException $e ){
  200. throw new Exception( "Update user status failed! {$this->providerId} returned an error: $e" );
  201. }
  202. }
  203.  
  204. /**
  205. * load the user latest activity
  206. * - timeline : all the stream
  207. * - me : the user activity only
  208. */
  209. function getUserActivity( $stream )
  210. {
  211. try{
  212. if( $stream == "me" ){
  213. $response = $this->api->api( '/me/feed' );
  214. }
  215. else{
  216. $response = $this->api->api('/me/home');
  217. }
  218. }
  219. catch( FacebookApiException $e ){
  220. throw new Exception( "User activity stream request failed! {$this->providerId} returned an error: $e" );
  221. }
  222.  
  223. if( ! $response || ! count( $response['data'] ) ){
  224. return ARRAY();
  225. }
  226.  
  227. $activities = ARRAY();
  228.  
  229. foreach( $response['data'] as $item ){
  230. if( $stream == "me" && $item["from"]["id"] != $this->api->getUser() ){
  231. continue;
  232. }
  233.  
  234. $ua = new Hybrid_User_Activity();
  235.  
  236. $ua->id = (array_key_exists("id",$item))?$item["id"]:"";
  237. $ua->date = (array_key_exists("created_time",$item))?strtotime($item["created_time"]):"";
  238.  
  239. if( $item["type"] == "video" ){
  240. $ua->text = (array_key_exists("link",$item))?$item["link"]:"";
  241. }
  242.  
  243. if( $item["type"] == "link" ){
  244. $ua->text = (array_key_exists("link",$item))?$item["link"]:"";
  245. }
  246.  
  247. if( empty( $ua->text ) && isset( $item["story"] ) ){
  248. $ua->text = (array_key_exists("link",$item))?$item["link"]:"";
  249. }
  250.  
  251. if( empty( $ua->text ) && isset( $item["message"] ) ){
  252. $ua->text = (array_key_exists("message",$item))?$item["message"]:"";
  253. }
  254.  
  255. if( ! empty( $ua->text ) ){
  256. $ua->user->identifier = (array_key_exists("id",$item["from"]))?$item["from"]["id"]:"";
  257. $ua->user->displayName = (array_key_exists("name",$item["from"]))?$item["from"]["name"]:"";
  258. $ua->user->profileURL = "https://www.facebook.com/profile.php?id=" . $ua->user->identifier;
  259. $ua->user->photoURL = "https://graph.facebook.com/" . $ua->user->identifier . "/picture?type=square";
  260.  
  261. $activities[] = $ua;
  262. }
  263. }
  264.  
  265. return $activities;
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement