1 : <?php
2 :
3 : /**
4 : * Redirect the end-user's user agent with error message.
5 : *
6 : * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
7 : *
8 : * @ingroup oauth2_error
9 : */
10 : class OAuth2RedirectException extends OAuth2ServerException {
11 :
12 : protected $redirectUri;
13 :
14 : /**
15 : * @param $redirect_uri
16 : * An absolute URI to which the authorization server will redirect the
17 : * user-agent to when the end-user authorization step is completed.
18 : * @param $error
19 : * A single error code as described in Section 4.1.2.1
20 : * @param $error_description
21 : * (optional) A human-readable text providing additional information,
22 : * used to assist in the understanding and resolution of the error
23 : * occurred.
24 : * @param $state
25 : * (optional) REQUIRED if the "state" parameter was present in the client
26 : * authorization request. Set to the exact value received from the client.
27 : *
28 : * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1.2.1
29 : *
30 : * @ingroup oauth2_error
31 : */
32 : public function __construct($redirect_uri, $error, $error_description = NULL, $state = NULL) {
33 0 : parent::__construct(OAuth2::HTTP_FOUND, $error, $error_description);
34 :
35 0 : $this->redirectUri = $redirect_uri;
36 0 : if ($state) {
37 0 : $this->errorData['state'] = $state;
38 0 : }
39 :
40 0 : }
41 :
42 : /**
43 : * Redirect the user agent.
44 : *
45 : * @ingroup oauth2_section_4
46 : */
47 : protected function sendHeaders() {
48 0 : $params = array('query' => $this->errorData);
49 0 : header("Location: " . $this->buildUri($this->redirectUri, $params));
50 0 : exit(); // No point in printing out data if we're redirecting
51 : }
52 :
53 : /**
54 : * Build the absolute URI based on supplied URI and parameters.
55 : *
56 : * @param $uri
57 : * An absolute URI.
58 : * @param $params
59 : * Parameters to be append as GET.
60 : *
61 : * @return
62 : * An absolute URI with supplied parameters.
63 : *
64 : * @ingroup oauth2_section_4
65 : */
66 : protected function buildUri($uri, $params) {
67 0 : $parse_url = parse_url($uri);
68 :
69 : // Add our params to the parsed uri
70 0 : foreach ( $params as $k => $v ) {
71 0 : if (isset($parse_url[$k]))
72 0 : $parse_url[$k] .= "&" . http_build_query($v);
73 : else
74 0 : $parse_url[$k] = http_build_query($v);
75 0 : }
76 :
77 : // Put humpty dumpty back together
78 0 : return ((isset($parse_url["scheme"])) ? $parse_url["scheme"] . "://" : "") . ((isset($parse_url["user"])) ? $parse_url["user"] . ((isset($parse_url["pass"])) ? ":" . $parse_url["pass"] : "") . "@" : "") . ((isset($parse_url["host"])) ? $parse_url["host"] : "") . ((isset($parse_url["port"])) ? ":" . $parse_url["port"] : "") . ((isset($parse_url["path"])) ? $parse_url["path"] : "") . ((isset($parse_url["query"])) ? "?" . $parse_url["query"] : "") . ((isset($parse_url["fragment"])) ? "#" . $parse_url["fragment"] : "");
79 : }
|