We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

profilesettings.php 20.4 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
5 6 7 8 9
 * Change profile settings
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
Evan Prodromou's avatar
Evan Prodromou committed
10 11 12
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Evan Prodromou's avatar
Evan Prodromou committed
13
 *
Evan Prodromou's avatar
Evan Prodromou committed
14 15 16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
Evan Prodromou's avatar
Evan Prodromou committed
18
 *
Evan Prodromou's avatar
Evan Prodromou committed
19 20
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 22
 *
 * @category  Settings
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @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/
Evan Prodromou's avatar
Evan Prodromou committed
30 31
 */

32
if (!defined('STATUSNET') && !defined('LACONICA')) {
33 34 35 36
    exit(1);
}

require_once INSTALLDIR.'/lib/accountsettingsaction.php';
Evan Prodromou's avatar
Evan Prodromou committed
37

38 39 40 41
/**
 * Change profile settings
 *
 * @category Settings
42
 * @package  StatusNet
43 44
 * @author   Evan Prodromou <evan@status.net>
 * @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 53 54 55 56
    /**
     * Title of the page
     *
     * @return string Title of the page
     */
    function title()
57
    {
58
        // TRANS: Page title for profile settings.
59
        return _('Profile settings');
60 61
    }

62 63 64 65 66 67
    /**
     * Instructions for use
     *
     * @return instructions for use
     */
    function getInstructions()
68
    {
69
        // TRANS: Usage instructions for profile settings.
70
        return _('You can update your personal profile info here '.
Evan Prodromou's avatar
Evan Prodromou committed
71
                 'so people know more about you.');
72 73
    }

74 75 76 77 78 79
    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('nickname');
    }

80 81 82 83 84 85 86 87 88
    /**
     * Content area of the page
     *
     * Shows a form for uploading an avatar.
     *
     * @return void
     */
    function showContent()
    {
89 90 91
        $user = common_current_user();
        $profile = $user->getProfile();

92
        $this->elementStart('form', array('method' => 'post',
Evan Prodromou's avatar
Evan Prodromou committed
93 94 95
                                          'id' => 'form_settings_profile',
                                          'class' => 'form_settings',
                                          'action' => common_local_url('profilesettings')));
sarven's avatar
sarven committed
96
        $this->elementStart('fieldset');
97
        // TRANS: Profile settings form legend.
sarven's avatar
sarven committed
98
        $this->element('legend', null, _('Profile information'));
99
        $this->hidden('token', common_session_token());
100

101
        // too much common patterns here... abstractable?
sarven's avatar
sarven committed
102
        $this->elementStart('ul', 'form_data');
103 104
        if (Event::handle('StartProfileFormData', array($this))) {
            $this->elementStart('li');
105
            // TRANS: Field label in form for profile settings.
106 107
            $this->input('nickname', _('Nickname'),
                         ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
108 109
                         // TRANS: Tooltip for field label in form for profile settings.
                         _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
110 111
            $this->elementEnd('li');
            $this->elementStart('li');
112
            // TRANS: Field label in form for profile settings.
113 114 115 116
            $this->input('fullname', _('Full name'),
                         ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
            $this->elementEnd('li');
            $this->elementStart('li');
117
            // TRANS: Field label in form for profile settings.
118 119
            $this->input('homepage', _('Homepage'),
                         ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
120 121
                         // TRANS: Tooltip for field label in form for profile settings.
                         _('URL of your homepage, blog, or profile on another site.'));
122 123
            $this->elementEnd('li');
            $this->elementStart('li');
124 125
            $maxBio = Profile::maxBio();
            if ($maxBio > 0) {
126 127 128 129 130 131
                // TRANS: Tooltip for field label in form for profile settings. Plural
                // TRANS: is decided by the number of characters available for the
                // TRANS: biography (%d).
                $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
                                       'Describe yourself and your interests in %d characters',
                                       $maxBio),
132 133
                                    $maxBio);
            } else {
134
                // TRANS: Tooltip for field label in form for profile settings.
135 136
                $bioInstr = _('Describe yourself and your interests');
            }
137 138
            // TRANS: Text area label in form for profile settings where users can provide.
            // TRANS: their biography.
139 140
            $this->textarea('bio', _('Bio'),
                            ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
141
                            $bioInstr);
142 143
            $this->elementEnd('li');
            $this->elementStart('li');
144
            // TRANS: Field label in form for profile settings.
145 146
            $this->input('location', _('Location'),
                         ($this->arg('location')) ? $this->arg('location') : $profile->location,
147
                         // TRANS: Tooltip for field label in form for profile settings.
148 149
                         _('Where you are, like "City, State (or Region), Country"'));
            $this->elementEnd('li');
150 151
            if (common_config('location', 'share') == 'user') {
                $this->elementStart('li');
152
                // TRANS: Checkbox label in form for profile settings.
153 154 155 156 157
                $this->checkbox('sharelocation', _('Share my current location when posting notices'),
                                ($this->arg('sharelocation')) ?
                                $this->arg('sharelocation') : $user->shareLocation());
                $this->elementEnd('li');
            }
158 159
            Event::handle('EndProfileFormData', array($this));
            $this->elementStart('li');
160
            // TRANS: Field label in form for profile settings.
161 162
            $this->input('tags', _('Tags'),
                         ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
163
                         // TRANS: Tooltip for field label in form for profile settings.
164 165 166 167
                         _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
            $this->elementEnd('li');
            $this->elementStart('li');
            $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 172 173 174 175 176 177 178 179
                            get_nice_language_list(), _('Preferred language'),
                            false, $language);
            $this->elementEnd('li');
            $timezone = common_timezone();
            $timezones = array();
            foreach(DateTimeZone::listIdentifiers() as $k => $v) {
                $timezones[$v] = $v;
            }
            $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 184 185 186 187
                            $timezones, _('What timezone are you normally in?'),
                            true, $timezone);
            $this->elementEnd('li');
            $this->elementStart('li');
            $this->checkbox('autosubscribe',
188
                            // TRANS: Checkbox label in form for profile settings.
189 190 191 192 193
                            _('Automatically subscribe to whoever '.
                              'subscribes to me (best for non-humans)'),
                            ($this->arg('autosubscribe')) ?
                            $this->boolean('autosubscribe') : $user->autosubscribe);
            $this->elementEnd('li');
194
        }
sarven's avatar
sarven committed
195
        $this->elementEnd('ul');
196 197
        // TRANS: Button to save input in profile settings.
        $this->submit('save', _m('BUTTON','Save'));
198

sarven's avatar
sarven committed
199
        $this->elementEnd('fieldset');
200
        $this->elementEnd('form');
201 202
    }

203 204 205 206 207 208 209 210 211
    /**
     * Handle a post
     *
     * Validate input and save changes. Reload the form with a success
     * or error message.
     *
     * @return void
     */
    function handlePost()
212
    {
213
        // CSRF protection
214 215
        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
216
            // TRANS: Form validation error.
217
            $this->showForm(_('There was a problem with your session token. '.
Evan Prodromou's avatar
Evan Prodromou committed
218
                              'Try again, please.'));
219
            return;
220 221
        }

222 223
        if (Event::handle('StartProfileSaveForm', array($this))) {

224 225 226 227 228 229 230
            try {
                $nickname = Nickname::normalize($this->trimmed('nickname'));
            } catch (NicknameException $e) {
                $this->showForm($e->getMessage());
                return;
            }

231 232 233 234 235 236 237 238 239 240
            $fullname = $this->trimmed('fullname');
            $homepage = $this->trimmed('homepage');
            $bio = $this->trimmed('bio');
            $location = $this->trimmed('location');
            $autosubscribe = $this->boolean('autosubscribe');
            $language = $this->trimmed('language');
            $timezone = $this->trimmed('timezone');
            $tagstring = $this->trimmed('tags');

            // Some validation
241
            if (!User::allowed_nickname($nickname)) {
242
                // TRANS: Validation error in form for profile settings.
243 244 245
                $this->showForm(_('Not a valid nickname.'));
                return;
            } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
Evan Prodromou's avatar
Evan Prodromou committed
246
                       !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
247
                // TRANS: Validation error in form for profile settings.
248 249 250
                $this->showForm(_('Homepage is not a valid URL.'));
                return;
            } 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 256 257
                // TRANS: Validation error in form for profile settings.
                // TRANS: Plural form is used based on the maximum number of allowed
                // TRANS: characters for the biography (%d).
258 259
                $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
                                           'Bio is too long (maximum %d characters).',
260
                                           Profile::maxBio()),
261
                                        Profile::maxBio()));
262 263
                return;
            } 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 267
                return;
            }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
268
                // TRANS: Validation error in form for profile settings.
269 270 271
                $this->showForm(_('Timezone not selected.'));
                return;
            } else if ($this->nicknameExists($nickname)) {
272
                // TRANS: Validation error in form for profile settings.
273 274 275
                $this->showForm(_('Nickname already in use. Try another one.'));
                return;
            } 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 279
                return;
            }
280

281 282 283 284 285
            if ($tagstring) {
                $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
            } else {
                $tags = array();
            }
286

287 288
            foreach ($tags as $tag) {
                if (!common_valid_profile_tag($tag)) {
289 290
                    // TRANS: Validation error in form for profile settings.
                    // TRANS: %s is an invalid tag.
291 292 293
                    $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
                    return;
                }
294
            }
295

296
            $user = common_current_user();
297

298
            $user->query('BEGIN');
299

300
            if ($user->nickname != $nickname ||
Evan Prodromou's avatar
Evan Prodromou committed
301 302
                $user->language != $language ||
                $user->timezone != $timezone) {
303

304
                common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
Evan Prodromou's avatar
Evan Prodromou committed
305
                             __FILE__);
306
                common_debug('Updating user language from ' . $user->language . ' to ' . $language,
Evan Prodromou's avatar
Evan Prodromou committed
307
                             __FILE__);
308
                common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
Evan Prodromou's avatar
Evan Prodromou committed
309
                             __FILE__);
310

311
                $original = clone($user);
312

313 314 315
                $user->nickname = $nickname;
                $user->language = $language;
                $user->timezone = $timezone;
316

317
                $result = $user->updateKeys($original);
318

319 320
                if ($result === false) {
                    common_log_db_error($user, 'UPDATE', __FILE__);
321
                    // TRANS: Server error thrown when user profile settings could not be updated.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
322
                    $this->serverError(_('Could not update user.'));
323 324 325 326
                    return;
                } else {
                    // Re-initialize language environment if it changed
                    common_init_language();
327 328 329 330
                    // Clear the site owner, in case nickname changed
                    if ($user->hasRole(Profile_role::OWNER)) {
                        User::blow('user:site_owner');
                    }
331
                }
332 333
            }

Evan Prodromou's avatar
Evan Prodromou committed
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 344
                if ($result === false) {
                    common_log_db_error($user, 'UPDATE', __FILE__);
345 346
                    // TRANS: Server error thrown when user profile settings could not be updated to
                    // TRANS: automatically subscribe to any subscriber.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
347
                    $this->serverError(_('Could not update user for autosubscribe.'));
348 349
                    return;
                }
350 351
            }

352
            $profile = $user->getProfile();
353

354
            $orig_profile = clone($profile);
355

356 357 358 359 360
            $profile->nickname = $user->nickname;
            $profile->fullname = $fullname;
            $profile->homepage = $homepage;
            $profile->bio = $bio;
            $profile->location = $location;
361 362 363

            $loc = Location::fromName($location);

364 365 366 367 368 369
            if (empty($loc)) {
                $profile->lat         = null;
                $profile->lon         = null;
                $profile->location_id = null;
                $profile->location_ns = null;
            } else {
370 371 372 373 374 375
                $profile->lat         = $loc->lat;
                $profile->lon         = $loc->lon;
                $profile->location_id = $loc->location_id;
                $profile->location_ns = $loc->location_ns;
            }

376
            $profile->profileurl = common_profile_url($nickname);
377

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
            if (common_config('location', 'share') == 'user') {

                $exists = false;

                $prefs = User_location_prefs::staticGet('user_id', $user->id);

                if (empty($prefs)) {
                    $prefs = new User_location_prefs();

                    $prefs->user_id = $user->id;
                    $prefs->created = common_sql_now();
                } else {
                    $exists = true;
                    $orig = clone($prefs);
                }

                $prefs->share_location = $this->boolean('sharelocation');

                if ($exists) {
                    $result = $prefs->update($orig);
                } else {
                    $result = $prefs->insert();
                }

                if ($result === false) {
                    common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
404
                    // TRANS: Server error thrown when user profile location preference settings could not be updated.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
405
                    $this->serverError(_('Could not save location prefs.'));
406 407 408 409
                    return;
                }
            }

410 411
            common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
            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.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
418
                $this->serverError(_('Could not save profile.'));
419 420
                return;
            }
421

422 423
            // Set the user tags
            $result = $user->setSelfTags($tags);
424

425
            if (!$result) {
426
                // TRANS: Server error thrown when user profile settings tags could not be saved.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
427
                $this->serverError(_('Could not save tags.'));
428 429
                return;
            }
430

431 432 433
            $user->query('COMMIT');
            Event::handle('EndProfileSaveForm', array($this));
            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 444 445
        $user = common_current_user();
        $other = User::staticGet('nickname', $nickname);
        if (!$other) {
Evan Prodromou's avatar
Evan Prodromou committed
446
            return false;
447 448 449 450
        } else {
            return $other->id != $user->id;
        }
    }
451 452

    function showAside() {
453 454
        $user = common_current_user();

455 456
        $this->elementStart('div', array('id' => 'aside_primary',
                                         'class' => 'aside'));
457 458 459

        $this->elementStart('div', array('id' => 'account_actions',
                                         'class' => 'section'));
460 461 462 463 464 465
        $this->elementStart('ul');
        if (Event::handle('StartProfileSettingsActions', array($this))) {
            if ($user->hasRight(Right::BACKUPACCOUNT)) {
                $this->elementStart('li');
                $this->element('a',
                               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 468 469 470 471 472 473
                               _('Backup account'));
                $this->elementEnd('li');
            }
            if ($user->hasRight(Right::DELETEACCOUNT)) {
                $this->elementStart('li');
                $this->element('a',
                               array('href' => common_local_url('deleteaccount')),
474
                               // TRANS: Option in profile settings to delete the account of the currently logged in user.
475 476 477 478 479 480 481
                               _('Delete account'));
                $this->elementEnd('li');
            }
            if ($user->hasRight(Right::RESTOREACCOUNT)) {
                $this->elementStart('li');
                $this->element('a',
                               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 484 485 486
                               _('Restore account'));
                $this->elementEnd('li');
            }
            Event::handle('EndProfileSettingsActions', array($this));
487
        }
488
        $this->elementEnd('ul');
489
        $this->elementEnd('div');
490
        $this->elementEnd('div');
491
    }
492
}