forked from SimpleMachines/SMF
/
Profile-Modify.php
4175 lines (3668 loc) · 142 KB
/
Profile-Modify.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
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
/**
* This file has the primary job of showing and editing people's profiles.
* It also allows the user to change some of their or another's preferences,
* and such things
*
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines http://www.simplemachines.org
* @copyright 2018 Simple Machines and individual contributors
* @license http://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1 Beta 4
*/
if (!defined('SMF'))
die('No direct access...');
/**
* This defines every profile field known to man.
*
* @param bool $force_reload Whether to reload the data
*/
function loadProfileFields($force_reload = false)
{
global $context, $profile_fields, $txt, $scripturl, $modSettings, $user_info, $smcFunc, $cur_profile, $language;
global $sourcedir, $profile_vars;
// Don't load this twice!
if (!empty($profile_fields) && !$force_reload)
return;
/* This horrific array defines all the profile fields in the whole world!
In general each "field" has one array - the key of which is the database column name associated with said field. Each item
can have the following attributes:
string $type: The type of field this is - valid types are:
- callback: This is a field which has its own callback mechanism for templating.
- check: A simple checkbox.
- hidden: This doesn't have any visual aspects but may have some validity.
- password: A password box.
- select: A select box.
- text: A string of some description.
string $label: The label for this item - default will be $txt[$key] if this isn't set.
string $subtext: The subtext (Small label) for this item.
int $size: Optional size for a text area.
array $input_attr: An array of text strings to be added to the input box for this item.
string $value: The value of the item. If not set $cur_profile[$key] is assumed.
string $permission: Permission required for this item (Excluded _any/_own subfix which is applied automatically).
function $input_validate: A runtime function which validates the element before going to the database. It is passed
the relevant $_POST element if it exists and should be treated like a reference.
Return types:
- true: Element can be stored.
- false: Skip this element.
- a text string: An error occured - this is the error message.
function $preload: A function that is used to load data required for this element to be displayed. Must return
true to be displayed at all.
string $cast_type: If set casts the element to a certain type. Valid types (bool, int, float).
string $save_key: If the index of this element isn't the database column name it can be overriden
with this string.
bool $is_dummy: If set then nothing is acted upon for this element.
bool $enabled: A test to determine whether this is even available - if not is unset.
string $link_with: Key which links this field to an overall set.
Note that all elements that have a custom input_validate must ensure they set the value of $cur_profile correct to enable
the changes to be displayed correctly on submit of the form.
*/
$profile_fields = array(
'avatar_choice' => array(
'type' => 'callback',
'callback_func' => 'avatar_select',
// This handles the permissions too.
'preload' => 'profileLoadAvatarData',
'input_validate' => 'profileSaveAvatarData',
'save_key' => 'avatar',
),
'bday1' => array(
'type' => 'callback',
'callback_func' => 'birthdate',
'permission' => 'profile_extra',
'preload' => function() use ($cur_profile, &$context)
{
// Split up the birthdate....
list ($uyear, $umonth, $uday) = explode('-', empty($cur_profile['birthdate']) || $cur_profile['birthdate'] === '1004-01-01' ? '--' : $cur_profile['birthdate']);
$context['member']['birth_date'] = array(
'year' => $uyear,
'month' => $umonth,
'day' => $uday,
);
return true;
},
'input_validate' => function(&$value) use (&$cur_profile, &$profile_vars)
{
if (isset($_POST['bday2'], $_POST['bday3']) && $value > 0 && $_POST['bday2'] > 0)
{
// Set to blank?
if ((int) $_POST['bday3'] == 1 && (int) $_POST['bday2'] == 1 && (int) $value == 1)
$value = '1004-01-01';
else
$value = checkdate($value, $_POST['bday2'], $_POST['bday3'] < 1004 ? 1004 : $_POST['bday3']) ? sprintf('%04d-%02d-%02d', $_POST['bday3'] < 1004 ? 1004 : $_POST['bday3'], $_POST['bday1'], $_POST['bday2']) : '1004-01-01';
}
else
$value = '1004-01-01';
$profile_vars['birthdate'] = $value;
$cur_profile['birthdate'] = $value;
return false;
},
),
// Setting the birthdate the old style way?
'birthdate' => array(
'type' => 'hidden',
'permission' => 'profile_extra',
'input_validate' => function(&$value) use ($cur_profile)
{
// @todo Should we check for this year and tell them they made a mistake :P? (based on coppa at least?)
if (preg_match('/(\d{4})[\-\., ](\d{2})[\-\., ](\d{2})/', $value, $dates) === 1)
{
$value = checkdate($dates[2], $dates[3], $dates[1] < 4 ? 4 : $dates[1]) ? sprintf('%04d-%02d-%02d', $dates[1] < 4 ? 4 : $dates[1], $dates[2], $dates[3]) : '1004-01-01';
return true;
}
else
{
$value = empty($cur_profile['birthdate']) ? '1004-01-01' : $cur_profile['birthdate'];
return false;
}
},
),
'date_registered' => array(
'type' => 'date',
'value' => empty($cur_profile['date_registered']) ? $txt['not_applicable'] : strftime('%Y-%m-%d', $cur_profile['date_registered'] + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
'label' => $txt['date_registered'],
'log_change' => true,
'permission' => 'moderate_forum',
'input_validate' => function(&$value) use ($txt, $user_info, $modSettings, $cur_profile, $context)
{
// Bad date! Go try again - please?
if (($value = strtotime($value)) === -1)
{
$value = $cur_profile['date_registered'];
return $txt['invalid_registration'] . ' ' . strftime('%d %b %Y ' . (strpos($user_info['time_format'], '%H') !== false ? '%I:%M:%S %p' : '%H:%M:%S'), forum_time(false));
}
// As long as it doesn't equal "N/A"...
elseif ($value != $txt['not_applicable'] && $value != strtotime(strftime('%Y-%m-%d', $cur_profile['date_registered'] + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600)))
$value = $value - ($user_info['time_offset'] + $modSettings['time_offset']) * 3600;
else
$value = $cur_profile['date_registered'];
return true;
},
),
'email_address' => array(
'type' => 'email',
'label' => $txt['user_email_address'],
'subtext' => $txt['valid_email'],
'log_change' => true,
'permission' => 'profile_password',
'js_submit' => !empty($modSettings['send_validation_onChange']) ? '
form_handle.addEventListener(\'submit\', function(event)
{
if (this.email_address.value != "'. (!empty($cur_profile['email_address']) ? $cur_profile['email_address'] : '') . '")
{
alert('. JavaScriptEscape($txt['email_change_logout']) . ');
return true;
}
}, false);' : '',
'input_validate' => function(&$value)
{
global $context, $old_profile, $profile_vars, $sourcedir, $modSettings;
if (strtolower($value) == strtolower($old_profile['email_address']))
return false;
$isValid = profileValidateEmail($value, $context['id_member']);
// Do they need to revalidate? If so schedule the function!
if ($isValid === true && !empty($modSettings['send_validation_onChange']) && !allowedTo('moderate_forum'))
{
require_once($sourcedir . '/Subs-Members.php');
$profile_vars['validation_code'] = generateValidationCode();
$profile_vars['is_activated'] = 2;
$context['profile_execute_on_save'][] = 'profileSendActivation';
unset($context['profile_execute_on_save']['reload_user']);
}
return $isValid;
},
),
// Selecting group membership is a complicated one so we treat it separate!
'id_group' => array(
'type' => 'callback',
'callback_func' => 'group_manage',
'permission' => 'manage_membergroups',
'preload' => 'profileLoadGroups',
'log_change' => true,
'input_validate' => 'profileSaveGroups',
),
'id_theme' => array(
'type' => 'callback',
'callback_func' => 'theme_pick',
'permission' => 'profile_extra',
'enabled' => $modSettings['theme_allow'] || allowedTo('admin_forum'),
'preload' => function() use ($smcFunc, &$context, $cur_profile, $txt)
{
$request = $smcFunc['db_query']('', '
SELECT value
FROM {db_prefix}themes
WHERE id_theme = {int:id_theme}
AND variable = {string:variable}
LIMIT 1', array(
'id_theme' => $cur_profile['id_theme'],
'variable' => 'name',
)
);
list ($name) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$context['member']['theme'] = array(
'id' => $cur_profile['id_theme'],
'name' => empty($cur_profile['id_theme']) ? $txt['theme_forum_default'] : $name
);
return true;
},
'input_validate' => function(&$value)
{
$value = (int) $value;
return true;
},
),
'lngfile' => array(
'type' => 'select',
'options' => function() use (&$context)
{
return $context['profile_languages'];
},
'label' => $txt['preferred_language'],
'permission' => 'profile_identity',
'preload' => 'profileLoadLanguages',
'enabled' => !empty($modSettings['userLanguage']),
'value' => empty($cur_profile['lngfile']) ? $language : $cur_profile['lngfile'],
'input_validate' => function(&$value) use (&$context, $cur_profile)
{
// Load the languages.
profileLoadLanguages();
if (isset($context['profile_languages'][$value]))
{
if ($context['user']['is_owner'] && empty($context['password_auth_failed']))
$_SESSION['language'] = $value;
return true;
}
else
{
$value = $cur_profile['lngfile'];
return false;
}
},
),
// The username is not always editable - so adjust it as such.
'member_name' => array(
'type' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? 'text' : 'label',
'label' => $txt['username'],
'subtext' => allowedTo('admin_forum') && !isset($_GET['changeusername']) ? '[<a href="' . $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=account;changeusername" style="font-style: italic;">' . $txt['username_change'] . '</a>]' : '',
'log_change' => true,
'permission' => 'profile_identity',
'prehtml' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? '<div class="alert">' . $txt['username_warning'] . '</div>' : '',
'input_validate' => function(&$value) use ($sourcedir, $context, $user_info, $cur_profile)
{
if (allowedTo('admin_forum'))
{
// We'll need this...
require_once($sourcedir . '/Subs-Auth.php');
// Maybe they are trying to change their password as well?
$resetPassword = true;
if (isset($_POST['passwrd1']) && $_POST['passwrd1'] != '' && isset($_POST['passwrd2']) && $_POST['passwrd1'] == $_POST['passwrd2'] && validatePassword($_POST['passwrd1'], $value, array($cur_profile['real_name'], $user_info['username'], $user_info['name'], $user_info['email'])) == null)
$resetPassword = false;
// Do the reset... this will send them an email too.
if ($resetPassword)
resetPassword($context['id_member'], $value);
elseif ($value !== null)
{
validateUsername($context['id_member'], trim(preg_replace('~[\t\n\r \x0B\0' . ($context['utf8'] ? '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}' : '\x00-\x08\x0B\x0C\x0E-\x19\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $value)));
updateMemberData($context['id_member'], array('member_name' => $value));
// Call this here so any integrated systems will know about the name change (resetPassword() takes care of this if we're letting SMF generate the password)
call_integration_hook('integrate_reset_pass', array($cur_profile['member_name'], $value, $_POST['passwrd1']));
}
}
return false;
},
),
'passwrd1' => array(
'type' => 'password',
'label' => ucwords($txt['choose_pass']),
'subtext' => $txt['password_strength'],
'size' => 20,
'value' => '',
'permission' => 'profile_password',
'save_key' => 'passwd',
// Note this will only work if passwrd2 also exists!
'input_validate' => function(&$value) use ($sourcedir, $user_info, $smcFunc, $cur_profile)
{
// If we didn't try it then ignore it!
if ($value == '')
return false;
// Do the two entries for the password even match?
if (!isset($_POST['passwrd2']) || $value != $_POST['passwrd2'])
return 'bad_new_password';
// Let's get the validation function into play...
require_once($sourcedir . '/Subs-Auth.php');
$passwordErrors = validatePassword($value, $cur_profile['member_name'], array($cur_profile['real_name'], $user_info['username'], $user_info['name'], $user_info['email']));
// Were there errors?
if ($passwordErrors != null)
return 'password_' . $passwordErrors;
// Set up the new password variable... ready for storage.
$value = hash_password($cur_profile['member_name'], un_htmlspecialchars($value));
return true;
},
),
'passwrd2' => array(
'type' => 'password',
'label' => ucwords($txt['verify_pass']),
'size' => 20,
'value' => '',
'permission' => 'profile_password',
'is_dummy' => true,
),
'personal_text' => array(
'type' => 'text',
'label' => $txt['personal_text'],
'log_change' => true,
'input_attr' => array('maxlength="50"'),
'size' => 50,
'permission' => 'profile_blurb',
'input_validate' => function(&$value) use ($smcFunc)
{
if ($smcFunc['strlen']($value) > 50)
return 'personal_text_too_long';
return true;
},
),
// This does ALL the pm settings
'pm_prefs' => array(
'type' => 'callback',
'callback_func' => 'pm_settings',
'permission' => 'pm_read',
'preload' => function() use (&$context, $cur_profile)
{
$context['display_mode'] = $cur_profile['pm_prefs'] & 3;
$context['receive_from'] = !empty($cur_profile['pm_receive_from']) ? $cur_profile['pm_receive_from'] : 0;
return true;
},
'input_validate' => function(&$value) use (&$cur_profile, &$profile_vars)
{
// Simple validate and apply the two "sub settings"
$value = max(min($value, 2), 0);
$cur_profile['pm_receive_from'] = $profile_vars['pm_receive_from'] = max(min((int) $_POST['pm_receive_from'], 4), 0);
return true;
},
),
'posts' => array(
'type' => 'int',
'label' => $txt['profile_posts'],
'log_change' => true,
'size' => 7,
'permission' => 'moderate_forum',
'input_validate' => function(&$value)
{
if (!is_numeric($value))
return 'digits_only';
else
$value = $value != '' ? strtr($value, array(',' => '', '.' => '', ' ' => '')) : 0;
return true;
},
),
'real_name' => array(
'type' => allowedTo('profile_displayed_name_own') || allowedTo('profile_displayed_name_any') || allowedTo('moderate_forum') ? 'text' : 'label',
'label' => $txt['name'],
'subtext' => $txt['display_name_desc'],
'log_change' => true,
'input_attr' => array('maxlength="60"'),
'permission' => 'profile_displayed_name',
'enabled' => allowedTo('profile_displayed_name_own') || allowedTo('profile_displayed_name_any') || allowedTo('moderate_forum'),
'input_validate' => function(&$value) use ($context, $smcFunc, $sourcedir, $cur_profile)
{
$value = trim(preg_replace('~[\t\n\r \x0B\0' . ($context['utf8'] ? '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}' : '\x00-\x08\x0B\x0C\x0E-\x19\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $value));
if (trim($value) == '')
return 'no_name';
elseif ($smcFunc['strlen']($value) > 60)
return 'name_too_long';
elseif ($cur_profile['real_name'] != $value)
{
require_once($sourcedir . '/Subs-Members.php');
if (isReservedName($value, $context['id_member']))
return 'name_taken';
}
return true;
},
),
'secret_question' => array(
'type' => 'text',
'label' => $txt['secret_question'],
'subtext' => $txt['secret_desc'],
'size' => 50,
'permission' => 'profile_password',
),
'secret_answer' => array(
'type' => 'text',
'label' => $txt['secret_answer'],
'subtext' => $txt['secret_desc2'],
'size' => 20,
'postinput' => '<span class="smalltext"><a href="' . $scripturl . '?action=helpadmin;help=secret_why_blank" onclick="return reqOverlayDiv(this.href);"><span class="generic_icons help"></span> ' . $txt['secret_why_blank'] . '</a></span>',
'value' => '',
'permission' => 'profile_password',
'input_validate' => function(&$value)
{
$value = $value != '' ? md5($value) : '';
return true;
},
),
'signature' => array(
'type' => 'callback',
'callback_func' => 'signature_modify',
'permission' => 'profile_signature',
'enabled' => substr($modSettings['signature_settings'], 0, 1) == 1,
'preload' => 'profileLoadSignatureData',
'input_validate' => 'profileValidateSignature',
),
'show_online' => array(
'type' => 'check',
'label' => $txt['show_online'],
'permission' => 'profile_identity',
'enabled' => !empty($modSettings['allow_hideOnline']) || allowedTo('moderate_forum'),
),
'smiley_set' => array(
'type' => 'callback',
'callback_func' => 'smiley_pick',
'enabled' => !empty($modSettings['smiley_sets_enable']),
'permission' => 'profile_extra',
'preload' => function() use ($modSettings, &$context, $txt, $cur_profile, $smcFunc)
{
$context['member']['smiley_set']['id'] = empty($cur_profile['smiley_set']) ? '' : $cur_profile['smiley_set'];
$context['smiley_sets'] = explode(',', 'none,,' . $modSettings['smiley_sets_known']);
$set_names = explode("\n", $txt['smileys_none'] . "\n" . $txt['smileys_forum_board_default'] . "\n" . $modSettings['smiley_sets_names']);
foreach ($context['smiley_sets'] as $i => $set)
{
$context['smiley_sets'][$i] = array(
'id' => $smcFunc['htmlspecialchars']($set),
'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
'selected' => $set == $context['member']['smiley_set']['id']
);
if ($context['smiley_sets'][$i]['selected'])
$context['member']['smiley_set']['name'] = $set_names[$i];
}
return true;
},
'input_validate' => function(&$value)
{
global $modSettings;
$smiley_sets = explode(',', $modSettings['smiley_sets_known']);
if (!in_array($value, $smiley_sets) && $value != 'none')
$value = '';
return true;
},
),
// Pretty much a dummy entry - it populates all the theme settings.
'theme_settings' => array(
'type' => 'callback',
'callback_func' => 'theme_settings',
'permission' => 'profile_extra',
'is_dummy' => true,
'preload' => function() use (&$context, $user_info, $modSettings)
{
loadLanguage('Settings');
$context['allow_no_censored'] = false;
if ($user_info['is_admin'] || $context['user']['is_owner'])
$context['allow_no_censored'] = !empty($modSettings['allow_no_censored']);
return true;
},
),
'tfa' => array(
'type' => 'callback',
'callback_func' => 'tfa',
'permission' => 'profile_password',
'enabled' => !empty($modSettings['tfa_mode']),
'preload' => function() use (&$context, $cur_profile)
{
$context['tfa_enabled'] = !empty($cur_profile['tfa_secret']);
return true;
},
),
'time_format' => array(
'type' => 'callback',
'callback_func' => 'timeformat_modify',
'permission' => 'profile_extra',
'preload' => function() use (&$context, $user_info, $txt, $cur_profile, $modSettings)
{
$context['easy_timeformats'] = array(
array('format' => '', 'title' => $txt['timeformat_default']),
array('format' => '%B %d, %Y, %I:%M:%S %p', 'title' => $txt['timeformat_easy1']),
array('format' => '%B %d, %Y, %H:%M:%S', 'title' => $txt['timeformat_easy2']),
array('format' => '%Y-%m-%d, %H:%M:%S', 'title' => $txt['timeformat_easy3']),
array('format' => '%d %B %Y, %H:%M:%S', 'title' => $txt['timeformat_easy4']),
array('format' => '%d-%m-%Y, %H:%M:%S', 'title' => $txt['timeformat_easy5'])
);
$context['member']['time_format'] = $cur_profile['time_format'];
$context['current_forum_time'] = timeformat(time() - $user_info['time_offset'] * 3600, false);
$context['current_forum_time_js'] = strftime('%Y,' . ((int) strftime('%m', time() + $modSettings['time_offset'] * 3600) - 1) . ',%d,%H,%M,%S', time() + $modSettings['time_offset'] * 3600);
$context['current_forum_time_hour'] = (int) strftime('%H', forum_time(false));
return true;
},
),
'timezone' => array(
'type' => 'select',
'options' => smf_list_timezones(),
'permission' => 'profile_extra',
'label' => $txt['timezone'],
'input_validate' => function($value)
{
$tz = smf_list_timezones();
if (!isset($tz[$value]))
return 'bad_timezone';
return true;
},
),
'usertitle' => array(
'type' => 'text',
'label' => $txt['custom_title'],
'log_change' => true,
'input_attr' => array('maxlength="50"'),
'size' => 50,
'permission' => 'profile_title',
'enabled' => !empty($modSettings['titlesEnable']),
'input_validate' => function(&$value) use ($smcFunc)
{
if ($smcFunc['strlen']($value) > 50)
return 'user_title_too_long';
return true;
},
),
'website_title' => array(
'type' => 'text',
'label' => $txt['website_title'],
'subtext' => $txt['include_website_url'],
'size' => 50,
'permission' => 'profile_website',
'link_with' => 'website',
),
'website_url' => array(
'type' => 'url',
'label' => $txt['website_url'],
'subtext' => $txt['complete_url'],
'size' => 50,
'permission' => 'profile_website',
// Fix the URL...
'input_validate' => function(&$value)
{
if (strlen(trim($value)) > 0 && strpos($value, '://') === false)
$value = 'http://' . $value;
if (strlen($value) < 8 || (substr($value, 0, 7) !== 'http://' && substr($value, 0, 8) !== 'https://'))
$value = '';
$value = (string) validate_iri(sanitize_iri($value));
return true;
},
'link_with' => 'website',
),
);
call_integration_hook('integrate_load_profile_fields', array(&$profile_fields));
$disabled_fields = !empty($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
// For each of the above let's take out the bits which don't apply - to save memory and security!
foreach ($profile_fields as $key => $field)
{
// Do we have permission to do this?
if (isset($field['permission']) && !allowedTo(($context['user']['is_owner'] ? array($field['permission'] . '_own', $field['permission'] . '_any') : $field['permission'] . '_any')) && !allowedTo($field['permission']))
unset($profile_fields[$key]);
// Is it enabled?
if (isset($field['enabled']) && !$field['enabled'])
unset($profile_fields[$key]);
// Is it specifically disabled?
if (in_array($key, $disabled_fields) || (isset($field['link_with']) && in_array($field['link_with'], $disabled_fields)))
unset($profile_fields[$key]);
}
}
/**
* Setup the context for a page load!
*
* @param array $fields The profile fields to display. Each item should correspond to an item in the $profile_fields array generated by loadProfileFields
*/
function setupProfileContext($fields)
{
global $profile_fields, $context, $cur_profile, $txt;
// Some default bits.
$context['profile_prehtml'] = '';
$context['profile_posthtml'] = '';
$context['profile_javascript'] = '';
$context['profile_onsubmit_javascript'] = '';
call_integration_hook('integrate_setup_profile_context', array(&$fields));
// Make sure we have this!
loadProfileFields(true);
// First check for any linked sets.
foreach ($profile_fields as $key => $field)
if (isset($field['link_with']) && in_array($field['link_with'], $fields))
$fields[] = $key;
$i = 0;
$last_type = '';
foreach ($fields as $key => $field)
{
if (isset($profile_fields[$field]))
{
// Shortcut.
$cur_field = &$profile_fields[$field];
// Does it have a preload and does that preload succeed?
if (isset($cur_field['preload']) && !$cur_field['preload']())
continue;
// If this is anything but complex we need to do more cleaning!
if ($cur_field['type'] != 'callback' && $cur_field['type'] != 'hidden')
{
if (!isset($cur_field['label']))
$cur_field['label'] = isset($txt[$field]) ? $txt[$field] : $field;
// Everything has a value!
if (!isset($cur_field['value']))
$cur_field['value'] = isset($cur_profile[$field]) ? $cur_profile[$field] : '';
// Any input attributes?
$cur_field['input_attr'] = !empty($cur_field['input_attr']) ? implode(',', $cur_field['input_attr']) : '';
}
// Was there an error with this field on posting?
if (isset($context['profile_errors'][$field]))
$cur_field['is_error'] = true;
// Any javascript stuff?
if (!empty($cur_field['js_submit']))
$context['profile_onsubmit_javascript'] .= $cur_field['js_submit'];
if (!empty($cur_field['js']))
$context['profile_javascript'] .= $cur_field['js'];
// Any template stuff?
if (!empty($cur_field['prehtml']))
$context['profile_prehtml'] .= $cur_field['prehtml'];
if (!empty($cur_field['posthtml']))
$context['profile_posthtml'] .= $cur_field['posthtml'];
// Finally put it into context?
if ($cur_field['type'] != 'hidden')
{
$last_type = $cur_field['type'];
$context['profile_fields'][$field] = &$profile_fields[$field];
}
}
// Bodge in a line break - without doing two in a row ;)
elseif ($field == 'hr' && $last_type != 'hr' && $last_type != '')
{
$last_type = 'hr';
$context['profile_fields'][$i++]['type'] = 'hr';
}
}
// Some spicy JS.
addInlineJavaScript('
var form_handle = document.forms.creator;
createEventListener(form_handle);
'. (!empty($context['require_password']) ? '
form_handle.addEventListener(\'submit\', function(event)
{
if (this.oldpasswrd.value == "")
{
event.preventDefault();
alert('. (JavaScriptEscape($txt['required_security_reasons'])) . ');
return false;
}
}, false);' : ''), true);
// Any onsubmit javascript?
if (!empty($context['profile_onsubmit_javascript']))
addInlineJavaScript($context['profile_onsubmit_javascript'], true);
// Any totally custom stuff?
if (!empty($context['profile_javascript']))
addInlineJavaScript($context['profile_javascript'], true);
// Free up some memory.
unset($profile_fields);
}
/**
* Save the profile changes.
*/
function saveProfileFields()
{
global $profile_fields, $profile_vars, $context, $old_profile, $post_errors, $cur_profile;
// Load them up.
loadProfileFields();
// This makes things easier...
$old_profile = $cur_profile;
// This allows variables to call activities when they save - by default just to reload their settings
$context['profile_execute_on_save'] = array();
if ($context['user']['is_owner'])
$context['profile_execute_on_save']['reload_user'] = 'profileReloadUser';
// Assume we log nothing.
$context['log_changes'] = array();
// Cycle through the profile fields working out what to do!
foreach ($profile_fields as $key => $field)
{
if (!isset($_POST[$key]) || !empty($field['is_dummy']) || (isset($_POST['preview_signature']) && $key == 'signature'))
continue;
// What gets updated?
$db_key = isset($field['save_key']) ? $field['save_key'] : $key;
// Right - we have something that is enabled, we can act upon and has a value posted to it. Does it have a validation function?
if (isset($field['input_validate']))
{
$is_valid = $field['input_validate']($_POST[$key]);
// An error occurred - set it as such!
if ($is_valid !== true)
{
// Is this an actual error?
if ($is_valid !== false)
{
$post_errors[$key] = $is_valid;
$profile_fields[$key]['is_error'] = $is_valid;
}
// Retain the old value.
$cur_profile[$key] = $_POST[$key];
continue;
}
}
// Are we doing a cast?
$field['cast_type'] = empty($field['cast_type']) ? $field['type'] : $field['cast_type'];
// Finally, clean up certain types.
if ($field['cast_type'] == 'int')
$_POST[$key] = (int) $_POST[$key];
elseif ($field['cast_type'] == 'float')
$_POST[$key] = (float) $_POST[$key];
elseif ($field['cast_type'] == 'check')
$_POST[$key] = !empty($_POST[$key]) ? 1 : 0;
// If we got here we're doing OK.
if ($field['type'] != 'hidden' && (!isset($old_profile[$key]) || $_POST[$key] != $old_profile[$key]))
{
// Set the save variable.
$profile_vars[$db_key] = $_POST[$key];
// And update the user profile.
$cur_profile[$key] = $_POST[$key];
// Are we logging it?
if (!empty($field['log_change']) && isset($old_profile[$key]))
$context['log_changes'][$key] = array(
'previous' => $old_profile[$key],
'new' => $_POST[$key],
);
}
// Logging group changes are a bit different...
if ($key == 'id_group' && $field['log_change'])
{
profileLoadGroups();
// Any changes to primary group?
if ($_POST['id_group'] != $old_profile['id_group'])
{
$context['log_changes']['id_group'] = array(
'previous' => !empty($old_profile[$key]) && isset($context['member_groups'][$old_profile[$key]]) ? $context['member_groups'][$old_profile[$key]]['name'] : '',
'new' => !empty($_POST[$key]) && isset($context['member_groups'][$_POST[$key]]) ? $context['member_groups'][$_POST[$key]]['name'] : '',
);
}
// Prepare additional groups for comparison.
$additional_groups = array(
'previous' => !empty($old_profile['additional_groups']) ? explode(',', $old_profile['additional_groups']) : array(),
'new' => !empty($_POST['additional_groups']) ? array_diff($_POST['additional_groups'], array(0)) : array(),
);
sort($additional_groups['previous']);
sort($additional_groups['new']);
// What about additional groups?
if ($additional_groups['previous'] != $additional_groups['new'])
{
foreach ($additional_groups as $type => $groups)
{
foreach ($groups as $id => $group)
{
if (isset($context['member_groups'][$group]))
$additional_groups[$type][$id] = $context['member_groups'][$group]['name'];
else
unset($additional_groups[$type][$id]);
}
$additional_groups[$type] = implode(', ', $additional_groups[$type]);
}
$context['log_changes']['additional_groups'] = $additional_groups;
}
}
}
// @todo Temporary
if ($context['user']['is_owner'])
$changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
else
$changeOther = allowedTo('profile_extra_any');
if ($changeOther && empty($post_errors))
{
makeThemeChanges($context['id_member'], isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
if (!empty($_REQUEST['sa']))
{
$custom_fields_errors = makeCustomFieldChanges($context['id_member'], $_REQUEST['sa'], false, true);
if (!empty($custom_fields_errors))
$post_errors = array_merge($post_errors, $custom_fields_errors);
}
}
// Free memory!
unset($profile_fields);
}
/**
* Save the profile changes
*
* @param array &$profile_vars The items to save
* @param array &$post_errors An array of information about any errors that occurred
* @param int $memID The ID of the member whose profile we're saving
*/
function saveProfileChanges(&$profile_vars, &$post_errors, $memID)
{
global $user_profile, $context;
// These make life easier....
$old_profile = &$user_profile[$memID];
// Permissions...
if ($context['user']['is_owner'])
{
$changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own', 'profile_website_any', 'profile_website_own', 'profile_signature_any', 'profile_signature_own'));
}
else
$changeOther = allowedTo(array('profile_extra_any', 'profile_website_any', 'profile_signature_any'));
// Arrays of all the changes - makes things easier.
$profile_bools = array();
$profile_ints = array();
$profile_floats = array();
$profile_strings = array(
'buddy_list',
'ignore_boards',
);
if (isset($_POST['sa']) && $_POST['sa'] == 'ignoreboards' && empty($_POST['ignore_brd']))
$_POST['ignore_brd'] = array();
unset($_POST['ignore_boards']); // Whatever it is set to is a dirty filthy thing. Kinda like our minds.
if (isset($_POST['ignore_brd']))
{
if (!is_array($_POST['ignore_brd']))
$_POST['ignore_brd'] = array($_POST['ignore_brd']);
foreach ($_POST['ignore_brd'] as $k => $d)
{
$d = (int) $d;
if ($d != 0)
$_POST['ignore_brd'][$k] = $d;
else
unset($_POST['ignore_brd'][$k]);
}
$_POST['ignore_boards'] = implode(',', $_POST['ignore_brd']);
unset($_POST['ignore_brd']);
}
// Here's where we sort out all the 'other' values...
if ($changeOther)
{
makeThemeChanges($memID, isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
//makeAvatarChanges($memID, $post_errors);
if (!empty($_REQUEST['sa']))
makeCustomFieldChanges($memID, $_REQUEST['sa'], false);
foreach ($profile_bools as $var)
if (isset($_POST[$var]))
$profile_vars[$var] = empty($_POST[$var]) ? '0' : '1';
foreach ($profile_ints as $var)
if (isset($_POST[$var]))
$profile_vars[$var] = $_POST[$var] != '' ? (int) $_POST[$var] : '';
foreach ($profile_floats as $var)
if (isset($_POST[$var]))
$profile_vars[$var] = (float) $_POST[$var];
foreach ($profile_strings as $var)
if (isset($_POST[$var]))
$profile_vars[$var] = $_POST[$var];
}
}
/**
* Make any theme changes that are sent with the profile.
*
* @param int $memID The ID of the user
* @param int $id_theme The ID of the theme
*/
function makeThemeChanges($memID, $id_theme)
{
global $modSettings, $smcFunc, $context, $user_info;
$reservedVars = array(
'actual_theme_url',
'actual_images_url',
'base_theme_dir',
'base_theme_url',
'default_images_url',
'default_theme_dir',
'default_theme_url',
'default_template',
'images_url',
'number_recent_posts',
'smiley_sets_default',
'theme_dir',
'theme_id',
'theme_layers',
'theme_templates',
'theme_url',
);
// Can't change reserved vars.
if ((isset($_POST['options']) && count(array_intersect(array_keys($_POST['options']), $reservedVars)) != 0) || (isset($_POST['default_options']) && count(array_intersect(array_keys($_POST['default_options']), $reservedVars)) != 0))
fatal_lang_error('no_access', false);
// Don't allow any overriding of custom fields with default or non-default options.
$request = $smcFunc['db_query']('', '
SELECT col_name
FROM {db_prefix}custom_fields
WHERE active = {int:is_active}',
array(
'is_active' => 1,
)
);
$custom_fields = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$custom_fields[] = $row['col_name'];
$smcFunc['db_free_result']($request);
// These are the theme changes...
$themeSetArray = array();
if (isset($_POST['options']) && is_array($_POST['options']))
{
foreach ($_POST['options'] as $opt => $val)
{
if (in_array($opt, $custom_fields))
continue;
// These need to be controlled.