Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require MODX_CORE_PATH . 'components/facebook/facebook.php';
- //what permissions do we want
- $par = array();
- $par['req_perms'] = "email,user_hometown,user_website";
- //
- // Where do these come from? Add them as Properties to the snippet!
- //
- $appId= $modx->getOption('appId',$scriptProperties,'');
- $secret= $modx->getOption('secret',$scriptProperties,'');
- if(empty($appId) || empty($secret)){
- $output='No AppID or Secret Provided, please obtain from developer.facebook.com';
- return $output;
- }
- // Create our Application instance.
- $facebook = new Facebook(array(
- 'appId' => $appId,
- 'secret' => $secret,
- 'cookie' => true,
- ));
- $user = $facebook->getUser();
- /*
- * Make the appID and current session available to the front end
- * (in case you want to add more social features using Javascript, specify those in the init() call)
- */
- $modx->toPlaceholder('facebook_session',json_encode($user));
- $modx->toPlaceholder('facebook_app_id',$facebook->getAppId());
- $output="";
- $me = null;
- // Session based API call.
- if ($user) {
- try {
- $uid = $facebook->getUser();
- $me = $facebook->api('/me');
- } catch (FacebookApiException $e) {
- error_log($e);
- }
- }
- // login or logout url will be needed depending on current user state.
- if ($me) {
- //die(print_r($me));
- $contexts = empty($contexts) ? array($modx->context->get('key')) : explode(',', $contexts);
- foreach (array_keys($contexts) as $ctxKey) {
- $contexts[$ctxKey] = trim($contexts[$ctxKey]);
- }
- $user = $modx->getObject('modUser', array('remote_key:=' => $me['id'], 'remote_key:!=' => null));
- if(empty($user)){
- //they're new!
- //facebook may pass multiple hometowns back
- if(!empty($me['hometown'])){
- if(is_array($me['hometown'])){
- $homet=$me['hometown'][0];
- }else{
- $homet=$me['hometown'];
- }
- }
- // Create an empty modx user and populate with facebook data
- $user = $modx->newObject('modUser');
- $user->fromArray(
- array('username' => $me['name'],'active' => true
- ,'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)
- )
- );
- //We'll also toss a profile on to save their email and photo and such
- $profile = $modx->newObject('modUserProfile');
- $profile->fromArray(array(
- //old email line - isset($me['email']) ? $me['email'] : 'facebook-user@facebook.com'
- 'email' => isset($me['email'])
- ,'fullname' => $me['name']
- ,'city' => $homet
- ,'photo'=> 'http://graph.facebook.com/'. $me['id'] .'/picture'
- ));
- $user->addOne($profile, 'Profile');
- /** Login, (C) 2010, Jason Coward, Shaun McCormick**/
- /* if usergroups set */
- $usergroups = $modx->getOption('usergroups',$scriptProperties,'');
- if (!empty($usergroups)) {
- $usergroups = explode(',',$usergroups);
- foreach ($usergroups as $usergroupMeta) {
- $usergroupMeta = explode(':',$usergroupMeta);
- if (empty($usergroupMeta[0])) continue;
- /* get usergroup */
- $pk = array();
- $pk[intval($usergroupMeta[0]) > 0 ? 'id' : 'name'] = $usergroupMeta[0];
- $usergroup = $modx->getObject('modUserGroup',$pk);
- if (!$usergroup) continue;
- /* get role */
- $rolePk = !empty($usergroupMeta[1]) ? $usergroupMeta[1] : 'Member';
- $role = $modx->getObject('modUserGroupRole',array('name' => $rolePk));
- /* create membership */
- $member = $modx->newObject('modUserGroupMember');
- $member->set('member',0);
- $member->set('user_group',$usergroup->get('id'));
- if (!empty($role)) {
- $member->set('role',$role->get('id'));
- } else {
- $member->set('role',1);
- }
- $user->addMany($member,'UserGroupMembers');
- }//end foreach
- }//end user grops
- /** End Login Code Block froom Login, (C) 2010 Jason Coward, Shaun McCormick **/
- $saved = $user->save();
- }//end if new user
- //new or not, they're logged in
- if(!$user->hasSessionContext('web')){
- foreach ($contexts as $context) {
- $user->addSessionContext($context);
- }
- $modx->sendRedirect('/');
- }
- } else {
- //else give them the chance to login
- /* This should parse the generated URL with a chunk, for now you can edit the image or text here */
- $output.= '<a href="'.$facebook->getLoginUrl($par).'"><img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"></a>';
- }
- return $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement