3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
24 class ProfilesettingsAction extends SettingsAction {
26 function get_instructions() {
27 return _('You can update your personal profile info here '.
28 'so people know more about you.');
31 function show_form($msg=NULL, $success=false) {
32 $user = common_current_user();
33 $profile = $user->getProfile();
34 $this->form_header(_('Profile settings'), $msg, $success);
36 common_element_start('form', array('method' => 'post',
37 'id' => 'profilesettings',
39 common_local_url('profilesettings')));
40 # too much common patterns here... abstractable?
41 common_input('nickname', _('Nickname'),
42 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
43 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
44 common_input('fullname', _('Full name'),
45 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
46 common_input('email', _('Email address'),
47 ($this->arg('email')) ? $this->arg('email') : $user->email,
48 _('Used only for updates, announcements, and password recovery'));
49 common_input('homepage', _('Homepage'),
50 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
51 _('URL of your homepage, blog, or profile on another site'));
52 common_textarea('bio', _('Bio'),
53 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
54 _('Describe yourself and your interests in 140 chars'));
55 common_input('location', _('Location'),
56 ($this->arg('location')) ? $this->arg('location') : $profile->location,
57 _('Where you are, like "City, State (or Region), Country"'));
58 common_submit('submit', _('Save'));
59 common_element_end('form');
63 function handle_post() {
65 $nickname = $this->trimmed('nickname');
66 $fullname = $this->trimmed('fullname');
67 $email = $this->trimmed('email');
68 $homepage = $this->trimmed('homepage');
69 $bio = $this->trimmed('bio');
70 $location = $this->trimmed('location');
74 if ($email && !Validate::email($email, true)) {
75 $this->show_form(_('Not a valid email address.'));
77 } else if (!Validate::string($nickname, array('min_length' => 1,
79 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
80 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
82 } else if (!User::allowed_nickname($nickname)) {
83 $this->show_form(_('Not a valid nickname.'));
85 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
86 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
87 $this->show_form(_('Homepage is not a valid URL.'));
89 } else if (!is_null($fullname) && strlen($fullname) > 255) {
90 $this->show_form(_('Full name is too long (max 255 chars).'));
92 } else if (!is_null($bio) && strlen($bio) > 140) {
93 $this->show_form(_('Bio is too long (max 140 chars).'));
95 } else if (!is_null($location) && strlen($location) > 255) {
96 $this->show_form(_('Location is too long (max 255 chars).'));
98 } else if ($this->nickname_exists($nickname)) {
99 $this->show_form(_('Nickname already in use. Try another one.'));
101 } else if ($this->email_exists($email)) {
102 $this->show_form(_('Email address already exists.'));
106 $user = common_current_user();
108 $user->query('BEGIN');
110 if ($user->nickname != $nickname) {
112 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
115 $original = clone($user);
117 $user->nickname = $nickname;
119 $result = $user->updateKeys($original);
121 if ($result === FALSE) {
122 common_log_db_error($user, 'UPDATE', __FILE__);
123 common_server_error(_('Couldn\'t update user.'));
128 if ($user->email != $email) {
130 common_debug('Updating user email from ' . $user->email . ' to ' . $email,
133 # We don't update email directly; it gets done by confirmemail
135 $confirm = new Confirm_address();
137 $confirm->code = common_confirmation_code(128);
138 $confirm->user_id = $user->id;
139 $confirm->address = $email;
140 $confirm->address_type = 'email';
142 $result = $confirm->insert();
145 common_log_db_error($confirm, 'INSERT', __FILE__);
146 common_server_error(_('Couldn\'t confirm email.'));
150 # XXX: try not to do this in the middle of a transaction
152 mail_confirm_address($confirm->code,
157 $profile = $user->getProfile();
159 $orig_profile = clone($profile);
161 $profile->nickname = $user->nickname;
162 $profile->fullname = $fullname;
163 $profile->homepage = $homepage;
164 $profile->bio = $bio;
165 $profile->location = $location;
166 $profile->profileurl = common_profile_url($nickname);
168 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
169 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
171 $result = $profile->update($orig_profile);
174 common_log_db_error($profile, 'UPDATE', __FILE__);
175 common_server_error(_('Couldn\'t save profile.'));
179 $user->query('COMMIT');
181 common_broadcast_profile($profile);
183 $this->show_form(_('Settings saved.'), TRUE);
186 function nickname_exists($nickname) {
187 $user = common_current_user();
188 $other = User::staticGet('nickname', $nickname);
192 return $other->id != $user->id;
196 function email_exists($email) {
197 $user = common_current_user();
198 $other = User::staticGet('email', $email);
202 return $other->id != $user->id;