forked from cultuurnet/culturefeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
culturefeed.pages.inc
191 lines (148 loc) · 5.75 KB
/
culturefeed.pages.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
function culturefeed_oauth_connect($type = CultureFeed::AUTHORIZE_TYPE_REGULAR, $application_key = NULL) {
global $conf;
// We don't want to cache this type of page.
$conf['cache'] = FALSE;
// If an application key was passed, fetch the shared secret for it.
$shared_secret = NULL;
if (isset($application_key)) {
$shared_secret = culturefeed_get_consumer_shared_secret($application_key);
}
// Load the consumer instance.
$cf = DrupalCultureFeed::getConsumerInstance($application_key, $shared_secret);
// Construct the callback url.
$options = array('absolute' => TRUE);
if (isset($_GET['destination'])) {
$options['query']['destination'] = $_GET['destination'];
unset($_GET['destination']);
}
if (isset($_GET['closepopup'])) {
$options['query']['closepopup'] = 'true';
}
if (isset($_GET['dob'])) {
$options['query']['dob'] = $_GET['dob'];
}
// Add the language parameter.
$options['query']['lang'] = culturefeed_search_get_preferred_language();
$callback_url = url('culturefeed/oauth/authorize' . (isset($application_key) ? '/' . $application_key : ''), $options);
// Fetch the request token.
try {
$token = $cf->getRequestToken($callback_url);
}
catch (Exception $e) {
drupal_set_message('Er is een fout opgetreden bij het inloggen. Probeer later opnieuw.', 'error');
watchdog_exception('culturefeed', $e);
drupal_goto('<front>');
}
if (!$token) {
drupal_set_message('Er is een fout opgetreden bij het inloggen. Probeer later opnieuw.', 'error');
drupal_goto('<front>');
}
// Save the token and secret in the session.
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
$via = NULL;
if (isset($_GET['via'])) {
$via = $_GET['via'];
}
$skip_authorization = FALSE;
if (isset($_GET['skipAuthorization'])) {
$skip_authorization = TRUE;
}
// Fetch the authorisation url...
$auth_url = $cf->getUrlAuthorize($token, $callback_url, $type, FALSE, $skip_authorization, $via);
// ... and redirect the user to it.
drupal_goto($auth_url);
}
function culturefeed_oauth_authorize($application_key = NULL) {
global $conf;
// We don't want to cache this type of page.
$conf['cache'] = FALSE;
if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
// If an application key was passed, fetch the shared secret for it.
$shared_secret = NULL;
if (isset($application_key)) {
$shared_secret = culturefeed_get_consumer_shared_secret($application_key);
}
try {
$token = DrupalCultureFeed::getInstance($_GET['oauth_token'], $_SESSION['oauth_token_secret'], $application_key, $shared_secret)->getAccessToken($_GET['oauth_verifier']);
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
$cf_account = DrupalCultureFeed::getInstance($token['oauth_token'], $token['oauth_token_secret'], $application_key, $shared_secret)->getUser($token['userId']);
}
catch (Exception $e) {
drupal_set_message('Er is een fout opgetreden bij het inloggen. Probeer later opnieuw.', 'error');
watchdog_exception('culturefeed', $e);
drupal_goto();
}
// Check if the user is already known in our system.
$uid = db_query("SELECT uid FROM {culturefeed_user} cfu WHERE cfu.cf_uid = :cf_uid", array(':cf_uid' => $token['userId']))->fetchField();
if (!$uid) {
$account = culturefeed_create_user($cf_account);
}
else {
$account = user_load($uid);
}
// If a token was passed, save it after deleting a possible previous entry.
if ($token) {
if (!isset($application_key)) {
$application_key = variable_get('culturefeed_api_application_key', '');
}
db_delete('culturefeed_token')
->condition('cf_uid', $token['userId'])
->condition('application_key', $application_key)
->execute();
db_insert('culturefeed_token')
->fields(array(
'cf_uid' => $token['userId'],
'token' => $token['oauth_token'],
'secret' => $token['oauth_token_secret'],
'application_key' => $application_key,
))
->execute();
}
if ($account) {
global $user;
$user = $account;
user_login_finalize();
if (isset($_GET['closepopup'])) {
if (isset($_GET['destination'])) {
$action = 'window.opener.location.href="' . url($_GET['destination'], array('absolute' => TRUE)) . '";';
}
else {
$destination = '';
drupal_alter('culturefeed_login_redirect_destination', $destination);
if (!empty($destination)) {
$action = 'window.opener.location.href="' . url($destination, array('absolute' => TRUE)) . '";';
}
else {
$action = 'window.opener.location.reload();';
}
}
// We don't want to render all blocks, so we return minimal html.
print '<html>';
print '<head>';
print '<title>You should not see this</title>';
print '<script type="text/javascript">try { ' . $action . ' } catch (err) { } window.close();</script>';
print '<p>Als deze pagina niet automatisch sluit, klik dan op onderstaande link om verder te gaan.</p>';
print '<p><a href="' . url('') . '">' . url('', array('absolute' => TRUE)) . '</a></p>';
print '</head>';
print '<body>';
print '</body>';
print '</html>';
}
else {
if (isset($_GET['destination'])) {
drupal_goto($_GET['destination']);
}
else {
$destination = '';
$destination = drupal_alter('culturefeed_login_redirect_destination', $destination);
drupal_goto($destination);
}
}
}
}
drupal_page_footer();
exit();
}