]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Profile/Index.php
Issue 10640 - Inverse user display settings
[friendica.git] / src / Module / Settings / Profile / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Profile;
32 use Friendica\Model\ProfileField;
33 use Friendica\Model\User;
34 use Friendica\Module\BaseSettings;
35 use Friendica\Module\Security\Login;
36 use Friendica\Network\HTTPException;
37 use Friendica\Util\DateTimeFormat;
38 use Friendica\Util\Strings;
39 use Friendica\Util\Temporal;
40
41 class Index extends BaseSettings
42 {
43         public static function post(array $parameters = [])
44         {
45                 if (!local_user()) {
46                         return;
47                 }
48
49                 $profile = Profile::getByUID(local_user());
50                 if (!DBA::isResult($profile)) {
51                         return;
52                 }
53
54                 self::checkFormSecurityTokenRedirectOnError('/settings/profile', 'settings_profile');
55
56                 Hook::callAll('profile_post', $_POST);
57
58                 $dob = trim($_POST['dob'] ?? '');
59
60                 if ($dob && !in_array($dob, ['0000-00-00', DBA::NULL_DATE])) {
61                         $y = substr($dob, 0, 4);
62                         if ((!ctype_digit($y)) || ($y < 1900)) {
63                                 $ignore_year = true;
64                         } else {
65                                 $ignore_year = false;
66                         }
67
68                         if (strpos($dob, '0000-') === 0 || strpos($dob, '0001-') === 0) {
69                                 $ignore_year = true;
70                                 $dob = substr($dob, 5);
71                         }
72
73                         if ($ignore_year) {
74                                 $dob = '0000-' . DateTimeFormat::utc('1900-' . $dob, 'm-d');
75                         } else {
76                                 $dob = DateTimeFormat::utc($dob, 'Y-m-d');
77                         }
78                 }
79
80                 $name = Strings::escapeTags(trim($_POST['name'] ?? ''));
81                 if (!strlen($name)) {
82                         notice(DI::l10n()->t('Profile Name is required.'));
83                         return;
84                 }
85
86                 $about = Strings::escapeTags(trim($_POST['about']));
87                 $address = Strings::escapeTags(trim($_POST['address']));
88                 $locality = Strings::escapeTags(trim($_POST['locality']));
89                 $region = Strings::escapeTags(trim($_POST['region']));
90                 $postal_code = Strings::escapeTags(trim($_POST['postal_code']));
91                 $country_name = Strings::escapeTags(trim($_POST['country_name']));
92                 $pub_keywords = self::cleanKeywords(Strings::escapeTags(trim($_POST['pub_keywords'])));
93                 $prv_keywords = self::cleanKeywords(Strings::escapeTags(trim($_POST['prv_keywords'])));
94                 $xmpp = Strings::escapeTags(trim($_POST['xmpp']));
95                 $matrix = Strings::escapeTags(trim($_POST['matrix']));
96                 $homepage = Strings::escapeTags(trim($_POST['homepage']));
97                 if ((strpos($homepage, 'http') !== 0) && (strlen($homepage))) {
98                         // neither http nor https in URL, add them
99                         $homepage = 'http://' . $homepage;
100                 }
101
102                 $profileFields = DI::profileField()->selectByUserId(local_user());
103
104                 $profileFields = DI::profileField()->updateCollectionFromForm(
105                         local_user(),
106                         $profileFields,
107                         $_REQUEST['profile_field'],
108                         $_REQUEST['profile_field_order']
109                 );
110
111                 DI::profileField()->saveCollection($profileFields);
112
113                 $result = Profile::update(
114                         [
115                                 'name'         => $name,
116                                 'about'        => $about,
117                                 'dob'          => $dob,
118                                 'address'      => $address,
119                                 'locality'     => $locality,
120                                 'region'       => $region,
121                                 'postal-code'  => $postal_code,
122                                 'country-name' => $country_name,
123                                 'xmpp'         => $xmpp,
124                                 'matrix'       => $matrix,
125                                 'homepage'     => $homepage,
126                                 'pub_keywords' => $pub_keywords,
127                                 'prv_keywords' => $prv_keywords,
128                         ],
129                         local_user()
130                 );
131
132                 if (!$result) {
133                         notice(DI::l10n()->t('Profile couldn\'t be updated.'));
134                         return;
135                 }
136         }
137
138         public static function content(array $parameters = [])
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 = ACL::getDefaultUserPermissions($profileField->permissionset->toArray());
165
166                         $custom_fields[] = [
167                                 'id' => $profileField->id,
168                                 'legend' => $profileField->label,
169                                 'fields' => [
170                                         'label' => ['profile_field[' . $profileField->id . '][label]', DI::l10n()->t('Label:'), $profileField->label],
171                                         'value' => ['profile_field[' . $profileField->id . '][value]', DI::l10n()->t('Value:'), $profileField->value],
172                                         'acl' => ACL::getFullSelectorHTML(
173                                                 DI::page(),
174                                                 $a->getLoggedInUserId(),
175                                                 false,
176                                                 $defaultPermissions,
177                                                 ['network' => Protocol::DFRN],
178                                                 'profile_field[' . $profileField->id . ']'
179                                         ),
180                                 ],
181                                 'permissions' => DI::l10n()->t('Field Permissions'),
182                                 'permdesc' => DI::l10n()->t("(click to open/close)"),
183                         ];
184                 };
185
186                 $custom_fields[] = [
187                         'id' => 'new',
188                         'legend' => DI::l10n()->t('Add a new profile field'),
189                         'fields' => [
190                                 'label' => ['profile_field[new][label]', DI::l10n()->t('Label:')],
191                                 'value' => ['profile_field[new][value]', DI::l10n()->t('Value:')],
192                                 'acl' => ACL::getFullSelectorHTML(
193                                         DI::page(),
194                                         $a->getLoggedInUserId(),
195                                         false,
196                                         ['allow_cid' => []],
197                                         ['network' => Protocol::DFRN],
198                                         'profile_field[new]'
199                                 ),
200                         ],
201                         'permissions' => DI::l10n()->t('Field Permissions'),
202                         'permdesc' => DI::l10n()->t("(click to open/close)"),
203                 ];
204
205                 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/profile/index_head.tpl'), [
206                         '$baseurl' => DI::baseUrl()->get(true),
207                 ]);
208
209                 $personal_account = !in_array($profile['page-flags'], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]);
210
211                 $tpl = Renderer::getMarkupTemplate('settings/profile/index.tpl');
212                 $o .= Renderer::replaceMacros($tpl, [
213                         '$personal_account' => $personal_account,
214
215                         '$form_security_token' => self::getFormSecurityToken('settings_profile'),
216                         '$form_security_token_photo' => self::getFormSecurityToken('settings_profile_photo'),
217
218                         '$profile_action' => DI::l10n()->t('Profile Actions'),
219                         '$banner' => DI::l10n()->t('Edit Profile Details'),
220                         '$submit' => DI::l10n()->t('Submit'),
221                         '$profpic' => DI::l10n()->t('Change Profile Photo'),
222                         '$profpiclink' => '/photos/' . $profile['nickname'],
223                         '$viewprof' => DI::l10n()->t('View Profile'),
224
225                         '$lbl_personal_section' => DI::l10n()->t('Personal'),
226                         '$lbl_picture_section' => DI::l10n()->t('Profile picture'),
227                         '$lbl_location_section' => DI::l10n()->t('Location'),
228                         '$lbl_miscellaneous_section' => DI::l10n()->t('Miscellaneous'),
229                         '$lbl_custom_fields_section' => DI::l10n()->t('Custom Profile Fields'),
230
231                         '$lbl_profile_photo' => DI::l10n()->t('Upload Profile Photo'),
232
233                         '$baseurl' => DI::baseUrl()->get(true),
234                         '$nickname' => $profile['nickname'],
235                         '$name' => ['name', DI::l10n()->t('Display name:'), $profile['name']],
236                         '$about' => ['about', DI::l10n()->t('Description:'), $profile['about']],
237                         '$dob' => Temporal::getDateofBirthField($profile['dob'], $profile['timezone']),
238                         '$address' => ['address', DI::l10n()->t('Street Address:'), $profile['address']],
239                         '$locality' => ['locality', DI::l10n()->t('Locality/City:'), $profile['locality']],
240                         '$region' => ['region', DI::l10n()->t('Region/State:'), $profile['region']],
241                         '$postal_code' => ['postal_code', DI::l10n()->t('Postal/Zip Code:'), $profile['postal-code']],
242                         '$country_name' => ['country_name', DI::l10n()->t('Country:'), $profile['country-name']],
243                         '$age' => ((intval($profile['dob'])) ? '(' . DI::l10n()->t('Age: ') . DI::l10n()->tt('%d year old', '%d years old', Temporal::getAgeByTimezone($profile['dob'], $profile['timezone'])) . ')' : ''),
244                         '$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.')],
245                         '$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.')],
246                         '$homepage' => ['homepage', DI::l10n()->t('Homepage URL:'), $profile['homepage']],
247                         '$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)')],
248                         '$prv_keywords' => ['prv_keywords', DI::l10n()->t('Private Keywords:'), $profile['prv_keywords'], DI::l10n()->t('(Used for searching profiles, never shown to others)')],
249                         '$custom_fields_description' => DI::l10n()->t("<p>Custom fields appear on <a href=\"%s\">your profile page</a>.</p>
250                                 <p>You can use BBCodes in the field values.</p>
251                                 <p>Reorder by dragging the field title.</p>
252                                 <p>Empty the label field to remove a custom field.</p>
253                                 <p>Non-public fields can only be seen by the selected Friendica contacts or the Friendica contacts in the selected groups.</p>",
254                                 'profile/' . $profile['nickname']
255                         ),
256                         '$custom_fields' => $custom_fields,
257                 ]);
258
259                 $arr = ['profile' => $profile, 'entry' => $o];
260                 Hook::callAll('profile_edit', $arr);
261
262                 return $o;
263         }
264
265         private static function cleanKeywords($keywords)
266         {
267                 $keywords = str_replace(',', ' ', $keywords);
268                 $keywords = explode(' ', $keywords);
269
270                 $cleaned = [];
271                 foreach ($keywords as $keyword) {
272                         $keyword = trim(strtolower($keyword));
273                         $keyword = trim($keyword, '#');
274                         if ($keyword != '') {
275                                 $cleaned[] = $keyword;
276                         }
277                 }
278
279                 $keywords = implode(', ', $cleaned);
280
281                 return $keywords;
282         }
283 }