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

profilesettings.php 18.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

50
class ProfilesettingsAction extends AccountSettingsAction
51
{
52 53 54 55 56
    /**
     * Title of the page
     *
     * @return string Title of the page
     */
Evan Prodromou's avatar
Evan Prodromou committed
57

58
    function title()
59
    {
60
        // TRANS: Page title for profile settings.
61
        return _('Profile settings');
62 63
    }

64 65 66 67 68
    /**
     * Instructions for use
     *
     * @return instructions for use
     */
69

70
    function getInstructions()
71
    {
72
        // TRANS: Usage instructions for profile settings.
73
        return _('You can update your personal profile info here '.
Evan Prodromou's avatar
Evan Prodromou committed
74
                 'so people know more about you.');
75 76
    }

77 78 79 80 81 82
    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('nickname');
    }

83 84 85 86 87 88 89
    /**
     * Content area of the page
     *
     * Shows a form for uploading an avatar.
     *
     * @return void
     */
90

91 92
    function showContent()
    {
93 94 95
        $user = common_current_user();
        $profile = $user->getProfile();

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

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

sarven's avatar
sarven committed
203
        $this->elementEnd('fieldset');
204
        $this->elementEnd('form');
205 206
    }

207 208 209 210 211 212 213 214
    /**
     * Handle a post
     *
     * Validate input and save changes. Reload the form with a success
     * or error message.
     *
     * @return void
     */
215

216
    function handlePost()
217
    {
218
        // CSRF protection
219 220 221
        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
            $this->showForm(_('There was a problem with your session token. '.
Evan Prodromou's avatar
Evan Prodromou committed
222
                              'Try again, please.'));
223
            return;
224 225
        }

226 227
        if (Event::handle('StartProfileSaveForm', array($this))) {

228 229 230 231 232 233 234
            try {
                $nickname = Nickname::normalize($this->trimmed('nickname'));
            } catch (NicknameException $e) {
                $this->showForm($e->getMessage());
                return;
            }

235 236 237 238 239 240 241 242 243 244
            $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
245
            if (!User::allowed_nickname($nickname)) {
246
                // TRANS: Validation error in form for profile settings.
247 248 249
                $this->showForm(_('Not a valid nickname.'));
                return;
            } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
Evan Prodromou's avatar
Evan Prodromou committed
250
                       !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
251
                // TRANS: Validation error in form for profile settings.
252 253 254
                $this->showForm(_('Homepage is not a valid URL.'));
                return;
            } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
255
                // TRANS: Validation error in form for profile settings.
256
                $this->showForm(_('Full name is too long (maximum 255 characters).'));
257
                return;
258
            } else if (Profile::bioTooLong($bio)) {
259 260 261
                // 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).
262 263
                $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
                                           'Bio is too long (maximum %d characters).',
264
                                           Profile::maxBio()),
265
                                        Profile::maxBio()));
266 267
                return;
            } else if (!is_null($location) && mb_strlen($location) > 255) {
268
                // TRANS: Validation error in form for profile settings.
269
                $this->showForm(_('Location is too long (maximum 255 characters).'));
270 271
                return;
            }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
272
                // TRANS: Validation error in form for profile settings.
273 274 275
                $this->showForm(_('Timezone not selected.'));
                return;
            } else if ($this->nicknameExists($nickname)) {
276
                // TRANS: Validation error in form for profile settings.
277 278 279
                $this->showForm(_('Nickname already in use. Try another one.'));
                return;
            } else if (!is_null($language) && strlen($language) > 50) {
280
                // TRANS: Validation error in form for profile settings.
281
                $this->showForm(_('Language is too long (maximum 50 characters).'));
282 283
                return;
            }
284

285 286 287 288 289
            if ($tagstring) {
                $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
            } else {
                $tags = array();
            }
290

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

300
            $user = common_current_user();
301

302
            $user->query('BEGIN');
303

304
            if ($user->nickname != $nickname ||
Evan Prodromou's avatar
Evan Prodromou committed
305 306
                $user->language != $language ||
                $user->timezone != $timezone) {
307

308
                common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
Evan Prodromou's avatar
Evan Prodromou committed
309
                             __FILE__);
310
                common_debug('Updating user language from ' . $user->language . ' to ' . $language,
Evan Prodromou's avatar
Evan Prodromou committed
311
                             __FILE__);
312
                common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
Evan Prodromou's avatar
Evan Prodromou committed
313
                             __FILE__);
314

315
                $original = clone($user);
316

317 318 319
                $user->nickname = $nickname;
                $user->language = $language;
                $user->timezone = $timezone;
320

321
                $result = $user->updateKeys($original);
322

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

Evan Prodromou's avatar
Evan Prodromou committed
338
            // XXX: XOR
339
            if ($user->autosubscribe ^ $autosubscribe) {
340

341
                $original = clone($user);
342

343
                $user->autosubscribe = $autosubscribe;
344

345
                $result = $user->update($original);
346

347 348
                if ($result === false) {
                    common_log_db_error($user, 'UPDATE', __FILE__);
349 350
                    // TRANS: Server error thrown when user profile settings could not be updated to
                    // TRANS: automatically subscribe to any subscriber.
351 352 353
                    $this->serverError(_('Couldn\'t update user for autosubscribe.'));
                    return;
                }
354 355
            }

356
            $profile = $user->getProfile();
357

358
            $orig_profile = clone($profile);
359

360 361 362 363 364
            $profile->nickname = $user->nickname;
            $profile->fullname = $fullname;
            $profile->homepage = $homepage;
            $profile->bio = $bio;
            $profile->location = $location;
365 366 367

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

368 369 370 371 372 373
            if (empty($loc)) {
                $profile->lat         = null;
                $profile->lon         = null;
                $profile->location_id = null;
                $profile->location_ns = null;
            } else {
374 375 376 377 378 379
                $profile->lat         = $loc->lat;
                $profile->lon         = $loc->lon;
                $profile->location_id = $loc->location_id;
                $profile->location_ns = $loc->location_ns;
            }

380
            $profile->profileurl = common_profile_url($nickname);
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
            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__);
408
                    // TRANS: Server error thrown when user profile location preference settings could not be updated.
409 410 411 412 413
                    $this->serverError(_('Couldn\'t save location prefs.'));
                    return;
                }
            }

414 415
            common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
            common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
416

417
            $result = $profile->update($orig_profile);
418

419
            if ($result === false) {
420
                common_log_db_error($profile, 'UPDATE', __FILE__);
421
                // TRANS: Server error thrown when user profile settings could not be saved.
422 423 424
                $this->serverError(_('Couldn\'t save profile.'));
                return;
            }
425

426 427
            // Set the user tags
            $result = $user->setSelfTags($tags);
428

429
            if (!$result) {
430
                // TRANS: Server error thrown when user profile settings tags could not be saved.
431 432 433
                $this->serverError(_('Couldn\'t save tags.'));
                return;
            }
434

435 436 437
            $user->query('COMMIT');
            Event::handle('EndProfileSaveForm', array($this));
            common_broadcast_profile($profile);
438

439
            // TRANS: Confirmation shown when user profile settings are saved.
440
            $this->showForm(_('Settings saved.'), true);
441

442
        }
443 444
    }

445
    function nicknameExists($nickname)
446
    {
447 448 449
        $user = common_current_user();
        $other = User::staticGet('nickname', $nickname);
        if (!$other) {
Evan Prodromou's avatar
Evan Prodromou committed
450
            return false;
451 452 453 454
        } else {
            return $other->id != $user->id;
        }
    }
455
}