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()
28 return _('You can update your personal profile info here '.
29 'so people know more about you.');
32 function show_form($msg=null, $success=false)
34 $this->form_header(_('Profile settings'), $msg, $success);
35 $this->show_settings_form();
36 common_element('h2', null, _('Avatar'));
37 $this->show_avatar_form();
38 common_element('h2', null, _('Change password'));
39 $this->show_password_form();
40 // common_element('h2', null, _('Delete my account'));
41 // $this->show_delete_form();
45 function handle_post()
50 $token = $this->trimmed('token');
51 if (!$token || $token != common_session_token()) {
52 $this->show_form(_('There was a problem with your session token. Try again, please.'));
56 if ($this->arg('save')) {
57 $this->save_profile();
58 } else if ($this->arg('upload')) {
59 $this->upload_avatar();
60 } else if ($this->arg('changepass')) {
61 $this->change_password();
66 function show_settings_form()
69 $user = common_current_user();
70 $profile = $user->getProfile();
72 common_element_start('form', array('method' => 'POST',
73 'id' => 'profilesettings',
75 common_local_url('profilesettings')));
76 common_hidden('token', common_session_token());
78 # too much common patterns here... abstractable?
80 common_input('nickname', _('Nickname'),
81 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
82 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
83 common_input('fullname', _('Full name'),
84 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
85 common_input('homepage', _('Homepage'),
86 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
87 _('URL of your homepage, blog, or profile on another site'));
88 common_textarea('bio', _('Bio'),
89 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
90 _('Describe yourself and your interests in 140 chars'));
91 common_input('location', _('Location'),
92 ($this->arg('location')) ? $this->arg('location') : $profile->location,
93 _('Where you are, like "City, State (or Region), Country"'));
94 common_input('tags', _('Tags'),
95 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
96 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
98 $language = common_language();
99 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), true, $language);
100 $timezone = common_timezone();
101 $timezones = array();
102 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
105 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), true, $timezone);
107 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
108 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
110 common_submit('save', _('Save'));
112 common_element_end('form');
117 function show_avatar_form()
120 $user = common_current_user();
121 $profile = $user->getProfile();
124 common_log_db_error($user, 'SELECT', __FILE__);
125 $this->server_error(_('User without matching profile'));
129 $original = $profile->getOriginalAvatar();
132 common_element_start('form', array('enctype' => 'multipart/form-data',
136 common_local_url('profilesettings')));
137 common_hidden('token', common_session_token());
140 common_element('img', array('src' => $original->url,
141 'class' => 'avatar original',
142 'width' => $original->width,
143 'height' => $original->height,
144 'alt' => $user->nickname));
147 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
150 common_element('img', array('src' => $avatar->url,
151 'class' => 'avatar profile',
152 'width' => AVATAR_PROFILE_SIZE,
153 'height' => AVATAR_PROFILE_SIZE,
154 'alt' => $user->nickname));
158 common_element('input', array('name' => 'MAX_FILE_SIZE',
160 'id' => 'MAX_FILE_SIZE',
161 'value' => MAX_AVATAR_SIZE));
163 common_element_start('p');
166 common_element('input', array('name' => 'avatarfile',
168 'id' => 'avatarfile'));
169 common_element_end('p');
171 common_submit('upload', _('Upload'));
172 common_element_end('form');
176 function show_password_form()
179 $user = common_current_user();
180 common_element_start('form', array('method' => 'POST',
183 common_local_url('profilesettings')));
185 common_hidden('token', common_session_token());
187 # Users who logged in with OpenID won't have a pwd
188 if ($user->password) {
189 common_password('oldpassword', _('Old password'));
191 common_password('newpassword', _('New password'),
192 _('6 or more characters'));
193 common_password('confirm', _('Confirm'),
194 _('same as password above'));
195 common_submit('changepass', _('Change'));
196 common_element_end('form');
199 function save_profile()
201 $nickname = $this->trimmed('nickname');
202 $fullname = $this->trimmed('fullname');
203 $homepage = $this->trimmed('homepage');
204 $bio = $this->trimmed('bio');
205 $location = $this->trimmed('location');
206 $autosubscribe = $this->boolean('autosubscribe');
207 $language = $this->trimmed('language');
208 $timezone = $this->trimmed('timezone');
209 $tagstring = $this->trimmed('tags');
213 if (!Validate::string($nickname, array('min_length' => 1,
215 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
216 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
218 } else if (!User::allowed_nickname($nickname)) {
219 $this->show_form(_('Not a valid nickname.'));
221 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
222 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
223 $this->show_form(_('Homepage is not a valid URL.'));
225 } else if (!is_null($fullname) && strlen($fullname) > 255) {
226 $this->show_form(_('Full name is too long (max 255 chars).'));
228 } else if (!is_null($bio) && strlen($bio) > 140) {
229 $this->show_form(_('Bio is too long (max 140 chars).'));
231 } else if (!is_null($location) && strlen($location) > 255) {
232 $this->show_form(_('Location is too long (max 255 chars).'));
234 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
235 $this->show_form(_('Timezone not selected.'));
237 } else if ($this->nickname_exists($nickname)) {
238 $this->show_form(_('Nickname already in use. Try another one.'));
240 } else if (!is_null($language) && strlen($language) > 50) {
241 $this->show_form(_('Language is too long (max 50 chars).'));
246 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
251 foreach ($tags as $tag) {
252 if (!common_valid_profile_tag($tag)) {
253 $this->show_form(sprintf(_('Invalid tag: "%s"'), $tag));
258 $user = common_current_user();
260 $user->query('BEGIN');
262 if ($user->nickname != $nickname ||
263 $user->language != $language ||
264 $user->timezone != $timezone) {
266 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
268 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
270 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
273 $original = clone($user);
275 $user->nickname = $nickname;
276 $user->language = $language;
277 $user->timezone = $timezone;
279 $result = $user->updateKeys($original);
281 if ($result === false) {
282 common_log_db_error($user, 'UPDATE', __FILE__);
283 common_server_error(_('Couldn\'t update user.'));
286 # Re-initialize language environment if it changed
287 common_init_language();
293 if ($user->autosubscribe ^ $autosubscribe) {
295 $original = clone($user);
297 $user->autosubscribe = $autosubscribe;
299 $result = $user->update($original);
301 if ($result === false) {
302 common_log_db_error($user, 'UPDATE', __FILE__);
303 common_server_error(_('Couldn\'t update user for autosubscribe.'));
308 $profile = $user->getProfile();
310 $orig_profile = clone($profile);
312 $profile->nickname = $user->nickname;
313 $profile->fullname = $fullname;
314 $profile->homepage = $homepage;
315 $profile->bio = $bio;
316 $profile->location = $location;
317 $profile->profileurl = common_profile_url($nickname);
319 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
320 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
322 $result = $profile->update($orig_profile);
325 common_log_db_error($profile, 'UPDATE', __FILE__);
326 common_server_error(_('Couldn\'t save profile.'));
332 $result = $user->setSelfTags($tags);
335 common_server_error(_('Couldn\'t save tags.'));
339 $user->query('COMMIT');
341 common_broadcast_profile($profile);
343 $this->show_form(_('Settings saved.'), true);
347 function upload_avatar()
349 switch ($_FILES['avatarfile']['error']) {
350 case UPLOAD_ERR_OK: # success, jump out
352 case UPLOAD_ERR_INI_SIZE:
353 case UPLOAD_ERR_FORM_SIZE:
354 $this->show_form(_('That file is too big.'));
356 case UPLOAD_ERR_PARTIAL:
357 @unlink($_FILES['avatarfile']['tmp_name']);
358 $this->show_form(_('Partial upload.'));
361 $this->show_form(_('System error uploading file.'));
365 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
368 @unlink($_FILES['avatarfile']['tmp_name']);
369 $this->show_form(_('Not an image or corrupt file.'));
379 $this->show_form(_('Unsupported image file format.'));
383 $user = common_current_user();
384 $profile = $user->getProfile();
386 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
387 $this->show_form(_('Avatar updated.'), true);
389 $this->show_form(_('Failed updating avatar.'));
392 @unlink($_FILES['avatarfile']['tmp_name']);
395 function nickname_exists($nickname)
397 $user = common_current_user();
398 $other = User::staticGet('nickname', $nickname);
402 return $other->id != $user->id;
406 function change_password()
409 $user = common_current_user();
410 assert(!is_null($user)); # should already be checked
414 $newpassword = $this->arg('newpassword');
415 $confirm = $this->arg('confirm');
416 $token = $this->arg('token');
418 if (0 != strcmp($newpassword, $confirm)) {
419 $this->show_form(_('Passwords don\'t match.'));
423 if ($user->password) {
424 $oldpassword = $this->arg('oldpassword');
426 if (!common_check_user($user->nickname, $oldpassword)) {
427 $this->show_form(_('Incorrect old password'));
432 $original = clone($user);
434 $user->password = common_munge_password($newpassword, $user->id);
436 $val = $user->validate();
438 $this->show_form(_('Error saving user; invalid.'));
442 if (!$user->update($original)) {
443 common_server_error(_('Can\'t save new password.'));
447 $this->show_form(_('Password saved.'), true);