/application/third_party/hybridauth/additional-providers/hybridauth-yammer/thirdparty/Yammer/Yammer.php

https://gitlab.com/alfianfaiz/appstar · PHP · 279 lines · 192 code · 60 blank · 27 comment · 32 complexity · 3ad2ea28f022ad3060bd5364982c3a5d MD5 · raw file

  1. <?php
  2. /*!
  3. * HybridAuth
  4. * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
  5. * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
  6. */
  7. // A service client for the OAuth 2 flow.
  8. // v0.1
  9. class YammerOAuth2Client extends OAuth2Client
  10. {
  11. public function authenticate( $code )
  12. {
  13. $params = array(
  14. "client_id" => $this->client_id,
  15. "client_secret" => $this->client_secret,
  16. "grant_type" => "authorization_code",
  17. "redirect_uri" => $this->redirect_uri,
  18. "code" => $code
  19. );
  20. $response = $this->request( $this->token_url, $params, $this->curl_authenticate_method );
  21. $response = $this->parseRequestResult( $response );
  22. if( ! $response || ! isset( $response->access_token ) ){
  23. throw new Exception( "The Authorization Service has return: " . $response->error );
  24. }
  25. if( isset( $response->access_token ) ) $this->access_token = $response->access_token->token;//for yammer
  26. if( isset( $response->refresh_token ) ) $this->refresh_token = $response->refresh_token;
  27. if( isset( $response->expires_in ) ) $this->access_token_expires_in = $response->expires_in;
  28. // calculate when the access token expire
  29. if( isset($response->expires_in)) {
  30. $this->access_token_expires_at = time() + $response->expires_in;
  31. }
  32. return $response;
  33. }
  34. }
  35. class OAuth2Client
  36. {
  37. public $api_base_url = "";
  38. public $authorize_url = "";
  39. public $token_url = "";
  40. public $token_info_url = "";
  41. public $client_id = "" ;
  42. public $client_secret = "" ;
  43. public $redirect_uri = "" ;
  44. public $access_token = "" ;
  45. public $refresh_token = "" ;
  46. public $access_token_expires_in = "" ;
  47. public $access_token_expires_at = "" ;
  48. //--
  49. public $sign_token_name = "access_token";
  50. public $decode_json = true;
  51. public $curl_time_out = 30;
  52. public $curl_connect_time_out = 30;
  53. public $curl_ssl_verifypeer = false;
  54. public $curl_header = array();
  55. public $curl_useragent = "OAuth/2 Simple PHP Client v0.1; HybridAuth http://hybridauth.sourceforge.net/";
  56. public $curl_authenticate_method = "POST";
  57. public $curl_proxy = null;
  58. //--
  59. public $http_code = "";
  60. public $http_info = "";
  61. //--
  62. public function __construct( $client_id = false, $client_secret = false, $redirect_uri='' )
  63. {
  64. $this->client_id = $client_id;
  65. $this->client_secret = $client_secret;
  66. $this->redirect_uri = $redirect_uri;
  67. }
  68. public function authorizeUrl( $extras = array() )
  69. {
  70. $params = array(
  71. "client_id" => $this->client_id,
  72. "redirect_uri" => $this->redirect_uri,
  73. "response_type" => "code"
  74. );
  75. if( count($extras) )
  76. foreach( $extras as $k=>$v )
  77. $params[$k] = $v;
  78. return $this->authorize_url . "?" . http_build_query($params, '', '&');
  79. }
  80. public function authenticate( $code )
  81. {
  82. $params = array(
  83. "client_id" => $this->client_id,
  84. "client_secret" => $this->client_secret,
  85. "grant_type" => "authorization_code",
  86. "redirect_uri" => $this->redirect_uri,
  87. "code" => $code
  88. );
  89. $response = $this->request( $this->token_url, $params, $this->curl_authenticate_method );
  90. $response = $this->parseRequestResult( $response );
  91. if( ! $response || ! isset( $response->access_token ) ){
  92. throw new Exception( "The Authorization Service has return: " . $response->error );
  93. }
  94. if( isset( $response->access_token ) ) $this->access_token = $response->access_token;
  95. if( isset( $response->refresh_token ) ) $this->refresh_token = $response->refresh_token;
  96. if( isset( $response->expires_in ) ) $this->access_token_expires_in = $response->expires_in;
  97. // calculate when the access token expire
  98. if( isset($response->expires_in)) {
  99. $this->access_token_expires_at = time() + $response->expires_in;
  100. }
  101. return $response;
  102. }
  103. public function authenticated()
  104. {
  105. if ( $this->access_token ){
  106. if ( $this->token_info_url && $this->refresh_token ){
  107. // check if this access token has expired,
  108. $tokeninfo = $this->tokenInfo( $this->access_token );
  109. // if yes, access_token has expired, then ask for a new one
  110. if( $tokeninfo && isset( $tokeninfo->error ) ){
  111. $response = $this->refreshToken( $this->refresh_token );
  112. // if wrong response
  113. if( ! isset( $response->access_token ) || ! $response->access_token ){
  114. throw new Exception( "The Authorization Service has return an invalid response while requesting a new access token. given up!" );
  115. }
  116. // set new access_token
  117. $this->access_token = $response->access_token;
  118. }
  119. }
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Format and sign an oauth for provider api
  126. */
  127. public function api( $url, $method = "GET", $parameters = array() )
  128. {
  129. if ( strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0 ) {
  130. $url = $this->api_base_url . $url;
  131. }
  132. $parameters[$this->sign_token_name] = $this->access_token;
  133. $this->curl_header = array( 'Authorization: Bearer ' . $this->access_token );
  134. $response = null;
  135. switch( $method ){
  136. case 'GET' : $response = $this->request( $url, $parameters, "GET" ); break;
  137. case 'POST' : $response = $this->request( $url, $parameters, "POST" ); break;
  138. }
  139. if( $response && $this->decode_json ){
  140. $response = json_decode( $response );
  141. }
  142. return $response;
  143. }
  144. /**
  145. * GET wrapper for provider apis request
  146. */
  147. function get( $url, $parameters = array() )
  148. {
  149. return $this->api( $url, 'GET', $parameters );
  150. }
  151. /**
  152. * POST wrapper for provider apis request
  153. */
  154. function post( $url, $parameters = array() )
  155. {
  156. return $this->api( $url, 'POST', $parameters );
  157. }
  158. // -- tokens
  159. public function tokenInfo($accesstoken)
  160. {
  161. $params['access_token'] = $this->access_token;
  162. $response = $this->request( $this->token_info_url, $params );
  163. return $this->parseRequestResult( $response );
  164. }
  165. public function refreshToken( $parameters = array() )
  166. {
  167. $params = array(
  168. "client_id" => $this->client_id,
  169. "client_secret" => $this->client_secret,
  170. "grant_type" => "refresh_token"
  171. );
  172. foreach($parameters as $k=>$v ){
  173. $params[$k] = $v;
  174. }
  175. $response = $this->request( $this->token_url, $params, "POST" );
  176. return $this->parseRequestResult( $response );
  177. }
  178. // -- utilities
  179. protected function request( $url, $params=false, $type="GET" )
  180. {
  181. Hybrid_Logger::info( "Enter OAuth2Client::request( $url )" );
  182. Hybrid_Logger::debug( "OAuth2Client::request(). dump request params: ", serialize( $params ) );
  183. if( $type == "GET" ){
  184. $url = $url . ( strpos( $url, '?' ) ? '&' : '?' ) . http_build_query($params, '', '&');
  185. }
  186. $this->http_info = array();
  187. $ch = curl_init();
  188. curl_setopt($ch, CURLOPT_URL , $url );
  189. curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1 );
  190. curl_setopt($ch, CURLOPT_TIMEOUT , $this->curl_time_out );
  191. curl_setopt($ch, CURLOPT_USERAGENT , $this->curl_useragent );
  192. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $this->curl_connect_time_out );
  193. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , $this->curl_ssl_verifypeer );
  194. curl_setopt($ch, CURLOPT_HTTPHEADER , $this->curl_header );
  195. if($this->curl_proxy){
  196. curl_setopt( $ch, CURLOPT_PROXY , $this->curl_proxy);
  197. }
  198. if( $type == "POST" ){
  199. curl_setopt($ch, CURLOPT_POST, 1);
  200. if($params) curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
  201. }
  202. $response = curl_exec($ch);
  203. Hybrid_Logger::debug( "OAuth2Client::request(). dump request info: ", serialize( curl_getinfo($ch) ) );
  204. Hybrid_Logger::debug( "OAuth2Client::request(). dump request result: ", serialize( $response ) );
  205. $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  206. $this->http_info = array_merge($this->http_info, curl_getinfo($ch));
  207. curl_close ($ch);
  208. return $response;
  209. }
  210. protected function parseRequestResult( $result )
  211. {
  212. if( json_decode( $result ) ) return json_decode( $result );
  213. parse_str( $result, $ouput );
  214. $result = new StdClass();
  215. foreach( $ouput as $k => $v )
  216. $result->$k = $v;
  217. return $result;
  218. }
  219. }