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
27 function get_instructions()
29 return _('You can update your personal profile info here '.
30 'so people know more about you.');
33 function show_form($msg=null, $success=false)
35 $this->form_header(_('Profile settings'), $msg, $success);
36 $this->show_settings_form();
37 common_element('h2', null, _('Avatar'));
38 $this->show_avatar_form();
39 common_element('h2', null, _('Change password'));
40 $this->show_password_form();
41 // common_element('h2', null, _('Delete my account'));
42 // $this->show_delete_form();
46 function handle_post()
51 $token = $this->trimmed('token');
52 if (!$token || $token != common_session_token()) {
53 $this->show_form(_('There was a problem with your session token. Try again, please.'));
57 if ($this->arg('save')) {
58 $this->save_profile();
59 } else if ($this->arg('upload')) {
60 $this->upload_avatar();
61 } else if ($this->arg('changepass')) {
62 $this->change_password();
67 function show_settings_form()
70 $user = common_current_user();
71 $profile = $user->getProfile();
73 common_element_start('form', array('method' => 'POST',
74 'id' => 'profilesettings',
76 common_local_url('profilesettings')));
77 common_hidden('token', common_session_token());
79 # too much common patterns here... abstractable?
81 common_input('nickname', _('Nickname'),
82 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
83 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
84 common_input('fullname', _('Full name'),
85 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
86 common_input('homepage', _('Homepage'),
87 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
88 _('URL of your homepage, blog, or profile on another site'));
89 common_textarea('bio', _('Bio'),
90 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
91 _('Describe yourself and your interests in 140 chars'));
92 common_input('location', _('Location'),
93 ($this->arg('location')) ? $this->arg('location') : $profile->location,
94 _('Where you are, like "City, State (or Region), Country"'));
95 common_input('tags', _('Tags'),
96 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
97 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
99 $language = common_language();
100 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), true, $language);
101 $timezone = common_timezone();
102 $timezones = array();
103 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
106 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), true, $timezone);
108 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
109 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
111 common_submit('save', _('Save'));
113 common_element_end('form');
118 function show_avatar_form()
121 $user = common_current_user();
122 $profile = $user->getProfile();
125 common_log_db_error($user, 'SELECT', __FILE__);
126 $this->server_error(_('User without matching profile'));
130 $original = $profile->getOriginalAvatar();
133 common_element_start('form', array('enctype' => 'multipart/form-data',
137 common_local_url('profilesettings')));
138 common_hidden('token', common_session_token());
141 common_element('img', array('src' => $original->url,
142 'class' => 'avatar original',
143 'width' => $original->width,
144 'height' => $original->height,
145 'alt' => $user->nickname));
148 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
151 common_element('img', array('src' => $avatar->url,
152 'class' => 'avatar profile',
153 'width' => AVATAR_PROFILE_SIZE,
154 'height' => AVATAR_PROFILE_SIZE,
155 'alt' => $user->nickname));
159 common_element('input', array('name' => 'MAX_FILE_SIZE',
161 'id' => 'MAX_FILE_SIZE',
162 'value' => MAX_AVATAR_SIZE));
164 common_element_start('p');
167 common_element('input', array('name' => 'avatarfile',
169 'id' => 'avatarfile'));
170 common_element_end('p');
172 common_submit('upload', _('Upload'));
173 common_element_end('form');
177 function show_password_form()
180 $user = common_current_user();
181 common_element_start('form', array('method' => 'POST',
184 common_local_url('profilesettings')));
186 common_hidden('token', common_session_token());
188 # Users who logged in with OpenID won't have a pwd
189 if ($user->password) {
190 common_password('oldpassword', _('Old password'));
192 common_password('newpassword', _('New password'),
193 _('6 or more characters'));
194 common_password('confirm', _('Confirm'),
195 _('same as password above'));
196 common_submit('changepass', _('Change'));
197 common_element_end('form');
200 function save_profile()
202 $nickname = $this->trimmed('nickname');
203 $fullname = $this->trimmed('fullname');
204 $homepage = $this->trimmed('homepage');
205 $bio = $this->trimmed('bio');
206 $location = $this->trimmed('location');
207 $autosubscribe = $this->boolean('autosubscribe');
208 $language = $this->trimmed('language');
209 $timezone = $this->trimmed('timezone');
210 $tagstring = $this->trimmed('tags');
214 if (!Validate::string($nickname, array('min_length' => 1,
216 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
217 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
219 } else if (!User::allowed_nickname($nickname)) {
220 $this->show_form(_('Not a valid nickname.'));
222 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
223 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
224 $this->show_form(_('Homepage is not a valid URL.'));
226 } else if (!is_null($fullname) && strlen($fullname) > 255) {
227 $this->show_form(_('Full name is too long (max 255 chars).'));
229 } else if (!is_null($bio) && strlen($bio) > 140) {
230 $this->show_form(_('Bio is too long (max 140 chars).'));
232 } else if (!is_null($location) && strlen($location) > 255) {
233 $this->show_form(_('Location is too long (max 255 chars).'));
235 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
236 $this->show_form(_('Timezone not selected.'));
238 } else if ($this->nickname_exists($nickname)) {
239 $this->show_form(_('Nickname already in use. Try another one.'));
241 } else if (!is_null($language) && strlen($language) > 50) {
242 $this->show_form(_('Language is too long (max 50 chars).'));
247 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
252 foreach ($tags as $tag) {
253 if (!common_valid_profile_tag($tag)) {
254 $this->show_form(sprintf(_('Invalid tag: "%s"'), $tag));
259 $user = common_current_user();
261 $user->query('BEGIN');
263 if ($user->nickname != $nickname ||
264 $user->language != $language ||
265 $user->timezone != $timezone) {
267 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
269 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
271 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
274 $original = clone($user);
276 $user->nickname = $nickname;
277 $user->language = $language;
278 $user->timezone = $timezone;
280 $result = $user->updateKeys($original);
282 if ($result === false) {
283 common_log_db_error($user, 'UPDATE', __FILE__);
284 common_server_error(_('Couldn\'t update user.'));
287 # Re-initialize language environment if it changed
288 common_init_language();
294 if ($user->autosubscribe ^ $autosubscribe) {
296 $original = clone($user);
298 $user->autosubscribe = $autosubscribe;
300 $result = $user->update($original);
302 if ($result === false) {
303 common_log_db_error($user, 'UPDATE', __FILE__);
304 common_server_error(_('Couldn\'t update user for autosubscribe.'));
309 $profile = $user->getProfile();
311 $orig_profile = clone($profile);
313 $profile->nickname = $user->nickname;
314 $profile->fullname = $fullname;
315 $profile->homepage = $homepage;
316 $profile->bio = $bio;
317 $profile->location = $location;
318 $profile->profileurl = common_profile_url($nickname);
320 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
321 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
323 $result = $profile->update($orig_profile);
326 common_log_db_error($profile, 'UPDATE', __FILE__);
327 common_server_error(_('Couldn\'t save profile.'));
333 $result = $user->setSelfTags($tags);
336 common_server_error(_('Couldn\'t save tags.'));
340 $user->query('COMMIT');
342 common_broadcast_profile($profile);
344 $this->show_form(_('Settings saved.'), true);
348 function upload_avatar()
350 switch ($_FILES['avatarfile']['error']) {
351 case UPLOAD_ERR_OK: # success, jump out
353 case UPLOAD_ERR_INI_SIZE:
354 case UPLOAD_ERR_FORM_SIZE:
355 $this->show_form(_('That file is too big.'));
357 case UPLOAD_ERR_PARTIAL:
358 @unlink($_FILES['avatarfile']['tmp_name']);
359 $this->show_form(_('Partial upload.'));
362 $this->show_form(_('System error uploading file.'));
366 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
369 @unlink($_FILES['avatarfile']['tmp_name']);
370 $this->show_form(_('Not an image or corrupt file.'));
380 $this->show_form(_('Unsupported image file format.'));
384 $user = common_current_user();
385 $profile = $user->getProfile();
387 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
388 $this->show_form(_('Avatar updated.'), true);
390 $this->show_form(_('Failed updating avatar.'));
393 @unlink($_FILES['avatarfile']['tmp_name']);
396 function nickname_exists($nickname)
398 $user = common_current_user();
399 $other = User::staticGet('nickname', $nickname);
403 return $other->id != $user->id;
407 function change_password()
410 $user = common_current_user();
411 assert(!is_null($user)); # should already be checked
415 $newpassword = $this->arg('newpassword');
416 $confirm = $this->arg('confirm');
417 $token = $this->arg('token');
419 if (0 != strcmp($newpassword, $confirm)) {
420 $this->show_form(_('Passwords don\'t match.'));
424 if ($user->password) {
425 $oldpassword = $this->arg('oldpassword');
427 if (!common_check_user($user->nickname, $oldpassword)) {
428 $this->show_form(_('Incorrect old password'));
433 $original = clone($user);
435 $user->password = common_munge_password($newpassword, $user->id);
437 $val = $user->validate();
439 $this->show_form(_('Error saving user; invalid.'));
443 if (!$user->update($original)) {
444 common_server_error(_('Can\'t save new password.'));
448 $this->show_form(_('Password saved.'), true);