1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Maniaplanet\WebServices\ManiaConnect;
14:
15: 16: 17:
18: abstract class Client extends \Maniaplanet\WebServices\HTTPClient
19: {
20: 21: 22: 23: 24:
25:
26: const TOKEN_PATH = '/oauth2/token/';
27:
28: protected $loginURL = 'https://ws.maniaplanet.com/oauth2/authorize/';
29: protected $logoutURL = 'https://ws.maniaplanet.com/oauth2/authorize/logout/';
30:
31: 32: 33: 34: 35: 36: 37:
38: static protected $persistance;
39:
40: static function setPersistance(Persistance $object)
41: {
42: if(self::$persistance)
43: {
44: throw new \Maniaplanet\WebServices\Exception(
45: 'You must set the persistance object before instanciating the '.
46: 'services.');
47: }
48: self::$persistance = $object;
49: }
50:
51: function __construct($username = null, $password = null)
52: {
53: parent::__construct($username, $password);
54:
55:
56: if(!self::$persistance)
57: {
58: self::$persistance = new Session();
59: }
60:
61: self::$persistance->init();
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71:
72: function getLoginURL($scope = null, $redirectURI = null)
73: {
74: $redirectURI = $redirectURI ? : $this->getCurrentURI();
75: self::$persistance->setVariable('redirect_uri', $redirectURI);
76: return $this->getAuthorizationURL($redirectURI, $scope);
77: }
78:
79: 80: 81: 82: 83: 84: 85: 86:
87: function getLogoutURL($redirectURI = null)
88: {
89: $redirectURI = $redirectURI ? : $this->getCurrentURI();
90: return $this->logoutURL.'?'.http_build_query(array('redirect_uri' => $redirectURI),
91: '', '&');
92: }
93:
94: 95: 96: 97:
98: function logout()
99: {
100: self::$persistance->destroy();
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110:
111: protected function getCurrentURI()
112: {
113: $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
114: $current_uri = $protocol.$_SERVER['HTTP_HOST'].$this->getRequestURI();
115: $parts = parse_url($current_uri);
116:
117: $query = '';
118: if(!empty($parts['query']))
119: {
120: $params = array();
121: parse_str($parts['query'], $params);
122: $params = array_filter($params);
123: if(!empty($params))
124: {
125: $query = '?'.http_build_query($params, '', '&');
126: }
127: }
128:
129:
130: $port = isset($parts['port']) &&
131: (($protocol === 'http://' && $parts['port'] !== 80) || ($protocol === 'https://' && $parts['port'] !== 443)) ? ':'.$parts['port']
132: : '';
133:
134:
135: return $protocol.$parts['host'].$port.$parts['path'].$query;
136: }
137:
138: 139: 140: 141: 142: 143: 144: 145:
146: protected function getRequestURI()
147: {
148: if(isset($_SERVER['REQUEST_URI']))
149: {
150: $uri = $_SERVER['REQUEST_URI'];
151: }
152: else
153: {
154: if(isset($_SERVER['argv']))
155: {
156: $uri = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['argv'][0];
157: }
158: elseif(isset($_SERVER['QUERY_STRING']))
159: {
160: $uri = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
161: }
162: else
163: {
164: $uri = $_SERVER['SCRIPT_NAME'];
165: }
166: }
167:
168: $uri = '/'.ltrim($uri, '/');
169:
170: return $uri;
171: }
172:
173: private function getAuthorizationURL($redirectURI, $scope = 'basic')
174: {
175: $params = http_build_query(array(
176: 'client_id' => $this->username,
177: 'redirect_uri' => $redirectURI,
178: 'scope' => $scope,
179: 'response_type' => 'code',
180: ), '', '&');
181: return $this->loginURL.'?'.$params;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192:
193: protected function getAccessToken()
194: {
195: $token = self::$persistance->getVariable('access_token');
196: if($token)
197: {
198: return $token;
199: }
200: if(isset($_REQUEST['code']))
201: {
202: $code = $_REQUEST['code'];
203: if($code)
204: {
205: $redirectURI = self::$persistance->getVariable('redirect_uri') ? : $this->getCurrentURI();
206: $accessToken = $this->getAccessTokenFromCode($code, $redirectURI);
207: self::$persistance->setVariable('access_token', $accessToken);
208: return $accessToken;
209: }
210: }
211: }
212:
213: private function getAccessTokenFromCode($authorizationCode, $redirectURI)
214: {
215: $contentType = $this->contentType;
216: $serializeCallback = $this->serializeCallback;
217: $this->contentType = 'application/x-www-form-urlencoded';
218: $this->serializeCallback = null;
219:
220: $params = http_build_query(array(
221: 'client_id' => $this->username,
222: 'client_secret' => $this->password,
223: 'redirect_uri' => $redirectURI,
224: 'grant_type' => 'authorization_code',
225: 'code' => $authorizationCode,
226: ), '', '&');
227:
228: try
229: {
230: $response = $this->execute('POST', self::TOKEN_PATH, array($params));
231: }
232: catch(\Maniaplanet\WebServices\Exception $e)
233: {
234: switch($e->getMessage())
235: {
236: case 'invalid_request':
237: $message =
238: 'invalid_request: The request is missing a required '.
239: 'parameter, includes an unsupported parameter or '.
240: 'parameter value, or is otherwise malformed.';
241: break;
242:
243: case 'invalid_client':
244: $message =
245: 'invalid_client: Application authentication failed. ';
246: break;
247:
248: case 'invalid_grant':
249: $message =
250: 'invalid_grant: The provided access grant is invalid, '.
251: 'expired, or revoked (e.g. invalid assertion, expired '.
252: 'authorization token, bad end-user password credentials, '.
253: 'or mismatching authorization code and redirection URI).';
254: break;
255:
256: default:
257: throw $e;
258: }
259:
260: throw new \Maniaplanet\WebServices\Exception($message, $e->getCode(),
261: $e->getHTTPStatusCode(), $e->getHTTPStatusMessage());
262: }
263:
264:
265: $this->contentType = $contentType;
266: $this->serializeCallback = $serializeCallback;
267:
268: self::$persistance->deleteVariable('redirect_uri');
269: self::$persistance->deleteVariable('code');
270:
271: return $response->access_token;
272: }
273:
274: 275: 276: 277: 278: 279:
280: protected function executeOAuth2($method, $ressource, array $params = array())
281: {
282: $this->headers = array(sprintf('Authorization: Bearer %s', self::$persistance->getVariable('access_token')));
283:
284: $this->enableAuth = false;
285: try
286: {
287: $result = $this->execute($method, $ressource, $params);
288: $this->enableAuth = true;
289: return $result;
290: }
291: catch(Exception $e)
292: {
293: $this->enableAuth = true;
294: throw $e;
295: }
296: }
297:
298: }
299:
300: ?>