]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
jQuery cookie plugin 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('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         $profile = $this->scoped;
86         $user = $this->scoped->getUser();
87
88         $this->elementStart('form', array('method' => 'post',
89                                           'id' => 'form_settings_profile',
90                                           'class' => 'form_settings',
91                                           'action' => common_local_url('profilesettings')));
92         $this->elementStart('fieldset');
93         // TRANS: Profile settings form legend.
94         $this->element('legend', null, _('Profile information'));
95         $this->hidden('token', common_session_token());
96
97         // too much common patterns here... abstractable?
98         $this->elementStart('ul', 'form_data');
99         if (Event::handle('StartProfileFormData', array($this))) {
100             $this->elementStart('li');
101             // TRANS: Field label in form for profile settings.
102             $this->input('nickname', _('Nickname'),
103                          $this->arg('nickname') ?: $profile->nickname,
104                          // TRANS: Tooltip for field label in form for profile settings.
105                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'),
106                          null, false,   // "name" (will be set to id), then "required"
107                          !common_config('profile', 'changenick') ? array('disabled'=>'disabled') : array());
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') : $this->scoped->shareLocation());
154                 $this->elementEnd('li');
155             }
156             Event::handle('EndProfileFormData', array($this));
157             $this->elementStart('li');
158             // TRANS: Field label in form for profile settings.
159             $this->input('tags', _('Tags'),
160                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
161                          // TRANS: Tooltip for field label in form for profile settings.
162                          _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
163             $this->elementEnd('li');
164             $this->elementStart('li');
165             $language = common_language();
166             // TRANS: Dropdownlist label in form for profile settings.
167             $this->dropdown('language', _('Language'),
168                          // TRANS: Tooltip for dropdown list label in form for profile settings.
169                             get_nice_language_list(), _('Preferred language.'),
170                             false, $language);
171             $this->elementEnd('li');
172             $timezone = common_timezone();
173             $timezones = array();
174             foreach(DateTimeZone::listIdentifiers() as $k => $v) {
175                 $timezones[$v] = $v;
176             }
177             $this->elementStart('li');
178             // TRANS: Dropdownlist label in form for profile settings.
179             $this->dropdown('timezone', _('Timezone'),
180                          // TRANS: Tooltip for dropdown list label in form for profile settings.
181                             $timezones, _('What timezone are you normally in?'),
182                             true, $timezone);
183             $this->elementEnd('li');
184             $this->elementStart('li');
185             $this->checkbox('autosubscribe',
186                             // TRANS: Checkbox label in form for profile settings.
187                             _('Automatically subscribe to whoever '.
188                               'subscribes to me (best for non-humans)'),
189                             ($this->arg('autosubscribe')) ?
190                             $this->boolean('autosubscribe') : $user->autosubscribe);
191             $this->elementEnd('li');
192             $this->elementStart('li');
193             $this->dropdown('subscribe_policy',
194                             // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
195                             _('Subscription policy'),
196                             // TRANS: Dropdown field option for following policy.
197                             array(User::SUBSCRIBE_POLICY_OPEN     => _('Let anyone follow me'),
198                                   // TRANS: Dropdown field option for following policy.
199                                   User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
200                             // TRANS: Dropdown field title on group edit form.
201                             _('Whether other users need your permission to follow your updates.'),
202                             false,
203                             (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
204             $this->elementEnd('li');
205         }
206         $this->elementStart('li');
207         $this->checkbox('private_stream',
208                         // TRANS: Checkbox label in profile settings.
209                         _('Make updates visible only to my followers'),
210                         ($this->arg('private_stream')) ?
211                         $this->boolean('private_stream') : $user->private_stream);
212         $this->elementEnd('li');
213         $this->elementEnd('ul');
214         // TRANS: Button to save input in profile settings.
215         $this->submit('save', _m('BUTTON','Save'));
216
217         $this->elementEnd('fieldset');
218         $this->elementEnd('form');
219     }
220
221     /**
222      * Handle a post
223      *
224      * Validate input and save changes. Reload the form with a success
225      * or error message.
226      *
227      * @return void
228      */
229     function handlePost()
230     {
231         // CSRF protection
232         $token = $this->trimmed('token');
233         if (!$token || $token != common_session_token()) {
234             // TRANS: Form validation error.
235             $this->showForm(_('There was a problem with your session token. '.
236                               'Try again, please.'));
237             return;
238         }
239
240         if (Event::handle('StartProfileSaveForm', array($this))) {
241
242             // $nickname will only be set if this changenick value is true.
243             if (common_config('profile', 'changenick') == true) {
244                 try {
245                     $nickname = Nickname::normalize($this->trimmed('nickname'), true);
246                 } catch (NicknameTakenException $e) {
247                     // Abort only if the nickname is occupied by another local profile
248                     if ($e->profile->id != $this->scoped->id) {
249                         $this->showForm($e->getMessage());
250                         return;
251                     }
252                     $nickname = Nickname::normalize($this->trimmed('nickname')); // without in-use check this time
253                 } catch (NicknameException $e) {
254                     $this->showForm($e->getMessage());
255                     return;
256                 }
257             }
258
259             $fullname = $this->trimmed('fullname');
260             $homepage = $this->trimmed('homepage');
261             $bio = $this->trimmed('bio');
262             $location = $this->trimmed('location');
263             $autosubscribe = $this->booleanintstring('autosubscribe');
264             $subscribe_policy = $this->trimmed('subscribe_policy');
265             $private_stream = $this->booleanintstring('private_stream');
266             $language = $this->trimmed('language');
267             $timezone = $this->trimmed('timezone');
268             $tagstring = $this->trimmed('tags');
269
270             // Some validation
271             if (!is_null($homepage) && (strlen($homepage) > 0) &&
272                        !common_valid_http_url($homepage)) {
273                 // TRANS: Validation error in form for profile settings.
274                 $this->showForm(_('Homepage is not a valid URL.'));
275                 return;
276             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
277                 // TRANS: Validation error in form for profile settings.
278                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
279                 return;
280             } else if (Profile::bioTooLong($bio)) {
281                 // TRANS: Validation error in form for profile settings.
282                 // TRANS: Plural form is used based on the maximum number of allowed
283                 // TRANS: characters for the biography (%d).
284                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
285                                            'Bio is too long (maximum %d characters).',
286                                            Profile::maxBio()),
287                                         Profile::maxBio()));
288                 return;
289             } else if (!is_null($location) && mb_strlen($location) > 255) {
290                 // TRANS: Validation error in form for profile settings.
291                 $this->showForm(_('Location is too long (maximum 255 characters).'));
292                 return;
293             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
294                 // TRANS: Validation error in form for profile settings.
295                 $this->showForm(_('Timezone not selected.'));
296                 return;
297             } else if (!is_null($language) && strlen($language) > 50) {
298                 // TRANS: Validation error in form for profile settings.
299                 $this->showForm(_('Language is too long (maximum 50 characters).'));
300                 return;
301             }
302
303             $tags = array();
304             $tag_priv = array();
305             if (is_string($tagstring) && strlen($tagstring) > 0) {
306
307                 $tags = preg_split('/[\s,]+/', $tagstring);
308
309                 foreach ($tags as &$tag) {
310                     $private = @$tag[0] === '.';
311
312                     $tag = common_canonical_tag($tag);
313                     if (!common_valid_profile_tag($tag)) {
314                         // TRANS: Validation error in form for profile settings.
315                         // TRANS: %s is an invalid tag.
316                         $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
317                         return;
318                     }
319
320                     $tag_priv[$tag] = $private;
321                 }
322             }
323
324             $user = common_current_user();
325             $user->query('BEGIN');
326
327             // $user->nickname is updated through Profile->update();
328
329             // XXX: XOR
330             if (($user->autosubscribe ^ $autosubscribe)
331                     || ($user->private_stream ^ $private_stream)
332                     || $user->timezone != $timezone
333                     || $user->language != $language
334                     || $user->subscribe_policy != $subscribe_policy) {
335
336                 $original = clone($user);
337
338                 $user->autosubscribe    = $autosubscribe;
339                 $user->language         = $language;
340                 $user->private_stream   = $private_stream;
341                 $user->subscribe_policy = $subscribe_policy;
342                 $user->timezone         = $timezone;
343
344                 $result = $user->update($original);
345                 if ($result === false) {
346                     common_log_db_error($user, 'UPDATE', __FILE__);
347                     // TRANS: Server error thrown when user profile settings could not be updated to
348                     // TRANS: automatically subscribe to any subscriber.
349                     $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
350                 }
351
352                 // Re-initialize language environment if it changed
353                 common_init_language();
354             }
355
356             $profile = $user->getProfile();
357
358             $orig_profile = clone($profile);
359
360             if (common_config('profile', 'changenick') == true && $profile->nickname !== $nickname) {
361                 assert(Nickname::normalize($nickname)===$nickname);
362                 common_debug("Changing user nickname from '{$profile->nickname}' to '{$nickname}'.");
363                 $profile->nickname = $nickname;
364                 $profile->profileurl = common_profile_url($profile->nickname);
365             }
366             $profile->fullname = $fullname;
367             $profile->homepage = $homepage;
368             $profile->bio = $bio;
369             $profile->location = $location;
370
371             $loc = Location::fromName($location);
372
373             if (empty($loc)) {
374                 $profile->lat         = null;
375                 $profile->lon         = null;
376                 $profile->location_id = null;
377                 $profile->location_ns = null;
378             } else {
379                 $profile->lat         = $loc->lat;
380                 $profile->lon         = $loc->lon;
381                 $profile->location_id = $loc->location_id;
382                 $profile->location_ns = $loc->location_ns;
383             }
384
385             if (common_config('location', 'share') == 'user') {
386
387                 $exists = false;
388
389                 $prefs = User_location_prefs::getKV('user_id', $user->id);
390
391                 if (empty($prefs)) {
392                     $prefs = new User_location_prefs();
393
394                     $prefs->user_id = $user->id;
395                     $prefs->created = common_sql_now();
396                 } else {
397                     $exists = true;
398                     $orig = clone($prefs);
399                 }
400
401                 $prefs->share_location = $this->booleanintstring('sharelocation');
402
403                 if ($exists) {
404                     $result = $prefs->update($orig);
405                 } else {
406                     $result = $prefs->insert();
407                 }
408
409                 if ($result === false) {
410                     common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
411                     // TRANS: Server error thrown when user profile location preference settings could not be updated.
412                     $this->serverError(_('Could not save location prefs.'));
413                 }
414             }
415
416             common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
417             common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
418
419             $result = $profile->update($orig_profile);
420
421             if ($result === false) {
422                 common_log_db_error($profile, 'UPDATE', __FILE__);
423                 // TRANS: Server error thrown when user profile settings could not be saved.
424                 $this->serverError(_('Could not save profile.'));
425             }
426
427             // Set the user tags
428             $result = $user->setSelfTags($tags, $tag_priv);
429
430             if (!$result) {
431                 // TRANS: Server error thrown when user profile settings tags could not be saved.
432                 $this->serverError(_('Could not save tags.'));
433             }
434
435             $user->query('COMMIT');
436             Event::handle('EndProfileSaveForm', array($this));
437
438             // TRANS: Confirmation shown when user profile settings are saved.
439             $this->showForm(_('Settings saved.'), true);
440
441         }
442     }
443
444     function showAside() {
445         $user = common_current_user();
446
447         $this->elementStart('div', array('id' => 'aside_primary',
448                                          'class' => 'aside'));
449
450         $this->elementStart('div', array('id' => 'account_actions',
451                                          'class' => 'section'));
452         $this->elementStart('ul');
453         if (Event::handle('StartProfileSettingsActions', array($this))) {
454             if ($user->hasRight(Right::BACKUPACCOUNT)) {
455                 $this->elementStart('li');
456                 $this->element('a',
457                                array('href' => common_local_url('backupaccount')),
458                                // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
459                                _('Backup account'));
460                 $this->elementEnd('li');
461             }
462             if ($user->hasRight(Right::DELETEACCOUNT)) {
463                 $this->elementStart('li');
464                 $this->element('a',
465                                array('href' => common_local_url('deleteaccount')),
466                                // TRANS: Option in profile settings to delete the account of the currently logged in user.
467                                _('Delete account'));
468                 $this->elementEnd('li');
469             }
470             if ($user->hasRight(Right::RESTOREACCOUNT)) {
471                 $this->elementStart('li');
472                 $this->element('a',
473                                array('href' => common_local_url('restoreaccount')),
474                                // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
475                                _('Restore account'));
476                 $this->elementEnd('li');
477             }
478             Event::handle('EndProfileSettingsActions', array($this));
479         }
480         $this->elementEnd('ul');
481         $this->elementEnd('div');
482         $this->elementEnd('div');
483     }
484 }