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