]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
L10n/i18n updated.
[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->elementEnd('ul');
207         // TRANS: Button to save input in profile settings.
208         $this->submit('save', _m('BUTTON','Save'));
209
210         $this->elementEnd('fieldset');
211         $this->elementEnd('form');
212     }
213
214     /**
215      * Handle a post
216      *
217      * Validate input and save changes. Reload the form with a success
218      * or error message.
219      *
220      * @return void
221      */
222     function handlePost()
223     {
224         // CSRF protection
225         $token = $this->trimmed('token');
226         if (!$token || $token != common_session_token()) {
227             // TRANS: Form validation error.
228             $this->showForm(_('There was a problem with your session token. '.
229                               'Try again, please.'));
230             return;
231         }
232
233         if (Event::handle('StartProfileSaveForm', array($this))) {
234
235             try {
236                 $nickname = Nickname::normalize($this->trimmed('nickname'));
237             } catch (NicknameException $e) {
238                 $this->showForm($e->getMessage());
239                 return;
240             }
241
242             $fullname = $this->trimmed('fullname');
243             $homepage = $this->trimmed('homepage');
244             $bio = $this->trimmed('bio');
245             $location = $this->trimmed('location');
246             $autosubscribe = $this->boolean('autosubscribe');
247             $subscribe_policy = $this->trimmed('subscribe_policy');
248             $language = $this->trimmed('language');
249             $timezone = $this->trimmed('timezone');
250             $tagstring = $this->trimmed('tags');
251
252             // Some validation
253             if (!User::allowed_nickname($nickname)) {
254                 // TRANS: Validation error in form for profile settings.
255                 $this->showForm(_('Not a valid nickname.'));
256                 return;
257             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
258                        !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
259                 // TRANS: Validation error in form for profile settings.
260                 $this->showForm(_('Homepage is not a valid URL.'));
261                 return;
262             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
263                 // TRANS: Validation error in form for profile settings.
264                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
265                 return;
266             } else if (Profile::bioTooLong($bio)) {
267                 // TRANS: Validation error in form for profile settings.
268                 // TRANS: Plural form is used based on the maximum number of allowed
269                 // TRANS: characters for the biography (%d).
270                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
271                                            'Bio is too long (maximum %d characters).',
272                                            Profile::maxBio()),
273                                         Profile::maxBio()));
274                 return;
275             } else if (!is_null($location) && mb_strlen($location) > 255) {
276                 // TRANS: Validation error in form for profile settings.
277                 $this->showForm(_('Location is too long (maximum 255 characters).'));
278                 return;
279             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
280                 // TRANS: Validation error in form for profile settings.
281                 $this->showForm(_('Timezone not selected.'));
282                 return;
283             } else if ($this->nicknameExists($nickname)) {
284                 // TRANS: Validation error in form for profile settings.
285                 $this->showForm(_('Nickname already in use. Try another one.'));
286                 return;
287             } else if (!is_null($language) && strlen($language) > 50) {
288                 // TRANS: Validation error in form for profile settings.
289                 $this->showForm(_('Language is too long (maximum 50 characters).'));
290                 return;
291             }
292
293             if ($tagstring) {
294                 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
295             } else {
296                 $tags = array();
297             }
298
299             foreach ($tags as $tag) {
300                 if (!common_valid_profile_tag($tag)) {
301                     // TRANS: Validation error in form for profile settings.
302                     // TRANS: %s is an invalid tag.
303                     $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
304                     return;
305                 }
306             }
307
308             $user = common_current_user();
309
310             $user->query('BEGIN');
311
312             if ($user->nickname != $nickname ||
313                 $user->language != $language ||
314                 $user->timezone != $timezone) {
315
316                 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
317                              __FILE__);
318                 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
319                              __FILE__);
320                 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
321                              __FILE__);
322
323                 $original = clone($user);
324
325                 $user->nickname = $nickname;
326                 $user->language = $language;
327                 $user->timezone = $timezone;
328
329                 $result = $user->updateKeys($original);
330
331                 if ($result === false) {
332                     common_log_db_error($user, 'UPDATE', __FILE__);
333                     // TRANS: Server error thrown when user profile settings could not be updated.
334                     $this->serverError(_('Could not update user.'));
335                     return;
336                 } else {
337                     // Re-initialize language environment if it changed
338                     common_init_language();
339                     // Clear the site owner, in case nickname changed
340                     if ($user->hasRole(Profile_role::OWNER)) {
341                         User::blow('user:site_owner');
342                     }
343                 }
344             }
345
346             // XXX: XOR
347             if (($user->autosubscribe ^ $autosubscribe) || $user->subscribe_policy != $subscribe_policy) {
348
349                 $original = clone($user);
350
351                 $user->autosubscribe = $autosubscribe;
352                 $user->subscribe_policy = $subscribe_policy;
353
354                 $result = $user->update($original);
355
356                 if ($result === false) {
357                     common_log_db_error($user, 'UPDATE', __FILE__);
358                     // TRANS: Server error thrown when user profile settings could not be updated to
359                     // TRANS: automatically subscribe to any subscriber.
360                     $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
361                     return;
362                 }
363             }
364
365             $profile = $user->getProfile();
366
367             $orig_profile = clone($profile);
368
369             $profile->nickname = $user->nickname;
370             $profile->fullname = $fullname;
371             $profile->homepage = $homepage;
372             $profile->bio = $bio;
373             $profile->location = $location;
374
375             $loc = Location::fromName($location);
376
377             if (empty($loc)) {
378                 $profile->lat         = null;
379                 $profile->lon         = null;
380                 $profile->location_id = null;
381                 $profile->location_ns = null;
382             } else {
383                 $profile->lat         = $loc->lat;
384                 $profile->lon         = $loc->lon;
385                 $profile->location_id = $loc->location_id;
386                 $profile->location_ns = $loc->location_ns;
387             }
388
389             $profile->profileurl = common_profile_url($nickname);
390
391             if (common_config('location', 'share') == 'user') {
392
393                 $exists = false;
394
395                 $prefs = User_location_prefs::staticGet('user_id', $user->id);
396
397                 if (empty($prefs)) {
398                     $prefs = new User_location_prefs();
399
400                     $prefs->user_id = $user->id;
401                     $prefs->created = common_sql_now();
402                 } else {
403                     $exists = true;
404                     $orig = clone($prefs);
405                 }
406
407                 $prefs->share_location = $this->boolean('sharelocation');
408
409                 if ($exists) {
410                     $result = $prefs->update($orig);
411                 } else {
412                     $result = $prefs->insert();
413                 }
414
415                 if ($result === false) {
416                     common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
417                     // TRANS: Server error thrown when user profile location preference settings could not be updated.
418                     $this->serverError(_('Could not save location prefs.'));
419                     return;
420                 }
421             }
422
423             common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
424             common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
425
426             $result = $profile->update($orig_profile);
427
428             if ($result === false) {
429                 common_log_db_error($profile, 'UPDATE', __FILE__);
430                 // TRANS: Server error thrown when user profile settings could not be saved.
431                 $this->serverError(_('Could not save profile.'));
432                 return;
433             }
434
435             // Set the user tags
436             $result = $user->setSelfTags($tags);
437
438             if (!$result) {
439                 // TRANS: Server error thrown when user profile settings tags could not be saved.
440                 $this->serverError(_('Could not save tags.'));
441                 return;
442             }
443
444             $user->query('COMMIT');
445             Event::handle('EndProfileSaveForm', array($this));
446             common_broadcast_profile($profile);
447
448             // TRANS: Confirmation shown when user profile settings are saved.
449             $this->showForm(_('Settings saved.'), true);
450
451         }
452     }
453
454     function nicknameExists($nickname)
455     {
456         $user = common_current_user();
457         $other = User::staticGet('nickname', $nickname);
458         if (!$other) {
459             return false;
460         } else {
461             return $other->id != $user->id;
462         }
463     }
464
465     function showAside() {
466         $user = common_current_user();
467
468         $this->elementStart('div', array('id' => 'aside_primary',
469                                          'class' => 'aside'));
470
471         $this->elementStart('div', array('id' => 'account_actions',
472                                          'class' => 'section'));
473         $this->elementStart('ul');
474         if (Event::handle('StartProfileSettingsActions', array($this))) {
475             if ($user->hasRight(Right::BACKUPACCOUNT)) {
476                 $this->elementStart('li');
477                 $this->element('a',
478                                array('href' => common_local_url('backupaccount')),
479                                // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
480                                _('Backup account'));
481                 $this->elementEnd('li');
482             }
483             if ($user->hasRight(Right::DELETEACCOUNT)) {
484                 $this->elementStart('li');
485                 $this->element('a',
486                                array('href' => common_local_url('deleteaccount')),
487                                // TRANS: Option in profile settings to delete the account of the currently logged in user.
488                                _('Delete account'));
489                 $this->elementEnd('li');
490             }
491             if ($user->hasRight(Right::RESTOREACCOUNT)) {
492                 $this->elementStart('li');
493                 $this->element('a',
494                                array('href' => common_local_url('restoreaccount')),
495                                // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
496                                _('Restore account'));
497                 $this->elementEnd('li');
498             }
499             Event::handle('EndProfileSettingsActions', array($this));
500         }
501         $this->elementEnd('ul');
502         $this->elementEnd('div');
503         $this->elementEnd('div');
504     }
505 }