forked from fmestrone/social_connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.php
204 lines (186 loc) · 7.77 KB
/
start.php
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
<?php
elgg_register_event_handler('init', 'system', 'social_connect_init');
function social_connect_init() {
// add connect form after login form
if ( !elgg_get_plugin_setting('social_bar_hide_login', 'social_connect') ) {
elgg_extend_view('forms/login' , 'social_connect/connect', 501);
}
// add connect form before register form
if ( !elgg_get_plugin_setting('social_bar_hide_register', 'social_connect') ) {
elgg_extend_view('forms/register', 'social_connect/connect', 499);
}
// extend admin style sheet
elgg_extend_view('css/admin' , 'social_connect/admincss');
// extend main style sheet
elgg_extend_view('css/elgg' , 'social_connect/css');
// handle 'public_pages','wall_garden' hook to allow plugin to work in walled garden too
elgg_register_plugin_hook_handler('public_pages', 'walled_garden', 'social_connect_public_pages');
// register this plugin for version updates
if ( elgg_is_active_plugin('version_check') ) {
version_check_register_plugin('social_connect');
}
}
function social_connect_public_pages($hook, $type, $return_value, $params) {
// add to the current list of public pages that should be available from the walled garden
$return_value[] = 'mod/social_connect/authenticate\\.php';
$return_value[] = 'mod/social_connect/index\\.php';
// return the modified value
return $return_value;
}
function social_connect_handle_authentication($user_profile, $provider) {
global $CONFIG;
global $HA_SOCIAL_CONNECT_PROVIDERS_CONFIG;
$social_connect_cant_register = elgg_get_plugin_setting('social_bar_hide_register', 'social_connect');
$social_connect_cant_login = elgg_get_plugin_setting('social_bar_hide_login', 'social_connect');
$ignore_access = elgg_get_ignore_access();
$provider_name = $HA_SOCIAL_CONNECT_PROVIDERS_CONFIG[$provider]['provider_name'];
$user_uid = $user_profile->identifier;
// establish the value for the proceeding hook
$default_proceed = elgg_get_plugin_setting("ha_settings_{$provider}_hook1_default", 'social_connect');
if ( !$default_proceed || $default_proceed == 'global' ) {
$default_proceed = elgg_get_plugin_setting('ha_settings_hook1_default', 'social_connect');
}
if ( !$default_proceed ) {
$default_proceed = SOCIAL_CONNECT_DEFAULT_PROCEED;
} else if ( $default_proceed == 'true' ) {
$default_proceed = true;
} else if ( $default_proceed == 'false' ) {
$default_proceed = false;
}
// the arguments for social connect events and hooks
$args = array(
'mode' => null,
'userid' => $user_uid,
'provider' => $HA_SOCIAL_CONNECT_PROVIDERS_CONFIG[$provider],
'user' => null,
'profile' => $user_profile,
);
// look for users that have already connected via this plugin
$options = array(
'type' => 'user',
'plugin_id' => 'social_connect',
'plugin_user_setting_name_value_pairs' => array(
"$provider/uid" => $user_uid
),
'plugin_user_setting_name_value_pairs_operator' => 'AND',
'limit' => 0
);
$users = elgg_get_entities_from_plugin_user_settings($options);
if ( !$users ) { // user has not connected with plugin before
if ( $social_connect_cant_register ) {
system_message(elgg_echo('social_connect:register:not_allowed'));
return;
}
$args['mode'] = 'connect';
elgg_set_ignore_access(true);
$proceed = elgg_trigger_plugin_hook('social_connect', 'user', $args, $default_proceed);
elgg_set_ignore_access($ignore_access);
if ( $proceed === false ) { // hook prevented social connection
return;
} else if ( $proceed === 'email' || $proceed === 'emailOnly' ) { // hook wants to try and connect via email address
// check whether the user already exists with the email provided
$useremail = $user_profile->email;
if ( $useremail && ($users = get_user_by_email($useremail)) ) {
social_connect_user($user_uid, $users[0], $user_profile, $provider);
system_message(sprintf(elgg_echo('social_connect:connect:ok'), $provider_name));
$args['mode'] = 'email';
$args['user'] = $users[0];
elgg_set_ignore_access(true);
elgg_trigger_event('social_connect', 'user', $args);
elgg_set_ignore_access($ignore_access);
return;
}
if ( $proceed === 'emailOnly' ) { // hook wants only email address connection or failure
register_error(sprintf(elgg_echo('social_connect:connect:emailnotfound'), $proceed));
return;
}
}
// email connection not required or failed, so register a new user
$userlogin = str_replace(' ', '', $user_profile->displayName);
if ( !$userlogin ) {
$userlogin = $provider . '_user_' . rand(1000, 9999);
}
$org_userlogin = $userlogin;
while ( get_user_by_username($userlogin) ) {
$userlogin = $org_userlogin . '_' . rand(1000, 9999);
}
unset($org_userlogin);
$password = generate_random_cleartext_password();
$username = $user_profile->displayName;
$user = new ElggUser();
$user->username = $userlogin;
$user->name = $username;
$user->email = $user_profile->email;
$user->access_id = ACCESS_PUBLIC;
$user->salt = generate_random_cleartext_password();
$user->password = generate_user_password($user, $password);
$user->owner_guid = 0;
$user->container_guid = 0;
if ( $user->save() ) {
if ( $user->email && elgg_get_plugin_setting('notify_new_user', 'social_connect') ) {
$email = elgg_echo('email:social_connect:body', array($userlogin, $password));
set_user_notification_setting($user->getGUID(), 'email', true);
notify_user($user->guid, $CONFIG->site->guid, elgg_echo('email:social_connect:subject', array($provider_name)), $email, NULL, 'email');
}
} else {
register_error(sprintf(elgg_echo('social_connect:register:bad'), $provider_name));
return;
}
system_message(sprintf(elgg_echo('social_connect:register:ok'), $provider_name));
social_connect_user($user_uid, $user, $user_profile, $provider);
$args['mode'] = 'register';
$args['user'] = $user;
elgg_set_ignore_access(true);
elgg_trigger_event('social_connect', 'user', $args);
elgg_set_ignore_access($ignore_access);
} elseif ( count($users) == 1 ) { // one user has already been registered on Elgg with this provider
if ( $social_connect_cant_login ) {
system_message(elgg_echo('social_connect:login:not_allowed'));
return;
}
$args['mode'] = 'login';
$args['user'] = $users[0];
elgg_set_ignore_access(true);
if ( elgg_trigger_plugin_hook('social_connect', 'user', $args, (bool)$default_proceed) ) { // if not, hook prevented social connection
login($users[0]);
system_message(sprintf(elgg_echo('social_connect:login:ok'), $provider_name));
}
elgg_set_ignore_access($ignore_access);
} else {
throw new Exception(sprintf(elgg_echo('social_connect:login:bad'), $provider_name));
}
}
function social_connect_user($user_uid, $user, $user_profile, $provider) {
// register user && provider
elgg_set_plugin_user_setting("$provider/uid", $user_uid, $user->guid, 'social_connect');
login($user);
# {{{ user image
if ( $user_profile->photoURL ) {
$sizes = array(
'topbar' => array(16, 16, TRUE),
'tiny' => array(25, 25, TRUE),
'small' => array(40, 40, TRUE),
'medium' => array(100, 100, TRUE),
'large' => array(200, 200, FALSE),
'master' => array(550, 550, FALSE),
);
$filehandler = new ElggFile();
$filehandler->owner_guid = $user->guid;
foreach ( $sizes as $size => $dimensions ) {
$image = get_resized_image_from_existing_file(
$user_profile->photoURL,
$dimensions[0],
$dimensions[1],
$dimensions[2]
);
$filehandler->setFilename("profile/{$user->guid}{$size}.jpg");
if ( !$filehandler->exists() ) {
$filehandler->open('write');
$filehandler->write($image);
$filehandler->close();
}
}
$user->icontime = time();
}
# }}} user image
}