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