1. <?php
  2. require MODX_CORE_PATH . 'components/facebook/facebook.php';
  3.  
  4. //what permissions do we want
  5. $par = array();
  6. $par['req_perms'] = "email,user_hometown,user_website";
  7.  
  8.  
  9. //
  10. // Where do these come from? Add them as Properties to the snippet!
  11. //
  12. $appId= $modx->getOption('appId',$scriptProperties,'');
  13. $secret= $modx->getOption('secret',$scriptProperties,'');
  14.  
  15. if(empty($appId) || empty($secret)){
  16. $output='No AppID or Secret Provided, please obtain from developer.facebook.com';
  17. return $output;
  18. }
  19.  
  20.  
  21. // Create our Application instance.
  22. $facebook = new Facebook(array(
  23.   'appId' => $appId,
  24.   'secret' => $secret,
  25.   'cookie' => true,
  26. ));
  27.  
  28. $user = $facebook->getUser();
  29.  
  30.  
  31. /*
  32. * Make the appID and current session available to the front end
  33. * (in case you want to add more social features using Javascript, specify those in the init() call)
  34. */
  35. $modx->toPlaceholder('facebook_session',json_encode($user));
  36. $modx->toPlaceholder('facebook_app_id',$facebook->getAppId());
  37.  
  38. $output="";
  39.  
  40. $me = null;
  41. // Session based API call.
  42. if ($user) {
  43.   try {
  44.     $uid = $facebook->getUser();
  45.     $me = $facebook->api('/me');
  46.  
  47.   } catch (FacebookApiException $e) {
  48.     error_log($e);
  49.   }
  50. }
  51.  
  52. // login or logout url will be needed depending on current user state.
  53. if ($me) {
  54. //die(print_r($me));
  55.  
  56.     $contexts = empty($contexts) ? array($modx->context->get('key')) : explode(',', $contexts);
  57.         foreach (array_keys($contexts) as $ctxKey) {
  58.         $contexts[$ctxKey] = trim($contexts[$ctxKey]);
  59.         }
  60.  
  61.     $user = $modx->getObject('modUser',  array('remote_key:=' => $me['id'], 'remote_key:!=' => null));
  62.  
  63.     if(empty($user)){
  64.             //they're new!
  65.         //facebook may pass multiple hometowns back
  66.         if(!empty($me['hometown'])){
  67.             if(is_array($me['hometown'])){
  68.                $homet=$me['hometown'][0];
  69.             }else{
  70.                    $homet=$me['hometown'];
  71.             }
  72.         }
  73.  
  74.         // Create an empty modx user and populate with facebook data
  75.         $user = $modx->newObject('modUser');
  76.         $user->fromArray(
  77.                           array('username' => $me['name'],'active' => true
  78.                         ,'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)
  79.                      )
  80.                    );
  81.  
  82.                //We'll also toss a profile on to save their email and photo and such
  83.         $profile = $modx->newObject('modUserProfile');
  84.         $profile->fromArray(array(
  85.                 //old email line - isset($me['email']) ? $me['email'] : 'facebook-user@facebook.com'
  86.         'email' =>  isset($me['email'])
  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.