]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
Merge branch 'master' of https://git.gnu.io/gnu/gnu-social into social-master
[quix0rs-gnu-social.git] / actions / profilesettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Change profile settings
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('GNUSOCIAL')) { exit(1); }
33
34 /**
35  * Change profile settings
36  *
37  * @category Settings
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @author   Zach Copley <zach@status.net>
41  * @author   Sarven Capadisli <csarven@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class ProfilesettingsAction extends SettingsAction
46 {
47     /**
48      * Title of the page
49      *
50      * @return string Title of the page
51      */
52     function title()
53     {
54         // TRANS: Page title for profile settings.
55         return _('Profile settings');
56     }
57
58     /**
59      * Instructions for use
60      *
61      * @return instructions for use
62      */
63     function getInstructions()
64     {
65         // TRANS: Usage instructions for profile settings.
66         return _('You can update your personal profile info here '.
67                  'so people know more about you.');
68     }
69
70     function showScripts()
71     {
72         parent::showScripts();
73         $this->autofocus('fullname');
74     }
75
76     /**
77      * Content area of the page
78      *
79      * Shows a form for uploading an avatar.
80      *
81      * @return void
82      */
83     function showContent()
84     {
85         $profile = $this->scoped;
86         $user = $this->scoped->getUser();
87
88         $this->elementStart('form', array('method' => 'post',
89                                           'id' => 'form_settings_profile',
90                                           'class' => 'form_settings',
91                                           'action' => common_local_url('profilesettings')));
92         $this->elementStart('fieldset');
93         // TRANS: Profile settings form legend.
94         $this->element('legend', null, _('Profile information'));
95         $this->hidden('token', common_session_token());
96
97         // too much common patterns here... abstractable?
98         $this->elementStart('ul', 'form_data');
99         if (Event::handle('StartProfileFormData', array($this))) {
100             $this->elementStart('li');
101             // TRANS: Field label in form for profile settings.
102             $this->input('nickname', _('Nickname'),
103                          $this->arg('nickname') ?: $profile->nickname,
104                          // TRANS: Tooltip for field label in form for profile settings.
105                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'),
106                          null, false,   // "name" (will be set to id), then "required"
107                          !common_config('profile', 'changenick')
108                                         ? array('disabled' => 'disabled', 'placeholder' => null)
109                                         : array('placeholder' => null));
110             $this->elementEnd('li');
111             $this->elementStart('li');
112             // TRANS: Field label in form for profile settings.
113             $this->input('fullname', _('Full name'),
114                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
115             $this->elementEnd('li');
116             $this->elementStart('li');
117             // TRANS: Field label in form for profile settings.
118             $this->input('homepage', _('Homepage'),
119                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
120                          // TRANS: Tooltip for field label in form for profile settings.
121                          _('URL of your homepage, blog, or profile on another site.'));
122             $this->elementEnd('li');
123             $this->elementStart('li');
124             $maxBio = Profile::maxBio();
125             if ($maxBio > 0) {
126                 // TRANS: Tooltip for field label in form for profile settings. Plural
127                 // TRANS: is decided by the number of characters available for the
128                 // TRANS: biography (%d).
129                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
130                                        'Describe yourself and your interests in %d characters.',
131                                        $maxBio),
132                                     $maxBio);
133             } else {
134                 // TRANS: Tooltip for field label in form for profile settings.
135                 $bioInstr = _('Describe yourself and your interests.');
136             }
137             // TRANS: Text area label in form for profile settings where users can provide
138             // TRANS: their biography.
139             $this->textarea('bio', _('Bio'),
140                             ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
141                             $bioInstr);
142             $this->elementEnd('li');
143             $this->elementStart('li');
144             // TRANS: Field label in form for profile settings.
145             $this->input('location', _('Location'),
146                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
147                          // TRANS: Tooltip for field label in form for profile settings.
148                          _('Where you are, like "City, State (or Region), Country".'));
149             $this->elementEnd('li');
150             if (common_config('location', 'share') == 'user') {
151                 $this->elementStart('li');
152                 // TRANS: Checkbox label in form for profile settings.
153                 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
154                                 ($this->arg('sharelocation')) ?
155                                 $this->arg('sharelocation') : $this->scoped->shareLocation());
156                 $this->elementEnd('li');
157             }
158             Event::handle('EndProfileFormData', array($this));
159             $this->elementStart('li');
160             // TRANS: Field label in form for profile settings.
161             $this->input('tags', _('Tags'),
162                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
163                          // TRANS: Tooltip for field label in form for profile settings.
164                          _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
165             $this->elementEnd('li');
166             $this->elementStart('li');
167             $language = common_language();
168             // TRANS: Dropdownlist label in form for profile settings.
169             $this->dropdown('language', _('Language'),
170                          // TRANS: Tooltip for dropdown list label in form for profile settings.
171                             get_nice_language_list(), _('Preferred language.'),
172                             false, $language);
173             $this->elementEnd('li');
174             $timezone = common_timezone();
175             $timezones = array();
176             foreach(DateTimeZone::listIdentifiers() as $k => $v) {
177                 $timezones[$v] = $v;
178             }
179             $this->elementStart('li');
180             // TRANS: Dropdownlist label in form for profile settings.
181             $this->dropdown('timezone', _('Timezone'),
182                          // TRANS: Tooltip for dropdown list label in form for profile settings.
183                             $timezones, _('What timezone are you normally in?'),
184                             true, $timezone);
185             $this->elementEnd('li');
186             $this->elementStart('li');
187             $this->checkbox('autosubscribe',
188                             // TRANS: Checkbox label in form for profile settings.
189                             _('Automatically subscribe to whoever '.
190                               'subscribes to me (best for non-humans)'),
191                             ($this->arg('autosubscribe')) ?
192                             $this->boolean('autosubscribe') : $user->autosubscribe);
193             $this->elementEnd('li');
194             $this->elementStart('li');
195             $this->dropdown('subscribe_policy',
196                             // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
197                             _('Subscription policy'),
198                             // TRANS: Dropdown field option for following policy.
199                             array(User::SUBSCRIBE_POLICY_OPEN     => _('Let anyone follow me'),
200                                   // TRANS: Dropdown field option for following policy.
201                                   User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
202                             // TRANS: Dropdown field title on group edit form.
203                             _('Whether other users need your permission to follow your updates.'),
204                             false,
205                             (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
206             $this->elementEnd('li');
207         }
208         $this->elementStart('li');
209         $this->checkbox('private_stream',
210                         // TRANS: Checkbox label in profile settings.
211                         _('Make updates visible only to my followers'),
212                         ($this->arg('private_stream')) ?
213                         $this->boolean('private_stream') : $user->private_stream);
214         $this->elementEnd('li');
215         $this->elementEnd('ul');
216         // TRANS: Button to save input in profile settings.
217         $this->submit('save', _m('BUTTON','Save'));
218
219         $this->elementEnd('fieldset');
220         $this->elementEnd('form');
221     }
222
223     /**
224      * Handle a post
225      *
226      * Validate input and save changes. Reload the form with a success
227      * or error message.
228      *
229      * @return void
230      */
231     function handlePost()
232     {
233         // CSRF protection
234         $token = $this->trimmed('token');
235         if (!$token || $token != common_session_token()) {
236             // TRANS: Form validation error.
237             $this->showForm(_('There was a problem with your session token. '.
238                               'Try again, please.'));
239             return;
240         }
241
242         if (Event::handle('StartProfileSaveForm', array($this))) {
243
244             // $nickname will only be set if this changenick value is true.
245             if (common_config('profile', 'changenick') == true) {
246                 try {
247                     $nickname = Nickname::normalize($this->trimmed('nickname'), true);
248                 } catch (NicknameTakenException $e) {
249                     // Abort only if the nickname is occupied by another local profile
250                     if ($e->profile->id != $this->scoped->id) {
251                         $this->showForm($e->getMessage());
252                         return;
253                     }
254                     $nickname = Nickname::normalize($this->trimmed('nickname')); // without in-use check this time
255                 } catch (NicknameException $e) {
256                     $this->showForm($e->getMessage());
257                     return;
258                 }
259             }
260
261             $fullname = $this->trimmed('fullname');
262             $homepage = $this->trimmed('homepage');
263             $bio = $this->trimmed('bio');
264             $location = $this->trimmed('location');
265             $autosubscribe = $this->booleanintstring('autosubscribe');
266             $subscribe_policy = $this->trimmed('subscribe_policy');
267             $private_stream = $this->booleanintstring('private_stream');
268             $language = $this->trimmed('language');
269             $timezone = $this->trimmed('timezone');
270             $tagstring = $this->trimmed('tags');
271
272             // Some validation
273             if (!is_null($homepage) && (strlen($homepage) > 0) &&
274                        !common_valid_http_url($homepage)) {
275                 // TRANS: Validation error in form for profile settings.
276                 $this->showForm(_('Homepage is not a valid URL.'));
277                 return;
278             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
279                 // TRANS: Validation error in form for profile settings.
280                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
281                 return;
282             } else if (Profile::bioTooLong($bio)) {
283                 // TRANS: Validation error in form for profile settings.
284                 // TRANS: Plural form is used based on the maximum number of allowed
285                 // TRANS: characters for the biography (%d).
286                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
287                                            'Bio is too long (maximum %d characters).',
288                                            Profile::maxBio()),
289                                         Profile::maxBio()));
290                 return;
291             } else if (!is_null($location) && mb_strlen($location) > 255) {
292                 // TRANS: Validation error in form for profile settings.
293                 $this->showForm(_('Location is too long (maximum 255 characters).'));
294                 return;
295             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
296                 // TRANS: Validation error in form for profile settings.
297                 $this->showForm(_('Timezone not selected.'));
298                 return;
299             } else if (!is_null($language) && strlen($language) > 50) {
300                 // TRANS: Validation error in form for profile settings.
301                 $this->showForm(_('Language is too long (maximum 50 characters).'));
302                 return;
303             }
304
305             $tags = array();
306             $tag_priv = array();
307             if (is_string($tagstring) && strlen($tagstring) > 0) {
308
309                 $tags = preg_split('/[\s,]+/', $tagstring);
310
311                 foreach ($tags as &$tag) {
312                     $private = @$tag[0] === '.';
313
314                     $tag = common_canonical_tag($tag);
315                     if (!common_valid_profile_tag($tag)) {
316                         // TRANS: Validation error in form for profile settings.
317                         // TRANS: %s is an invalid tag.
318                         $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
319                         return;
320                     }
321
322                     $tag_priv[$tag] = $private;
323                 }
324             }
325
326             $user = common_current_user();
327             $user->query('BEGIN');
328
329             // $user->nickname is updated through Profile->update();
330
331             // XXX: XOR
332             if (($user->autosubscribe ^ $autosubscribe)
333                     || ($user->private_stream ^ $private_stream)
334                     || $user->timezone != $timezone
335                     || $user->language != $language
336                     || $user->subscribe_policy != $subscribe_policy) {
337
338                 $original = clone($user);
339
340                 $user->autosubscribe    = $autosubscribe;
341                 $user->language         = $language;
342                 $user->private_stream   = $private_stream;
343                 $user->subscribe_policy = $subscribe_policy;
344                 $user->timezone         = $timezone;
345
346                 $result = $user->update($original);
347                 if ($result === false) {
348                     common_log_db_error($user, 'UPDATE', __FILE__);
349                     // TRANS: Server error thrown when user profile settings could not be updated to
350                     // TRANS: automatically subscribe to any subscriber.
351                     $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
352                 }
353
354                 // Re-initialize language environment if it changed
355                 common_init_language();
356             }
357
358             $profile = $user->getProfile();
359
360             $orig_profile = clone($profile);
361
362             if (common_config('profile', 'changenick') == true && $profile->nickname !== $nickname) {
363                 assert(Nickname::normalize($nickname)===$nickname);
364                 common_debug("Changing user nickname from '{$profile->nickname}' to '{$nickname}'.");
365                 $profile->nickname = $nickname;
366                 $profile->profileurl = common_profile_url($profile->nickname);
367             }
368             $profile->fullname = $fullname;
369             $profile->homepage = $homepage;
370             $profile->bio = $bio;
371             $profile->location = $location;
372
373             $loc = Location::fromName($location);
374
375             if (empty($loc)) {
376                 $profile->lat         = null;
377                 $profile->lon         = null;
378                 $profile->location_id = null;
379                 $profile->location_ns = null;
380             } else {
381                 $profile->lat         = $loc->lat;
382                 $profile->lon         = $loc->lon;
383                 $profile->location_id = $loc->location_id;
384                 $profile->location_ns = $loc->location_ns;
385             }
386
387             if (common_config('location', 'share') == 'user') {
388
389                 $exists = false;
390
391                 $prefs = User_location_prefs::getKV('user_id', $user->id);
392
393                 if (empty($prefs)) {
394                     $prefs = new User_location_prefs();
395
396                     $prefs->user_id = $user->id;
397                     $prefs->created = common_sql_now();
398                 } else {
399                     $exists = true;
400                     $orig = clone($prefs);
401                 }
402
403                 $prefs->share_location = $this->booleanintstring('sharelocation');
404
405                 if ($exists) {
406                     $result = $prefs->update($orig);
407                 } else {
408                     $result = $prefs->insert();
409                 }
410
411                 if ($result === false) {
412                     common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
413                     // TRANS: Server error thrown when user profile location preference settings could not be updated.
414                     $this->serverError(_('Could not save location prefs.'));
415                 }
416             }
417
418             common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
419             common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
420
421             $result = $profile->update($orig_profile);
422
423             if ($result === false) {
424                 common_log_db_error($profile, 'UPDATE', __FILE__);
425                 // TRANS: Server error thrown when user profile settings could not be saved.
426                 $this->serverError(_('Could not save profile.'));
427             }
428
429             // Set the user tags
430             $result = $user->setSelfTags($tags, $tag_priv);
431
432             if (!$result) {
433                 // TRANS: Server error thrown when user profile settings tags could not be saved.
434                 $this->serverError(_('Could not save tags.'));
435             }
436
437             $user->query('COMMIT');
438             Event::handle('EndProfileSaveForm', array($this));
439
440             // TRANS: Confirmation shown when user profile settings are saved.
441             $this->showForm(_('Settings saved.'), true);
442
443         }
444     }
445
446     function showAside() {
447         $user = common_current_user();
448
449         $this->elementStart('div', array('id' => 'aside_primary',
450                                          'class' => 'aside'));
451
452         $this->elementStart('div', array('id' => 'account_actions',
453                                          'class' => 'section'));
454         $this->elementStart('ul');
455         if (Event::handle('StartProfileSettingsActions', array($this))) {
456             if ($user->hasRight(Right::BACKUPACCOUNT)) {
457                 $this->elementStart('li');
458                 $this->element('a',
459                                array('href' => common_local_url('backupaccount')),
460                                // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
461                                _('Backup account'));
462                 $this->elementEnd('li');
463             }
464             if ($user->hasRight(Right::DELETEACCOUNT)) {
465                 $this->elementStart('li');
466                 $this->element('a',
467                                array('href' => common_local_url('deleteaccount')),
468                                // TRANS: Option in profile settings to delete the account of the currently logged in user.
469                                _('Delete account'));
470                 $this->elementEnd('li');
471             }
472             if ($user->hasRight(Right::RESTOREACCOUNT)) {
473                 $this->elementStart('li');
474                 $this->element('a',
475                                array('href' => common_local_url('restoreaccount')),
476                                // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
477                                _('Restore account'));
478                 $this->elementEnd('li');
479             }
480             Event::handle('EndProfileSettingsActions', array($this));
481         }
482         $this->elementEnd('ul');
483         $this->elementEnd('div');
484         $this->elementEnd('div');
485     }
486 }