forked from ding2/ding_provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_provider.module
622 lines (573 loc) · 19.7 KB
/
ding_provider.module
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?php
/**
* @file
* Implements a common interface to backend providers.
*/
/**
* Implements hook_requirements().
*/
function ding_provider_requirements($phase) {
$requirements = array();
$t = get_t();
$providers = array();
$conflicting = FALSE;
// Check if we have conflicting provider types
foreach (module_implements('ding_provider') as $module) {
$provider_module = module_invoke($module, 'ding_provider');
if (array_key_exists('provides', $provider_module)) {
foreach ($provider_module['provides'] as $provides) {
$providers[$provides['prefix']][] = $module;
}
}
}
foreach ($providers as $providertype => $providermodules) {
if (count($providermodules) > 1) {
$conflicting[] = t('%type implemented by @providers. Using @module.', array(
'@providers' => implode(', ', $providermodules),
'%type' => $providertype,
'@module' => end($providermodules),
)
);
}
}
switch ($phase) {
case 'runtime':
if ($callback = variable_get('ding_provider_ajax_disabled', FALSE)) {
$requirements['ding_provider_ajax_conflict'] = array(
'title' => $t('Ding provider AJAX authentication'),
'description' => $t('Disabled'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Ding Provider is unable to override the system/ajax page, because some other module already has overridden it with %callback.', array('%callback' => $callback)),
);
}
if ($conflicting == TRUE) {
$requirements['ding_provider_conflict'] = array(
'title' => $t('Ding provider Information'),
'description' => $t('Possible provider conflict'),
'severity' => REQUIREMENT_WARNING,
'value' => implode('<br />', $conflicting),
);
}
break;
}
return $requirements;
}
/**
* Implements hook_menu().
*/
function ding_provider_menu() {
$items['ding_provider_error'] = array(
'title' => 'Internal error',
'page callback' => 'ding_provider_error_page',
'access callback' => TRUE,
);
$items['admin/config/ding/provider'] = array(
'title' => 'Ding provider',
'page callback' => 'ding_provider_admin_overview',
'access arguments' => array('administer ding provider'),
'file' => 'ding_provider.admin.inc'
);
$items['admin/config/ding/provider/overview'] = array(
'title' => 'Overview',
'description' => 'Providers overview',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
'file' => 'system.admin.inc',
);
$providers = _ding_provider_get_provider();
foreach ($providers as $name => $provider) {
if ($provider['global settings']) {
$items['admin/config/ding/provider/' . $provider['module']] = array(
'title' => $provider['title'],
'page callback' => 'drupal_get_form',
'page arguments' => array($provider['global settings']),
'access arguments' => array('administer ding provider'),
'type' => MENU_LOCAL_TASK,
'weight' => -1,
);
if ($provider['global settings file']) {
$items['admin/config/ding/provider/' . $provider['module']]['file'] = $provider['global settings file'];
}
}
}
return $items;
}
/**
* Implements hook_menu_alter().
*/
function ding_provider_menu_alter(&$items) {
// Override system/ajax with a version that caches
// DingProviderAuthException.
$callback = $items['system/ajax']['page callback'];
if ($callback == 'ajax_form_callback') {
$items['system/ajax']['page callback'] = 'ding_provider_ajax_form_callback';
$items['system/ajax']['file path'] = drupal_get_path('module', 'ding_provider');
$items['system/ajax']['file'] = 'ding_provider.module';
// Delete var, but only if it is set.
if (variable_get('ding_provider_ajax_disabled', NULL)) {
variable_del('ding_provider_ajax_disabled');
}
}
else {
// Set a variable for hook_requirements.
variable_set('ding_provider_ajax_disabled', $callback);
}
}
/**
* Implements hook_permission().
*/
function ding_provider_permission() {
return array(
'administer ding provider' => array(
'title' => t('Administer Ding provider'),
),
);
}
/**
* Implements hook_ding_install_tasks().
*/
function ding_provider_ding_install_tasks() {
$users = _ding_provider_get_provider_users();
$providers = _ding_provider_get_provider();
$return = array();
foreach ($users as $provider => $info) {
// Add global provider settings if required.
if ($info['ding_profile'] && sizeof($info['required']) && isset($providers[$provider]) && $form_id = $providers[$provider]['global settings']) {
$return[$form_id] = array(
// TODO: This doesn't work for potx, we need the generated strings in
// plain form somewhere.
'display_name' => st($providers[$provider]['title']),
'type' => 'form',
);
if ($providers[$provider]['global settings file']) {
$return[$form_id]['file'] = $providers[$provider]['global settings file'];
}
}
// Add settings for individual providers to installer if someone asked for
// it and there's modules that requires this provider.
if ($info['ding_profile'] && sizeof($info['required']) && $form_id = ding_provider_implements($provider, 'settings')) {
$return[$form_id] = array(
// TODO: This doesn't work for potx, we need the generated stings in
// plain form somewhere.
'display_name' => st(drupal_ucfirst($provider) . ' provider'),
'type' => 'form',
);
if ($providers[$provider]['file']) {
$return[$form_id]['file'] = $providers[$provider]['file'];
}
}
}
return $return;
}
/**
* Error page callback.
*/
function ding_provider_error_page() {
return array(
'#markup' => t("Internal error. Please contact the site administrator if the problem persists."),
);
}
/**
* Returns whether a provider implements a given function.
*/
function ding_provider_implements($type, $hook) {
try {
return (bool) _ding_provider_function($type, $hook, TRUE);
}
catch (Exception $e) {
return FALSE;
}
}
/**
* Invoke a provider function.
*
* Simplest of the provider invoking functions. Returns the result of calling
* the provider function, or throws DingProviderAuthException if not
* authenticated.
*
* @see ding_provider_invoke_page()
* @see ding_provider_get_form()
* @see ding_provider_ajax_form_callback()
*/
function ding_provider_invoke($type, $hook) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$function = _ding_provider_function($type, $hook);
if ($function) {
return call_user_func_array($function, $args);
}
}
/**
* Get a form from a provider.
*/
function ding_provider_form($type, $form_id) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$function = _ding_provider_function($type, $form_id);
if ($function) {
return drupal_get_form($function, $args);
}
}
/**
* Invoke a provider function, handling authentication.
*
* Will redirect to the authentication page if DingProviderAuthException is
* thrown, and redirect back on successful authentication.
*
* @see ding_provider_invoke()
* @see ding_provider_get_form()
* @see ding_provider_ajax_form_callback()
*/
function ding_provider_invoke_page($type, $hook) {
$args = func_get_args();
array_shift($args);
array_shift($args);
try {
$function = _ding_provider_function($type, $hook);
if ($function) {
return call_user_func_array($function, $args);
}
}
catch (Exception $e) {
if ($e instanceof DingProviderUserException) {
// Rethrow user exceptions.
throw $e;
}
elseif ($e instanceof DingProviderAuthException) {
// Redirect to auth page
if (module_exists('ding_user') && ($authpage = ding_user_auth_page())) {
// @todo redirect to auth page
drupal_goto($authpage, array('query' => ding_provider_get_destination()));
}
}
if (arg(0) != 'ding_provider_error') {
watchdog('ding_provider', 'Uncaught exception in ding_provider_invoke_page: @message', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
// Redirect to error page.
drupal_goto('ding_provider_error');
}
else {
return NULL;
}
}
}
/**
* Wrapper around drupal_get_form() that handles authentication.
*
* Will redirect to the authentication page if DingProviderAuthException is
* thrown, and restart form processing on successful authentication.
*
* @see ding_provider_invoke()
* @see ding_provider_invoke_page()
* @see ding_provider_ajax_form_callback()
*/
function ding_provider_get_form($form_id) {
$form_state = array();
$args = func_get_args();
// Remove $form_id from the arguments.
array_shift($args);
$form_state['build_info']['args'] = $args;
return ding_provider_build_form($form_id, $form_state);
}
/**
* Wrapper around drupal_build_form() that handles authentication.
*
* Will redirect to the authentication page if DingProviderAuthException is
* thrown, and restart form processing on successful authentication.
*
* @see ding_provider_get_form()
*/
function ding_provider_build_form($form_id, &$form_state) {
$messages = NULL;
try {
if (isset($_REQUEST['dp_form_id']) && !empty($_REQUEST['dp_form_id'])) {
// Load saved submission.
if ($cached = cache_get('ding_provider_' . $form_id, 'cache_form')) {
$form_state['input'] = $cached->data;
$messages = $form_state['input']['#ding_provider_messages'];
unset($form_state['input']['#ding_provider_messages']);
cache_clear_all('ding_provider_' . $form_id, 'cache_form');
}
}
$old_redirect = isset($form_state['no_redirect']) ? $form_state['no_redirect'] : FALSE;
$form_state['no_redirect'] = TRUE;
$form = drupal_build_form($form_id, $form_state);
// Set any messages we saved.
_ding_provider_message_reset($messages);
$form_state['no_redirect'] = $old_redirect;
if ($form_state['executed'] == TRUE) {
// Redirect if the form was submitted.
drupal_redirect_form($form_state);
}
return $form;
}
catch (Exception $e) {
if ($e instanceof DingProviderUserException) {
// Rethrow user exceptions.
throw $e;
}
elseif ($e instanceof DingProviderAuthException) {
if (module_exists('ding_user') && ($authpage = ding_user_auth_page())) {
// Something needs auth, save state and redirect to authentication page.
$expire = 21600;
$input = $form_state['method'] == 'get' ? $_GET : $_POST;
$input['#ding_provider_messages'] = drupal_get_messages();
cache_set('ding_provider_' . $form_id, $input, 'cache_form', REQUEST_TIME + $expire);
$options = array('query' => ding_provider_get_destination(array('dp_form_id' => $form_id)));
drupal_goto($authpage, $options);
}
}
elseif ($e instanceof DingProviderConsentException) {
if (module_exists('ding_user_concent') && ($authpage = ding_user_consent_page())) {
// Something needs auth, save state and redirect to authentication page.
$expire = 21600;
$input = $form_state['method'] == 'get' ? $_GET : $_POST;
$input['#ding_provider_messages'] = drupal_get_messages();
cache_set('ding_provider_' . $form_id, $input, 'cache_form', REQUEST_TIME + $expire);
$options = array('query' => ding_provider_get_destination(array('dp_form_id' => $form_id)));
drupal_goto($authpage, $options);
}
}
watchdog('ding_provider', 'Uncaught exception in ding_provider_build_form: @message', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
// Redirect to error page.
drupal_goto('ding_provider_error');
}
}
/**
* Replacement for ajax_form_callback() that handles authentication.
*
* Sends a log in AJAX command (implemented in ding_user), which will resubmit
* the AJAX request on successful submission.
*
* @see ding_provider_invoke()
* @see ding_provider_invoke_page()
* @see ding_provider_get_form()
*/
function ding_provider_ajax_form_callback() {
global $user;
$messages = NULL;
try {
if (isset($_REQUEST['dp_form_id']) && !empty($_REQUEST['dp_form_id'])) {
$form_id = (string) $_REQUEST['dp_form_id'];
// Load saved submission.
if ($cached = cache_get('ding_provider_' . $form_id, 'cache_form')) {
$form_state['input'] = $cached->data;
$messages = $form_state['input']['#ding_provider_messages'];
$orig_user = $form_state['input']['#ding_provider_user'];
unset($form_state['input']['#ding_provider_messages']);
unset($form_state['input']['#ding_provider_user']);
cache_clear_all('ding_provider_' . $form_id, 'cache_form');
}
}
// If the user changed (likely because they just logged in),
// things get complicated as ajax_get_form wont find the cached
// form. So we reset the user momentarily.
if (isset($orig_user) && $user->uid != $orig_user->uid) {
$new_user = $user;
$user = $orig_user;
}
list($form, $form_state, $form_id) = ajax_get_form();
if (isset($new_user)) {
$user = $new_user;
}
drupal_process_form($form['#form_id'], $form, $form_state);
// Set any messages we saved.
_ding_provider_message_reset($messages);
if (!empty($form_state['triggering_element'])) {
$callback = $form_state['triggering_element']['#ajax']['callback'];
}
if (!empty($callback) && function_exists($callback)) {
$x = $callback($form, $form_state);
return $x;
}
}
catch (Exception $e) {
if ($e instanceof DingProviderUserException) {
// Rethrow user exceptions.
throw $e;
}
elseif ($e instanceof DingProviderAuthException || $e instanceof EntityMalformedException) {
if (module_exists('ding_user')) {
// Something needs auth, save state and redirect to authentication page.
$expire = 21600;
$input = $form_state['method'] == 'get' ? $_GET : $_POST;
$input['#ding_provider_messages'] = drupal_get_messages();
$input['#ding_provider_user'] = $user;
cache_set('ding_provider_' . $form_id, $input, 'cache_form', REQUEST_TIME + $expire);
$options = array('query' => array('dp_form_id' => $form_id) + ding_provider_get_destination());
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_ding_user_authenticate(array('dp_form_id' => $form_id))
),
);
}
}
elseif ($e instanceof DingProviderConsentException) {
if (module_exists('ding_user_consent')) {
// Something needs auth, save state and redirect to authentication page.
$expire = 21600;
$input = $form_state['method'] == 'get' ? $_GET : $_POST;
$input['#ding_provider_messages'] = drupal_get_messages();
$input['#ding_provider_user'] = $user;
cache_set('ding_provider_' . $form_id, $input, 'cache_form', REQUEST_TIME + $expire);
$options = array('query' => array('dp_form_id' => $form_id) + ding_provider_get_destination());
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_ding_user_consent(array('dp_form_id' => $form_id))
),
);
}
}
}
// Else show internal error.
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_alert(t("Internal error. Please contact the site administrator if the problem persists.")),
),
);
}
/**
* Re-sets messages, avoiding duplicates.
*/
function _ding_provider_message_reset($messages) {
if (is_array($messages)) {
foreach ($messages as $type => $message_list) {
foreach ($message_list as $message) {
drupal_set_message($message, $type, FALSE);
}
}
}
}
/**
* Alternative to drupal_get_destination() which will encode an existing
* destination into the new.
*/
function ding_provider_get_destination($query_args = array()) {
// If destination is set, create a new one embedding the old, and
// unset destination, so drupal_goto wont go to the old
// destination.
$path = $_GET['q'];
$query = drupal_http_build_query($query_args + drupal_get_query_parameters());
unset($_REQUEST['destination']);
if ($query != '') {
$path .= '?' . $query;
}
$destination = array(
'destination' => $path,
);
return $destination;
}
/**
* Get the provide type
**/
function ding_provider_get_provider_type() {
foreach (module_implements('ding_provider') as $module) {
// simply return the first - there can be only one
return $module;
}
}
/**
* Returns the given provider.
*/
function _ding_provider_get_provider($type = NULL) {
static $providers;
if (!isset($providers)) {
$providers = array();
foreach (module_implements('ding_provider') as $module) {
$module_provides = module_invoke($module, 'ding_provider') + array(
'title' => $module . '.module',
'settings' => NULL,
'file' => NULL,
'provides' => array(),
);
foreach ($module_provides['provides'] as $name => $module_provider) {
$module_provider['module'] = $module;
$module_provider['title'] = $module_provides['title'];
$module_provider['global settings'] = $module_provides['settings'];
$module_provider['global settings file'] = $module_provides['file'];
$providers[$name] = $module_provider;
}
}
}
if (!$type) {
return $providers;
}
if (isset($providers[$type])) {
return $providers[$type];
}
return NULL;
}
/**
* Returns user information on the given provider.
*/
function _ding_provider_get_provider_users($type = NULL) {
static $provider_users;
if (!isset($provider_users)) {
$provider_users = array();
foreach (module_implements('ding_provider_user') as $module) {
$module_uses = module_invoke($module, 'ding_provider_user');
foreach ($module_uses as $name => $options) {
if (!isset($provider_users[$name])) {
$provider_users[$name] = array(
'ding_profile' => FALSE,
'required' => array(),
'users' => array(),
);
}
if (!empty($options['install time setup'])) {
$provider_users[$name]['ding_profile'] = TRUE;
}
if ($options['required']) {
$provider_users[$name]['required'][] = $module;
}
else {
$provider_users[$name]['users'][] = $module;
}
}
}
}
if (!$type) {
return $provider_users;
}
if (isset($provider_users[$type])) {
return $provider_users[$type];
}
return NULL;
}
/**
* Returns the provider function for a hook, loading files if necessary. Logs
* an error if the provider or hook isn't implemented, unless $quiet has been
* specified.
*
* @param $type Provider name.
* @param $hook Hook name.
* @param $quiet Whether to suppress errors.
* @return
* String or NULL.
*/
function _ding_provider_function($type, $hook, $quiet = FALSE) {
$provider = _ding_provider_get_provider($type);
if ($provider) {
$function = $provider['module'] . '_' . (isset($provider['prefix']) ? $provider['prefix'] . '_' : '') . $hook;
if (isset($provider['file'])) {
require_once DRUPAL_ROOT . '/' . $provider['file'];
}
if (function_exists($function)) {
return $function;
}
elseif (!$quiet) {
// Trigger an error. This might be a module attempting to use a wrong
// hook, or an improperly implemented plugin. In either case, it's
// programmer error.
throw new DingProviderDoesntImplement(t('Ding @type provider (@module module) does not implement @function', array('@type' => $type, '@function' => $hook, '@module' => $provider['module'])), E_USER_ERROR);
}
}
elseif (!$quiet) {
throw new DingProviderNoProvider(t("Provider module not configured for provider type @type.", array('@type' => $type)));
}
return NULL;
}