forked from backdrop-contrib/hybridauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybridauth.module
1331 lines (1202 loc) · 42.5 KB
/
hybridauth.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Main file for the HybridAuth module.
*/
define('HYBRIDAUTH_HASH_SALT', 'hybridauth_hash_salt');
/**
* Implements hook_hook_info().
*/
function hybridauth_hook_info() {
$hooks = array(
'hybridauth_provider_config_alter',
'hybridauth_username_alter',
'hybridauth_profile_alter',
'hybridauth_userinfo_alter',
'hybridauth_registration_form',
'hybridauth_registration_block',
'hybridauth_identity_added',
'hybridauth_identity_deleted',
'hybridauth_user_preinsert',
'hybridauth_user_insert',
'hybridauth_user_login',
'hybridauth_user_blocked',
'hybridauth_forms_list_alter',
'hybridauth_destination_options_alter',
'hybridauth_destination_error_options_alter',
);
return array_fill_keys($hooks, array('group' => 'hybridauth'));
}
/**
* Implements hook_permission().
*/
function hybridauth_permission() {
$permissions = array(
'use hybridauth' => array(
'title' => t('Use HybridAuth'),
'description' => t('Login through HybridAuth, manage own HybridAuth identities.'),
),
);
return $permissions;
}
/**
* Implements hook_menu().
*/
function hybridauth_menu() {
$items = array();
$items['admin/config/people/hybridauth'] = array(
'title' => 'HybridAuth',
'description' => 'Manage HybridAuth social sign-on settings.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('hybridauth_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'hybridauth.admin.inc',
);
$items['admin/config/people/hybridauth/provider/%hybridauth_provider'] = array(
'title callback' => 'hybridauth_get_provider_name',
'title arguments' => array(5),
'page callback' => 'backdrop_get_form',
'page arguments' => array('hybridauth_admin_provider_settings', 5),
'access arguments' => array('administer site configuration'),
'file' => 'hybridauth.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/reports/hybridauth'] = array(
'title' => 'HybridAuth identities',
'description' => 'View HybridAuth identities counts grouped by authentication provider.',
'page callback' => 'hybridauth_report',
'access arguments' => array('access site reports'),
'file' => 'hybridauth.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['hybridauth/endpoint'] = array(
'page callback' => 'hybridauth_endpoint',
'access arguments' => array('use hybridauth'),
'file' => 'hybridauth.pages.inc',
'type' => MENU_CALLBACK,
);
$items['hybridauth/window/%hybridauth_provider'] = array(
'page callback' => 'hybridauth_window_start',
'page arguments' => array(2),
'access arguments' => array('use hybridauth'),
'file' => 'hybridauth.pages.inc',
'type' => MENU_CALLBACK,
);
$items['hybridauth/providers/%/%'] = array(
'page callback' => 'hybridauth_providers',
'page arguments' => array(2, 3),
'access callback' => TRUE,
'file' => 'hybridauth.pages.inc',
'type' => MENU_CALLBACK,
);
$items['user/%user/hybridauth'] = array(
'title' => 'HybridAuth',
'page callback' => 'hybridauth_user_identity',
'page arguments' => array(1),
'access callback' => 'user_edit_access',
'access arguments' => array(1),
'file' => 'hybridauth.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['user/%user/hybridauth/delete'] = array(
'title' => 'Delete HybridAuth identity',
'page callback' => 'backdrop_get_form',
'page arguments' => array('hybridauth_user_identity_delete', 1, 4),
'access callback' => 'user_edit_access',
'access arguments' => array(1),
'file' => 'hybridauth.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu argument loader.
*/
function hybridauth_provider_load($requested_provider_id) {
foreach (array_keys(hybridauth_providers_list()) as $provider_id) {
if (strtolower($requested_provider_id) === strtolower($provider_id)) {
return $provider_id;
}
}
return $requested_provider_id;
}
/**
* Implements hook_menu_site_status_alter().
*/
function hybridauth_menu_site_status_alter(&$menu_site_status, $path) {
// Allow access to hybridauth path even if site is in offline mode.
if ($menu_site_status == MENU_SITE_OFFLINE && user_is_anonymous() && substr($path, 0, 10) == 'hybridauth') {
$menu_site_status = MENU_SITE_ONLINE;
}
}
/**
* Implements hook_block_info().
*/
function hybridauth_block_info() {
$blocks['hybridauth'] = array(
'info' => t('User login - HybridAuth widget only'),
'cache' => BACKDROP_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function hybridauth_block_view($delta = '') {
if ($delta == 'hybridauth' && !user_is_logged_in() && user_access('use hybridauth')) {
$block = array();
$block['subject'] = t('User login');
$block['content'] = array(
'hybridauth' => array(
'#type' => 'hybridauth_widget',
),
);
return $block;
}
}
/**
* Implements hook_user_login().
*/
function hybridauth_user_login(&$edit, $account) {
if ($hybridauth_session_data = _hybridauth_session_load_by_uid($account->uid)) {
$hybridauth_session_data = unserialize($hybridauth_session_data['data']);
$hybridauth_session_data = is_array($hybridauth_session_data) ? $hybridauth_session_data : array();
if (array_key_exists('HYBRIDAUTH::STORAGE', $_SESSION)) {
$hybridauth_session_data_current = $_SESSION['HYBRIDAUTH::STORAGE'];
$hybridauth_session_data = array_merge($hybridauth_session_data, $hybridauth_session_data_current);
}
// Call Hybrid_Auth::restoreSessionData() to set stored data.
$_SESSION['HYBRIDAUTH::STORAGE'] = $hybridauth_session_data;
_hybridauth_session_save(serialize($hybridauth_session_data), $account->uid);
}
}
/**
* Implements hook_user_logout().
*/
function hybridauth_user_logout($account) {
// Try to get HybridAuth instance.
if ($hybridauth = hybridauth_get_instance()) {
// Call Hybrid_Auth::getSessionData() to get stored data.
if (array_key_exists('HYBRIDAUTH::STORAGE', $_SESSION)) {
_hybridauth_session_save(serialize($_SESSION['HYBRIDAUTH::STORAGE']), $account->uid);
}
}
}
/**
* Implements hook_user_delete().
*/
function hybridauth_user_delete($account) {
_hybridauth_identity_delete_by_uid($account->uid);
_hybridauth_session_delete_by_uid($account->uid);
}
/**
* Implements hook_user_cancel().
*/
/*function hybridauth_user_cancel($edit, $account, $method) {
if (in_array($method, array('user_cancel_reassign', 'user_cancel_delete'))) {
_hybridauth_identity_delete_by_uid($account->uid);
}
}*/
/**
* Implements hook_form_alter().
*/
function hybridauth_form_alter(&$form, &$form_state, $form_id) {
_hybridauth_form_alter($form, $form_state, $form_id);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function hybridauth_form_comment_form_alter(&$form, &$form_state, $form_id) {
_hybridauth_form_alter($form, $form_state, 'comment_form');
}
/**
* Implements hook_config_info().
*/
function hybridauth_config_info() {
$prefixes['hybridauth.settings'] = array(
'label' => t('HybridAuth settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Adds HybridAuth widget to enabled forms.
*/
function _hybridauth_form_alter(&$form, &$form_state, $form_id) {
if (!user_is_logged_in() && user_access('use hybridauth') &&
in_array($form_id, array_filter(config_get('hybridauth.settings', 'hybridauth_forms')))) {
$form['hybridauth'] = array(
'#type' => 'hybridauth_widget',
);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function hybridauth_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$config = config('hybridauth.settings');
if ($form['#form_id'] == 'user_profile_form' && !empty($form['#user']->data['hybridauth'])) {
if ($form['account']['name']['#access'] && $config->get('hybridauth_disable_username_change')) {
$form['account']['name']['#access'] = FALSE;
}
if ($config->get('hybridauth_remove_password_fields')) {
$form['#validate'] = array_filter($form['#validate'], 'hybridauth_user_profile_form_validate_filter');
unset($form['account']['pass']);
unset($form['account']['current_pass']);
unset($form['account']['current_pass_required_values']);
}
}
}
/**
* Helper function to remove password validation function.
*/
function hybridauth_user_profile_form_validate_filter($value) {
return !($value == 'user_validate_current_pass');
}
/**
* Implements hook_element_info().
*/
function hybridauth_element_info() {
$config = config('hybridauth.settings');
$types = array();
$path = backdrop_get_path('module', 'hybridauth');
$types['hybridauth_widget'] = array(
'#input' => FALSE,
'#theme' => 'hybridauth_widget',
'#pre_render' => array('hybridauth_widget_pre_render'),
// '#theme_wrappers' => array('form_element'),
'#hybridauth_id' => 'hybridauth',
'#title' => $config->get('hybridauth_widget_title'),
'#weight' => $config->get('hybridauth_widget_weight'),
'#hybridauth_widget_type' => $config->get('hybridauth_widget_type'),
'#hybridauth_widget_use_overlay' => $config->get('hybridauth_widget_use_overlay'),
'#hybridauth_widget_link_text' => $config->get('hybridauth_widget_link_text'),
'#hybridauth_widget_link_title' => $config->get('hybridauth_widget_link_title'),
'#hybridauth_widget_icon_pack' => $config->get('hybridauth_widget_icon_pack'),
'#hybridauth_widget_button_text' => $config->get('hybridauth_widget_button_text'),
'#hybridauth_widget_hide_links' => $config->get('hybridauth_widget_hide_links'),
'#hybridauth_destination' => $config->get('hybridauth_destination'),
'#hybridauth_destination_error' => $config->get('hybridauth_destination_error'),
'#hybridauth_query' => array(),
'#hybridauth_onclick' => '',
'#attached' => array(
// Add cookie library for last used provider feature.
'library' => array(
array('system', 'jquery.cookie'),
),
'js' => array(
$path . '/js/hybridauth.onclick.js',
),
'css' => array(
$path . '/css/hybridauth.css',
$path . '/css/hybridauth.modal.css',
),
),
);
return $types;
}
/**
* Pre-render callback for the 'hybridauth_widget' element.
*/
function hybridauth_widget_pre_render($element) {
$config = config('hybridauth.settings');
$query = $element['#hybridauth_query'];
$element['#hybridauth_destination'] = trim($element['#hybridauth_destination']);
$element['#hybridauth_destination_error'] = trim($element['#hybridauth_destination_error']);
// Process destinations; HTTP_REFERER is needed for widget in modals
// to return to the current page.
foreach (array('destination', 'destination_error') as $key) {
if ($element['#hybridauth_' . $key] == '[HTTP_REFERER]' && isset($_SERVER['HTTP_REFERER'])) {
// We need to make this link relative, see https://www.drupal.org/node/2616284.
global $base_root, $base_path;
$path = str_replace($base_root . $base_path, '', $_SERVER['HTTP_REFERER']);
// Remove language prefix if the site is multilingual.
if (backdrop_multilingual()) {
list(, $path) = language_url_split_prefix($path, language_list());
}
$query += array(
$key => $path,
);
}
elseif ($element['#hybridauth_' . $key]) {
$query += array(
$key => $element['#hybridauth_' . $key],
);
}
else {
$destination = backdrop_get_destination();
$query += array(
$key => $destination['destination'],
);
}
}
$element['#hybridauth_query'] = $query;
$element['providers'] = array();
switch ($element['#hybridauth_widget_type']) {
case 'list':
$class = array('hybridauth-widget-provider');
$rel = array('nofollow');
foreach (hybridauth_get_enabled_providers() as $provider_id => $provider_name) {
$window_type = $config->get('hybridauth_provider_' . $provider_id . '_window_type');
$window_width = $config->get('hybridauth_provider_' . $provider_id . '_window_width');
$window_height = $config->get('hybridauth_provider_' . $provider_id . '_window_height');
$query_mod = $element['#hybridauth_query'];
$class_mod = $class;
$rel_mod = $rel;
switch ($window_type) {
case 'colorbox':
$class_mod[] = 'colorbox-load';
$query_mod += array(
'width' => $window_width,
'height' => $window_height,
'iframe' => 'true',
);
break;
case 'shadowbox':
$rel_mod = array('shadowbox;width=' . $window_width . ';height=' . $window_height, 'nofollow');
break;
case 'fancybox':
$class_mod[] = 'fancybox';
$class_mod[] = 'fancybox.iframe';
$class_mod[] = '{width:' . $window_width . ',height:' . $window_height . '}';
break;
case 'lightbox2':
$rel_mod = array('lightframe[|width:' . $window_width . 'px; height:' . $window_height . 'px;]', 'nofollow');
break;
}
// Determine onclick behavior.
$onclick = '';
if ($element['#hybridauth_onclick'] === FALSE) {
}
elseif (!empty($element['#hybridauth_onclick'])) {
$onclick = $element['#hybridauth_onclick'];
}
elseif ($window_type == 'current') {
$class_mod[] = 'hybridauth-onclick-current';
}
elseif ($window_type == 'popup') {
$class_mod[] = 'hybridauth-onclick-popup';
}
if (!empty($element['#hybridauth_widget_button_text'])) {
$button_text = check_plain($element['#hybridauth_widget_button_text']);
}
else {
$button_text = t('Login with {provider}');
}
$text = array(
'#theme' => 'hybridauth_provider_icon',
'#icon_pack' => $element['#hybridauth_widget_icon_pack'],
'#provider_id' => $provider_id,
'#provider_name' => $provider_name,
'#button_text' => str_replace('{provider}', $provider_name, $button_text),
);
$path = 'hybridauth/window/' . $provider_id;
$url = url($path, array('query' => $query_mod));
if ($element['#hybridauth_widget_hide_links']) {
$path = 'user';
}
$options = array(
'html' => TRUE,
'query' => $query_mod,
'attributes' => array(
'title' => $provider_name,
'class' => $class_mod,
'rel' => $rel_mod,
'data-hybridauth-provider' => $provider_id,
'data-hybridauth-url' => $url,
// jQuery Mobile compatibility - so it doesn't use AJAX.
'data-ajax' => 'false',
// Add authentication window width and height.
'data-hybridauth-width' => $window_width,
'data-hybridauth-height' => $window_height,
) + ($onclick ? array('onclick' => $onclick) : array()),
);
$element['providers'][$provider_id] = array(
'text' => $text,
'path' => $path,
'options' => $options,
);
}
break;
case 'button':
case 'link':
$provider_id = 'none';
$class = $dialog_attributes = array();
if ($element['#hybridauth_widget_use_overlay']) {
backdrop_add_library('system', 'backdrop.ajax');
$class = array('use-ajax', 'hybridauthmodal');
$dialog_attributes = array(
'data-dialog' => true,
'data-dialog-options' => '{
"dialogClass" : "hybridauth-modal",
"minHeight" : 200,
"minWidth" : 400,
"minHeight" : 300,
"minWidth" : 500
}',
);
$element['#attached']['js'][] = array(
'data' => $settings,
'type' => 'setting',
);
}
$text = $element['#hybridauth_widget_link_text'];
if ($element['#hybridauth_widget_type'] == 'button') {
$text = array(
'#theme' => 'hybridauth_provider_icon',
'#icon_pack' => $element['#hybridauth_widget_icon_pack'],
'#provider_id' => $provider_id,
'#provider_name' => $element['#hybridauth_widget_link_title'],
);
}
$path = 'hybridauth/providers/nojs/' . $element['#hybridauth_widget_icon_pack'];
$options = array(
'html' => TRUE,
'query' => $element['#hybridauth_query'],
'attributes' => array(
'title' => $element['#hybridauth_widget_link_title'],
'class' => $class,
'rel' => array('nofollow'),
) + $dialog_attributes,
);
$element['providers'][$provider_id] = array(
'text' => $text,
'path' => $path,
'options' => $options,
);
break;
}
_hybridauth_add_icon_pack_files($element['#hybridauth_widget_icon_pack'], $element);
return $element;
}
/**
* Implements hook_theme().
*/
function hybridauth_theme($existing, $type, $theme, $path) {
return array(
'hybridauth_widget' => array(
'render element' => 'element',
'template' => 'templates/hybridauth_widget',
'file' => 'hybridauth.theme.inc',
),
'hybridauth_provider_icon' => array(
'variables' => array(
'icon_pack' => 'zocial',
'provider_id' => NULL,
'provider_name' => NULL,
'button_text' => NULL,
),
'template' => 'templates/hybridauth_provider_icon',
'file' => 'hybridauth.theme.inc',
),
);
}
/**
* Implements hook_username_alter().
*/
function hybridauth_username_alter(&$name, $account) {
// Don't alter anonymous users or objects that do not have any user ID.
if (empty($account->uid)) {
return;
}
$hybridauth_pattern = config_get('hybridauth.settings', 'hybridauth_display_name');
if (!empty($hybridauth_pattern) && !module_exists('realname')) {
$account2 = user_load($account->uid);
if (!empty($account2->data['hybridauth'])) {
$pattern = str_replace('[user:name]', $account2->name, $hybridauth_pattern);
$hybridauth_name = token_replace($pattern, array('user' => $account2), array('clear' => TRUE));
$name = trim(strip_tags($hybridauth_name));
}
if (empty($name)) {
$name = $account2->name;
}
}
}
/**
* Implements hook_realname_alter().
*/
function hybridauth_realname_alter(&$realname, $account) {
$config = config('hybridauth.settings');
if (!empty($account->data['hybridauth']) && $config->get('hybridauth_override_realname')) {
$hybridauth_pattern = $config->get('hybridauth_display_name');
if (!empty($hybridauth_pattern)) {
$pattern = str_replace('[user:name]', $realname, $hybridauth_pattern);
$hybridauth_name = token_replace($pattern, array('user' => $account), array('clear' => TRUE));
$realname = trim(strip_tags($hybridauth_name));
}
}
}
/**
* Implements hook_field_extra_fields().
*/
function hybridauth_field_extra_fields() {
$extra['user']['user']['display'] = array(
'hybridauth_identities' => array(
'label' => t('HybridAuth identities'),
'description' => t('HybridAuth identities.'),
'weight' => 10,
),
);
return $extra;
}
/**
* Implements hook_user_view().
*/
function hybridauth_user_view($account, $view_mode, $langcode) {
$identities = _hybridauth_identity_load_by_uid($account->uid);
$providers = hybridauth_providers_list();
$header = array(t('Authentication provider'), t('Identity'));
$rows = array();
foreach ($identities as $identity) {
$data = unserialize($identity['data']);
$profile_link = '';
if (!empty($data['profileURL'])) {
$profile_link = l($data['profileURL'], $data['profileURL'], array('attributes' => array('target' => '_blank'), 'external' => TRUE));
}
$rows[] = array(
$providers[$data['provider']]['name'],
$profile_link,
);
}
$account->content['hybridauth_identities'] = array(
'#type' => 'item',
'#title' => t('HybridAuth identities'),
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#sticky' => FALSE,
'#empty' => t('There are no connected identities.'),
// Adding the #name property so that our container has the 'form-item-hybridauth' class.
// @see theme_form_element().
'#name' => 'hybridauth',
);
}
/**
* Implements hook_hybridauth_icon_pack().
*/
function hybridauth_hybridauth_icon_pack($name) {
$filepath = backdrop_get_path('module', 'hybridauth') . '/plugins/icon_pack/';
$plugins = array(
'hybridauth_16' => array(
'title' => t('HybridAuth') . ' 16px',
'css' => $filepath . 'hybridauth_16/hybridauth_16.css',
'path' => $filepath . 'hybridauth_16/',
),
'hybridauth_24' => array(
'title' => t('HybridAuth') . ' 24px',
'css' => $filepath . 'hybridauth_24/hybridauth_24.css',
'path' => $filepath . 'hybridauth_24/',
),
'hybridauth_32' => array(
'title' => t('HybridAuth') . ' 32px',
'css' => $filepath . 'hybridauth_32/hybridauth_32.css',
'path' => $filepath . 'hybridauth_32/',
),
'hybridauth_48' => array(
'title' => t('HybridAuth') . ' 48px',
'css' => $filepath . 'hybridauth_48/hybridauth_48.css',
'path' => $filepath . 'hybridauth_48/',
),
'zocial' => array(
'title' => t('Zocial icons'),
'css' => $filepath . 'zocial/library/css/zocial.css',
'path' => $filepath . 'zocial/',
'plugin' => array(
'include' => $filepath . 'zocial/zocial.inc',
'icon_classes_callback' => 'hybridauth_zocial_icon_classes_callback',
),
),
'zocial_buttons' => array(
'title' => t('Zocial buttons'),
'css' => $filepath . 'zocial/library/css/zocial.css',
'path' => $filepath . 'zocial/',
'plugin' => array(
'include' => $filepath . 'zocial/zocial.inc',
'icon_classes_callback' => 'hybridauth_zocial_icon_classes_callback',
),
),
);
return (isset($plugins[$name])) ? $plugins[$name] : $plugins;
}
/**
* Load metadata for a single icon pack without loading all icon packs.
*/
function hybridauth_get_icon_pack($name) {
$plugins = module_invoke_all('hybridauth_icon_pack', $name);
return $plugins;
}
/**
* Load metadata for all icon packs.
*/
function hybridauth_get_icon_packs() {
$plugins = module_invoke_all('hybridauth_icon_pack', NULL);
return $plugins;
}
/**
* Implements hook_hybridauth_get_providers().
*
* @param array $ids
* An array of strings representing name(s) of provider(s), or an empty
* array to return all providers.
*
* @return array
* An array of metadata about the provider(s) requested.
*/
function hybridauth_hybridauth_get_providers($ids = array()) {
$all_providers = hybridauth_providers_list();
$providers = array();
foreach($ids as $id) {
if (!empty($all_providers[$id])) {
$providers[$id] = $all_providers[$id];
}
}
return $providers;
}
/**
* Load metadata for a single provider without loading all providers.
*/
function hybridauth_get_provider($id) {
$provider = module_invoke_all('hybridauth_get_providers', array($id));
return $provider;
}
/**
* Implements hook_email_registration_name().
*/
function hybridauth_email_registration_name($edit, $account) {
global $_hybridauth_data;
if (!empty($_hybridauth_data) || !empty($account->data['hybridauth'])) {
return $account->name;
}
return NULL;
}
/**
* Implements hook_robotstxt().
*/
function hybridauth_robotstxt() {
return array(
'#HybridAuth paths',
'Disallow: /hybridauth/',
'Disallow: /?q=hybridauth/',
);
}
/**
* Internal functions.
*/
function hybridauth_providers_list($reset = FALSE) {
$providers = &backdrop_static(__FUNCTION__, NULL);
$path = array (
'path' => backdrop_get_path('module', 'hybridauth'),
);
if (!isset($providers)) {
$raw_providers = array();
foreach (hybridauth_providers_files($reset) as $provider_name => $file) {
$provider_id = strtolower($provider_name);
$plugin = array();
$default_plugin_file = BACKDROP_ROOT . '/' . backdrop_get_path('module', 'hybridauth') . '/plugins/provider/' . $provider_name . '/' . $provider_name . '.inc';
if (file_exists($default_plugin_file)) {
require_once $default_plugin_file;
}
if (function_exists('hybridauth_provider_' . $provider_id . '_configuration_form_callback')) {
$plugin['configuration_form_callback'] = 'hybridauth_provider_' . $provider_id . '_configuration_form_callback';
}
if (function_exists('hybridauth_provider_' . $provider_id . '_configuration_callback')) {
$plugin['configuration_callback'] = 'hybridauth_provider_' . $provider_id . '_configuration_callback';
}
$raw_providers[$provider_name] = array(
'name' => t($provider_name),
'plugin' => $plugin + $path,
);
}
$providers = config_get('hybridauth.settings', 'hybridauth_providers');
$enabled_providers = array_filter($providers);
foreach (array_keys($enabled_providers + $raw_providers) as $provider_id) {
$providers[$provider_id] = $raw_providers[$provider_id];
}
}
ksort($providers);
return $providers;
}
/**
* Returns available providers files, keyed by filename without extension.
*/
function hybridauth_providers_files($reset = FALSE) {
$files = array();
// Try to get cached value if allowed: $reset == FALSE.
if (!$reset) {
$cache = cache_get('hybridauth_providers_files');
if (!empty($cache->data)) {
$files = $cache->data;
}
}
// Scan Providers directory if $reset == TRUE or there was no cached value.
if (empty($files)) {
$files = file_scan_directory(_hybridauth_library_path() . '/Provider', '/\.php$/', array('key' => 'name'));
cache_set('hybridauth_providers_files', $files, 'cache', CACHE_TEMPORARY);
}
return $files;
}
/**
* Returns HybridAuth fields.
*/
function hybridauth_fields_list() {
return array(
'provider' => t('Authentication provider'),
'identifier' => t('UID'),
'profileURL' => t('Profile URL'),
'webSiteURL' => t('Website URL'),
'photoURL' => t('Photo URL'),
'displayName' => t('Nickname'),
'description' => t('Short bio or about me'),
'firstName' => t('First name'),
'lastName' => t('Last name'),
'gender' => t('Gender'),
'language' => t('Language'),
'age' => t('Age'),
'birthDay' => t('Birth day'),
'birthMonth' => t('Birth month'),
'birthYear' => t('Birth year'),
'email' => t('E-mail address'),
'emailVerified' => t('Verified email address'),
'phone' => t('Phone number'),
'address' => t('Address'),
'country' => t('Country'),
'region' => t('State or region'),
'city' => t('City'),
'zip' => t('Postal code or zipcode'),
);
}
/**
* Returns supported forms.
*/
function hybridauth_forms_list() {
$forms = array(
'user_login' => t('User login form'),
'user_login_block' => t('User login block'),
'user_register_form' => t('User registration form'),
'comment_form' => t('Comment form'),
);
backdrop_alter('hybridauth_forms_list', $forms);
return $forms;
}
/**
* Returns auth provider name by provider ID.
*/
function hybridauth_get_provider_name($provider_id) {
$providers = hybridauth_providers_list();
return isset($providers[$provider_id]) ? $providers[$provider_id]['name'] : NULL;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function hybridauth_get_enabled_providers() {
$providers = &backdrop_static(__FUNCTION__, NULL);
if (!isset($providers)) {
$providers = array();
$all_providers = hybridauth_providers_list();
$enabled_providers = array_filter(config_get('hybridauth.settings', 'hybridauth_providers'));
foreach (array_keys($enabled_providers) as $provider_id) {
$providers[$provider_id] = $all_providers[$provider_id]['name'];
}
}
return $providers;
}
/**
* Returns HybridAuth object or exception code.
*/
function hybridauth_get_instance() {
$controller = &backdrop_static(__FUNCTION__);
if (!isset($controller)) {
$controller = FALSE;
if ($lib_path = _hybridauth_library_path()) {
if (file_exists($lib_path . '/autoload.php')) {
require_once $lib_path . '/autoload.php';
}
$config = hybridauth_get_config();
try {
$controller = new Hybridauth\Hybridauth($config);
}
catch (Exception $e) {
watchdog_exception('hybridauth', $e);
$controller = $e->getCode();
}
}
}
return $controller;
}
/**
* Returns HybridAuth storage.
*/
function hybridauth_get_storage() {
$storage = &backdrop_static(__FUNCTION__);
if (!isset($storage)) {
$storage = FALSE;
try {
$storage = new Hybridauth\Storage\Session();
}
catch(Exception $e) {
watchdog_exception('hybridauth', $e);
$storage = $e->getCode();
}
}
return $storage;
}
/**
* Returns HybridAuth config.
*/
function hybridauth_get_config() {
$ha_config = &backdrop_static(__FUNCTION__, NULL);
if (!isset($ha_config)) {
$logfile = file_directory_temp() . '/hybridauth.debug.log';
$hybridauth_debug = config_get('hybridauth.settings', 'hybridauth_debug');
if ($hybridauth_debug && file_put_contents($logfile, PHP_EOL . 'HYBRIDAUTH DEBUG LOG START' . PHP_EOL . PHP_EOL, FILE_APPEND) === FALSE) {
watchdog('hybridauth', 'Failed to write to debug log file @logfile.', array('@logfile' => $logfile), WATCHDOG_ERROR);
$hybridauth_debug = FALSE;
}
$ha_config = array(
'callback' => url('hybridauth/endpoint', array('absolute' => TRUE, 'language' => _hybridauth_language_default())),
'providers' => array(),
'debug_mode' => $hybridauth_debug,
'debug_file' => $logfile,
);
// Special case Facebook: we need to provide a language-aware base URL
// because otherwise there is a redirect_uri mismatch in the Facebook SDK.
if ($_GET['q'] === 'hybridauth/window/Facebook') {
$ha_config['callback'] = url('hybridauth/endpoint', array('absolute' => TRUE));
}
if ($proxy = config_get('hybridauth.settings', 'hybridauth_proxy')) {
$ha_config['proxy'] = $proxy;
}
foreach (hybridauth_get_enabled_providers() as $provider_id => $provider_name) {
if ($provider_config = hybridauth_get_provider_config($provider_id)) {
$ha_config['providers'][$provider_id] = $provider_config;
}
}
}
return $ha_config;
}
/**
* Returns provider config.
*/
function hybridauth_get_provider_config($provider_id) {
$lowercase_provider_id = strtolower($provider_id);
$config = config('hybridauth.settings');
$provider_config = array(
'enabled' => array_key_exists($provider_id, array_filter($config->get('hybridauth_providers'))),
'keys' => array(
'id' => trim($config->get('hybridauth_provider_' . $lowercase_provider_id . '_keys_id')),
'key' => trim($config->get('hybridauth_provider_' . $lowercase_provider_id . '_keys_key')),
'secret' => trim($config->get('hybridauth_provider_' . $lowercase_provider_id . '_keys_secret')),
),
'scope' => $config->get('hybridauth_provider_' . $lowercase_provider_id . '_scope'),
'display' => $config->get('hybridauth_provider_' . $lowercase_provider_id . '_display'),
'hauth_return_to' => url('hybridauth/endpoint', array('absolute' => TRUE, 'language' => _hybridauth_language_default())),
);
if (is_array($provider_config['scope'])) {
$provider_config['scope'] = array_filter($config['scope']);
}
$provider = hybridauth_get_provider($provider_id);
if (!empty($provider) && file_exists(BACKDROP_ROOT . '/' . $provider[$provider_id]['plugin']['path'] . '/plugins/provider/' . $provider_id . '/' . $provider_id . '.inc')) {
require_once BACKDROP_ROOT . '/' . $provider[$provider_id]['plugin']['path'] . '/plugins/provider/' . $provider_id . '/' . $provider_id . '.inc';
if (!empty($provider[$provider_id]['plugin']['configuration_callback'])) {
$function = $provider[$provider_id]['plugin']['configuration_callback'];
$function($provider_config, $provider_id);
}
}
// Allow other modules to alter the provider config.
backdrop_alter('hybridauth_provider_config', $provider_config, $provider_id);
return $provider_config;
}
/**
* Returns connected providers for the current user.
*/
function hybridauth_get_connected_providers() {