]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
Translator comments added
[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 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
37
38 /**
39  * Change profile settings
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Zach Copley <zach@status.net>
45  * @author   Sarven Capadisli <csarven@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class ProfilesettingsAction extends AccountSettingsAction
50 {
51     /**
52      * Title of the page
53      *
54      * @return string Title of the page
55      */
56     function title()
57     {
58         // TRANS: Page title for profile settings.
59         return _('Profile settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67     function getInstructions()
68     {
69         // TRANS: Usage instructions for profile settings.
70         return _('You can update your personal profile info here '.
71                  'so people know more about you.');
72     }
73
74     function showScripts()
75     {
76         parent::showScripts();
77         $this->autofocus('nickname');
78     }
79
80     /**
81      * Content area of the page
82      *
83      * Shows a form for uploading an avatar.
84      *
85      * @return void
86      */
87     function showContent()
88     {
89         $user = common_current_user();
90         $profile = $user->getProfile();
91
92         $this->elementStart('form', array('method' => 'post',
93                                           'id' => 'form_settings_profile',
94                                           'class' => 'form_settings',
95                                           'action' => common_local_url('profilesettings')));
96         $this->elementStart('fieldset');
97         // TRANS: Profile settings form legend.
98         $this->element('legend', null, _('Profile information'));
99         $this->hidden('token', common_session_token());
100
101         // too much common patterns here... abstractable?
102         $this->elementStart('ul', 'form_data');
103         if (Event::handle('StartProfileFormData', array($this))) {
104             $this->elementStart('li');
105             // TRANS: Field label in form for profile settings.
106             $this->input('nickname', _('Nickname'),
107                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
108                          // TRANS: Tooltip for field label in form for profile settings.
109                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
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') : $user->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         }
195         $this->elementEnd('ul');
196         // TRANS: Button to save input in profile settings.
197         $this->submit('save', _m('BUTTON','Save'));
198
199         $this->elementEnd('fieldset');
200         $this->elementEnd('form');
201     }
202
203     /**
204      * Handle a post
205      *
206      * Validate input and save changes. Reload the form with a success
207      * or error message.
208      *
209      * @return void
210      */
211     function handlePost()
212     {
213         // CSRF protection
214         $token = $this->trimmed('token');
215         if (!$token || $token != common_session_token()) {
216             $this->showForm(_('There was a problem with your session token. '.
217                               'Try again, please.'));
218             return;
219         }
220
221         if (Event::handle('StartProfileSaveForm', array($this))) {
222
223             try {
224                 $nickname = Nickname::normalize($this->trimmed('nickname'));
225             } catch (NicknameException $e) {
226                 $this->showForm($e->getMessage());
227                 return;
228             }
229
230             $fullname = $this->trimmed('fullname');
231             $homepage = $this->trimmed('homepage');
232             $bio = $this->trimmed('bio');
233             $location = $this->trimmed('location');
234             $autosubscribe = $this->boolean('autosubscribe');
235             $language = $this->trimmed('language');
236             $timezone = $this->trimmed('timezone');
237             $tagstring = $this->trimmed('tags');
238
239             // Some validation
240             if (!User::allowed_nickname($nickname)) {
241                 // TRANS: Validation error in form for profile settings.
242                 $this->showForm(_('Not a valid nickname.'));
243                 return;
244             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
245                        !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
246                 // TRANS: Validation error in form for profile settings.
247                 $this->showForm(_('Homepage is not a valid URL.'));
248                 return;
249             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
250                 // TRANS: Validation error in form for profile settings.
251                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
252                 return;
253             } else if (Profile::bioTooLong($bio)) {
254                 // TRANS: Validation error in form for profile settings.
255                 // TRANS: Plural form is used based on the maximum number of allowed
256                 // TRANS: characters for the biography (%d).
257                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
258                                            'Bio is too long (maximum %d characters).',
259                                            Profile::maxBio()),
260                                         Profile::maxBio()));
261                 return;
262             } else if (!is_null($location) && mb_strlen($location) > 255) {
263                 // TRANS: Validation error in form for profile settings.
264                 $this->showForm(_('Location is too long (maximum 255 characters).'));
265                 return;
266             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
267                 // TRANS: Validation error in form for profile settings.
268                 $this->showForm(_('Timezone not selected.'));
269                 return;
270             } else if ($this->nicknameExists($nickname)) {
271                 // TRANS: Validation error in form for profile settings.
272                 $this->showForm(_('Nickname already in use. Try another one.'));
273                 return;
274             } else if (!is_null($language) && strlen($language) > 50) {
275                 // TRANS: Validation error in form for profile settings.
276                 $this->showForm(_('Language is too long (maximum 50 characters).'));
277                 return;
278             }
279
280             if ($tagstring) {
281                 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
282             } else {
283                 $tags = array();
284             }
285
286             foreach ($tags as $tag) {
287                 if (!common_valid_profile_tag($tag)) {
288                     // TRANS: Validation error in form for profile settings.
289                     // TRANS: %s is an invalid tag.
290                     $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
291                     return;
292                 }
293             }
294
295             $user = common_current_user();
296
297             $user->query('BEGIN');
298
299             if ($user->nickname != $nickname ||
300                 $user->language != $language ||
301                 $user->timezone != $timezone) {
302
303                 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
304                              __FILE__);
305                 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
306                              __FILE__);
307                 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
308                              __FILE__);
309
310                 $original = clone($user);
311
312                 $user->nickname = $nickname;
313                 $user->language = $language;
314                 $user->timezone = $timezone;
315
316                 $result = $user->updateKeys($original);
317
318                 if ($result === false) {
319                     common_log_db_error($user, 'UPDATE', __FILE__);
320                     // TRANS: Server error thrown when user profile settings could not be updated.
321                     $this->serverError(_('Could not update user.'));
322                     return;
323                 } else {
324                     // Re-initialize language environment if it changed
325                     common_init_language();
326                     // Clear the site owner, in case nickname changed
327                     if ($user->hasRole(Profile_role::OWNER)) {
328                         User::blow('user:site_owner');
329                     }
330                 }
331             }
332
333             // XXX: XOR
334             if ($user->autosubscribe ^ $autosubscribe) {
335
336                 $original = clone($user);
337
338                 $user->autosubscribe = $autosubscribe;
339
340                 $result = $user->update($original);
341
342                 if ($result === false) {
343                     common_log_db_error($user, 'UPDATE', __FILE__);
344                     // TRANS: Server error thrown when user profile settings could not be updated to
345                     // TRANS: automatically subscribe to any subscriber.
346                     $this->serverError(_('Could not update user for autosubscribe.'));
347                     return;
348                 }
349             }
350
351             $profile = $user->getProfile();
352
353             $orig_profile = clone($profile);
354
355             $profile->nickname = $user->nickname;
356             $profile->fullname = $fullname;
357             $profile->homepage = $homepage;
358             $profile->bio = $bio;
359             $profile->location = $location;
360
361             $loc = Location::fromName($location);
362
363             if (empty($loc)) {
364                 $profile->lat         = null;
365                 $profile->lon         = null;
366                 $profile->location_id = null;
367                 $profile->location_ns = null;
368             } else {
369                 $profile->lat         = $loc->lat;
370                 $profile->lon         = $loc->lon;
371                 $profile->location_id = $loc->location_id;
372                 $profile->location_ns = $loc->location_ns;
373             }
374
375             $profile->profileurl = common_profile_url($nickname);
376
377             if (common_config('location', 'share') == 'user') {
378
379                 $exists = false;
380
381                 $prefs = User_location_prefs::staticGet('user_id', $user->id);
382
383                 if (empty($prefs)) {
384                     $prefs = new User_location_prefs();
385
386                     $prefs->user_id = $user->id;
387                     $prefs->created = common_sql_now();
388                 } else {
389                     $exists = true;
390                     $orig = clone($prefs);
391                 }
392
393                 $prefs->share_location = $this->boolean('sharelocation');
394
395                 if ($exists) {
396                     $result = $prefs->update($orig);
397                 } else {
398                     $result = $prefs->insert();
399                 }
400
401                 if ($result === false) {
402                     common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
403                     // TRANS: Server error thrown when user profile location preference settings could not be updated.
404                     $this->serverError(_('Could not save location prefs.'));
405                     return;
406                 }
407             }
408
409             common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
410             common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
411
412             $result = $profile->update($orig_profile);
413
414             if ($result === false) {
415                 common_log_db_error($profile, 'UPDATE', __FILE__);
416                 // TRANS: Server error thrown when user profile settings could not be saved.
417                 $this->serverError(_('Could not save profile.'));
418                 return;
419             }
420
421             // Set the user tags
422             $result = $user->setSelfTags($tags);
423
424             if (!$result) {
425                 // TRANS: Server error thrown when user profile settings tags could not be saved.
426                 $this->serverError(_('Could not save tags.'));
427                 return;
428             }
429
430             $user->query('COMMIT');
431             Event::handle('EndProfileSaveForm', array($this));
432             common_broadcast_profile($profile);
433
434             // TRANS: Confirmation shown when user profile settings are saved.
435             $this->showForm(_('Settings saved.'), true);
436
437         }
438     }
439
440     function nicknameExists($nickname)
441     {
442         $user = common_current_user();
443         $other = User::staticGet('nickname', $nickname);
444         if (!$other) {
445             return false;
446         } else {
447             return $other->id != $user->id;
448         }
449     }
450
451     function showAside() {
452         $user = common_current_user();
453
454         $this->elementStart('div', array('id' => 'aside_primary',
455                                          'class' => 'aside'));
456         $this->elementStart('ul');
457         if (Event::handle('StartProfileSettingsActions', array($this))) {
458             if ($user->hasRight(Right::BACKUPACCOUNT)) {
459                 $this->elementStart('li');
460                 $this->element('a',
461                                array('href' => common_local_url('backupaccount')),
462                                _('Backup account'));
463                 $this->elementEnd('li');
464             }
465             if ($user->hasRight(Right::DELETEACCOUNT)) {
466                 $this->elementStart('li');
467                 $this->element('a',
468                                array('href' => common_local_url('deleteaccount')),
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                                _('Restore account'));
477                 $this->elementEnd('li');
478             }
479             Event::handle('EndProfileSettingsActions', array($this));
480         }
481         $this->elementEnd('ul');
482         $this->elementEnd('div');
483     }
484 }