Cheetah
ChFaceBookConnectModule.php
Go to the documentation of this file.
1 <?php
2 
8 require_once(CH_DIRECTORY_PATH_INC . 'profiles.inc.php');
9 
10 ch_import('ChWsbConnectModule');
11 ch_import('ChWsbInstallerUtils');
12 ch_import('ChWsbProfilesController');
13 ch_import('ChWsbAlerts');
14 
16 {
18 
32  function __construct(&$aModule)
33  {
34  parent::__construct($aModule);
35 
36  require_once(CH_DIRECTORY_PATH_PLUGINS . 'facebook-php-sdk/src/Facebook/autoload.php');
37 
38  // Create our Application instance.
39  $this->oFacebook = null;
40 
41  if (!empty($this->_oConfig->mApiID) && !empty($this->_oConfig->mApiSecret)) {
42  session_start();
43  $this->oFacebook = new Facebook\Facebook(array(
44  'app_id' => $this->_oConfig->mApiID,
45  'app_secret' => $this->_oConfig->mApiSecret,
46  'default_graph_version' => 'v2.4',
47  ));
48  }
49  }
50 
57  {
58  parent::_actionAdministration('ch_facebook_connect_api_key', '_ch_facebook_settings',
59  '_ch_facebook_information', '_ch_facebook_information_block', $this->_oConfig->sPageReciver);
60  }
61 
68  {
69  if (isLogged()) {
70  header('Location:' . $this->_oConfig->sDefaultRedirectUrl);
71  exit;
72  }
73 
74  if (!$this->_oConfig->mApiID || !$this->_oConfig->mApiSecret) {
75  $sCode = MsgBox(_t('_ch_facebook_profile_error_api_keys'));
76  }
77 
78  if ($sError = $this->_setAccessToken()) {
80  }
81 
82  if (!$sCode) {
83 
84  //we already logged in facebook
85  try {
86  $oResponse = $this->oFacebook->get('/me?fields=' . $this->_oConfig->sFaceBookFields);
87  $aFacebookProfileInfo = $oResponse->getDecodedBody();
88  $aFacebookProfileInfo['nick_name'] = $aFacebookProfileInfo['name'];
89 
90  } catch (Facebook\Exceptions\FacebookResponseException $e) {
91  $sCode = MsgBox($e->getMessage());
92  } catch (Facebook\Exceptions\FacebookSDKException $e) {
93  $sCode = MsgBox($e->getMessage());
94  }
95 
96  //process profile info
97  if ($aFacebookProfileInfo) {
98 
99  // try define user id
100  $iProfileId = $this->_oDb
101  ->getProfileId($aFacebookProfileInfo['id']);
102 
103  if ($iProfileId) {
104  // get profile info
105  $aCheetahProfileInfo = getProfileInfo($iProfileId);
106  $this->setLogged($iProfileId, $aCheetahProfileInfo['Password']);
107  } else {
108  $sAlternativeNickName = '';
109 
110  //process profile's nickname
111  $aFacebookProfileInfo['nick_name'] = $this
112  ->_proccesNickName($aFacebookProfileInfo['first_name']);
113 
114  //-- profile nickname already used by other person --//
115  if (getID($aFacebookProfileInfo['nick_name'])) {
116  $sAlternativeNickName = $this
117  ->getAlternativeName($aFacebookProfileInfo['nick_name']);
118  }
119  //--
120 
121  //try to get profile's image
122  if ($oFacebookProfileImageResponse = $this->oFacebook->get('/me/picture?type=large&redirect=false')) {
123 
124  $aFacebookProfileImage = $oFacebookProfileImageResponse->getDecodedBody();
125  $aFacebookProfileInfo['picture'] = isset($aFacebookProfileImage['data']['url']) && !$aFacebookProfileImage['data']['is_silhouette']
126  ? $aFacebookProfileImage['data']['url']
127  : '';
128  }
129 
130  $this->getJoinAfterPaymentPage($aFacebookProfileInfo);
131 
132  //create new profile
133  $this->_createProfile($aFacebookProfileInfo, $sAlternativeNickName);
134  }
135  } else {
136  // FB profile info is not defined;
137  $sCode = MsgBox(_t('_ch_facebook_profile_error_info'));
138  }
139  }
140 
141 
142  $this->_oTemplate->getPage(_t('_ch_facebook'), $sCode);
143  }
144 
150  function actionLoginForm()
151  {
152  $sCode = '';
153 
154  if (isLogged()) {
155  header('Location:' . $this->_oConfig->sDefaultRedirectUrl);
156  exit;
157  }
158 
159  if (!$this->_oConfig->mApiID || !$this->_oConfig->mApiSecret) {
160  $sCode = MsgBox(_t('_ch_facebook_profile_error_api_keys'));
161  } else {
162 
163  $oFacebookRedirectLoginHelper = $this->oFacebook->getRedirectLoginHelper();
164 
165  //redirect to facebook login form
166  $sLoginUrl = $oFacebookRedirectLoginHelper->getLoginUrl(
167  $this->_oConfig->aFaceBookReqParams['redirect_uri'],
168  explode(',', $this->_oConfig->aFaceBookReqParams['scope'])
169  );
170 
171  header('location: ' . $sLoginUrl);
172  exit;
173  }
174 
175  $this->_oTemplate->getPage(_t('_ch_facebook'), $sCode);
176  }
177 
178  function serviceSupported()
179  {
180  return 1;
181  }
182 
183  function serviceLogin($aFacebookProfileInfo, $sToken = '')
184  {
185  if (getParam('enable_cheetah_footer') == 'on') {
186  return array('error' => _t('_ch_facebook_error_unlicensed_site'));
187  }
188 
189  if ($sError = $this->_setAccessToken($sToken)) {
190  return array('error' => $sError);
191  }
192 
193  // try define user id
194  $iProfileId = $this->_oDb
195  ->getProfileId($aFacebookProfileInfo['id']);
196 
197  $aTmp['profile_id'] = $iProfileId;
198  $aFacebookProfileInfoCheck = false;
199  try {
200  $oResponse = $this->oFacebook->get('/' . $aFacebookProfileInfo['id'] . '?fields=' . $this->_oConfig->sFaceBookFields);
201  $aFacebookProfileInfoCheck = $oResponse->getDecodedBody();
202  } catch (Facebook\Exceptions\FacebookResponseException $e) {
203  return array('error' => $e->getMessage());
204  } catch (Facebook\Exceptions\FacebookSDKException $e) {
205  return array('error' => $e->getMessage());
206  }
207 
208  if (!isset($aFacebookProfileInfoCheck['id']) || $aFacebookProfileInfoCheck['id'] != $aFacebookProfileInfo['id']) {
209  return array('error' => _t('_ch_facebook_profile_error_info'));
210  }
211 
212  if ($iProfileId) {
213 
214  $aCheetahProfileInfo = getProfileInfo($iProfileId);
215  $this->setLogged($iProfileId, '', '', false);
216 
217  require_once(CH_DIRECTORY_PATH_ROOT . 'xmlrpc/ChWsbXMLRPCUser.php');
218 
219  return array(
220  'member_id' => $iProfileId,
221  'member_pwd_hash' => $aCheetahProfileInfo['Password'],
222  'member_username' => getUsername($iProfileId),
223  'protocol_ver' => CH_XMLRPC_PROTOCOL_VER,
224  );
225  } else {
226 
227  $sAlternativeNickName = '';
228 
229  //process profile's nickname
230  $aFacebookProfileInfo['nick_name'] = $this->_proccesNickName($aFacebookProfileInfo['name']);
231 
232  //-- profile nickname already used by other person --//
233  if (getID($aFacebookProfileInfo['nick_name'])) {
234  $sAlternativeNickName = $this
235  ->getAlternativeName($aFacebookProfileInfo['nick_name']);
236  }
237  //--
238 
239  //try to get profile's image
240  if ($oFacebookProfileImageResponse = $this->oFacebook->get('/' . $aFacebookProfileInfo['id'] . '/picture?type=large&redirect=false')) {
241 
242  $aFacebookProfileImage = $oFacebookProfileImageResponse->getDecodedBody();
243  $aFacebookProfileInfo['picture'] = isset($aFacebookProfileImage['data']['url']) && !$aFacebookProfileImage['data']['is_silhouette']
244  ? $aFacebookProfileImage['data']['url']
245  : '';
246  }
247 
248  // mobile app doesn't support redirect to join form (or any other redirects)
249  if ('join' == $this->_oConfig->sRedirectPage) {
250  $this->_oConfig->sRedirectPage = 'pedit';
251  }
252 
253  //create new profile
254  $mixed = $this->_createProfileRaw($aFacebookProfileInfo, $sAlternativeNickName, false, true);
255 
256  if (is_string($mixed)) { // known error occured
257 
258  return array(
259  'error' => $mixed,
260  'protocol_ver' => CH_XMLRPC_PROTOCOL_VER,
261  );
262 
263  } elseif (is_array($mixed) && isset($mixed['profile_id'])) { // everything is good
264 
265  $iProfileId = $mixed['profile_id'];
266  $aCheetahProfileInfo = getProfileInfo($iProfileId);
267  $sMemberAvatar = !empty($mixed['remote_profile_info']['picture']) ? $mixed['remote_profile_info']['picture'] : '';
268 
269  //assign avatar
270  if ($sMemberAvatar && !$mixed['existing_profile']) {
271  $this->_assignAvatar($sMemberAvatar, $iProfileId);
272  }
273 
274  return array(
275  'member_id' => $iProfileId,
276  'member_pwd_hash' => $aCheetahProfileInfo['Password'],
277  'member_username' => getUsername($iProfileId),
278  'protocol_ver' => CH_XMLRPC_PROTOCOL_VER,
279  'existing_profile' => isset($mixed['existing_profile']) && $mixed['existing_profile'],
280  );
281 
282  } else { // unknown error
283 
284  return array(
285  'error' => _t('_Error Occured'),
286  'protocol_ver' => CH_XMLRPC_PROTOCOL_VER,
287  );
288 
289  }
290 
291  }
292  }
293 
301  {
302  if (!$this->_oConfig->bAutoFriends) {
303  return;
304  }
305 
306  try {
307  //get friends from facebook
308  $oFriendsResponse = $this->oFacebook->get('/me/friends?limit=5000');
309  } catch (Facebook\Exceptions\FacebookResponseException $e) {
310  return;
311  } catch (Facebook\Exceptions\FacebookSDKException $e) {
312  return;
313  }
314 
315  // paginate through the result
316  $oPagesEdge = $oFriendsResponse->getGraphEdge();
317  do {
318  foreach ($oPagesEdge as $oPage) {
319  $aFriend = $oPage->asArray();
320 
321  $iFriendId = $this->_oDb->getProfileId($aFriend['id']);
322  if ($iFriendId && !is_friends($iProfileId, $iFriendId)) {
323  //add to friends list
324  $this->_oDb->makeFriend($iProfileId, $iFriendId);
325 
326  //create system alert
327  $oZ = new ChWsbAlerts('friend', 'accept', $iProfileId, $iFriendId);
328  $oZ->alert();
329  }
330  }
331  } while ($oPagesEdge = $this->oFacebook->next($oPagesEdge));
332 
333  }
334 
340  protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
341  {
342  // process the date of birth
343  if (isset($aProfileInfo['birthday'])) {
344  $aProfileInfo['birthday'] = isset($aProfileInfo['birthday'])
345  ? date('Y-m-d', strtotime($aProfileInfo['birthday']))
346  : '';
347  }
348 
349  // define user's country and city
350  $aLocation = array();
351  if (isset($aProfileInfo['location']['name'])) {
352  $aLocation = $aProfileInfo['location']['name'];
353  } elseif (isset($aProfileInfo['hometown']['name'])) {
354  $aLocation = $aProfileInfo['hometown']['name'];
355  }
356 
357  if ($aLocation) {
358  $aCountryInfo = explode(',', $aLocation);
359  $sCountry = $this->_oDb->getCountryCode(trim($aCountryInfo[1]));
360  $sCity = trim($aCountryInfo[0]);
361 
362  //set default country name, especially for American brothers
363  if ($sCity && !$sCountry) {
364  $sCountry = $this->_oConfig->sDefaultCountryCode;
365  }
366  }
367 
368  // try define the user's email
369  $sEmail = !empty($aProfileInfo['email'])
370  ? $aProfileInfo['email']
371  : $aProfileInfo['proxied_email'];
372 
373  // fill array with all needed values
374  $aProfileFields = array(
375  'NickName' => $aProfileInfo['nick_name'] . $sAlternativeName,
376  'Email' => $sEmail,
377  'Sex' => isset($aProfileInfo['gender']) ? $aProfileInfo['gender'] : '',
378  'DateOfBirth' => $aProfileInfo['birthday'],
379 
380  'Password' => $aProfileInfo['password'],
381 
382  'FullName' => (isset($aProfileInfo['first_name']) ? $aProfileInfo['first_name'] : '') . (isset($aProfileInfo['last_name']) ? ' ' . $aProfileInfo['last_name'] : ''),
383 
384  'DescriptionMe' => clear_xss(isset($aProfileInfo['bio']) ? $aProfileInfo['bio'] : ''),
385  'Interests' => isset($aProfileInfo['interests']) ? $aProfileInfo['interests'] : '',
386 
387  'Religion' => isset($aProfileInfo['religion']) ? $aProfileInfo['religion'] : '',
388  'Country' => $sCountry,
389  'City' => $sCity,
390  );
391 
392  return $aProfileFields;
393  }
394 
401  function _proccesNickName($sProfileName)
402  {
403  $sProfileName = preg_replace("/^http:\/\/|^https:\/\/|\/$/", '', $sProfileName);
404  $sProfileName = str_replace('/', '_', $sProfileName);
405  $sProfileName = str_replace('.', '-', $sProfileName);
406 
407  return $sProfileName;
408  }
409 
410  function _setAccessToken($sToken = '')
411  {
412  if ($sToken) {
413  $this->oFacebook->setDefaultAccessToken($sToken);
414 
415  return '';
416  }
417 
418  $oFacebookRedirectLoginHelper = $this->oFacebook->getRedirectLoginHelper();
419 
420  try {
421  $sAccessToken = $oFacebookRedirectLoginHelper->getAccessToken();
422  } catch (Facebook\Exceptions\FacebookResponseException $e) {
423  // When Graph returns an error
424  return $e->getMessage();
425  } catch (Facebook\Exceptions\FacebookSDKException $e) {
426  // When validation fails or other local issues
427  return $e->getMessage();
428  }
429 
430  if (!isset($sAccessToken)) {
431  return $oFacebookRedirectLoginHelper->getError() ? $oFacebookRedirectLoginHelper->getErrorDescription() : _t('_Error occured');
432  }
433 
434  $this->oFacebook->setDefaultAccessToken($sAccessToken);
435 
436  return '';
437  }
438 
439 }
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
ChFaceBookConnectModule\__construct
__construct(&$aModule)
Definition: ChFaceBookConnectModule.php:32
$sToken
$sToken
Definition: get_file.php:13
ChFaceBookConnectModule\_convertRemoteFields
_convertRemoteFields($aProfileInfo, $sAlternativeName='')
Definition: ChFaceBookConnectModule.php:340
ChWsbConnectModule\_createProfileRaw
_createProfileRaw($aProfileInfo, $sAlternativeName='', $isAutoFriends=true, $isSetLoggedIn=true)
Definition: ChWsbConnectModule.php:254
MsgBox
MsgBox($sText, $iTimer=0)
Definition: design.inc.php:175
ChWsbModule\isLogged
isLogged()
Definition: ChWsbModule.php:113
ChWsbConnectModule\getJoinAfterPaymentPage
getJoinAfterPaymentPage($aProfileInfo)
Definition: ChWsbConnectModule.php:116
ChFaceBookConnectModule\$oFacebook
$oFacebook
Definition: ChFaceBookConnectModule.php:17
ChFaceBookConnectModule\serviceLogin
serviceLogin($aFacebookProfileInfo, $sToken='')
Definition: ChFaceBookConnectModule.php:183
is_friends
is_friends($id1, $id2)
Definition: utils.inc.php:140
$sCode
$sCode
Definition: explanation.php:19
$aModule
$aModule
Definition: classifieds.php:21
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
php
$oZ
$oZ
Definition: db.php:20
ChFaceBookConnectModule\actionAdministration
actionAdministration()
Definition: ChFaceBookConnectModule.php:56
$oPage
$oPage
Definition: search_home.php:83
ChFaceBookConnectModule
Definition: ChFaceBookConnectModule.php:16
ChWsbAlerts
Definition: ChWsbAlerts.php:39
exit
exit
Definition: cart.php:21
ChFaceBookConnectModule\actionLoginForm
actionLoginForm()
Definition: ChFaceBookConnectModule.php:150
ChWsbConnectModule\setLogged
setLogged($iProfileId, $sPassword, $sCallbackUrl='', $bRedirect=true)
Definition: ChWsbConnectModule.php:73
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
CH_XMLRPC_PROTOCOL_VER
const CH_XMLRPC_PROTOCOL_VER
Definition: ChWsbXMLRPCUser.php:8
ChFaceBookConnectModule\_proccesNickName
_proccesNickName($sProfileName)
Definition: ChFaceBookConnectModule.php:401
ChFaceBookConnectModule\actionLoginCallback
actionLoginCallback()
Definition: ChFaceBookConnectModule.php:67
ChFaceBookConnectModule\serviceSupported
serviceSupported()
Definition: ChFaceBookConnectModule.php:178
ChWsbConnectModule\_assignAvatar
_assignAvatar($sAvatarUrl, $iProfileId=false)
Definition: ChWsbConnectModule.php:157
getUsername
getUsername( $ID='')
Definition: profiles.inc.php:443
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
Facebook
$aProfileInfo
$aProfileInfo
Definition: short_profile_info.php:23
ChWsbConnectModule\_createProfile
_createProfile($aProfileInfo, $sAlternativeName='')
Definition: ChWsbConnectModule.php:193
ChFaceBookConnectModule\_makeFriends
_makeFriends($iProfileId)
Definition: ChFaceBookConnectModule.php:300
$sCountry
$sCountry
Definition: browse.php:26
getID
getID( $str, $with_email=1)
Definition: admin.inc.php:139
getProfileInfo
getProfileInfo($iProfileID=0, $checkActiveStatus=false, $forceCache=false)
Definition: profiles.inc.php:249
$sError
$sError
Definition: index.php:425
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Facebook\Facebook
Definition: Facebook.php:52
ChFaceBookConnectModule\_setAccessToken
_setAccessToken($sToken='')
Definition: ChFaceBookConnectModule.php:410
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29
ChWsbConnectModule
Definition: ChWsbConnectModule.php:11
clear_xss
clear_xss($val)
Definition: utils.inc.php:700