This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathculturefeed.pages.inc
235 lines (183 loc) · 7.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?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);
// Get all query params, so they are available after login.
$options['query'] = drupal_get_query_parameters(NULL, array('q'));
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
if (isset($_GET['redirect'])) {
unset($_GET['redirect']);
}
if (isset($_GET['closepopup'])) {
$options['query']['closepopup'] = 'true';
}
$skip_authorization = FALSE;
if (isset($_GET['skipAuthorization'])) {
$skip_authorization = TRUE;
}
$via = NULL;
if (isset($_GET['via'])) {
$via = $_GET['via'];
}
$full_callback_url = url('culturefeed/oauth/authorize' . (isset($application_key) ? '/' . $application_key : ''), $options);
// Fetch the request token.
try {
$token = $cf->getRequestToken($full_callback_url);
}
catch (Exception $e) {
drupal_set_message(t('An error occurred while logging in. Please try again later.'), 'error');
watchdog_exception('culturefeed', $e);
drupal_goto('<front>');
}
if (!$token) {
drupal_set_message(t('An error occurred while logging in. Please try again later.'), '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'];
// Fetch the authorisation url...
$callback_url = url('culturefeed/oauth/authorize' . (isset($application_key) ? '/' . $application_key : ''), array('absolute' => TRUE));
$auth_url = $cf->getUrlAuthorize($token, $callback_url, $type, FALSE, $skip_authorization, $via, culturefeed_get_preferred_language(), variable_get('culturefeed_api_application_key', ''));
// ... 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(t('An error occurred while logging in. Please try again later.'), '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();
global $user;
if (!$uid) {
$account = culturefeed_create_user($cf_account, $user);
}
else {
$cf_uid = db_query("SELECT cf_uid FROM {culturefeed_user} cfu WHERE cfu.uid = :uid", array(':uid' => $user->uid))->fetchField();
// If the drupal user already exist with another cf_uid we update the reference
if (!$cf_uid && $user->uid && $user->uid != $uid) {
$query = db_update('culturefeed_user')
->condition('cf_uid', $cf_account->id)
->fields(array('uid' => $user->uid))
->execute();
$uid = $user->uid;
}
$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']) . '";';
}
else {
$destination = '';
drupal_alter('culturefeed_login_redirect_destination', $destination);
if (!empty($destination)) {
$action = 'window.opener.location.href="' . url($destination) . '";';
}
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();
}
else {
$destination = '';
drupal_alter('culturefeed_login_redirect_destination', $destination);
drupal_goto($destination);
}
}
}
}
drupal_page_footer();
exit();
}
/**
* Check if the user is authenticated. If he is redirect to destination.
*/
function culturefeed_authenticated_action_page() {
if (DrupalCultureFeed::isCultureFeedUser()) {
$query = array();
if (!empty($_GET['query'])) {
parse_str($_GET['query'], $query);
}
$redirect = isset($_GET['redirect']) ? $_GET['redirect'] : '';
// Destination always wins in drupal_goto.
if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
$redirect = $_GET['destination'];
}
$url = drupal_parse_url($redirect);
// Check if the user will have access to the redirect path.
// If not it will cause endless loop.
if (!drupal_valid_path($url['path'])) {
drupal_set_title(t('Access denied'));
return t('You are not authorized to access this page.');
}
drupal_goto($url['path'], array('query' => $url['query'], 'fragment' => $url['fragment']));
}
return array('#theme' => 'culturefeed_authenticated_page');
}