Advertisement
FireBot

facebook-connect-modx

Nov 27th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2.  
  3. require '/facebook-connect/facebook.php';
  4.  
  5. //what permissions do we want
  6. $par = array();
  7. $par['req_perms'] = "email,user_hometown,user_website";
  8.  
  9.  
  10. //
  11. // Where do these come from? Add them as Properties to the snippet!
  12. //
  13. $appId= $modx->getOption('appId',$scriptProperties,'');
  14. $secret= $modx->getOption('secret',$scriptProperties,'');
  15.  
  16. if(empty($appId) || empty($secret)){
  17. $output='No AppID or Secret Provided, please obtain from developer.facebook.com';
  18. return $output;
  19. }
  20.  
  21.  
  22. // Create our Application instance.
  23. $facebook =    
  24. new Facebook(array(
  25.   'appId' => $appId,
  26.   'secret' => $secret,
  27.   'cookie' => true,
  28. ));      
  29. $session = $facebook->getSession();
  30.  
  31.  
  32. /*
  33. * Make the appID and current session available to the front end
  34. * (in case you want to add more social features using Javascript, specify those in the init() call)
  35. */
  36. $modx->toPlaceholder('facebook_session',json_encode($session));
  37. $modx->toPlaceholder('facebook_app_id',$facebook->getAppId());
  38.  
  39. $output="";
  40.  
  41. $me = null;
  42. // Session based API call.
  43. if ($session) {
  44.   try {
  45.     $uid = $facebook->getUser();
  46.     $me = $facebook->api('/me');
  47.  
  48.   } catch (FacebookApiException $e) {
  49.     error_log($e);
  50.   }
  51. }
  52.  
  53. // login or logout url will be needed depending on current user state.
  54. if ($me) {
  55. //die(print_r($me));
  56.  
  57.     $contexts = empty($contexts) ? array($modx->context->get('key')) : explode(',', $contexts);
  58.         foreach (array_keys($contexts) as $ctxKey) {
  59.         $contexts[$ctxKey] = trim($contexts[$ctxKey]);
  60.         }
  61.  
  62.     $user = $modx->getObject('modUser',  array('remote_key:=' => $me['id'], 'remote_key:!=' => null));
  63.  
  64.     if(empty($user)){
  65.             //their new!
  66.         //facebook may pass multiple hometowns back
  67.         if(!empty($me['hometown'])){
  68.             if(is_array($me['hometown'])){
  69.                $homet=$me['hometown'][0];
  70.             }else{
  71.                    $homet=$me['hometown'];
  72.             }
  73.         }
  74.  
  75.         // Create an empty modx user and populate with facebvook data
  76.         $user = $modx->newObject('modUser');
  77.         $user->fromArray(
  78.                           array('username' => $me['name'],'active' => true
  79.                         ,'remote_key' => $me['id'] ,'remote_data' => $me //store the remote data as json object in db (in case you need more info bout the FB user later)
  80.                      )
  81.                    );
  82.  
  83. //We'll also toss a profile on to save their email and photo and such
  84.         $profile = $modx->newObject('modUserProfile');
  85.         $profile->fromArray(array(
  86.         'email' => isset($me['email']) ? $me['email'] : 'facebook-user@facebook.com'
  87.         ,'fullname' => $me['name']
  88.         ,'city' => $homet
  89.                 ,'photo'=> 'http://graph.facebook.com/'. $me['id'] .'/picture'
  90.         ));
  91.         $user->addOne($profile, 'Profile');
  92.  
  93.  
  94. /** Login, (C) 2010, Jason Coward, Shaun McCormick**/
  95.  
  96.         /* if usergroups set */
  97.         $usergroups = $modx->getOption('usergroups',$scriptProperties,'');
  98.         if (!empty($usergroups)) {
  99.             $usergroups = explode(',',$usergroups);
  100.  
  101.             foreach ($usergroups as $usergroupMeta) {
  102.             $usergroupMeta = explode(':',$usergroupMeta);
  103.             if (empty($usergroupMeta[0])) continue;
  104.  
  105.             /* get usergroup */
  106.             $pk = array();
  107.             $pk[intval($usergroupMeta[0]) > 0 ? 'id' : 'name'] = $usergroupMeta[0];
  108.             $usergroup = $modx->getObject('modUserGroup',$pk);
  109.             if (!$usergroup) continue;
  110.  
  111.             /* get role */
  112.             $rolePk = !empty($usergroupMeta[1]) ? $usergroupMeta[1] : 'Member';
  113.             $role = $modx->getObject('modUserGroupRole',array('name' => $rolePk));
  114.  
  115.             /* create membership */
  116.             $member = $modx->newObject('modUserGroupMember');
  117.             $member->set('member',0);
  118.             $member->set('user_group',$usergroup->get('id'));
  119.             if (!empty($role)) {
  120.                 $member->set('role',$role->get('id'));
  121.             } else {
  122.                 $member->set('role',1);
  123.             }
  124.             $user->addMany($member,'UserGroupMembers');
  125.             }//end foreach
  126.         }//end user grops
  127. /** End Login Code Block froom Login, (C) 2010 Jason Coward, Shaun McCormick **/
  128.         $saved = $user->save();
  129.     }//end if new user
  130.  
  131.     //new or not, they're logged in
  132.         if(!$user->hasSessionContext('web')){
  133.        foreach ($contexts as $context) {
  134.         $user->addSessionContext($context);
  135.        }
  136.  
  137.        $modx->sendRedirect('/');
  138.         }
  139.  
  140. } else {
  141.     //else give them the chance to login
  142. /* This should parse the generated URL with a chunk, for now you can edit the image or text here */
  143.     $output.= '<a href="'.$facebook->getLoginUrl($par).'"><img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"></a>';
  144. }
  145.  
  146.  
  147. return $output;
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement