/**
  * @return mixed
  */
 public function pubpriv()
 {
     /**
      * Verify CSRF token.
      */
     if ($_POST['_token'] !== Session::token()) {
         return Response::json(array('error' => true));
     }
     /**
      * Session validation
      */
     $session = Session::get('uber_profile');
     if (!isset($session) || $session['utid'] !== $_POST['utid']) {
         return Response::json(array('error' => true));
     }
     /**
      * Find Uber row and change public/private status.
      */
     $uber = Uber::where('utid', $_POST['utid'])->first();
     $status = $_POST['status'] == 1 ? false : true;
     $uber->public = $status;
     $uber->save();
     /**
      * Respond with json success data.
      */
     return Response::json(array('success' => true, 'dump' => $uber->public));
 }
 /**
  * Execute the command.
  *
  * @return void
  */
 public function handle()
 {
     $user_id = Auth::user()->id;
     $ubers = User::find($user_id)->ubers;
     /**
      * If they don't have any Uber accounts, let's go home.
      */
     if (!$ubers->toArray()) {
         $this->go_home();
     }
     /**
      * If the user ID is returned (deauth successful),
      * delete the object from DB.
      */
     $results = Uber::where('access_token', $this->request->input('delete_uber'))->get();
     $uber_object = $results[0];
     $uber_object->delete();
     $params = array('uber' => 'deleted');
     /**
      *  Redirect as needed.
      */
     $queryString = http_build_query($params);
     header("Location: " . action('PagesController@home', $queryString));
     die;
 }
 /**
  * Execute the command.
  *
  * @return void
  */
 public function handle()
 {
     /**
      * If scope/code not set, go home.
      */
     if (!isset($_GET['code'])) {
         $this->go_home();
     }
     /**
      * This method is used to get the access token from stripe,
      * by passing in the Auth returned code.
      */
     $client = new Client();
     $response = $client->post('https://login.uber.com/oauth/token', ['body' => ['client_id' => env('UBER_CLIENT_ID'), 'client_secret' => env('UBER_CLIENT_SECRET'), 'redirect_uri' => env('UBER_REDIRECT_URI'), 'code' => $_GET['code'], 'grant_type' => 'authorization_code']])->json();
     $client = new UberClient(array('access_token' => $response['access_token'], 'server_token' => env('UBER_SERVER_TOKEN'), 'use_sandbox' => false, 'version' => 'v1', 'locale' => 'en_US'));
     $uber_profile = $client->getProfile();
     $client_new = new UberClient(array('access_token' => $response['access_token'], 'server_token' => env('UBER_SERVER_TOKEN'), 'use_sandbox' => false, 'version' => 'v1.1', 'locale' => 'en_US'));
     $history = $client_new->getHistory(array('limit' => 50, 'offset' => 0));
     if ($history->count == 0) {
         $queryString = http_build_query(array('failed' => '2'));
         header("Location: " . action('PagesController@home', $queryString));
     }
     /**
      * We're not using users or accounts here, so we're going to
      * save the data inside a Session. We will use a common key
      * naming system that prevents multiple sessions being
      * created for the same user / data.
      * @todo encrypt sessions (http://laravel.com/docs/5.0/session)
      * @todo and SAVE profile data in cache, DB, if successful
      */
     $utid = substr($uber_profile->uuid, -8);
     $data = ['utid' => $utid, 'uuid' => $uber_profile->uuid, 'access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token']];
     /**
      * Handle DB adding, updating stuff.
      * @var DB $uber
      */
     $uber = Uber::firstOrCreate(['uuid' => $uber_profile->uuid]);
     $uber->utid = $utid;
     $uber->access_token = $response['access_token'];
     $uber->refresh_token = $response['refresh_token'];
     $uber->save();
     /**
      * Save to session.
      */
     Session::put('uber_profile', $data);
     if ($response['access_token']) {
         $params = array('utid' => $utid);
     } else {
         $params = array('uber' => 'failed', 'error' => 1);
     }
     /**
      * Add them to MailChimp List
      */
     $MailChimp = new MailChimp(env('MAILCHIMP_ID'));
     $MailChimp->call('lists/subscribe', array('id' => env('MAILCHIMP_LIST'), 'email' => array('email' => $uber_profile->email), 'merge_vars' => array('FNAME' => $uber_profile->first_name, 'LNAME' => $uber_profile->last_name), 'double_optin' => false, 'update_existing' => true, 'replace_interests' => false, 'send_welcome' => false));
     /**
      * Redirect as needed.
      */
     $queryString = http_build_query($params);
     header("Location: " . action('PagesController@home', $queryString));
     // Cannot die(); as this breaks the session storage
 }
Beispiel #4
0
 /**
  * @return mixed
  */
 public function uber()
 {
     return Uber::where('id', $this->utdb_id);
 }
 public function home()
 {
     /**
      * Add in a check for the uber_profile session.
      */
     $session = Session::get('uber_profile');
     $utid = isset($_GET['utid']) ? $_GET['utid'] : '';
     if (!$utid && $session) {
         $utid = $session['utid'];
     }
     $uber = Uber::where('utid', $utid)->get()->first();
     if ($uber) {
         try {
             $uber->uber()->getProfile();
         } catch (Exception $e) {
             $client = new Client();
             $response = $client->post('https://login.uber.com/oauth/token', ['body' => ['client_id' => env('UBER_CLIENT_ID'), 'client_secret' => env('UBER_CLIENT_SECRET'), 'redirect_uri' => env('UBER_REDIRECT_URI'), 'refresh_token' => $uber->refresh_token, 'grant_type' => 'refresh_token']])->json();
             if (isset($response['access_token'])) {
                 $uber->access_token = $response['access_token'];
                 $uber->refresh_token = $response['refresh_token'];
                 $uber->save();
             }
         }
     }
     if ($uber && $uber->get_user_total_rides() > 0) {
         $total_rides = $uber->get_user_total_rides();
         if ($total_rides > 10000) {
             $trips_message = 'Uber God. You win.';
         } elseif ($total_rides > 5000) {
             $trips_message = 'Almost impossible!';
         } elseif ($total_rides > 2000) {
             $trips_message = 'Kudos to you. Bravo.';
         } elseif ($total_rides > 1000) {
             $trips_message = 'Wow. Holy wow.';
         } elseif ($total_rides > 500) {
             $trips_message = 'That\'s a fine effort!';
         } elseif ($total_rides > 200) {
             $trips_message = 'Okay - Nice!';
         } elseif ($total_rides > 100) {
             $trips_message = 'That\'s pretty good ' . $uber->get_first_name() . '!';
         } elseif ($total_rides > 50) {
             $trips_message = 'Going to the top!';
         } elseif ($total_rides > 10) {
             $trips_message = 'A beginner, but trying...';
         } else {
             $trips_message = 'Come on ' . $uber->get_first_name() . '...';
         }
         /**
          * Prepare data for this view, including all Uber stats.
          */
         $data = ['uber_client_id' => env('UBER_CLIENT_ID'), 'name' => $uber->get_first_name(), 'full_name' => $uber->get_full_name(), 'photo' => $uber->get_user_logo(), 'utid' => $utid, 'status' => $uber->public, 'trips_taken_count' => $total_rides, 'trips_message' => $trips_message, 'miles_driven_count' => number_format($uber->get_user_total_distance(), 2), 'miles_driven_average' => number_format($uber->get_user_total_distance_average(), 2), 'total_time_count' => display_seconds_pretty($uber->get_user_total_time()), 'total_time_average' => display_seconds_pretty($uber->get_user_total_time_average()), 'wait_time_count' => display_seconds_pretty($uber->get_user_wait_time()), 'wait_time_average' => display_seconds_pretty($uber->get_user_wait_time_average()), 'products' => $uber->get_product_usage()];
         /**
          * Check if the current session UTID matches the requested one.
          * If so, the current user is the owner of the data.
          */
         if (isset($session) && $session['utid'] == $utid) {
             $data['owner'] = true;
         } else {
             $data['owner'] = false;
         }
         return view('pages.dashboard', $data);
     } else {
         $data = ['uber_client_id' => env('UBER_CLIENT_ID'), 'uber_redirect_uri' => env('UBER_REDIRECT_URI')];
         if (isset($_GET['utid'])) {
             $data['message'] = 'This profile does not exist, is private or has no rides.';
         }
         return view('pages.auth', $data);
     }
 }