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('h2', NULL, _('Avatar'));
38 $original = $profile->getOriginalAvatar();
41 common_element('img', array('src' => $original->url,
42 'class' => 'avatar original',
43 'width' => $original->width,
44 'height' => $original->height,
45 'alt' => $user->nickname));
48 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
51 common_element('img', array('src' => $avatar->url,
52 'class' => 'avatar profile',
53 'width' => AVATAR_PROFILE_SIZE,
54 'height' => AVATAR_PROFILE_SIZE,
55 'alt' => $user->nickname));
58 common_element_start('form', array('enctype' => 'multipart/form-data',
60 'id' => 'profilesettings',
62 common_local_url('profilesettings')));
63 common_hidden('token', common_session_token());
65 common_element('input', array('name' => 'MAX_FILE_SIZE',
67 'id' => 'MAX_FILE_SIZE',
68 'value' => MAX_AVATAR_SIZE));
69 common_element('input', array('name' => 'avatarfile',
71 'id' => 'avatarfile'));
72 common_submit('upload', _('Upload'));
73 common_element_end('form');
75 common_element_start('form', array('method' => 'POST',
76 'id' => 'profilesettings',
78 common_local_url('profilesettings')));
79 common_hidden('token', common_session_token());
81 common_element('h2', NULL, _('Profile Settings'));
84 # too much common patterns here... abstractable?
85 common_input('nickname', _('Nickname'),
86 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
87 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
88 common_input('fullname', _('Full name'),
89 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
90 common_input('homepage', _('Homepage'),
91 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
92 _('URL of your homepage, blog, or profile on another site'));
93 common_textarea('bio', _('Bio'),
94 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
95 _('Describe yourself and your interests in 140 chars'));
96 common_input('location', _('Location'),
97 ($this->arg('location')) ? $this->arg('location') : $profile->location,
98 _('Where you are, like "City, State (or Region), Country"'));
100 $language = common_language();
101 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
102 $timezone = common_timezone();
103 $timezones = array();
104 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
107 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
109 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
110 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
112 common_submit('save', _('Save'));
114 common_element_end('form');
115 common_show_footer();
118 function handle_post() {
122 $token = $this->trimmed('token');
123 if (!$token || $token != common_session_token()) {
124 $this->show_form(_('There was a problem with your session token. Try again, please.'));
128 if ($this->arg('save')) {
129 $this->save_profile();
130 } else if ($this->arg('upload')) {
131 $this->upload_avatar();
135 function save_profile() {
136 $nickname = $this->trimmed('nickname');
137 $fullname = $this->trimmed('fullname');
138 $homepage = $this->trimmed('homepage');
139 $bio = $this->trimmed('bio');
140 $location = $this->trimmed('location');
141 $autosubscribe = $this->boolean('autosubscribe');
142 $language = $this->trimmed('language');
143 $timezone = $this->trimmed('timezone');
147 if (!Validate::string($nickname, array('min_length' => 1,
149 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
150 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
152 } else if (!User::allowed_nickname($nickname)) {
153 $this->show_form(_('Not a valid nickname.'));
155 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
156 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
157 $this->show_form(_('Homepage is not a valid URL.'));
159 } else if (!is_null($fullname) && strlen($fullname) > 255) {
160 $this->show_form(_('Full name is too long (max 255 chars).'));
162 } else if (!is_null($bio) && strlen($bio) > 140) {
163 $this->show_form(_('Bio is too long (max 140 chars).'));
165 } else if (!is_null($location) && strlen($location) > 255) {
166 $this->show_form(_('Location is too long (max 255 chars).'));
168 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
169 $this->show_form(_('Timezone not selected.'));
171 } else if ($this->nickname_exists($nickname)) {
172 $this->show_form(_('Nickname already in use. Try another one.'));
174 } else if (!is_null($language) && strlen($language) > 50) {
175 $this->show_form(_('Language is too long (max 50 chars).'));
178 $user = common_current_user();
180 $user->query('BEGIN');
182 if ($user->nickname != $nickname ||
183 $user->language != $language ||
184 $user->timezone != $timezone) {
186 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
188 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
190 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
193 $original = clone($user);
195 $user->nickname = $nickname;
196 $user->language = $language;
197 $user->timezone = $timezone;
199 $result = $user->updateKeys($original);
201 if ($result === FALSE) {
202 common_log_db_error($user, 'UPDATE', __FILE__);
203 common_server_error(_('Couldn\'t update user.'));
206 # Re-initialize language environment if it changed
207 common_init_language();
213 if ($user->autosubscribe ^ $autosubscribe) {
215 $original = clone($user);
217 $user->autosubscribe = $autosubscribe;
219 $result = $user->update($original);
221 if ($result === FALSE) {
222 common_log_db_error($user, 'UPDATE', __FILE__);
223 common_server_error(_('Couldn\'t update user for autosubscribe.'));
228 $profile = $user->getProfile();
230 $orig_profile = clone($profile);
232 $profile->nickname = $user->nickname;
233 $profile->fullname = $fullname;
234 $profile->homepage = $homepage;
235 $profile->bio = $bio;
236 $profile->location = $location;
237 $profile->profileurl = common_profile_url($nickname);
239 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
240 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
242 $result = $profile->update($orig_profile);
245 common_log_db_error($profile, 'UPDATE', __FILE__);
246 common_server_error(_('Couldn\'t save profile.'));
250 $user->query('COMMIT');
252 common_broadcast_profile($profile);
254 $this->show_form(_('Settings saved.'), TRUE);
258 function upload_avatar() {
259 switch ($_FILES['avatarfile']['error']) {
260 case UPLOAD_ERR_OK: # success, jump out
262 case UPLOAD_ERR_INI_SIZE:
263 case UPLOAD_ERR_FORM_SIZE:
264 $this->show_form(_('That file is too big.'));
266 case UPLOAD_ERR_PARTIAL:
267 @unlink($_FILES['avatarfile']['tmp_name']);
268 $this->show_form(_('Partial upload.'));
271 $this->show_form(_('System error uploading file.'));
275 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
278 @unlink($_FILES['avatarfile']['tmp_name']);
279 $this->show_form(_('Not an image or corrupt file.'));
289 $this->show_form(_('Unsupported image file format.'));
293 $user = common_current_user();
294 $profile = $user->getProfile();
296 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
297 $this->show_form(_('Avatar updated.'), true);
299 $this->show_form(_('Failed updating avatar.'));
302 @unlink($_FILES['avatarfile']['tmp_name']);
305 function nickname_exists($nickname) {
306 $user = common_current_user();
307 $other = User::staticGet('nickname', $nickname);
311 return $other->id != $user->id;