BxFaceBookConnectModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup FacebookConnect Facebook Connect
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxFaceBookConnectModule extends BxBaseModConnectModule
  12. {
  13. protected $oFacebook;
  14. protected $sLastError;
  15. /**
  16. * Class constructor ;
  17. *
  18. * @param : $aModule (array) - contain some information about this module;
  19. * [ id ] - (integer) module's id ;
  20. * [ title ] - (string) module's title ;
  21. * [ vendor ] - (string) module's vendor ;
  22. * [ path ] - (string) path to this module ;
  23. * [ uri ] - (string) this module's URI ;
  24. * [ class_prefix ] - (string) this module's php classes file prefix ;
  25. * [ db_prefix ] - (string) this module's Db tables prefix ;
  26. * [ date ] - (string) this module's date installation ;
  27. */
  28. function __construct(&$aModule)
  29. {
  30. parent::__construct($aModule);
  31. require_once(BX_DIRECTORY_PATH_PLUGINS . 'facebook-php-sdk/src/Facebook/autoload.php');
  32. // Create our Application instance.
  33. $this -> oFacebook = null;
  34. if ($this -> _oConfig -> mApiID) {
  35. session_start();
  36. $this -> oFacebook = new Facebook\Facebook(array(
  37. 'app_id' => $this -> _oConfig -> mApiID,
  38. 'app_secret' => $this -> _oConfig -> mApiSecret,
  39. 'default_graph_version' => 'v2.4',
  40. ));
  41. }
  42. }
  43. /**
  44. * Facebook login callback url;
  45. *
  46. * @return (text) - html presentation data;
  47. */
  48. function actionLoginCallback()
  49. {
  50. if (isLogged()) {
  51. header ('Location:' . $this -> _oConfig -> sDefaultRedirectUrl);
  52. exit;
  53. }
  54. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  55. bx_import('BxDolLanguages');
  56. if (!$this -> _oConfig -> mApiID || !$this -> _oConfig -> mApiSecret)
  57. $this->_setLastError(_t('_bx_facebook_profile_error_api_keys'));
  58. if ($sError = $this->_setAccessToken())
  59. $this->_setLastError($sError);
  60. if (!$this->_getLastError()) {
  61. //we already logged in facebook
  62. try {
  63. $oResponse = $this -> oFacebook -> get('/me?fields=' . $this -> _oConfig -> sFaceBookFields);
  64. $aFacebookProfileInfo = $oResponse -> getDecodedBody();
  65. $aFacebookProfileInfo['nick_name'] = $aFacebookProfileInfo['name'];
  66. } catch (Facebook\Exceptions\FacebookResponseException $e) {
  67. $this->_setLastError($e->getMessage());
  68. } catch (Facebook\Exceptions\FacebookSDKException $e) {
  69. $this->_setLastError($e->getMessage());
  70. }
  71. //process profile info
  72. if($aFacebookProfileInfo) {
  73. // try define user id
  74. $iProfileId = $this -> _oDb
  75. -> getProfileId($aFacebookProfileInfo['id']);
  76. if ($iProfileId) {
  77. $this -> setLogged($iProfileId);
  78. }
  79. else {
  80. // process profile's nickname
  81. $aFacebookProfileInfo['nick_name'] = $this
  82. -> _proccesNickName($aFacebookProfileInfo['first_name']);
  83. // try to get profile's image
  84. if ($oFacebookProfileImageResponse = $this -> oFacebook -> get('/me/picture?type=large&redirect=false')) {
  85. $aFacebookProfileImage = $oFacebookProfileImageResponse -> getDecodedBody();
  86. $aFacebookProfileInfo['picture'] = isset($aFacebookProfileImage['data']['url']) && !$aFacebookProfileImage['data']['is_silhouette']
  87. ? $aFacebookProfileImage['data']['url']
  88. : '';
  89. }
  90. // create new profile
  91. $this -> _createProfile($aFacebookProfileInfo);
  92. }
  93. } else {
  94. // FB profile info is not defined;
  95. $this->_setLastError(_t('_bx_facebook_profile_error_info'));
  96. }
  97. }
  98. $this -> _oTemplate -> dislayPageError();
  99. }
  100. /**
  101. * Generare facebook login form;
  102. *
  103. * @return (text) - html presentation data;
  104. */
  105. function actionLoginForm()
  106. {
  107. if (isLogged()) {
  108. header ('Location:' . $this -> _oConfig -> sDefaultRedirectUrl);
  109. exit;
  110. }
  111. if (!$this -> _oConfig -> mApiID || !$this -> _oConfig -> mApiSecret) {
  112. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  113. bx_import('BxDolLanguages');
  114. $this->_setLastError(_t('_bx_facebook_profile_error_api_keys'));
  115. }
  116. else {
  117. $oFacebookRedirectLoginHelper = $this -> oFacebook -> getRedirectLoginHelper();
  118. //redirect to facebook login form
  119. $sLoginUrl = $oFacebookRedirectLoginHelper->getLoginUrl(
  120. $this -> _oConfig -> aFaceBookReqParams['redirect_uri'],
  121. explode(',', $this -> _oConfig -> aFaceBookReqParams['scope'])
  122. );
  123. header('location: ' . $sLoginUrl);
  124. exit;
  125. }
  126. $this -> _oTemplate -> dislayPageError();
  127. }
  128. function serviceLastError()
  129. {
  130. return MsgBox($this->sLastError ? $this->sLastError : _t('_Empty'));
  131. }
  132. /**
  133. * Make friends
  134. *
  135. * @param $iProfileId integer
  136. * @return void
  137. */
  138. protected function _makeFriends($iProfileId)
  139. {
  140. if (!$this->_oConfig->bAutoFriends) {
  141. return;
  142. }
  143. try {
  144. //get friends from facebook
  145. $oFriendsResponse = $this -> oFacebook -> get('/me/friends?limit=50');
  146. } catch(Facebook\Exceptions\FacebookResponseException $e) {
  147. return;
  148. } catch(Facebook\Exceptions\FacebookSDKException $e) {
  149. return;
  150. }
  151. // paginate through the result
  152. $oPagesEdge = $oFriendsResponse->getGraphEdge();
  153. do {
  154. foreach ($oPagesEdge as $oPage) {
  155. $aFriend = $oPage->asArray();
  156. $iFriendId = $this -> _oDb -> getProfileId($aFriend['id']);
  157. // TODO:
  158. }
  159. } while ($oPagesEdge = $this -> oFacebook -> next($oPagesEdge));
  160. }
  161. /**
  162. * @param $aProfileInfo - remote profile info
  163. * @param $sAlternativeName - suffix to add to NickName to make it unique
  164. * @return profile array info, ready for the local database
  165. */
  166. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  167. {
  168. // process the date of birth
  169. if( isset($aProfileInfo['birthday']) ) {
  170. $aProfileInfo['birthday'] = isset($aProfileInfo['birthday'])
  171. ? date('Y-m-d', strtotime($aProfileInfo['birthday']))
  172. : '';
  173. }
  174. // define user's country and city
  175. $aLocation = array();
  176. if (isset($aProfileInfo['location']['name']))
  177. $aLocation = $aProfileInfo['location']['name'];
  178. elseif (isset($aProfileInfo['hometown']['name']))
  179. $aLocation = $aProfileInfo['hometown']['name'];
  180. if($aLocation) {
  181. $aCountryInfo = explode(',', $aLocation);
  182. $sCountry = $this -> _oDb -> getCountryCode( trim($aCountryInfo[1]) );
  183. $sCity = trim($aCountryInfo[0]);
  184. //set default country name, especially for American brothers
  185. if($sCity && !$sCountry) {
  186. $sCountry = $this -> _oConfig -> sDefaultCountryCode;
  187. }
  188. }
  189. // fill array with all needed values
  190. $aProfileFields = array(
  191. 'name' => $aProfileInfo['nick_name'] . $sAlternativeName,
  192. 'email' => isset($aProfileInfo['email']) ? $aProfileInfo['email'] : '',
  193. 'gender' => isset($aProfileInfo['gender']) ? $aProfileInfo['gender'] : '',
  194. 'birthday' => isset($aProfileInfo['birthday']) ? $aProfileInfo['birthday'] : '',
  195. 'fullname' => (isset($aProfileInfo['first_name']) ? $aProfileInfo['first_name'] : '') . (isset($aProfileInfo['last_name']) ? ' ' . $aProfileInfo['last_name'] : ''),
  196. 'description' => clear_xss(isset($aProfileInfo['bio']) ? $aProfileInfo['bio'] : ''),
  197. 'interests' => clear_xss(isset($aProfileInfo['interests']) ? $aProfileInfo['interests'] : ''),
  198. 'religion' => clear_xss(isset($aProfileInfo['religion']) ? $aProfileInfo['religion'] : ''),
  199. 'country' => isset($sCountry) ? $sCountry : '',
  200. 'city' => isset($sCity) ? $sCity : '',
  201. 'picture' => $aProfileInfo['picture'],
  202. );
  203. return $aProfileFields;
  204. }
  205. /**
  206. * Function will clear all unnecessary sybmols from profile's nickname;
  207. *
  208. * @param : $sProfileName (string) - profile's nickname;
  209. * @return : (string) - cleared nickname;
  210. */
  211. protected function _proccesNickName($sProfileName)
  212. {
  213. $sProfileName = preg_replace("/^http:\/\/|^https:\/\/|\/$/", '', $sProfileName);
  214. $sProfileName = str_replace('/', '_', $sProfileName);
  215. $sProfileName = str_replace('.', '-', $sProfileName);
  216. return $sProfileName;
  217. }
  218. protected function _setAccessToken()
  219. {
  220. $oFacebookRedirectLoginHelper = $this -> oFacebook -> getRedirectLoginHelper();
  221. try {
  222. $sAccessToken = $oFacebookRedirectLoginHelper->getAccessToken();
  223. } catch(Facebook\Exceptions\FacebookResponseException $e) {
  224. // When Graph returns an error
  225. return $e->getMessage();
  226. } catch(Facebook\Exceptions\FacebookSDKException $e) {
  227. // When validation fails or other local issues
  228. return $e->getMessage();
  229. }
  230. if (!isset($sAccessToken))
  231. return $oFacebookRedirectLoginHelper->getErrorDescription() ? $oFacebookRedirectLoginHelper->getErrorDescription() : (bx_get('error_message') ? rawurldecode(bx_get('error_message')) : _t('_error occured'));
  232. $this -> oFacebook -> setDefaultAccessToken($sAccessToken);
  233. return '';
  234. }
  235. protected function _setLastError($s)
  236. {
  237. $this->sLastError = $s;
  238. }
  239. protected function _getLastError()
  240. {
  241. return $this->sLastError;
  242. }
  243. }
  244. /** @} */