]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
Fold password form into profile settings
[quix0rs-gnu-social.git] / actions / profilesettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 class ProfilesettingsAction extends SettingsAction {
25
26         function get_instructions() {
27                 return _('You can update your personal profile info here '.
28                                   'so people know more about you.');
29         }
30
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_show_footer();
39         }
40
41         function handle_post() {
42
43                 # CSRF protection
44
45                 $token = $this->trimmed('token');
46                 if (!$token || $token != common_session_token()) {
47                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
48                         return;
49                 }
50
51                 if ($this->arg('save')) {
52                         $this->save_profile();
53                 } else if ($this->arg('upload')) {
54                         $this->upload_avatar();
55                 } else if ($this->arg('changepass')) {
56                         $this->change_password();
57                 }
58         }
59
60         function show_settings_form() {
61
62                 $user = common_current_user();
63                 $profile = $user->getProfile();
64
65                 common_element_start('form', array('method' => 'POST',
66                                                                                    'id' => 'profilesettings',
67                                                                                    'action' =>
68                                                                                    common_local_url('profilesettings')));
69                 common_hidden('token', common_session_token());
70
71
72
73                 # too much common patterns here... abstractable?
74                 common_input('nickname', _('Nickname'),
75                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
76                                          _('1-64 lowercase letters or numbers, no punctuation or spaces'));
77                 common_input('fullname', _('Full name'),
78                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
79                 common_input('homepage', _('Homepage'),
80                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
81                                          _('URL of your homepage, blog, or profile on another site'));
82                 common_textarea('bio', _('Bio'),
83                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
84                                                 _('Describe yourself and your interests in 140 chars'));
85                 common_input('location', _('Location'),
86                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
87                                          _('Where you are, like "City, State (or Region), Country"'));
88
89                 $language = common_language();
90                 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
91                 $timezone = common_timezone();
92                 $timezones = array();
93                 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
94                         $timezones[$v] = $v;
95                 }
96                 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
97
98                 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
99                                                 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
100
101                 common_submit('save', _('Save'));
102
103                 common_element_end('form');
104
105
106         }
107
108         function show_avatar_form() {
109
110                 $user = common_current_user();
111                 $profile = $user->getProfile();
112
113                 $original = $profile->getOriginalAvatar();
114
115
116                 common_element_start('form', array('enctype' => 'multipart/form-data',
117                                                                                    'method' => 'POST',
118                                                                                    'id' => 'avatar',
119                                                                                    'action' =>
120                                                                                    common_local_url('profilesettings')));
121                 common_hidden('token', common_session_token());
122
123                 if ($original) {
124                         common_element('img', array('src' => $original->url,
125                                                                                 'class' => 'avatar original',
126                                                                                 'width' => $original->width,
127                                                                                 'height' => $original->height,
128                                                                                 'alt' => $user->nickname));
129                 }
130
131                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
132
133                 if ($avatar) {
134                         common_element('img', array('src' => $avatar->url,
135                                                                                 'class' => 'avatar profile',
136                                                                                 'width' => AVATAR_PROFILE_SIZE,
137                                                                                 'height' => AVATAR_PROFILE_SIZE,
138                                                                                 'alt' => $user->nickname));
139                 }
140
141
142                 common_element('input', array('name' => 'MAX_FILE_SIZE',
143                                                                           'type' => 'hidden',
144                                                                           'id' => 'MAX_FILE_SIZE',
145                                                                           'value' => MAX_AVATAR_SIZE));
146
147                 common_element_start('p');
148
149
150                 common_element('input', array('name' => 'avatarfile',
151                                                                           'type' => 'file',
152                                                                           'id' => 'avatarfile'));
153                 common_element_end('p');
154
155                 common_submit('upload', _('Upload'));
156                 common_element_end('form');
157
158         }
159
160         function show_password_form() {
161
162                 $user = common_current_user();
163                 common_element_start('form', array('method' => 'POST',
164                                                                                    'id' => 'password',
165                                                                                    'action' =>
166                                                                                    common_local_url('profilesettings')));
167
168                 common_hidden('token', common_session_token());
169
170                 # Users who logged in with OpenID won't have a pwd
171                 if ($user->password) {
172                         common_password('oldpassword', _('Old password'));
173                 }
174                 common_password('newpassword', _('New password'),
175                                                 _('6 or more characters'));
176                 common_password('confirm', _('Confirm'),
177                                                 _('same as password above'));
178                 common_submit('changepass', _('Change'));
179                 common_element_end('form');
180         }
181
182         function save_profile() {
183                 $nickname = $this->trimmed('nickname');
184                 $fullname = $this->trimmed('fullname');
185                 $homepage = $this->trimmed('homepage');
186                 $bio = $this->trimmed('bio');
187                 $location = $this->trimmed('location');
188                 $autosubscribe = $this->boolean('autosubscribe');
189                 $language = $this->trimmed('language');
190                 $timezone = $this->trimmed('timezone');
191
192                 # Some validation
193
194                 if (!Validate::string($nickname, array('min_length' => 1,
195                                                                                            'max_length' => 64,
196                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
197                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
198                         return;
199                 } else if (!User::allowed_nickname($nickname)) {
200                         $this->show_form(_('Not a valid nickname.'));
201                         return;
202                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
203                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
204                         $this->show_form(_('Homepage is not a valid URL.'));
205                         return;
206                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
207                         $this->show_form(_('Full name is too long (max 255 chars).'));
208                         return;
209                 } else if (!is_null($bio) && strlen($bio) > 140) {
210                         $this->show_form(_('Bio is too long (max 140 chars).'));
211                         return;
212                 } else if (!is_null($location) && strlen($location) > 255) {
213                         $this->show_form(_('Location is too long (max 255 chars).'));
214                         return;
215                 }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
216                         $this->show_form(_('Timezone not selected.'));
217                         return;
218                 } else if ($this->nickname_exists($nickname)) {
219                         $this->show_form(_('Nickname already in use. Try another one.'));
220                         return;
221                 } else if (!is_null($language) && strlen($language) > 50) {
222                         $this->show_form(_('Language is too long (max 50 chars).'));
223                 }
224
225                 $user = common_current_user();
226
227                 $user->query('BEGIN');
228
229                 if ($user->nickname != $nickname ||
230                         $user->language != $language ||
231                         $user->timezone != $timezone) {
232
233                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
234                                                  __FILE__);
235                         common_debug('Updating user language from ' . $user->language . ' to ' . $language,
236                                                  __FILE__);
237                         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
238                                                  __FILE__);
239
240                         $original = clone($user);
241
242                         $user->nickname = $nickname;
243                         $user->language = $language;
244                         $user->timezone = $timezone;
245
246                         $result = $user->updateKeys($original);
247
248                         if ($result === FALSE) {
249                                 common_log_db_error($user, 'UPDATE', __FILE__);
250                                 common_server_error(_('Couldn\'t update user.'));
251                                 return;
252                         } else {
253                                 # Re-initialize language environment if it changed
254                                 common_init_language();
255                         }
256                 }
257
258                 # XXX: XOR
259
260                 if ($user->autosubscribe ^ $autosubscribe) {
261
262                         $original = clone($user);
263
264                         $user->autosubscribe = $autosubscribe;
265
266                         $result = $user->update($original);
267
268                         if ($result === FALSE) {
269                                 common_log_db_error($user, 'UPDATE', __FILE__);
270                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
271                                 return;
272                         }
273                 }
274
275                 $profile = $user->getProfile();
276
277                 $orig_profile = clone($profile);
278
279                 $profile->nickname = $user->nickname;
280                 $profile->fullname = $fullname;
281                 $profile->homepage = $homepage;
282                 $profile->bio = $bio;
283                 $profile->location = $location;
284                 $profile->profileurl = common_profile_url($nickname);
285
286                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
287                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
288
289                 $result = $profile->update($orig_profile);
290
291                 if (!$result) {
292                         common_log_db_error($profile, 'UPDATE', __FILE__);
293                         common_server_error(_('Couldn\'t save profile.'));
294                         return;
295                 }
296
297                 $user->query('COMMIT');
298
299                 common_broadcast_profile($profile);
300
301                 $this->show_form(_('Settings saved.'), TRUE);
302         }
303
304
305         function upload_avatar() {
306                 switch ($_FILES['avatarfile']['error']) {
307                  case UPLOAD_ERR_OK: # success, jump out
308                         break;
309                  case UPLOAD_ERR_INI_SIZE:
310                  case UPLOAD_ERR_FORM_SIZE:
311                         $this->show_form(_('That file is too big.'));
312                         return;
313                  case UPLOAD_ERR_PARTIAL:
314                         @unlink($_FILES['avatarfile']['tmp_name']);
315                         $this->show_form(_('Partial upload.'));
316                         return;
317                  default:
318                         $this->show_form(_('System error uploading file.'));
319                         return;
320                 }
321
322                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
323
324                 if (!$info) {
325                         @unlink($_FILES['avatarfile']['tmp_name']);
326                         $this->show_form(_('Not an image or corrupt file.'));
327                         return;
328                 }
329
330                 switch ($info[2]) {
331                  case IMAGETYPE_GIF:
332                  case IMAGETYPE_JPEG:
333                  case IMAGETYPE_PNG:
334                         break;
335                  default:
336                         $this->show_form(_('Unsupported image file format.'));
337                         return;
338                 }
339
340                 $user = common_current_user();
341                 $profile = $user->getProfile();
342
343                 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
344                         $this->show_form(_('Avatar updated.'), true);
345                 } else {
346                         $this->show_form(_('Failed updating avatar.'));
347                 }
348
349                 @unlink($_FILES['avatarfile']['tmp_name']);
350         }
351
352         function nickname_exists($nickname) {
353                 $user = common_current_user();
354                 $other = User::staticGet('nickname', $nickname);
355                 if (!$other) {
356                         return false;
357                 } else {
358                         return $other->id != $user->id;
359                 }
360         }
361
362         function change_password() {
363
364                 $user = common_current_user();
365                 assert(!is_null($user)); # should already be checked
366
367                 # FIXME: scrub input
368
369                 $newpassword = $this->arg('newpassword');
370                 $confirm = $this->arg('confirm');
371                 $token = $this->arg('token');
372
373                 if (0 != strcmp($newpassword, $confirm)) {
374                         $this->show_form(_('Passwords don\'t match.'));
375                         return;
376                 }
377
378                 if ($user->password) {
379                         $oldpassword = $this->arg('oldpassword');
380
381                         if (!common_check_user($user->nickname, $oldpassword)) {
382                                 $this->show_form(_('Incorrect old password'));
383                                 return;
384                         }
385                 }
386
387                 $original = clone($user);
388
389                 $user->password = common_munge_password($newpassword, $user->id);
390
391                 $val = $user->validate();
392                 if ($val !== TRUE) {
393                         $this->show_form(_('Error saving user; invalid.'));
394                         return;
395                 }
396
397                 if (!$user->update($original)) {
398                         common_server_error(_('Can\'t save new password.'));
399                         return;
400                 }
401
402                 $this->show_form(_('Password saved.'), true);
403         }
404
405 }