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();
38 common_element('h2', NULL, _('Delete my account'));
39 $this->show_delete_form();
43 function handle_post() {
47 $token = $this->trimmed('token');
48 if (!$token || $token != common_session_token()) {
49 $this->show_form(_('There was a problem with your session token. Try again, please.'));
53 if ($this->arg('save')) {
54 $this->save_profile();
55 } else if ($this->arg('upload')) {
56 $this->upload_avatar();
57 } else if ($this->arg('changepass')) {
58 $this->change_password();
59 } else if ($this->arg('deleteaccount')) {
60 $this->delete_account();
65 function show_settings_form() {
67 $user = common_current_user();
68 $profile = $user->getProfile();
70 common_element_start('form', array('method' => 'POST',
71 'id' => 'profilesettings',
73 common_local_url('profilesettings')));
74 common_hidden('token', common_session_token());
76 # too much common patterns here... abstractable?
78 common_input('nickname', _('Nickname'),
79 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
80 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
81 common_input('fullname', _('Full name'),
82 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
83 common_input('homepage', _('Homepage'),
84 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
85 _('URL of your homepage, blog, or profile on another site'));
86 common_textarea('bio', _('Bio'),
87 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
88 _('Describe yourself and your interests in 140 chars'));
89 common_input('location', _('Location'),
90 ($this->arg('location')) ? $this->arg('location') : $profile->location,
91 _('Where you are, like "City, State (or Region), Country"'));
92 common_input('tags', _('Tags'),
93 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
94 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
96 $language = common_language();
97 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
98 $timezone = common_timezone();
100 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
103 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
105 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
106 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
108 common_submit('save', _('Save'));
110 common_element_end('form');
115 function show_avatar_form() {
117 $user = common_current_user();
118 $profile = $user->getProfile();
121 common_log_db_error($user, 'SELECT', __FILE__);
122 $this->server_error(_('User without matching profile'));
126 $original = $profile->getOriginalAvatar();
129 common_element_start('form', array('enctype' => 'multipart/form-data',
133 common_local_url('profilesettings')));
134 common_hidden('token', common_session_token());
137 common_element('img', array('src' => $original->url,
138 'class' => 'avatar original',
139 'width' => $original->width,
140 'height' => $original->height,
141 'alt' => $user->nickname));
144 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
147 common_element('img', array('src' => $avatar->url,
148 'class' => 'avatar profile',
149 'width' => AVATAR_PROFILE_SIZE,
150 'height' => AVATAR_PROFILE_SIZE,
151 'alt' => $user->nickname));
155 common_element('input', array('name' => 'MAX_FILE_SIZE',
157 'id' => 'MAX_FILE_SIZE',
158 'value' => MAX_AVATAR_SIZE));
160 common_element_start('p');
163 common_element('input', array('name' => 'avatarfile',
165 'id' => 'avatarfile'));
166 common_element_end('p');
168 common_submit('upload', _('Upload'));
169 common_element_end('form');
173 function show_password_form() {
175 $user = common_current_user();
176 common_element_start('form', array('method' => 'POST',
179 common_local_url('profilesettings')));
181 common_hidden('token', common_session_token());
183 # Users who logged in with OpenID won't have a pwd
184 if ($user->password) {
185 common_password('oldpassword', _('Old password'));
187 common_password('newpassword', _('New password'),
188 _('6 or more characters'));
189 common_password('confirm', _('Confirm'),
190 _('same as password above'));
191 common_submit('changepass', _('Change'));
192 common_element_end('form');
196 function show_feeds_list($feeds) {
197 common_element_start('div', array('class' => 'feedsdel'));
198 common_element('p', null, 'Feeds:');
199 common_element_start('ul', array('class' => 'xoxo'));
201 foreach ($feeds as $key => $value) {
202 $this->common_feed_item($feeds[$key]);
204 common_element_end('ul');
205 common_element_end('div');
208 function common_feed_item($feed) {
209 $user = common_current_user();
210 $nickname = $user->nickname;
212 switch($feed['item']) {
213 case 'notices': default:
214 $feed_classname = $feed['type'];
215 $feed_mimetype = "application/".$feed['type']."+xml";
216 $feed_title = "$nickname's ".$feed['version']." notice feed";
217 $feed['textContent'] = "RSS";
221 $feed_classname = "foaf";
222 $feed_mimetype = "application/".$feed['type']."+xml";
223 $feed_title = "$nickname's FOAF file";
224 $feed['textContent'] = "FOAF";
227 common_element_start('li');
228 common_element('a', array('href' => $feed['href'],
229 'class' => $feed_classname,
230 'type' => $feed_mimetype,
231 'title' => $feed_title),
232 $feed['textContent']);
233 common_element_end('li');
236 function show_delete_form() {
237 $user = common_current_user();
238 $notices = DB_DataObject::factory('notice');
239 $notices->profile_id = $user->id;
240 $notice_count = (int) $notices->count();
242 common_element_start('form', array('method' => 'POST',
245 common_local_url('profilesettings')));
247 common_hidden('token', common_session_token());
248 common_element('p', null, "You can copy your notices and contacts by saving the two links belowxbefore deleting your account. Be careful, this operation cannot be undone.");
251 $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('limit' => $notice_count, 'nickname' => $user->nickname)),
253 'version' => 'RSS 1.0',
254 'item' => 'notices'),
255 1=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
260 common_submit('deleteaccount', _('Delete my account'));
261 common_element_end('form');
264 function save_profile() {
265 $nickname = $this->trimmed('nickname');
266 $fullname = $this->trimmed('fullname');
267 $homepage = $this->trimmed('homepage');
268 $bio = $this->trimmed('bio');
269 $location = $this->trimmed('location');
270 $autosubscribe = $this->boolean('autosubscribe');
271 $language = $this->trimmed('language');
272 $timezone = $this->trimmed('timezone');
273 $tagstring = $this->trimmed('tags');
277 if (!Validate::string($nickname, array('min_length' => 1,
279 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
280 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
282 } else if (!User::allowed_nickname($nickname)) {
283 $this->show_form(_('Not a valid nickname.'));
285 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
286 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
287 $this->show_form(_('Homepage is not a valid URL.'));
289 } else if (!is_null($fullname) && strlen($fullname) > 255) {
290 $this->show_form(_('Full name is too long (max 255 chars).'));
292 } else if (!is_null($bio) && strlen($bio) > 140) {
293 $this->show_form(_('Bio is too long (max 140 chars).'));
295 } else if (!is_null($location) && strlen($location) > 255) {
296 $this->show_form(_('Location is too long (max 255 chars).'));
298 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
299 $this->show_form(_('Timezone not selected.'));
301 } else if ($this->nickname_exists($nickname)) {
302 $this->show_form(_('Nickname already in use. Try another one.'));
304 } else if (!is_null($language) && strlen($language) > 50) {
305 $this->show_form(_('Language is too long (max 50 chars).'));
310 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
315 foreach ($tags as $tag) {
316 if (!common_valid_profile_tag($tag)) {
317 $this->show_form(sprintf(_('Invalid tag: "%s"'), $tag));
322 $user = common_current_user();
324 $user->query('BEGIN');
326 if ($user->nickname != $nickname ||
327 $user->language != $language ||
328 $user->timezone != $timezone) {
330 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
332 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
334 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
337 $original = clone($user);
339 $user->nickname = $nickname;
340 $user->language = $language;
341 $user->timezone = $timezone;
343 $result = $user->updateKeys($original);
345 if ($result === FALSE) {
346 common_log_db_error($user, 'UPDATE', __FILE__);
347 common_server_error(_('Couldn\'t update user.'));
350 # Re-initialize language environment if it changed
351 common_init_language();
357 if ($user->autosubscribe ^ $autosubscribe) {
359 $original = clone($user);
361 $user->autosubscribe = $autosubscribe;
363 $result = $user->update($original);
365 if ($result === FALSE) {
366 common_log_db_error($user, 'UPDATE', __FILE__);
367 common_server_error(_('Couldn\'t update user for autosubscribe.'));
372 $profile = $user->getProfile();
374 $orig_profile = clone($profile);
376 $profile->nickname = $user->nickname;
377 $profile->fullname = $fullname;
378 $profile->homepage = $homepage;
379 $profile->bio = $bio;
380 $profile->location = $location;
381 $profile->profileurl = common_profile_url($nickname);
383 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
384 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
386 $result = $profile->update($orig_profile);
389 common_log_db_error($profile, 'UPDATE', __FILE__);
390 common_server_error(_('Couldn\'t save profile.'));
396 $result = $user->setSelfTags($tags);
399 common_server_error(_('Couldn\'t save tags.'));
403 $user->query('COMMIT');
405 common_broadcast_profile($profile);
407 $this->show_form(_('Settings saved.'), TRUE);
411 function upload_avatar() {
412 switch ($_FILES['avatarfile']['error']) {
413 case UPLOAD_ERR_OK: # success, jump out
415 case UPLOAD_ERR_INI_SIZE:
416 case UPLOAD_ERR_FORM_SIZE:
417 $this->show_form(_('That file is too big.'));
419 case UPLOAD_ERR_PARTIAL:
420 @unlink($_FILES['avatarfile']['tmp_name']);
421 $this->show_form(_('Partial upload.'));
424 $this->show_form(_('System error uploading file.'));
428 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
431 @unlink($_FILES['avatarfile']['tmp_name']);
432 $this->show_form(_('Not an image or corrupt file.'));
442 $this->show_form(_('Unsupported image file format.'));
446 $user = common_current_user();
447 $profile = $user->getProfile();
449 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
450 $this->show_form(_('Avatar updated.'), true);
452 $this->show_form(_('Failed updating avatar.'));
455 @unlink($_FILES['avatarfile']['tmp_name']);
458 function nickname_exists($nickname) {
459 $user = common_current_user();
460 $other = User::staticGet('nickname', $nickname);
464 return $other->id != $user->id;
468 function change_password() {
470 $user = common_current_user();
471 assert(!is_null($user)); # should already be checked
475 $newpassword = $this->arg('newpassword');
476 $confirm = $this->arg('confirm');
477 $token = $this->arg('token');
479 if (0 != strcmp($newpassword, $confirm)) {
480 $this->show_form(_('Passwords don\'t match.'));
484 if ($user->password) {
485 $oldpassword = $this->arg('oldpassword');
487 if (!common_check_user($user->nickname, $oldpassword)) {
488 $this->show_form(_('Incorrect old password'));
493 $original = clone($user);
495 $user->password = common_munge_password($newpassword, $user->id);
497 $val = $user->validate();
499 $this->show_form(_('Error saving user; invalid.'));
503 if (!$user->update($original)) {
504 common_server_error(_('Can\'t save new password.'));
508 $this->show_form(_('Password saved.'), true);
511 function delete_account() {
513 $user = common_current_user();
514 assert(!is_null($user)); # should already be checked
516 // delete avatar (profile_id and filename)
517 // delete fave (user_id)
518 // delete message (from_profile, to_profile)
519 // delete notice (profile_id) (also delete from notice_source and notice_tag)
520 // delete notice_inbox (user_id)
521 // delete subscription (subscriber, subscribed)
523 // delete user_openid (user_id)
524 // delete profile (id)
525 // delete tags tables (to verify)
526 // delete all the users notices
528 $this->show_form(_('Your account has been deleted.'), true);