]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
Email notify-on-fave moved to Profile_prefs (run upgrade.php)
[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('nickname');
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 = common_current_user();
86         $profile = $user->getProfile();
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')) ? $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             $this->elementEnd('li');
107             $this->elementStart('li');
108             // TRANS: Field label in form for profile settings.
109             $this->input('fullname', _('Full name'),
110                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
111             $this->elementEnd('li');
112             $this->elementStart('li');
113             // TRANS: Field label in form for profile settings.
114             $this->input('homepage', _('Homepage'),
115                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
116                          // TRANS: Tooltip for field label in form for profile settings.
117                          _('URL of your homepage, blog, or profile on another site.'));
118             $this->elementEnd('li');
119             $this->elementStart('li');
120             $maxBio = Profile::maxBio();
121             if ($maxBio > 0) {
122                 // TRANS: Tooltip for field label in form for profile settings. Plural
123                 // TRANS: is decided by the number of characters available for the
124                 // TRANS: biography (%d).
125                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
126                                        'Describe yourself and your interests in %d characters.',
127                                        $maxBio),
128                                     $maxBio);
129             } else {
130                 // TRANS: Tooltip for field label in form for profile settings.
131                 $bioInstr = _('Describe yourself and your interests.');
132             }
133             // TRANS: Text area label in form for profile settings where users can provide
134             // TRANS: their biography.
135             $this->textarea('bio', _('Bio'),
136                             ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
137                             $bioInstr);
138             $this->elementEnd('li');
139             $this->elementStart('li');
140             // TRANS: Field label in form for profile settings.
141             $this->input('location', _('Location'),
142                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
143                          // TRANS: Tooltip for field label in form for profile settings.
144                          _('Where you are, like "City, State (or Region), Country".'));
145             $this->elementEnd('li');
146             if (common_config('location', 'share') == 'user') {
147                 $this->elementStart('li');
148                 // TRANS: Checkbox label in form for profile settings.
149                 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
150                                 ($this->arg('sharelocation')) ?
151                                 $this->arg('sharelocation') : $this->scoped->shareLocation());
152                 $this->elementEnd('li');
153             }
154             Event::handle('EndProfileFormData', array($this));
155             $this->elementStart('li');
156             // TRANS: Field label in form for profile settings.
157             $this->input('tags', _('Tags'),
158                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
159                          // TRANS: Tooltip for field label in form for profile settings.
160                          _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
161             $this->elementEnd('li');
162             $this->elementStart('li');
163             $language = common_language();
164             // TRANS: Dropdownlist label in form for profile settings.
165             $this->dropdown('language', _('Language'),
166                          // TRANS: Tooltip for dropdown list label in form for profile settings.
167                             get_nice_language_list(), _('Preferred language.'),
168                             false, $language);
169             $this->elementEnd('li');
170             $timezone = common_timezone();
171             $timezones = array();
172             foreach(DateTimeZone::listIdentifiers() as $k => $v) {
173                 $timezones[$v] = $v;
174             }
175             $this->elementStart('li');
176             // TRANS: Dropdownlist label in form for profile settings.
177             $this->dropdown('timezone', _('Timezone'),
178                          // TRANS: Tooltip for dropdown list label in form for profile settings.
179                             $timezones, _('What timezone are you normally in?'),
180                             true, $timezone);
181             $this->elementEnd('li');
182             $this->elementStart('li');
183             $this->checkbox('autosubscribe',
184                             // TRANS: Checkbox label in form for profile settings.
185                             _('Automatically subscribe to whoever '.
186                               'subscribes to me (best for non-humans)'),
187                             ($this->arg('autosubscribe')) ?
188                             $this->boolean('autosubscribe') : $user->autosubscribe);
189             $this->elementEnd('li');
190             $this->elementStart('li');
191             $this->dropdown('subscribe_policy',
192                             // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
193                             _('Subscription policy'),
194                             // TRANS: Dropdown field option for following policy.
195                             array(User::SUBSCRIBE_POLICY_OPEN     => _('Let anyone follow me'),
196                                   // TRANS: Dropdown field option for following policy.
197                                   User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
198                             // TRANS: Dropdown field title on group edit form.
199                             _('Whether other users need your permission to follow your updates.'),
200                             false,
201                             (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
202             $this->elementEnd('li');
203         }
204         $this->elementStart('li');
205         $this->checkbox('private_stream',
206                         // TRANS: Checkbox label in profile settings.
207                         _('Make updates visible only to my followers'),
208                         ($this->arg('private_stream')) ?
209                         $this->boolean('private_stream') : $user->private_stream);
210         $this->elementEnd('li');
211         $this->elementEnd('ul');
212         // TRANS: Button to save input in profile settings.
213         $this->submit('save', _m('BUTTON','Save'));
214
215         $this->elementEnd('fieldset');
216         $this->elementEnd('form');
217     }
218
219     /**
220      * Handle a post
221      *
222      * Validate input and save changes. Reload the form with a success
223      * or error message.
224      *
225      * @return void
226      */
227     function handlePost()
228     {
229         // CSRF protection
230         $token = $this->trimmed('token');
231         if (!$token || $token != common_session_token()) {
232             // TRANS: Form validation error.
233             $this->showForm(_('There was a problem with your session token. '.
234                               'Try again, please.'));
235             return;
236         }
237
238         if (Event::handle('StartProfileSaveForm', array($this))) {
239
240             $nickname = $this->trimmed('nickname');
241             try {
242                 $nickname = Nickname::normalize($nickname, true);
243             } catch (NicknameTakenException $e) {
244                 // Abort only if the nickname is occupied by another local profile
245                 if ($e->profile->id != $this->scoped->id) {
246                     $this->showForm($e->getMessage());
247                     return;
248                 }
249                 $nickname = Nickname::normalize($nickname); // without in-use check this time
250             } catch (NicknameException $e) {
251                 $this->showForm($e->getMessage());
252                 return;
253             }
254
255             $fullname = $this->trimmed('fullname');
256             $homepage = $this->trimmed('homepage');
257             $bio = $this->trimmed('bio');
258             $location = $this->trimmed('location');
259             $autosubscribe = $this->boolean('autosubscribe');
260             $subscribe_policy = $this->trimmed('subscribe_policy');
261             $private_stream = $this->boolean('private_stream');
262             $language = $this->trimmed('language');
263             $timezone = $this->trimmed('timezone');
264             $tagstring = $this->trimmed('tags');
265
266             // Some validation
267             if (!is_null($homepage) && (strlen($homepage) > 0) &&
268                        !common_valid_http_url($homepage)) {
269                 // TRANS: Validation error in form for profile settings.
270                 $this->showForm(_('Homepage is not a valid URL.'));
271                 return;
272             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
273                 // TRANS: Validation error in form for profile settings.
274                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
275                 return;
276             } else if (Profile::bioTooLong($bio)) {
277                 // TRANS: Validation error in form for profile settings.
278                 // TRANS: Plural form is used based on the maximum number of allowed
279                 // TRANS: characters for the biography (%d).
280                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
281                                            'Bio is too long (maximum %d characters).',
282                                            Profile::maxBio()),
283                                         Profile::maxBio()));
284                 return;
285             } else if (!is_null($location) && mb_strlen($location) > 255) {
286                 // TRANS: Validation error in form for profile settings.
287                 $this->showForm(_('Location is too long (maximum 255 characters).'));
288                 return;
289             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
290                 // TRANS: Validation error in form for profile settings.
291                 $this->showForm(_('Timezone not selected.'));
292                 return;
293             } else if (!is_null($language) && strlen($language) > 50) {
294                 // TRANS: Validation error in form for profile settings.
295                 $this->showForm(_('Language is too long (maximum 50 characters).'));
296                 return;
297             }
298
299             $tags = array();
300             $tag_priv = array();
301             if (is_string($tagstring) && strlen($tagstring) > 0) {
302
303                 $tags = preg_split('/[\s,]+/', $tagstring);
304
305                 foreach ($tags as &$tag) {
306                     $private = @$tag[0] === '.';
307
308                     $tag = common_canonical_tag($tag);
309                     if (!common_valid_profile_tag($tag)) {
310                         // TRANS: Validation error in form for profile settings.
311                         // TRANS: %s is an invalid tag.
312                         $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
313                         return;
314                     }
315
316                     $tag_priv[$tag] = $private;
317                 }
318             }
319
320             $user = common_current_user();
321             $user->query('BEGIN');
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                     // TRANS: Server error thrown when user profile settings could not be updated to
344                     // TRANS: automatically subscribe to any subscriber.
345                     $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
346                 }
347
348                 // Re-initialize language environment if it changed
349                 common_init_language();
350             }
351
352             $profile = $user->getProfile();
353
354             $orig_profile = clone($profile);
355
356             $profile->nickname = $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::getKV('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                 }
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             }
419
420             // Set the user tags
421             $result = $user->setSelfTags($tags, $tag_priv);
422
423             if (!$result) {
424                 // TRANS: Server error thrown when user profile settings tags could not be saved.
425                 $this->serverError(_('Could not save tags.'));
426             }
427
428             $user->query('COMMIT');
429             Event::handle('EndProfileSaveForm', array($this));
430
431             // TRANS: Confirmation shown when user profile settings are saved.
432             $this->showForm(_('Settings saved.'), true);
433
434         }
435     }
436
437     function showAside() {
438         $user = common_current_user();
439
440         $this->elementStart('div', array('id' => 'aside_primary',
441                                          'class' => 'aside'));
442
443         $this->elementStart('div', array('id' => 'account_actions',
444                                          'class' => 'section'));
445         $this->elementStart('ul');
446         if (Event::handle('StartProfileSettingsActions', array($this))) {
447             if ($user->hasRight(Right::BACKUPACCOUNT)) {
448                 $this->elementStart('li');
449                 $this->element('a',
450                                array('href' => common_local_url('backupaccount')),
451                                // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
452                                _('Backup account'));
453                 $this->elementEnd('li');
454             }
455             if ($user->hasRight(Right::DELETEACCOUNT)) {
456                 $this->elementStart('li');
457                 $this->element('a',
458                                array('href' => common_local_url('deleteaccount')),
459                                // TRANS: Option in profile settings to delete the account of the currently logged in user.
460                                _('Delete account'));
461                 $this->elementEnd('li');
462             }
463             if ($user->hasRight(Right::RESTOREACCOUNT)) {
464                 $this->elementStart('li');
465                 $this->element('a',
466                                array('href' => common_local_url('restoreaccount')),
467                                // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
468                                _('Restore account'));
469                 $this->elementEnd('li');
470             }
471             Event::handle('EndProfileSettingsActions', array($this));
472         }
473         $this->elementEnd('ul');
474         $this->elementEnd('div');
475         $this->elementEnd('div');
476     }
477 }