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 $this->show_avatar_form();
38 function handle_post() {
42 $token = $this->trimmed('token');
43 if (!$token || $token != common_session_token()) {
44 $this->show_form(_('There was a problem with your session token. Try again, please.'));
48 if ($this->arg('save')) {
49 $this->save_profile();
50 } else if ($this->arg('upload')) {
51 $this->upload_avatar();
55 function show_settings_form() {
57 $user = common_current_user();
58 $profile = $user->getProfile();
60 common_element_start('form', array('method' => 'POST',
61 'id' => 'profilesettings',
63 common_local_url('profilesettings')));
64 common_hidden('token', common_session_token());
68 # too much common patterns here... abstractable?
69 common_input('nickname', _('Nickname'),
70 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
71 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
72 common_input('fullname', _('Full name'),
73 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
74 common_input('homepage', _('Homepage'),
75 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
76 _('URL of your homepage, blog, or profile on another site'));
77 common_textarea('bio', _('Bio'),
78 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
79 _('Describe yourself and your interests in 140 chars'));
80 common_input('location', _('Location'),
81 ($this->arg('location')) ? $this->arg('location') : $profile->location,
82 _('Where you are, like "City, State (or Region), Country"'));
84 $language = common_language();
85 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
86 $timezone = common_timezone();
88 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
91 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
93 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
94 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
96 common_submit('save', _('Save'));
98 common_element_end('form');
103 function show_avatar_form() {
105 $user = common_current_user();
106 $profile = $user->getProfile();
108 $original = $profile->getOriginalAvatar();
111 common_element_start('form', array('enctype' => 'multipart/form-data',
113 'id' => 'profilesettings',
115 common_local_url('profilesettings')));
116 common_hidden('token', common_session_token());
118 common_element_start('p');
119 common_element('label', array('for' => 'avatar'), 'Avatar');
122 common_element('img', array('src' => $original->url,
123 'class' => 'avatar original',
124 'width' => $original->width,
125 'height' => $original->height,
126 'alt' => $user->nickname));
129 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
132 common_element('img', array('src' => $avatar->url,
133 'class' => 'avatar profile',
134 'width' => AVATAR_PROFILE_SIZE,
135 'height' => AVATAR_PROFILE_SIZE,
136 'alt' => $user->nickname));
139 common_element_end('p');
141 common_element('input', array('name' => 'MAX_FILE_SIZE',
143 'id' => 'MAX_FILE_SIZE',
144 'value' => MAX_AVATAR_SIZE));
145 common_element('input', array('name' => 'avatarfile',
147 'id' => 'avatarfile'));
148 common_submit('upload', _('Upload'));
149 common_element_end('form');
153 function save_profile() {
154 $nickname = $this->trimmed('nickname');
155 $fullname = $this->trimmed('fullname');
156 $homepage = $this->trimmed('homepage');
157 $bio = $this->trimmed('bio');
158 $location = $this->trimmed('location');
159 $autosubscribe = $this->boolean('autosubscribe');
160 $language = $this->trimmed('language');
161 $timezone = $this->trimmed('timezone');
165 if (!Validate::string($nickname, array('min_length' => 1,
167 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
168 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
170 } else if (!User::allowed_nickname($nickname)) {
171 $this->show_form(_('Not a valid nickname.'));
173 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
174 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
175 $this->show_form(_('Homepage is not a valid URL.'));
177 } else if (!is_null($fullname) && strlen($fullname) > 255) {
178 $this->show_form(_('Full name is too long (max 255 chars).'));
180 } else if (!is_null($bio) && strlen($bio) > 140) {
181 $this->show_form(_('Bio is too long (max 140 chars).'));
183 } else if (!is_null($location) && strlen($location) > 255) {
184 $this->show_form(_('Location is too long (max 255 chars).'));
186 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
187 $this->show_form(_('Timezone not selected.'));
189 } else if ($this->nickname_exists($nickname)) {
190 $this->show_form(_('Nickname already in use. Try another one.'));
192 } else if (!is_null($language) && strlen($language) > 50) {
193 $this->show_form(_('Language is too long (max 50 chars).'));
196 $user = common_current_user();
198 $user->query('BEGIN');
200 if ($user->nickname != $nickname ||
201 $user->language != $language ||
202 $user->timezone != $timezone) {
204 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
206 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
208 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
211 $original = clone($user);
213 $user->nickname = $nickname;
214 $user->language = $language;
215 $user->timezone = $timezone;
217 $result = $user->updateKeys($original);
219 if ($result === FALSE) {
220 common_log_db_error($user, 'UPDATE', __FILE__);
221 common_server_error(_('Couldn\'t update user.'));
224 # Re-initialize language environment if it changed
225 common_init_language();
231 if ($user->autosubscribe ^ $autosubscribe) {
233 $original = clone($user);
235 $user->autosubscribe = $autosubscribe;
237 $result = $user->update($original);
239 if ($result === FALSE) {
240 common_log_db_error($user, 'UPDATE', __FILE__);
241 common_server_error(_('Couldn\'t update user for autosubscribe.'));
246 $profile = $user->getProfile();
248 $orig_profile = clone($profile);
250 $profile->nickname = $user->nickname;
251 $profile->fullname = $fullname;
252 $profile->homepage = $homepage;
253 $profile->bio = $bio;
254 $profile->location = $location;
255 $profile->profileurl = common_profile_url($nickname);
257 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
258 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
260 $result = $profile->update($orig_profile);
263 common_log_db_error($profile, 'UPDATE', __FILE__);
264 common_server_error(_('Couldn\'t save profile.'));
268 $user->query('COMMIT');
270 common_broadcast_profile($profile);
272 $this->show_form(_('Settings saved.'), TRUE);
276 function upload_avatar() {
277 switch ($_FILES['avatarfile']['error']) {
278 case UPLOAD_ERR_OK: # success, jump out
280 case UPLOAD_ERR_INI_SIZE:
281 case UPLOAD_ERR_FORM_SIZE:
282 $this->show_form(_('That file is too big.'));
284 case UPLOAD_ERR_PARTIAL:
285 @unlink($_FILES['avatarfile']['tmp_name']);
286 $this->show_form(_('Partial upload.'));
289 $this->show_form(_('System error uploading file.'));
293 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
296 @unlink($_FILES['avatarfile']['tmp_name']);
297 $this->show_form(_('Not an image or corrupt file.'));
307 $this->show_form(_('Unsupported image file format.'));
311 $user = common_current_user();
312 $profile = $user->getProfile();
314 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
315 $this->show_form(_('Avatar updated.'), true);
317 $this->show_form(_('Failed updating avatar.'));
320 @unlink($_FILES['avatarfile']['tmp_name']);
323 function nickname_exists($nickname) {
324 $user = common_current_user();
325 $other = User::staticGet('nickname', $nickname);
329 return $other->id != $user->id;