]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Profile/Index.php
Merge remote-tracking branch 'upstream/2021.12-rc' into user-banner
[friendica.git] / src / Module / Settings / Profile / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\Profile;
23
24 use Friendica\Core\ACL;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Theme;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Profile;
33 use Friendica\Profile\ProfileField\Collection\ProfileFields;
34 use Friendica\Profile\ProfileField\Entity\ProfileField;
35 use Friendica\Model\User;
36 use Friendica\Module\BaseSettings;
37 use Friendica\Module\Security\Login;
38 use Friendica\Network\HTTPException;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Temporal;
41
42 class Index extends BaseSettings
43 {
44         protected function post(array $request = [])
45         {
46                 if (!local_user()) {
47                         return;
48                 }
49
50                 $profile = Profile::getByUID(local_user());
51                 if (!DBA::isResult($profile)) {
52                         return;
53                 }
54
55                 self::checkFormSecurityTokenRedirectOnError('/settings/profile', 'settings_profile');
56
57                 Hook::callAll('profile_post', $_POST);
58
59                 $dob = trim($_POST['dob'] ?? '');
60
61                 if ($dob && !in_array($dob, ['0000-00-00', DBA::NULL_DATE])) {
62                         $y = substr($dob, 0, 4);
63                         if ((!ctype_digit($y)) || ($y < 1900)) {
64                                 $ignore_year = true;
65                         } else {
66                                 $ignore_year = false;
67                         }
68
69                         if (strpos($dob, '0000-') === 0 || strpos($dob, '0001-') === 0) {
70                                 $ignore_year = true;
71                                 $dob = substr($dob, 5);
72                         }
73
74                         if ($ignore_year) {
75                                 $dob = '0000-' . DateTimeFormat::utc('1900-' . $dob, 'm-d');
76                         } else {
77                                 $dob = DateTimeFormat::utc($dob, 'Y-m-d');
78                         }
79                 }
80
81                 $name = trim($_POST['name'] ?? '');
82                 if (!strlen($name)) {
83                         notice(DI::l10n()->t('Profile Name is required.'));
84                         return;
85                 }
86
87                 $about = trim($_POST['about']);
88                 $address = trim($_POST['address']);
89                 $locality = trim($_POST['locality']);
90                 $region = trim($_POST['region']);
91                 $postal_code = trim($_POST['postal_code']);
92                 $country_name = trim($_POST['country_name']);
93                 $pub_keywords = self::cleanKeywords(trim($_POST['pub_keywords']));
94                 $prv_keywords = self::cleanKeywords(trim($_POST['prv_keywords']));
95                 $xmpp = trim($_POST['xmpp']);
96                 $matrix = trim($_POST['matrix']);
97                 $homepage = trim($_POST['homepage']);
98                 if ((strpos($homepage, 'http') !== 0) && (strlen($homepage))) {
99                         // neither http nor https in URL, add them
100                         $homepage = 'http://' . $homepage;
101                 }
102
103                 $profileFieldsNew = self::getProfileFieldsFromInput(
104                         local_user(),
105                         $_REQUEST['profile_field'],
106                         $_REQUEST['profile_field_order']
107                 );
108
109                 DI::profileField()->saveCollectionForUser(local_user(), $profileFieldsNew);
110
111                 $result = Profile::update(
112                         [
113                                 'name'         => $name,
114                                 'about'        => $about,
115                                 'dob'          => $dob,
116                                 'address'      => $address,
117                                 'locality'     => $locality,
118                                 'region'       => $region,
119                                 'postal-code'  => $postal_code,
120                                 'country-name' => $country_name,
121                                 'xmpp'         => $xmpp,
122                                 'matrix'       => $matrix,
123                                 'homepage'     => $homepage,
124                                 'pub_keywords' => $pub_keywords,
125                                 'prv_keywords' => $prv_keywords,
126                         ],
127                         local_user()
128                 );
129
130                 if (!$result) {
131                         notice(DI::l10n()->t('Profile couldn\'t be updated.'));
132                         return;
133                 }
134
135                 DI::baseUrl()->redirect('settings/profile');
136         }
137
138         protected function content(array $request = []): string
139         {
140                 if (!local_user()) {
141                         notice(DI::l10n()->t('You must be logged in to use this module'));
142                         return Login::form();
143                 }
144
145                 parent::content();
146
147                 $o = '';
148
149                 $profile = User::getOwnerDataById(local_user());
150                 if (!DBA::isResult($profile)) {
151                         throw new HTTPException\NotFoundException();
152                 }
153
154                 $a = DI::app();
155
156                 DI::page()->registerFooterScript('view/asset/es-jquery-sortable/source/js/jquery-sortable-min.js');
157                 DI::page()->registerFooterScript(Theme::getPathForFile('js/module/settings/profile/index.js'));
158
159                 $custom_fields = [];
160
161                 $profileFields = DI::profileField()->selectByUserId(local_user());
162                 foreach ($profileFields as $profileField) {
163                         /** @var ProfileField $profileField */
164                         $defaultPermissions = $profileField->permissionSet->withAllowedContacts(
165                                 Contact::pruneUnavailable($profileField->permissionSet->allow_cid)
166                         );
167
168                         $custom_fields[] = [
169                                 'id' => $profileField->id,
170                                 'legend' => $profileField->label,
171                                 'fields' => [
172                                         'label' => ['profile_field[' . $profileField->id . '][label]', DI::l10n()->t('Label:'), $profileField->label],
173                                         'value' => ['profile_field[' . $profileField->id . '][value]', DI::l10n()->t('Value:'), $profileField->value],
174                                         'acl' => ACL::getFullSelectorHTML(
175                                                 DI::page(),
176                                                 $a->getLoggedInUserId(),
177                                                 false,
178                                                 $defaultPermissions->toArray(),
179                                                 ['network' => Protocol::DFRN],
180                                                 'profile_field[' . $profileField->id . ']'
181                                         ),
182                                 ],
183                                 'permissions' => DI::l10n()->t('Field Permissions'),
184                                 'permdesc' => DI::l10n()->t("(click to open/close)"),
185                         ];
186                 };
187
188                 $custom_fields[] = [
189                         'id' => 'new',
190                         'legend' => DI::l10n()->t('Add a new profile field'),
191                         'fields' => [
192                                 'label' => ['profile_field[new][label]', DI::l10n()->t('Label:')],
193                                 'value' => ['profile_field[new][value]', DI::l10n()->t('Value:')],
194                                 'acl' => ACL::getFullSelectorHTML(
195                                         DI::page(),
196                                         $a->getLoggedInUserId(),
197                                         false,
198                                         ['allow_cid' => []],
199                                         ['network' => Protocol::DFRN],
200                                         'profile_field[new]'
201                                 ),
202                         ],
203                         'permissions' => DI::l10n()->t('Field Permissions'),
204                         'permdesc' => DI::l10n()->t("(click to open/close)"),
205                 ];
206
207                 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/profile/index_head.tpl'), [
208                         '$baseurl' => DI::baseUrl()->get(true),
209                 ]);
210
211                 $personal_account = !in_array($profile['page-flags'], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]);
212
213                 $tpl = Renderer::getMarkupTemplate('settings/profile/index.tpl');
214                 $o .= Renderer::replaceMacros($tpl, [
215                         '$personal_account' => $personal_account,
216
217                         '$form_security_token' => self::getFormSecurityToken('settings_profile'),
218                         '$form_security_token_photo' => self::getFormSecurityToken('settings_profile_photo'),
219
220                         '$profile_action' => DI::l10n()->t('Profile Actions'),
221                         '$banner' => DI::l10n()->t('Edit Profile Details'),
222                         '$submit' => DI::l10n()->t('Submit'),
223                         '$profpic' => DI::l10n()->t('Change Profile Photo'),
224                         '$profpiclink' => '/photos/' . $profile['nickname'],
225                         '$viewprof' => DI::l10n()->t('View Profile'),
226
227                         '$lbl_personal_section' => DI::l10n()->t('Personal'),
228                         '$lbl_picture_section' => DI::l10n()->t('Profile picture'),
229                         '$lbl_location_section' => DI::l10n()->t('Location'),
230                         '$lbl_miscellaneous_section' => DI::l10n()->t('Miscellaneous'),
231                         '$lbl_custom_fields_section' => DI::l10n()->t('Custom Profile Fields'),
232
233                         '$lbl_profile_photo' => DI::l10n()->t('Upload Profile Photo'),
234
235                         '$baseurl' => DI::baseUrl()->get(true),
236                         '$nickname' => $profile['nickname'],
237                         '$name' => ['name', DI::l10n()->t('Display name:'), $profile['name']],
238                         '$about' => ['about', DI::l10n()->t('Description:'), $profile['about']],
239                         '$dob' => Temporal::getDateofBirthField($profile['dob'], $profile['timezone']),
240                         '$address' => ['address', DI::l10n()->t('Street Address:'), $profile['address']],
241                         '$locality' => ['locality', DI::l10n()->t('Locality/City:'), $profile['locality']],
242                         '$region' => ['region', DI::l10n()->t('Region/State:'), $profile['region']],
243                         '$postal_code' => ['postal_code', DI::l10n()->t('Postal/Zip Code:'), $profile['postal-code']],
244                         '$country_name' => ['country_name', DI::l10n()->t('Country:'), $profile['country-name']],
245                         '$age' => ((intval($profile['dob'])) ? '(' . DI::l10n()->t('Age: ') . DI::l10n()->tt('%d year old', '%d years old', Temporal::getAgeByTimezone($profile['dob'], $profile['timezone'])) . ')' : ''),
246                         '$xmpp' => ['xmpp', DI::l10n()->t('XMPP (Jabber) address:'), $profile['xmpp'], DI::l10n()->t('The XMPP address will be published so that people can follow you there.')],
247                         '$matrix' => ['matrix', DI::l10n()->t('Matrix (Element) address:'), $profile['matrix'], DI::l10n()->t('The Matrix address will be published so that people can follow you there.')],
248                         '$homepage' => ['homepage', DI::l10n()->t('Homepage URL:'), $profile['homepage']],
249                         '$pub_keywords' => ['pub_keywords', DI::l10n()->t('Public Keywords:'), $profile['pub_keywords'], DI::l10n()->t('(Used for suggesting potential friends, can be seen by others)')],
250                         '$prv_keywords' => ['prv_keywords', DI::l10n()->t('Private Keywords:'), $profile['prv_keywords'], DI::l10n()->t('(Used for searching profiles, never shown to others)')],
251                         '$custom_fields_description' => DI::l10n()->t("<p>Custom fields appear on <a href=\"%s\">your profile page</a>.</p>
252                                 <p>You can use BBCodes in the field values.</p>
253                                 <p>Reorder by dragging the field title.</p>
254                                 <p>Empty the label field to remove a custom field.</p>
255                                 <p>Non-public fields can only be seen by the selected Friendica contacts or the Friendica contacts in the selected groups.</p>",
256                                 'profile/' . $profile['nickname']
257                         ),
258                         '$custom_fields' => $custom_fields,
259                 ]);
260
261                 $arr = ['profile' => $profile, 'entry' => $o];
262                 Hook::callAll('profile_edit', $arr);
263
264                 return $o;
265         }
266
267         private static function getProfileFieldsFromInput(int $uid, array $profileFieldInputs, array $profileFieldOrder): ProfileFields
268         {
269                 $profileFields = new ProfileFields();
270
271                 // Returns an associative array of id => order values
272                 $profileFieldOrder = array_flip($profileFieldOrder);
273
274                 // Creation of the new field
275                 if (!empty($profileFieldInputs['new']['label'])) {
276                         $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString(
277                                 $uid,
278                                 DI::aclFormatter()->toString($profileFieldInputs['new']['contact_allow'] ?? ''),
279                                 DI::aclFormatter()->toString($profileFieldInputs['new']['group_allow'] ?? ''),
280                                 DI::aclFormatter()->toString($profileFieldInputs['new']['contact_deny'] ?? ''),
281                                 DI::aclFormatter()->toString($profileFieldInputs['new']['group_deny'] ?? '')
282                         ));
283
284                         $profileFields->append(DI::profileFieldFactory()->createFromValues(
285                                 $uid,
286                                 $profileFieldOrder['new'],
287                                 $profileFieldInputs['new']['label'],
288                                 $profileFieldInputs['new']['value'],
289                                 $permissionSet
290                         ));
291                 }
292
293                 unset($profileFieldInputs['new']);
294                 unset($profileFieldOrder['new']);
295
296                 foreach ($profileFieldInputs as $id => $profileFieldInput) {
297                         $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString(
298                                 $uid,
299                                 DI::aclFormatter()->toString($profileFieldInput['contact_allow'] ?? ''),
300                                 DI::aclFormatter()->toString($profileFieldInput['group_allow'] ?? ''),
301                                 DI::aclFormatter()->toString($profileFieldInput['contact_deny'] ?? ''),
302                                 DI::aclFormatter()->toString($profileFieldInput['group_deny'] ?? '')
303                         ));
304
305                         $profileFields->append(DI::profileFieldFactory()->createFromValues(
306                                 $uid,
307                                 $profileFieldOrder[$id],
308                                 $profileFieldInput['label'],
309                                 $profileFieldInput['value'],
310                                 $permissionSet
311                         ));
312                 }
313
314                 return $profileFields;
315         }
316
317         private static function cleanKeywords($keywords)
318         {
319                 $keywords = str_replace(',', ' ', $keywords);
320                 $keywords = explode(' ', $keywords);
321
322                 $cleaned = [];
323                 foreach ($keywords as $keyword) {
324                         $keyword = trim(strtolower($keyword));
325                         $keyword = trim($keyword, '#');
326                         if ($keyword != '') {
327                                 $cleaned[] = $keyword;
328                         }
329                 }
330
331                 $keywords = implode(', ', $cleaned);
332
333                 return $keywords;
334         }
335 }