Ejemplo n.º 1
0
 /**
  * OAuth2 response handler
  */
 public static function authenticateUser($code)
 {
     $retVal = false;
     $client = self::_createOAuth2();
     $auth = new OAuth2\Strategy\AuthCode($client);
     try {
         $token = $auth->getToken($code, ['redirect_uri' => REDDIT_HANDLER]);
         if ($token) {
             $response = $token->get('https://oauth.reddit.com/api/v1/me.json');
             $data = json_decode($response->body());
             if ($data) {
                 $user = self::getByName($data->name);
                 if (!$user) {
                     // Block out new user accounts
                     if ((int) $data->created > time() - REDDIT_MINAGE) {
                         $retVal = false;
                     } else {
                         $user = new User();
                         $user->name = $data->name;
                         $user->ip = $_SERVER['REMOTE_ADDR'];
                         if ($user->sync()) {
                             $retVal = true;
                         }
                     }
                 } else {
                     $retVal = true;
                 }
                 Lib\Session::set('user', $user);
             }
         }
     } catch (Exception $e) {
     }
     return $retVal;
 }
Ejemplo n.º 2
0
 /**
  * OAuth2 response handler
  */
 public static function authenticateUser($code)
 {
     $retVal = false;
     $client = self::_createOAuth2();
     $auth = new OAuth2\Strategy\AuthCode($client);
     try {
         $token = $auth->getToken($code, ['redirect_uri' => REDDIT_HANDLER]);
         if ($token) {
             $response = $token->get('https://oauth.reddit.com/api/v1/me.json');
             $data = json_decode($response->body());
             if ($data) {
                 $user = self::getByName($data->name);
                 if (!$user) {
                     $user = new User();
                     $user->name = $data->name;
                     $user->age = (int) $data->created;
                     $user->ip = $_SERVER['REMOTE_ADDR'];
                     if ($user->sync()) {
                         $retVal = true;
                     }
                 } else {
                     // This is to update any records that were created before age was tracked
                     if (!$user->age) {
                         $user->age = (int) $data->created;
                         $user->sync();
                     }
                     $retVal = true;
                 }
                 Lib\Session::set('user', $user);
             }
         }
     } catch (Exception $e) {
     }
     return $retVal;
 }