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 $this->form_header(_('Profile settings'), $msg, $success);
33 $this->show_settings_form();
34 common_element('h2', NULL, _('Avatar'));
35 $this->show_avatar_form();
36 common_element('h2', NULL, _('Change password'));
37 $this->show_password_form();
41 function handle_post() {
45 $token = $this->trimmed('token');
46 if (!$token || $token != common_session_token()) {
47 $this->show_form(_('There was a problem with your session token. Try again, please.'));
51 if ($this->arg('save')) {
52 $this->save_profile();
53 } else if ($this->arg('upload')) {
54 $this->upload_avatar();
55 } else if ($this->arg('changepass')) {
56 $this->change_password();
60 function show_settings_form() {
62 $user = common_current_user();
63 $profile = $user->getProfile();
65 common_element_start('form', array('method' => 'POST',
66 'id' => 'profilesettings',
68 common_local_url('profilesettings')));
69 common_hidden('token', common_session_token());
73 # too much common patterns here... abstractable?
74 common_input('nickname', _('Nickname'),
75 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
76 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
77 common_input('fullname', _('Full name'),
78 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
79 common_input('homepage', _('Homepage'),
80 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
81 _('URL of your homepage, blog, or profile on another site'));
82 common_textarea('bio', _('Bio'),
83 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
84 _('Describe yourself and your interests in 140 chars'));
85 common_input('location', _('Location'),
86 ($this->arg('location')) ? $this->arg('location') : $profile->location,
87 _('Where you are, like "City, State (or Region), Country"'));
89 $language = common_language();
90 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
91 $timezone = common_timezone();
93 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
96 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
98 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
99 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
101 common_submit('save', _('Save'));
103 common_element_end('form');
108 function show_avatar_form() {
110 $user = common_current_user();
111 $profile = $user->getProfile();
114 common_log_db_error($user, 'SELECT', __FILE__);
115 $this->server_error(_('User without matching profile'));
119 $original = $profile->getOriginalAvatar();
122 common_element_start('form', array('enctype' => 'multipart/form-data',
126 common_local_url('profilesettings')));
127 common_hidden('token', common_session_token());
130 common_element('img', array('src' => $original->url,
131 'class' => 'avatar original',
132 'width' => $original->width,
133 'height' => $original->height,
134 'alt' => $user->nickname));
137 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
140 common_element('img', array('src' => $avatar->url,
141 'class' => 'avatar profile',
142 'width' => AVATAR_PROFILE_SIZE,
143 'height' => AVATAR_PROFILE_SIZE,
144 'alt' => $user->nickname));
148 common_element('input', array('name' => 'MAX_FILE_SIZE',
150 'id' => 'MAX_FILE_SIZE',
151 'value' => MAX_AVATAR_SIZE));
153 common_element_start('p');
156 common_element('input', array('name' => 'avatarfile',
158 'id' => 'avatarfile'));
159 common_element_end('p');
161 common_submit('upload', _('Upload'));
162 common_element_end('form');
166 function show_password_form() {
168 $user = common_current_user();
169 common_element_start('form', array('method' => 'POST',
172 common_local_url('profilesettings')));
174 common_hidden('token', common_session_token());
176 # Users who logged in with OpenID won't have a pwd
177 if ($user->password) {
178 common_password('oldpassword', _('Old password'));
180 common_password('newpassword', _('New password'),
181 _('6 or more characters'));
182 common_password('confirm', _('Confirm'),
183 _('same as password above'));
184 common_submit('changepass', _('Change'));
185 common_element_end('form');
188 function save_profile() {
189 $nickname = $this->trimmed('nickname');
190 $fullname = $this->trimmed('fullname');
191 $homepage = $this->trimmed('homepage');
192 $bio = $this->trimmed('bio');
193 $location = $this->trimmed('location');
194 $autosubscribe = $this->boolean('autosubscribe');
195 $language = $this->trimmed('language');
196 $timezone = $this->trimmed('timezone');
200 if (!Validate::string($nickname, array('min_length' => 1,
202 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
203 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
205 } else if (!User::allowed_nickname($nickname)) {
206 $this->show_form(_('Not a valid nickname.'));
208 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
209 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
210 $this->show_form(_('Homepage is not a valid URL.'));
212 } else if (!is_null($fullname) && strlen($fullname) > 255) {
213 $this->show_form(_('Full name is too long (max 255 chars).'));
215 } else if (!is_null($bio) && strlen($bio) > 140) {
216 $this->show_form(_('Bio is too long (max 140 chars).'));
218 } else if (!is_null($location) && strlen($location) > 255) {
219 $this->show_form(_('Location is too long (max 255 chars).'));
221 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
222 $this->show_form(_('Timezone not selected.'));
224 } else if ($this->nickname_exists($nickname)) {
225 $this->show_form(_('Nickname already in use. Try another one.'));
227 } else if (!is_null($language) && strlen($language) > 50) {
228 $this->show_form(_('Language is too long (max 50 chars).'));
231 $user = common_current_user();
233 $user->query('BEGIN');
235 if ($user->nickname != $nickname ||
236 $user->language != $language ||
237 $user->timezone != $timezone) {
239 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
241 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
243 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
246 $original = clone($user);
248 $user->nickname = $nickname;
249 $user->language = $language;
250 $user->timezone = $timezone;
252 $result = $user->updateKeys($original);
254 if ($result === FALSE) {
255 common_log_db_error($user, 'UPDATE', __FILE__);
256 common_server_error(_('Couldn\'t update user.'));
259 # Re-initialize language environment if it changed
260 common_init_language();
266 if ($user->autosubscribe ^ $autosubscribe) {
268 $original = clone($user);
270 $user->autosubscribe = $autosubscribe;
272 $result = $user->update($original);
274 if ($result === FALSE) {
275 common_log_db_error($user, 'UPDATE', __FILE__);
276 common_server_error(_('Couldn\'t update user for autosubscribe.'));
281 $profile = $user->getProfile();
283 $orig_profile = clone($profile);
285 $profile->nickname = $user->nickname;
286 $profile->fullname = $fullname;
287 $profile->homepage = $homepage;
288 $profile->bio = $bio;
289 $profile->location = $location;
290 $profile->profileurl = common_profile_url($nickname);
292 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
293 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
295 $result = $profile->update($orig_profile);
298 common_log_db_error($profile, 'UPDATE', __FILE__);
299 common_server_error(_('Couldn\'t save profile.'));
303 $user->query('COMMIT');
305 common_broadcast_profile($profile);
307 $this->show_form(_('Settings saved.'), TRUE);
311 function upload_avatar() {
312 switch ($_FILES['avatarfile']['error']) {
313 case UPLOAD_ERR_OK: # success, jump out
315 case UPLOAD_ERR_INI_SIZE:
316 case UPLOAD_ERR_FORM_SIZE:
317 $this->show_form(_('That file is too big.'));
319 case UPLOAD_ERR_PARTIAL:
320 @unlink($_FILES['avatarfile']['tmp_name']);
321 $this->show_form(_('Partial upload.'));
324 $this->show_form(_('System error uploading file.'));
328 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
331 @unlink($_FILES['avatarfile']['tmp_name']);
332 $this->show_form(_('Not an image or corrupt file.'));
342 $this->show_form(_('Unsupported image file format.'));
346 $user = common_current_user();
347 $profile = $user->getProfile();
349 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
350 $this->show_form(_('Avatar updated.'), true);
352 $this->show_form(_('Failed updating avatar.'));
355 @unlink($_FILES['avatarfile']['tmp_name']);
358 function nickname_exists($nickname) {
359 $user = common_current_user();
360 $other = User::staticGet('nickname', $nickname);
364 return $other->id != $user->id;
368 function change_password() {
370 $user = common_current_user();
371 assert(!is_null($user)); # should already be checked
375 $newpassword = $this->arg('newpassword');
376 $confirm = $this->arg('confirm');
377 $token = $this->arg('token');
379 if (0 != strcmp($newpassword, $confirm)) {
380 $this->show_form(_('Passwords don\'t match.'));
384 if ($user->password) {
385 $oldpassword = $this->arg('oldpassword');
387 if (!common_check_user($user->nickname, $oldpassword)) {
388 $this->show_form(_('Incorrect old password'));
393 $original = clone($user);
395 $user->password = common_munge_password($newpassword, $user->id);
397 $val = $user->validate();
399 $this->show_form(_('Error saving user; invalid.'));
403 if (!$user->update($original)) {
404 common_server_error(_('Can\'t save new password.'));
408 $this->show_form(_('Password saved.'), true);