-
Notifications
You must be signed in to change notification settings - Fork 13
/
members.php
622 lines (533 loc) · 18.7 KB
/
members.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
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
<?php
/**
* Members theme
*
* Members library functions
*
* @package WordPress
* @subpackage community-portal
* @version 1.0.0
* @author Playground Inc.
*/
?>
<?php
$theme_directory = get_template_directory();
require "{$theme_directory}/class-privacysettings.php";
/**
* Get auth0
*
* @param integer $id WordPress user id.
**/
function mozilla_get_user_auth0( $id ) {
$meta = get_user_meta( $id );
return ( isset( $meta['wp_auth0_id'][0] ) ) ? $meta['wp_auth0_id'][0] : false;
}
/**
* Validate user name
**/
function mozilla_validate_username() {
if ( ! empty( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) {
if ( isset( $_GET['u'] ) ) {
$u = trim( sanitize_user( wp_unslash( $_GET['u'] ) ) );
if ( strlen( $u ) > 0 ) {
$current_user_id = get_current_user_id();
$query = new WP_User_Query(
array(
'search' => $u,
'search_columns' => array(
'user_nicename',
),
'exclude' => array( $current_user_id ),
)
);
echo count( $query->get_results() ) === 0 ? wp_json_encode( true ) : wp_json_encode( false );
}
}
}
die();
}
/**
* Validate email
**/
function mozilla_validate_email() {
if ( ! empty( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) {
if ( isset( $_GET['u'] ) ) {
$u = trim( sanitize_email( wp_unslash( $_GET['u'] ) ) );
if ( strlen( $u ) > 0 ) {
$current_user_id = get_current_user_id();
$query = new WP_User_Query(
array(
'search' => $u,
'search_columns' => array(
'user_email',
),
'exclude' => array( $current_user_id ),
)
);
echo count( $query->get_results() ) === 0 ? wp_json_encode( true ) : wp_json_encode( false );
die();
}
}
}
die();
}
/**
* Get a list of users
**/
function mozilla_get_users() {
$json_users = array();
if ( isset( $_GET['q'] ) ) {
$q = trim( sanitize_user( wp_unslash( $_GET['q'] ) ) );
if ( strlen( $q ) > 0 ) {
$current_user_id = get_current_user_id();
$query = new WP_User_Query(
array(
'search' => "*{$q}*",
'search_columns' => array(
'user_nicename',
),
'exclude' => array( $current_user_id ),
)
);
echo wp_json_encode( $query->get_results() );
}
}
die();
}
/**
* After user is created
*
* @param integer $user_id WordPress user id.
* @param array $userinfo User meta.
* @param boolean $is_new Is this a new user.
* @param string $id_token token string.
* @param string $access_token token string.
* @param string $refresh_token token string.
**/
function mozilla_post_user_creation( $user_id, $userinfo, $is_new, $id_token, $access_token, $refresh_token ) {
$meta = get_user_meta( $user_id );
$current_translation = mozilla_get_current_translation();
if ( $is_new || ! isset( $meta['agree'][0] ) || ( isset( $meta['agree'][0] ) && 'I Agree' !== $meta['agree'][0] ) ) {
$user = get_user_by( 'ID', $user_id );
if ( $current_translation ) {
wp_safe_redirect( "/{$current_translation}/people/{$user->data->user_nicename}/profile/edit/group/1/" );
} else {
wp_safe_redirect( "/people/{$user->data->user_nicename}/profile/edit/group/1/" );
}
exit();
}
if ( isset( $_COOKIE['mozilla-redirect'] ) ) {
$redirect = sanitize_text_field( wp_unslash( $_COOKIE['mozilla-redirect'] ) );
if ( strlen( $redirect ) > 0 ) {
unset( $_COOKIE['mozilla-redirect'] );
wp_safe_redirect( $redirect );
exit();
}
}
}
/**
* Update member information
**/
function mozilla_update_member() {
// Submitted Form!
if ( ! empty( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
if ( is_user_logged_in() && ! empty( $_REQUEST['my_nonce_field'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['my_nonce_field'] ) ), 'protect_content' ) ) {
$user = wp_get_current_user()->data;
// Get current meta to compare to!
$meta = get_user_meta( $user->ID );
$required = array(
'username',
'username_visibility',
'first_name',
'first_name_visibility',
'last_name',
'last_name_visibility',
'email',
'email_visibility',
'agree',
);
$additional_fields = array(
'image_url',
'profile_image_url_visibility',
'pronoun',
'city',
'country',
'profile_pronoun_visibility',
'bio',
'profile_bio_visibility',
'phone',
'profile_phone_visibility',
'discourse',
'profile_discourse_visibility',
'facebook',
'profile_facebook_visibility',
'twitter',
'profile_twitter_visibility',
'linkedin',
'profile_linkedin_visibility',
'github',
'profile_github_visibility',
'telegram',
'profile_telegram_visibility',
'mastodon',
'profile_mastodon_visibility',
'youtube',
'profile_youtube_visibility',
'pixelfed',
'profile_pixelfed_visibility',
'peertube',
'profile_peertube_visibility',
'matrix',
'profile_matrix_visibility',
'languages',
'profile_languages_visibility',
'tags',
'profile_tags_visibility',
'profile_groups_joined_visibility',
'profile_events_attended_visibility',
'profile_events_organized_visibility',
'profile_campaigns_visibility',
'profile_location_visibility',
);
// Add additional required fields after initial setup!
if ( isset( $meta['agree'][0] ) && 'I Agree' === $meta['agree'][0] ) {
unset( $required[8] );
$required[] = 'profile_location_visibility';
$_POST['edit'] = true;
}
$error = false;
foreach ( $required as $field ) {
if ( isset( $_POST[ $field ] ) ) {
if ( 'username' === $field ) {
$current_required_field = sanitize_user( wp_unslash( $_POST[ $field ] ) );
} elseif ( 'email' === $field ) {
$current_required_field = sanitize_email( wp_unslash( $_POST[ $field ] ) );
} else {
$current_required_field = sanitize_text_field( wp_unslash( $_POST[ $field ] ) );
}
if ( '' === $current_required_field || 0 === $current_required_field ) {
$error = true;
}
}
}
// Validate email and username!
if ( false === $error ) {
if ( isset( $_POST['email'] ) && isset( $_POST['username'] ) ) {
$post_email = trim( sanitize_email( wp_unslash( $_POST['email'] ) ) );
$post_user = trim( sanitize_user( wp_unslash( $_POST['username'] ) ) );
if ( ! filter_var( $post_email, FILTER_VALIDATE_EMAIL ) ) {
$error = true;
$_POST['email_error_message'] = __( 'Invalid email address', 'community-portal' );
}
$query = new WP_User_Query(
array(
'search' => $post_email,
'search_columns' => array(
'user_email',
),
'exclude' => array( $user->ID ),
)
);
if ( 0 !== count( $query->get_results() ) ) {
$error = true;
$_POST['email_error_message'] = __( 'This email is already in use', 'community-portal' );
}
$query = new WP_User_Query(
array(
'search' => $post_user,
'search_columns' => array(
'user_nicename',
),
'exclude' => array( $user->ID ),
)
);
// Validate email!
if ( 0 !== count( $query->get_results() ) ) {
$_POST['username_error_message'] = __( 'This username is already in use', 'community-portal' );
$error = true;
}
}
}
// Create the user and save meta data!
if ( false === $error ) {
$_POST['complete'] = true;
// Update regular WordPress user data!
$data = array(
'ID' => $user->ID,
'user_email' => trim( sanitize_email( wp_unslash( $_POST['email'] ) ) ),
);
// We need to udpate the user!
$username_update = trim( sanitize_user( wp_unslash( $_POST['username'] ) ) );
if ( $username_update !== $user->user_nicename ) {
$data['user_nicename'] = $username_update;
$user->user_nicename = $data['user_nicename'];
}
wp_update_user( $data );
// No longe need this key!
unset( $required[0] );
foreach ( $required as $field ) {
if ( isset( $_POST[ $field ] ) ) {
if ( 'username' === $field ) {
$form_data = trim( sanitize_user( wp_unslash( $_POST[ $field ] ) ) );
} else {
$form_data = trim( sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) );
}
update_user_meta( $user->ID, $field, $form_data );
}
}
// Update other fields here!
$addtional_meta = array();
foreach ( $additional_fields as $field ) {
if ( isset( $_POST[ $field ] ) ) {
if ( 'bio' === $field ) {
$current_additional_field = trim( sanitize_textarea_field( wp_unslash( $_POST[ $field ] ) ) );
} else {
if ( ! is_array( $_POST[ $field ] ) ) {
if ( 'image_url' === $field ) {
// Make sure the image is hosted with us!
if ( ! empty( $_SERVER['SERVER_NAME'] ) ) {
$url = trim( esc_url_raw( wp_unslash( $_POST[ $field ] ) ) );
$host_domain = esc_url_raw( wp_unslash( $_SERVER['SERVER_NAME'] ) );
$url_scheme = wp_parse_url( $host_domain );
if ( 'http' === $url_scheme['scheme'] && ( ! empty( $_SERVER['HTTPS'] ) && 'off' !== sanitize_text_field( wp_unslash( $_SERVER['HTTPS'] ) ) ) || ( ! empty( $_SERVER['SERVER_PORT'] ) && 443 === sanitize_key( wp_unslash( $_SERVER['SERVER_PORT'] ) ) ) ) {
$host_domain = str_replace( 'http', 'https', $host_domain );
}
if ( false !== stripos( $url, $host_domain ) ) {
$current_additional_field = $url;
} else {
$current_additional_field = false;
}
} else {
$current_additional_field = false;
}
} else {
$current_additional_field = trim( sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) );
}
} else {
// If we get an array we need to sanitize each value individually!
if ( ! empty( $_POST[ $field ] ) && is_array( $_POST[ $field ] ) ) {
$values = array();
$field_count = count( $_POST[ $field ] );
for ( $x = 0; $x < $field_count; $x++ ) {
if ( ! empty( $_POST[ $field ][ $x ] ) ) {
$values[] = sanitize_text_field( wp_unslash( $_POST[ $field ][ $x ] ) );
}
}
}
$current_additional_field = $values;
}
}
$additional_meta[ $field ] = $current_additional_field;
}
}
update_user_meta( $user->ID, 'community-meta-fields', $additional_meta );
$current_translation = mozilla_get_current_translation();
if ( $current_translation ) {
wp_safe_redirect( "/{$current_translation}/people/{$user->user_nicename}" );
} else {
wp_safe_redirect( "/people/{$user->user_nicename}" );
}
exit();
}
}
}
}
/**
* Check if user is logged in
**/
function mozilla_is_logged_in() {
$current_user = wp_get_current_user()->data;
return count( (array) $current_user ) > 0 ? true : false;
}
/**
* Get user information as object
*
* @param string $meta The meta key.
* @param boolean $is_me It's me Mario.
* @param boolean $logged_in are they logged in.
* @param string $prefix The prefix for the meta.
**/
function mozilla_user_meta_as_object( $meta, $is_me, $logged_in, $prefix = '' ) {
$object = new stdClass();
$object->value = isset( $meta[ $meta ][0] ) ? $meta[ $meta ][0] : false;
$object->display = mozilla_display_field( $meta, isset( $meta[ $prefix . $meta . '_visibility' ][0] ) ? $meta[ $prefix . $meta . '_visibility' ][0] : false, $is_me, $logged_in );
return $object;
}
/**
* Get user information
*
* @param object $me current user.
* @param object $user user to view.
* @param boolean $logged_in are they logged in.
**/
function mozilla_get_user_info( $me, $user, $logged_in ) {
// Username is ALWAYS public!
$object = new stdClass();
$object->value = $user->user_nicename;
$object->display = true;
$data = array(
'username' => $object,
'id' => $user->ID,
);
$is_me = $me && $logged_in && intval( $me->ID ) === intval( $user->ID );
$meta = get_user_meta( $user->ID );
$community_fields = isset( $meta['community-meta-fields'][0] ) ? unserialize( $meta['community-meta-fields'][0] ) : array();
// First Name!
$data['first_name'] = mozilla_user_meta_as_object( 'first_name', $is_me, $logged_in );
// Last Name!
$data['last_name'] = mozilla_user_meta_as_object( 'last_name', $is_me, $logged_in );
// Email!
$data['email'] = mozilla_user_meta_as_object( 'email', $is_me, $logged_in );
// Location!
global $countries;
$object = new stdClass();
if ( isset( $community_fields['country'] ) && strlen( $community_fields['country'] ) > 0 && isset( $countries[ $community_fields['country'] ] ) ) {
$object->value = $community_fields['country'];
} else {
$object->value = false;
}
$object->display = mozilla_display_field( 'location', isset( $meta['profile_location_visibility'][0] ) ? $meta['profile_location_visibility'][0] : false, $is_me, $logged_in );
$data['location'] = $object;
// City!
$object = new stdClass();
if ( isset( $community_fields['city'] ) && strlen( $community_fields['city'] ) > 0 ) {
$object->value = $community_fields['city'];
} else {
$object->value = false;
}
$object->display = mozilla_display_field( 'location', isset( $meta['profile_location_visibility'][0] ) ? $meta['profile_location_visibility'][0] : false, $is_me, $logged_in );
$data['city'] = $object;
// Profile Image!
$data['profile_image'] = mozilla_user_meta_as_object( 'image_url', $is_me, $logged_in, 'profile' );
// Bio!
$data['bio'] = mozilla_user_meta_as_object( 'bio', $is_me, $logged_in, 'profile' );
// Pronoun Visibility!
$data['pronoun'] = mozilla_user_meta_as_object( 'pronoun', $is_me, $logged_in, 'profile' );
// Phone!
$data['phone'] = mozilla_user_meta_as_object( 'phone', $is_me, $logged_in );
// Groups Joined!
$data['groups'] = mozilla_user_meta_as_object( 'groups_joined', $is_me, $logged_in, 'profile' );
// Events Attended!
$data['events_attended'] = mozilla_user_meta_as_object( 'events_attended', $is_me, $logged_in, 'profile' );
// Events Organized!
$data['events_organized'] = mozilla_user_meta_as_object( 'events_organized', $is_me, $logged_in, 'profile' );
// Campaigns!
$object = new StdClass();
$object->display = mozilla_display_field( 'campaigns_participated', isset( $community_fields['profile_campaigns_visibility'] ) ? $community_fields['profile_campaigns_visibility'] : false, $is_me, $logged_in );
$data['campaigns_participated'] = $object;
// Social Media!
$data['telegram'] = mozilla_user_meta_as_object( 'telegram', $is_me, $logged_in, 'profile' );
$data['facebook'] = mozilla_user_meta_as_object( 'facebook', $is_me, $logged_in, 'profile' );
$data['twitter'] = mozilla_user_meta_as_object( 'twitter', $is_me, $logged_in, 'profile' );
$data['linkedin'] = mozilla_user_meta_as_object( 'linkedin', $is_me, $logged_in, 'profile' );
$data['discourse'] = mozilla_user_meta_as_object( 'discourse', $is_me, $logged_in, 'profile' );
$data['github'] = mozilla_user_meta_as_object( 'github', $is_me, $logged_in, 'profile' );
$data['matrix'] = mozilla_user_meta_as_object( 'matrix', $is_me, $logged_in, 'profile' );
$data['mastodon'] = mozilla_user_meta_as_object( 'mastodon', $is_me, $logged_in, 'profile' );
$data['youtube'] = mozilla_user_meta_as_object( 'youtube', $is_me, $logged_in, 'profile' );
$data['pixelfed'] = mozilla_user_meta_as_object( 'pixelfed', $is_me, $logged_in, 'profile' );
$data['peertube'] = mozilla_user_meta_as_object( 'peertube', $is_me, $logged_in, 'profile' );
// Languages!
$data['languages'] = mozilla_user_meta_as_object( 'languages', $is_me, $logged_in, 'profile' );
// Tags!
$data['tags'] = mozilla_user_meta_as_object( 'tags', $is_me, $logged_in, 'profile' );
$object = null;
return $data;
}
/**
* Check whether to display or not
*
* @param string $field field to check.
* @param string $visibility visibility setting.
* @param boolean $is_me are they viewing themselves.
* @param boolean $logged_in are they logged in.
**/
function mozilla_display_field( $field, $visibility, $is_me, $logged_in ) {
if ( $is_me ) {
return true;
}
if ( 'first_name' === $field && $logged_in ) {
return true;
}
if ( intval( $visibility ) === PrivacySettings::PUBLIC_USERS ) {
return true;
}
if ( false === $visibility ) {
return false;
}
if ( $logged_in && intval( $visibility ) === PrivacySettings::REGISTERED_USERS ) {
return true;
}
return false;
}
/**
* Delete user
**/
function mozilla_delete_user() {
if ( ! empty( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user()->data;
$current_translation = mozilla_get_current_translation();
if ( $user ) {
$rand = substr( md5( time() ), 0, 8 );
$anonymous_email = "anonymous{$rand}@anonymous.com";
$user_check = get_user_by( 'email', $anonymous_email );
while ( false !== $user_check ) {
$rand = substr( md5( time() ), 0, 8 );
$anonymous_email = "anonymous{$rand}@anonymous.com";
$user_check = get_user_by( 'email', $anonymous_email );
}
$meta = get_user_meta( $user->ID );
$args = array(
'ID' => $user->ID,
'user_email' => $anonymous_email,
'display_name' => 'Anonymous',
'first_name' => 'Anonymous',
'last_name' => 'Anonymous',
'user_url' => '',
'user_nicename' => "Anonymous{$rand}",
'user_login' => "Anonymous{$rand}",
);
update_user_meta( $user->ID, 'nickname', 'Anonymous' );
update_user_meta( $user->ID, 'first_name', 'Anonymous' );
update_user_meta( $user->ID, 'last_name', 'Anonymous' );
update_user_meta( $user->ID, 'email', $anonymous_email );
wp_update_user( $args );
delete_user_meta( $user->ID, 'community-meta-fields' );
delete_user_meta( $user->ID, 'description', '' );
delete_user_meta( $user->ID, 'wp_auth0_obj' );
delete_user_meta( $user->ID, 'wp_auth0_id' );
delete_user_meta( $user->ID, 'first_name_visibility' );
delete_user_meta( $user->ID, 'last_name_visibility' );
delete_user_meta( $user->ID, 'email_visibility' );
wp_destroy_current_session();
wp_clear_auth_cookie();
wp_set_current_user( 0 );
echo wp_json_encode(
array(
'translation' => $current_translation,
'status' => 'success',
'msg' => 'Account Deleted',
)
);
} else {
echo wp_json_encode(
array(
'translation' => $current_translation,
'status' => 'error',
'msg' => 'No user',
)
);
}
} else {
echo wp_json_encode(
array(
'translation' => $current_translation,
'status' => 'error',
'msg' => 'Invalid Request',
)
);
}
}
die();
}