Advertisement
FireBot

modx-fb-connect-new

Nov 28th, 2011
163
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. 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.             //their 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 facebvook 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.         'email' => isset($me['email']) ? $me['email'] : 'facebook-user@facebook.com'
  86.         ,'fullname' => $me['name']
  87.         ,'city' => $homet
  88.                 ,'photo'=> 'http://graph.facebook.com/'. $me['id'] .'/picture'
  89.         ));
  90.         $user->addOne($profile, 'Profile');
  91.  
  92.  
  93. /** Login, (C) 2010, Jason Coward, Shaun McCormick**/
  94.  
  95.         /* if usergroups set */
  96.         $usergroups = $modx->getOption('usergroups',$scriptProperties,'');
  97.         if (!empty($usergroups)) {
  98.             $usergroups = explode(',',$usergroups);
  99.  
  100.             foreach ($usergroups as $usergroupMeta) {
  101.             $usergroupMeta = explode(':',$usergroupMeta);
  102.             if (empty($usergroupMeta[0])) continue;
  103.  
  104.             /* get usergroup */
  105.             $pk = array();
  106.             $pk[intval($usergroupMeta[0]) > 0 ? 'id' : 'name'] = $usergroupMeta[0];
  107.             $usergroup = $modx->getObject('modUserGroup',$pk);
  108.             if (!$usergroup) continue;
  109.  
  110.             /* get role */
  111.             $rolePk = !empty($usergroupMeta[1]) ? $usergroupMeta[1] : 'Member';
  112.             $role = $modx->getObject('modUserGroupRole',array('name' => $rolePk));
  113.  
  114.             /* create membership */
  115.             $member = $modx->newObject('modUserGroupMember');
  116.             $member->set('member',0);
  117.             $member->set('user_group',$usergroup->get('id'));
  118.             if (!empty($role)) {
  119.                 $member->set('role',$role->get('id'));
  120.             } else {
  121.                 $member->set('role',1);
  122.             }
  123.             $user->addMany($member,'UserGroupMembers');
  124.             }//end foreach
  125.         }//end user grops
  126. /** End Login Code Block froom Login, (C) 2010 Jason Coward, Shaun McCormick **/
  127.         $saved = $user->save();
  128.     }//end if new user
  129.  
  130.     //new or not, they're logged in
  131.         if(!$user->hasSessionContext('web')){
  132.        foreach ($contexts as $context) {
  133.         $user->addSessionContext($context);
  134.        }
  135.  
  136.        $modx->sendRedirect('/');
  137.         }
  138.  
  139. } else {
  140.     //else give them the chance to login
  141. /* This should parse the generated URL with a chunk, for now you can edit the image or text here */
  142.     $output.= '<a href="'.$facebook->getLoginUrl($par).'"><img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"></a>';
  143. }
  144.  
  145.  
  146. return $output;
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement