Esempio n. 1
0
 public function facebook()
 {
     $settings = Setting::first();
     if ($settings->user_registration) {
         // get data from input
         $code = Input::get('code');
         // check if code is valid
         // if code is provided get user data and login
         if (!empty($code)) {
             $facebook = new Facebook(array('appId' => $settings->fb_key, 'secret' => $settings->fb_secret_key, 'cookie' => true, 'oauth' => true));
             // This was a callback request from google, get the token
             $token = $facebook->getAccessToken();
             $facebook->setAccessToken($token);
             $user = $facebook->getUser();
             $user_profile = $facebook->api('/me');
             //print_r($user_profile); die();
             // Send a request with it
             $result = $facebook->api('/me');
             $oauth_userid = $result['id'];
             $oauth_username = $result['first_name'];
             $oauth_email = $result['email'];
             $headers = get_headers('http://graph.facebook.com/' . $oauth_userid . '/picture?type=large', 1);
             // just a precaution, check whether the header isset...
             if (isset($headers['Location'])) {
                 $oauth_picture = $headers['Location'];
                 // string
             } else {
                 $oauth_picture = '';
                 // nothing there? .. weird, but okay!
             }
             if (isset($oauth_userid) && isset($oauth_username) && isset($oauth_email) && isset($oauth_picture)) {
                 $fb_auth = OauthFacebook::where('oauth_userid', '=', $oauth_userid)->first();
                 if (isset($fb_auth->id)) {
                     $user = User::find($fb_auth->user_id);
                 } else {
                     // Execute Add or Login Oauth User
                     $user = User::where('email', '=', $oauth_email)->first();
                     if (!isset($user->id)) {
                         $username = $this->create_username_if_exists($oauth_username);
                         $email = $oauth_email;
                         $password = Hash::make($this->rand_string(15));
                         try {
                             $oauth_image = ImageHandler::uploadImage($oauth_picture, 'avatars', $username, 'url');
                         } catch (Exception $e) {
                             $oauth_image = 'default.jpg';
                         }
                         $user = $this->new_user($username, $email, $password, $oauth_image);
                         $this->new_user_points($user->id);
                         $new_oauth_user = new OauthFacebook();
                         $new_oauth_user->user_id = $user->id;
                         $new_oauth_user->oauth_userid = $oauth_userid;
                         $new_oauth_user->save();
                     } else {
                         // Redirect and send error message that email already exists. Let them know that they can request to reset password if they do not remember
                         return Redirect::to('/')->with(array('note' => Lang::get('lang.oauth_email_used'), 'note_type' => 'error'));
                     }
                 }
                 // Redirect to new User Login;
                 Auth::login($user);
                 $this->add_user_login_point();
                 return Redirect::to('/')->with(array('note' => Lang::get('lang.facebook_success'), 'note_type' => 'success'));
             } else {
                 // Something went wrong, redirect and send error msg
                 echo Lang::get('lang.oauth_error');
                 echo '<br />Info retrieved:<br />';
                 echo '<br />userid: ' . $oauth_userid;
                 echo '<br />username: '******'<br />email: ' . $oauth_email;
                 echo '<br />picture: ' . $oauth_picture;
             }
         } else {
             // get fb authorization
             $url = $fb->getAuthorizationUri();
             // return to facebook login url
             return Response::make()->header('Location', (string) $url);
         }
     } else {
         return Redirect::to('/')->with(array('note' => Lang::get('lang.signup_reg_closed'), 'note_type' => 'error'));
     }
 }
Esempio n. 2
0
 public function facebook()
 {
     if (Session::has('authtoken')) {
         Session::put('auth', '1');
     }
     $settings = Setting::first();
     if ($settings->user_registration) {
         // get data from input
         $code = Input::get('code');
         // get fb service
         $fb = OAuth::consumer('Facebook');
         // check if code is valid
         // if code is provided get user data and sign in
         if (!empty($code)) {
             // This was a callback request from google, get the token
             $token = $fb->requestAccessToken($code);
             // Send a request with it
             $result = json_decode($fb->request('/me?fields=picture,email,id,username'), true);
             $oauth_userid = $result['id'];
             $oauth_username = $result['username'];
             $oauth_email = $result['email'];
             $oauth_picture = 'http://graph.facebook.com/' . $oauth_userid . '/picture?type=large';
             if (isset($oauth_userid) && isset($oauth_username) && isset($oauth_email) && isset($oauth_picture)) {
                 $fb_auth = OauthFacebook::where('oauth_userid', '=', $oauth_userid)->first();
                 if (isset($fb_auth->id)) {
                     $user = User::find($fb_auth->user_id);
                 } else {
                     // Execute Add or Login Oauth User
                     $user = User::where('email', '=', $oauth_email)->first();
                     if (!isset($user->id)) {
                         $username = $this->create_username_if_exists($oauth_username);
                         $email = $oauth_email;
                         $password = Hash::make($this->rand_string(15));
                         $user = $this->new_user($username, $email, $password, $this->uploadImageFromURL($oauth_picture, $username));
                         $this->new_user_points($user->id);
                         $new_oauth_user = new OauthFacebook();
                         $new_oauth_user->user_id = $user->id;
                         $new_oauth_user->oauth_userid = $oauth_userid;
                         $new_oauth_user->save();
                     } else {
                         // Redirect and send error message that email already exists. Let them know that they can request to reset password if they do not remember
                         return Redirect::to('signin')->with(array('errors' => 'Email is already in use.'));
                     }
                 }
                 // Redirect to new User Login;
                 Auth::login($user, true);
                 if (Session::has('authtoken')) {
                     $user = User::where('id', '=', Auth::user()->id)->first();
                     if (count($user) != 0) {
                         $api_key = md5(microtime() . rand());
                         $token = Session::get('authtoken');
                         $user->token = $token;
                         $user->api_key = $api_key;
                         $user->api_key_date = date("Y-m-d H:i:s");
                         $user->save();
                     }
                     Session::forget('authtoken');
                     Session::forget('auth');
                     return "<script type='text/javascript'> window.close();</script>";
                 }
                 return Redirect::intended('/');
             } else {
                 // Something went wrong, redirect and send error msg
                 echo 'Some Oauth information was not able to get retrieved. Please try again.';
                 echo '<br />Info retrieved:<br />';
                 echo '<br />userid: ' . $oauth_userid;
                 echo '<br />username: '******'<br />email: ' . $oauth_email;
                 echo '<br />picture: ' . $oauth_picture;
             }
         } else {
             // get fb authorization
             $url = $fb->getAuthorizationUri();
             // return to facebook login url
             return Response::make()->header('Location', (string) $url);
         }
     } else {
         return Redirect::to('signin')->with(array('errors' => 'Sorry, Registration has been closed.'));
     }
 }