1: <?php
2: namespace Genetsis;
3:
4: use Exception;
5: use Genetsis\core\OAuthConfig;
6:
7: /**
8: * This class is used to build the links to different services of Genetsis ID.
9: *
10: * @package Genetsis
11: * @category Helper
12: * @version 2.0
13: * @access private
14: */
15: class URLBuilder
16: {
17: /**
18: * Returns the link for login process.
19: *
20: * @param string $scope Section-key Identifier of the web client. The
21: * section-key is located in "oauthconf.xml" file. If it's NULL,
22: * the default section will be used.
23: * @param string $social - to force login with social network. Optional. Values 'facebook', 'twitter'
24: * @param string $urlCallback Url for callback. A list of valid url is defined in "oauthconf.xml"
25: * If it's NULL default url will be used.
26: * @return string The URL for login process.
27: */
28: public static function getUrlLogin($scope = null, $social = null, $urlCallback = null)
29: {
30:
31: return self::buildLoginUrl(
32: OAuthConfig::getEndpointUrl('authorization_endpoint'),
33: OAuthConfig::getRedirectUrl('postLogin', $urlCallback),
34: $scope,
35: $social
36: );
37: }
38:
39: /**
40: * Returns the link for register form page.
41: *
42: * @param string $scope Section-key Identifier of the web client. The
43: * section-key is located in "oauthconf.xml" file. If it's NULL,
44: * the default section will be used.
45: * @param string $urlCallback Url for callback. A list of url is defined in "oauthconf.xml"
46: * If it's NULL the default url will be used.
47: * @return string The URL for register process.
48: */
49: public static function getUrlRegister($scope = null, $urlCallback = null)
50: {
51: return self::buildSignupUrl(
52: OAuthConfig::getEndpointUrl('signup_endpoint'),
53: OAuthConfig::getRedirectUrl('register', $urlCallback),
54: $scope
55: );
56: }
57:
58: /**
59: * Returns the link for edit account form page.
60: *
61: * @param string $scope Section-key Identifier of the web client. The
62: * section-key is located in "oauthconf.xml" file. If it's NULL,
63: * the default section will be used.
64: * @param string $urlCallback Url for callback. A list of url is defined in "oauthconf.xml"
65: * If it's NULL the default url will be used.
66: * @return string The URL for edit account process.
67: */
68: public static function getUrlEditAccount($scope = null, $urlCallback = null)
69: {
70: $params = array();
71: $params['client_id'] = OAuthConfig::getClientid();
72: $params['redirect_uri'] = OAuthConfig::getRedirectUrl('postEditAccount', $urlCallback);
73: $next_url = (OAuthConfig::getEndpointUrl('next_url') . '?' . http_build_query($params));
74: $cancel_url = (OAuthConfig::getEndpointUrl('cancel_url') . '?' . http_build_query($params));
75: unset($params);
76:
77: return self::buildEditAccountUrl(
78: OAuthConfig::getEndpointUrl('edit_account_endpoint'),
79: $next_url,
80: $cancel_url,
81: $scope
82: );
83: }
84:
85: /**
86: * Returns the URL to complete the account for a section (scope) given.
87: *
88: * @param string $scope Section-key Identifier of the web client. The
89: * section-key is located in "oauthconf.xml" file.
90: * @return string The URL for complete process.
91: */
92: public static function getUrlCompleteAccount($scope = null)
93: {
94: $params = array();
95: $params['client_id'] = OAuthConfig::getClientid();
96: $params['redirect_uri'] = OAuthConfig::getRedirectUrl('postEditAccount');
97: $next_url = OAuthConfig::getEndpointUrl('next_url') . '?' . http_build_query($params);
98: $cancel_url = OAuthConfig::getEndpointUrl('cancel_url') . '?' . http_build_query($params);
99: unset($params);
100:
101: return self::buildCompleteAccountUrl(
102: OAuthConfig::getEndpointUrl('complete_account_endpoint'),
103: $next_url,
104: $cancel_url,
105: $scope
106: );
107: }
108:
109: /**
110: * This method is commonly used for promotions or sweepstakes: if a
111: * user wants to participate in a promotion, the web client must
112: * ensure that the user is logged and have all the fields filled
113: * in order to let him participate.
114: *
115: * - If it is not logged, will return the login URL.
116: * - If it is logged the method will check
117: * - If the user have not enough PII to access to a section,
118: * returns the URL needed to force a consumer to fill all the
119: * PII needed to enter into a section
120: * - Else will return false (user logged and completed)
121: *
122: * The "scope" (section) is a group of fields configured in Genetsis ID for
123: * a web client.
124: *
125: * A section can be also defined as a "part" (section) of the website
126: * (web client) that only can be accesed by a user who have filled a
127: * set of personal information configured in Genetsis ID (all of the fields
128: * required for that section).
129: *
130: * @param string Section-key Identifier of the web client. The
131: * section-key is located in "oauthconf.xml" file.
132: * @return string With generated URL. If the user is not connected,
133: * will return login URL.
134: * @throws Exception if scope is empty.
135: */
136: public static function buildSignupPromotionUrl($scope)
137: {
138: try {
139: if (self::checkParam($scope)) {
140: throw new \Exception ('Scope section is empty');
141: }
142:
143: if (!Identity::isConnected()) {
144: return sefl::getUrlLogin($scope);
145: } else {
146: if (!Identity::checkUserComplete($scope)) {
147: return self::getUrlCompleteAccount($scope);
148: }
149: }
150: return false;
151: } catch (\Exception $e) {
152: Identity::getLogger()->debug('Error [' . __FUNCTION__ . '] - ' . $e->getMessage());
153: }
154: }
155:
156:
157: /**
158: * Set url for callback in redirectiosn section with regiter type.
159: * @param string $url
160: * @return boolean
161: */
162: public function setCallbackRegister($url)
163: {
164: return OAuthConfig::setCallbackRegister($url);
165: }
166:
167: /**
168: * Set url for callback in redirections section with confirm user type.
169: * @param string $url
170: * @return boolean
171: */
172: public function setCallbackConfirmUser($url)
173: {
174: return OAuthConfig::setCallbackConfirmUser($url);
175: }
176:
177: /**
178: * Set url for callback in redirections section with confirm postLogin type.
179: * @param string $url
180: * @return boolean
181: */
182: public function setCallbackPostLogin($url)
183: {
184: return OAuthConfig::setCallbackPostLogin($url);
185: }
186:
187: /**
188: * Set url for callback in redirections section with postChangeEmail type.
189: * @param string $url
190: * @return boolean
191: */
192: public function setCallbackPostChangeEmail($url)
193: {
194: return OAuthConfig::setCallbackPostChangeEmail($url);
195: }
196:
197: /**
198: * Set url for callback in redirections section with postEditAccount type.
199: * @param string $url
200: * @return boolean
201: */
202: public function setCallbackPostEditAccount($url)
203: {
204: return OAuthConfig::setCallbackPostEditAccount($url);
205: }
206:
207: /**
208: * Builds the URL to login process.
209: *
210: * @param string $endpoint_url The endpoint. Normally the 'authorization_endpoint' of
211: * OAuth server.
212: * @param string $redirect_url Where the user will be redirected, even on success or
213: * not.
214: * @param string $scope Section-key identifier of the web client. The
215: * section-key is located in "oauthconf.xml" file.
216: * @param string $social Social - to force login with social network. Optional. Values 'facebook', 'twitter'
217: * @return string The URL generated.
218: * @throws \Exception If there is an error.
219: */
220: private static function buildLoginUrl($endpoint_url, $redirect_url, $scope = null, $social = null)
221: {
222:
223: try {
224: if (self::checkParam($endpoint_url)) {
225: throw new Exception ('Endpoint URL is empty');
226: }
227: if (self::checkParam($redirect_url)) {
228: throw new Exception ('Redirect URL is empty');
229: }
230:
231: $endpoint_url = rtrim($endpoint_url, '?');
232: $params = array();
233: $params['client_id'] = OAuthConfig::getClientid();
234: $params['redirect_uri'] = $redirect_url;
235: $params['response_type'] = 'code';
236: if (!is_null($scope)) {
237: $params['scope'] = $scope;
238: }
239:
240: if ($social != null) {
241: $params['ck_auth_provider'] = $social;
242: }
243:
244: return $endpoint_url . '?' . http_build_query($params, null, '&');
245: } catch (Exception $e) {
246: Identity::getLogger()->debug('Error [' . __FUNCTION__ . '] - ' . $e->getMessage());
247: }
248: }
249:
250: /**
251: * Builds the URL to edit the user's data.
252: *
253: * @param string The endpoint. Normally the 'edit_account_endpoint' of
254: * OAuth server.
255: * @param string Where the user will be redirected when finished
256: * editing data.
257: * @param string Where the user will be redirected if the process is
258: * cancelled.
259: * @param string Section-key identifier of the web client. The
260: * section-key is located in "oauthconf.xml" file.
261: * @return string The URL generated.
262: * @throws \Exception If there is an error.
263: */
264: private static function buildEditAccountUrl($endpoint_url, $next_url, $cancel_url, $scope = null)
265: {
266: try {
267: if (self::checkParam($endpoint_url)) {
268: throw new Exception ('Endpoint URL is empty');
269: }
270: if (self::checkParam($next_url)) {
271: throw new Exception ('Next URL is empty');
272: }
273: if (self::checkParam($cancel_url)) {
274: throw new Exception ('Cancel URL is empty');
275: }
276:
277: $access_token = Identity::getThings()->getAccessToken();
278:
279: if (is_null($access_token)) {
280: throw new Exception ('Access token is empty');
281: }
282:
283: $endpoint_url = rtrim($endpoint_url, '?');
284: $params = array();
285: $params ['next'] = $next_url;
286: $params ['cancel_url'] = $cancel_url;
287: $params ['oauth_token'] = $access_token->getValue();
288: if (!is_null($scope)) {
289: $params ['scope'] = $scope;
290: }
291: unset ($access_token);
292:
293: return $endpoint_url . '?' . http_build_query($params, null, '&');
294: } catch (Exception $e) {
295: Identity::getLogger()->debug('Error [' . __FUNCTION__ . '] - ' . $e->getMessage());
296: }
297: }
298:
299: /**
300: * Builds the URL to sign up process.
301: *
302: * @param string The endpoint. Normally the 'signup_endpoint' of OAuth
303: * server.
304: * @param string Where the user will be redirected, even on success or
305: * not.
306: * @param string Section-key identifier of the web client. The
307: * section-key is located in "oauthconf.xml" file.
308: * @return string The URL generated.
309: * @throws \Exception If there is an error.
310: */
311: private static function buildSignupUrl($endpoint_url, $redirect_url, $scope = null)
312: {
313: try {
314:
315: $url = self::buildLoginUrl($endpoint_url, $redirect_url);
316: if (self::checkParam($url)) {
317: throw new Exception("Can't build sign up URL");
318: }
319:
320: $params = array();
321: $params['x_method'] = 'sign_up';
322: if (!is_null($scope)) {
323: $params ['scope'] = $scope;
324: }
325:
326: return $url . '&' . http_build_query($params, null, '&');
327: } catch (Exception $e) {
328: Identity::getLogger()->debug('Error [' . __FUNCTION__ . '] - ' . $e->getMessage());
329: }
330: }
331:
332: /**
333: * Builds the URL to fill up data for a specific section.
334: *
335: * @param string The endpoint. Normally the 'edit_account_endpoint' of
336: * OAuth server.
337: * @param string Where the user will be redirected when finished
338: * fill up data.
339: * @param string Where the user will be redirected if the process is
340: * cancelled.
341: * @param string Section-key identifier of the web client. The
342: * section-key is located in "oauthconf.xml" file.
343: * @return string The URL generated.
344: * @throws \Exception If there is an error.
345: */
346: private static function buildCompleteAccountUrl($endpoint_url, $next_url, $cancel_url, $scope)
347: {
348: try {
349: if (self::checkParam($endpoint_url)) {
350: throw new Exception ('Endpoint URL is empty');
351: }
352: if (self::checkParam($next_url)) {
353: throw new Exception ('Next URL is empty');
354: }
355: if (self::checkParam($cancel_url)) {
356: throw new Exception ('Cancel URL is empty');
357: }
358: $access_token = Identity::getThings()->getAccessToken();
359:
360: if (is_null($access_token)) {
361: throw new Exception ('Access token is empty');
362: }
363: if (self::checkParam($scope)) {
364: throw new Exception ('Scope section is empty');
365: }
366:
367: $endpoint_url = rtrim($endpoint_url, '?');
368: $params = array();
369: $params ['next'] = $next_url;
370: $params ['cancel_url'] = $cancel_url;
371: $params ['oauth_token'] = $access_token->getValue();
372: unset ($access_token);
373: $params['scope'] = $scope;
374:
375: return $endpoint_url . '?' . http_build_query($params, null, '&');
376: } catch (Exception $e) {
377: Identity::getLogger()->debug('Error [' . __FUNCTION__ . '] - ' . $e->getMessage());
378: }
379: }
380:
381: /**
382: * Check if param is null or empty or blank
383: *
384: * @param string $param The string to validate
385: * @return bool True if is null, empty or blank, False in other case
386: */
387: private static function checkParam($param)
388: {
389: $param = trim($param);
390: return empty($param);
391: }
392: }