Share Pastebin
Guest
Public paste!

Gravity OAuth class

By: a guest | Mar 20th, 2010 | Syntax: PHP | Size: 4.66 KB | Hits: 43 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2. /**
  3.  * Gravity OAuth class
  4.  * 2010 ElbertF http://elbertf.com
  5.  * http://www.gnu.org/licenses/gpl-2.0.txt GNU Public License
  6.  */
  7.  
  8. session_start();
  9.  
  10. $gravity = new GravityOAuth;
  11.  
  12. if ( $gravity )
  13. {
  14.         $userInfo = $gravity->get_user_info();
  15.  
  16.         echo 'User info: <pre>', print_r($userInfo), '</pre>';
  17. }
  18.  
  19. /*
  20.  * Gravity OAuth
  21.  */
  22. class GravityOAuth
  23. {
  24.         private
  25.                 /*
  26.                  * Set your client_id, client_secret and callback_url
  27.                  */
  28.                 $clientId     = '',
  29.                 $clientSecret = '',
  30.                 $callbackURL  = '',
  31.  
  32.                 $wrapVerificationCode     = '',
  33.                 $wrapVerificationCodeTTL  = ''
  34.                 ;
  35.        
  36.         public
  37.                 $auth = array()
  38.                 ;
  39.  
  40.         /*
  41.          * Initialize
  42.          */
  43.         function __construct()
  44.         {
  45.                 /*
  46.                  * Resume from saved session
  47.                  */
  48.                 if ( !empty($_SESSION['auth']) )
  49.                 {
  50.                         $this->auth = unserialize($_SESSION['auth']);
  51.                 }
  52.  
  53.                 /*
  54.                  * User authorized the request
  55.                  */
  56.                 if ( isset($_GET['wrap_verification_code']) && isset($_GET['wrap_verification_code_ttl']) )
  57.                 {
  58.                         $this->wrapVerificationCode    = $_GET['wrap_verification_code'];
  59.                         $this->wrapVerificationCodeTTL = $_GET['wrap_verification_code_ttl'];
  60.  
  61.                         $this->get_tokens();
  62.                 }
  63.  
  64.                 if ( $this->auth['wrapAccessToken'] )
  65.                 {
  66.                         $_SESSION['auth'] = serialize($this->auth);
  67.  
  68.                         return TRUE;
  69.                 }
  70.                 else
  71.                 {
  72.                         /*
  73.                          * Request authorization from user
  74.                          */
  75.                         header('Location: https://api.gravity.com/beta/wrap/authorize?wrap_client_id=' . $this->clientId . '&wrap_callback=' . rawurlencode($this->callbackURL));
  76.                 }
  77.         }
  78.  
  79.         /*
  80.          * Get tokens
  81.          */
  82.         private function get_tokens()
  83.         {
  84.                 $url = 'https://api.gravity.com/beta/wrap/access_token';
  85.  
  86.                 if ( !function_exists('curl_init') )
  87.                 {
  88.                         die('cURL is not installed, see http://www.php.net/manual/en/book.curl.php');
  89.                 }
  90.  
  91.                 $session = curl_init();
  92.  
  93.                 curl_setopt($session, CURLOPT_URL,            $url);
  94.                 curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
  95.                 curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE);
  96.                 curl_setopt($session, CURLOPT_POST,           TRUE);
  97.                 curl_setopt($session, CURLOPT_POSTFIELDS,     array(
  98.                         'wrap_client_id'         => $this->clientId,
  99.                         'wrap_client_secret'     => $this->clientSecret,
  100.                         'wrap_verification_code' => $this->wrapVerificationCode,
  101.                         'wrap_callback'          => $this->callbackURL
  102.                         ));
  103.  
  104.                 $json = curl_exec($session);
  105.                 $info = curl_getinfo($session);
  106.  
  107.                 if ( $info['http_code'] == 200 )
  108.                 {
  109.                         $r = json_decode($json);
  110.  
  111.                         $this->auth = array(
  112.                                 'wrapAccessToken'          => $r->wrap_access_token,
  113.                                 'wrapAccessTokenExpiresIn' => $r->wrap_access_token_expires_in,
  114.                                 'wrapRefreshToken'         => $r->wrap_refresh_token,
  115.                                 'gravityUsername'          => $r->gravity_username,
  116.                                 'gravityUserAvatar'        => $r->gravity_user_avatar
  117.                                 );
  118.                 }
  119.                 else
  120.                 {
  121.                         die('Failed to get access token, HTTP code ' .  $info['http_code']);
  122.                 }
  123.  
  124.                 curl_close($session);
  125.         }
  126.  
  127.         /*
  128.          * Refresh access token
  129.          */
  130.         private function refresh_access_token()
  131.         {
  132.                 $url = 'https://api.gravity.com/beta/wrap/access_token';
  133.  
  134.                 $session = curl_init();
  135.                                        
  136.                 curl_setopt($session, CURLOPT_URL,            $url);
  137.                 curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
  138.                 curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE);
  139.                 curl_setopt($session, CURLOPT_POST,           TRUE);
  140.                 curl_setopt($session, CURLOPT_POSTFIELDS,     array(
  141.                         'wrap_client_id'     => $this->clientId,
  142.                         'wrap_refresh_token' => $$this->auth['refreshToken']
  143.                         ));
  144.  
  145.                 $json = curl_exec($session);
  146.                 $info = curl_getinfo($session);
  147.  
  148.                 if ( $info['http_code'] == 200 )
  149.                 {
  150.                         $r = json_decode($json);
  151.  
  152.                         $this->auth = array(
  153.                                 'wrapAccessToken'          => $r->wrap_access_token,
  154.                                 'wrapAccessTokenExpiresIn' => $r->wrap_access_token_expires_in
  155.                                 );
  156.                 }
  157.                 else
  158.                 {
  159.                         die('Failed to refresh access token, HTTP code ' .  $info['http_code']);
  160.                 }      
  161.         }
  162.  
  163.         /*
  164.          * Get user information
  165.          */
  166.         function get_user_info()
  167.         {
  168.                 $url = 'https://api.gravity.com/beta/user/' . $this->auth['gravityUsername'] . '?wrap_access_token=' . $this->auth['wrapAccessToken'] . '&format=debug';
  169.  
  170.                 $session = curl_init();
  171.  
  172.                 curl_setopt($session, CURLOPT_URL,            $url);
  173.                 curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
  174.                 curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE);
  175.  
  176.                 $json = curl_exec($session);
  177.                 $info = curl_getinfo($session);
  178.  
  179.                 if ( $info['http_code'] == 200 )
  180.                 {
  181.                         return json_decode($json);
  182.                 }
  183.                 else
  184.                 {
  185.                         if ( $info['http_code'] == 401 )
  186.                         {
  187.                                 $this->refresh_access_token();
  188.  
  189.                                 // Try again with the new access token
  190.                                 return $this->get_user_info();
  191.                         }
  192.                         else
  193.                         {
  194.                                 die('Failed to get user info, HTTP code ' .  $info['http_code']);
  195.                         }
  196.                 }
  197.  
  198.                 curl_close($session);
  199.         }
  200. }