Example #1
0
 public function processRegistration($post_data, &$remember)
 {
     $remember = $this->remember;
     $facebook = new Facebook(array('appId' => $this->appID, 'secret' => $this->secret, 'cookie' => true));
     $session = $facebook->getSession();
     if (!$session) {
         return null;
     }
     try {
         $fbuser = $facebook->getUser();
     } catch (FacebookApiException $e) {
         error_log("Can't get Facebook user");
         return null;
     }
     $errors = array();
     if (is_int($fbuser)) {
         $errors['fbuserid'][] = 'No Facebook id is passed';
         throw new InputValidationException('No facebook user id', 0, $errors);
     }
     // checking if user with this Facebook ID already exists and if so, then logs them in
     $existing_user = User::getUserByFacebookID($fbuser);
     if (!is_null($existing_user)) {
         $existing_user->recordActivity(USERBASE_ACTIVITY_LOGIN_FB);
         return $existing_user;
     }
     try {
         $me = $facebook->api('/me');
     } catch (FacebookApiException $e) {
         error_log("Can't get /me API data");
         return null;
     }
     if (array_key_exists('name', $me)) {
         $name = $me['name'];
     } else {
         $errors['username'][] = "User doesn't have a name";
     }
     if (count($errors) > 0) {
         throw new ExistingUserException('User already exists', 0, $errors);
     }
     // ok, let's create a user
     $user = User::createNewFacebookUser($name, $fbuser, $me);
     $user->recordActivity(USERBASE_ACTIVITY_REGISTER_FB);
     return $user;
 }