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