]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
bcf7c4caf21cf4d467e1a9d5ec273e210bfa27b9
[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_element('h2', NULL, _('Delete my account'));
39                 $this->show_delete_form();
40                 common_show_footer();
41         }
42
43         function handle_post() {
44
45                 # CSRF protection
46
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.'));
50                         return;
51                 }
52
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();
61                 }
62
63         }
64
65         function show_settings_form() {
66
67                 $user = common_current_user();
68                 $profile = $user->getProfile();
69
70                 common_element_start('form', array('method' => 'POST',
71                                                                                    'id' => 'profilesettings',
72                                                                                    'action' =>
73                                                                                    common_local_url('profilesettings')));
74                 common_hidden('token', common_session_token());
75                 
76                 # too much common patterns here... abstractable?
77                 
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'));
95
96                 $language = common_language();
97                 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
98                 $timezone = common_timezone();
99                 $timezones = array();
100                 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
101                         $timezones[$v] = $v;
102                 }
103                 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
104
105                 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
106                                                 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
107
108                 common_submit('save', _('Save'));
109
110                 common_element_end('form');
111
112
113         }
114
115         function show_avatar_form() {
116
117                 $user = common_current_user();
118                 $profile = $user->getProfile();
119
120                 if (!$profile) {
121                         common_log_db_error($user, 'SELECT', __FILE__);
122                         $this->server_error(_('User without matching profile'));
123                         return;
124                 }
125                 
126                 $original = $profile->getOriginalAvatar();
127
128
129                 common_element_start('form', array('enctype' => 'multipart/form-data',
130                                                                                    'method' => 'POST',
131                                                                                    'id' => 'avatar',
132                                                                                    'action' =>
133                                                                                    common_local_url('profilesettings')));
134                 common_hidden('token', common_session_token());
135
136                 if ($original) {
137                         common_element('img', array('src' => $original->url,
138                                                                                 'class' => 'avatar original',
139                                                                                 'width' => $original->width,
140                                                                                 'height' => $original->height,
141                                                                                 'alt' => $user->nickname));
142                 }
143
144                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
145
146                 if ($avatar) {
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));
152                 }
153
154
155                 common_element('input', array('name' => 'MAX_FILE_SIZE',
156                                                                           'type' => 'hidden',
157                                                                           'id' => 'MAX_FILE_SIZE',
158                                                                           'value' => MAX_AVATAR_SIZE));
159
160                 common_element_start('p');
161
162
163                 common_element('input', array('name' => 'avatarfile',
164                                                                           'type' => 'file',
165                                                                           'id' => 'avatarfile'));
166                 common_element_end('p');
167
168                 common_submit('upload', _('Upload'));
169                 common_element_end('form');
170
171         }
172
173         function show_password_form() {
174
175                 $user = common_current_user();
176                 common_element_start('form', array('method' => 'POST',
177                                                                                    'id' => 'password',
178                                                                                    'action' =>
179                                                                                    common_local_url('profilesettings')));
180
181                 common_hidden('token', common_session_token());
182
183                 # Users who logged in with OpenID won't have a pwd
184                 if ($user->password) {
185                         common_password('oldpassword', _('Old password'));
186                 }
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');
193         }
194
195         function show_delete_form() {
196
197                 $user = common_current_user();
198                 common_element_start('form', array('method' => 'POST',
199                                                                                    'id' => 'password',
200                                                                                    'action' =>
201                                                                                    common_local_url('profilesettings')));
202
203                 common_hidden('token', common_session_token());
204
205                 common_submit('deleteaccount', _('Delete my account'));
206                 common_element_end('form');
207         }
208
209         function save_profile() {
210                 $nickname = $this->trimmed('nickname');
211                 $fullname = $this->trimmed('fullname');
212                 $homepage = $this->trimmed('homepage');
213                 $bio = $this->trimmed('bio');
214                 $location = $this->trimmed('location');
215                 $autosubscribe = $this->boolean('autosubscribe');
216                 $language = $this->trimmed('language');
217                 $timezone = $this->trimmed('timezone');
218                 $tagstring = $this->trimmed('tags');
219                 
220                 # Some validation
221
222                 if (!Validate::string($nickname, array('min_length' => 1,
223                                                                                            'max_length' => 64,
224                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
225                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
226                         return;
227                 } else if (!User::allowed_nickname($nickname)) {
228                         $this->show_form(_('Not a valid nickname.'));
229                         return;
230                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
231                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
232                         $this->show_form(_('Homepage is not a valid URL.'));
233                         return;
234                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
235                         $this->show_form(_('Full name is too long (max 255 chars).'));
236                         return;
237                 } else if (!is_null($bio) && strlen($bio) > 140) {
238                         $this->show_form(_('Bio is too long (max 140 chars).'));
239                         return;
240                 } else if (!is_null($location) && strlen($location) > 255) {
241                         $this->show_form(_('Location is too long (max 255 chars).'));
242                         return;
243                 }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
244                         $this->show_form(_('Timezone not selected.'));
245                         return;
246                 } else if ($this->nickname_exists($nickname)) {
247                         $this->show_form(_('Nickname already in use. Try another one.'));
248                         return;
249         } else if (!is_null($language) && strlen($language) > 50) {
250                 $this->show_form(_('Language is too long (max 50 chars).'));
251                         return;
252                 }
253
254                 if ($tagstring) {
255                         $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
256                 } else {
257                         $tags = array();
258                 }
259                         
260                 foreach ($tags as $tag) {
261                         if (!common_valid_profile_tag($tag)) {
262                                 $this->show_form(sprintf(_('Invalid tag: "%s"'), $tag));
263                                 return;
264                         }
265                 }
266                 
267                 $user = common_current_user();
268
269                 $user->query('BEGIN');
270
271                 if ($user->nickname != $nickname ||
272                         $user->language != $language ||
273                         $user->timezone != $timezone) {
274
275                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
276                                                  __FILE__);
277                         common_debug('Updating user language from ' . $user->language . ' to ' . $language,
278                                                  __FILE__);
279                         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
280                                                  __FILE__);
281
282                         $original = clone($user);
283
284                         $user->nickname = $nickname;
285                         $user->language = $language;
286                         $user->timezone = $timezone;
287
288                         $result = $user->updateKeys($original);
289
290                         if ($result === FALSE) {
291                                 common_log_db_error($user, 'UPDATE', __FILE__);
292                                 common_server_error(_('Couldn\'t update user.'));
293                                 return;
294                         } else {
295                                 # Re-initialize language environment if it changed
296                                 common_init_language();
297                         }
298                 }
299
300                 # XXX: XOR
301
302                 if ($user->autosubscribe ^ $autosubscribe) {
303
304                         $original = clone($user);
305
306                         $user->autosubscribe = $autosubscribe;
307
308                         $result = $user->update($original);
309
310                         if ($result === FALSE) {
311                                 common_log_db_error($user, 'UPDATE', __FILE__);
312                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
313                                 return;
314                         }
315                 }
316
317                 $profile = $user->getProfile();
318
319                 $orig_profile = clone($profile);
320
321                 $profile->nickname = $user->nickname;
322                 $profile->fullname = $fullname;
323                 $profile->homepage = $homepage;
324                 $profile->bio = $bio;
325                 $profile->location = $location;
326                 $profile->profileurl = common_profile_url($nickname);
327
328                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
329                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
330
331                 $result = $profile->update($orig_profile);
332
333                 if (!$result) {
334                         common_log_db_error($profile, 'UPDATE', __FILE__);
335                         common_server_error(_('Couldn\'t save profile.'));
336                         return;
337                 }
338
339                 # Set the user tags
340                 
341                 $result = $user->setSelfTags($tags);
342
343                 if (!$result) {
344                         common_server_error(_('Couldn\'t save tags.'));
345                         return;
346                 }
347                 
348                 $user->query('COMMIT');
349
350                 common_broadcast_profile($profile);
351
352                 $this->show_form(_('Settings saved.'), TRUE);
353         }
354
355
356         function upload_avatar() {
357                 switch ($_FILES['avatarfile']['error']) {
358                  case UPLOAD_ERR_OK: # success, jump out
359                         break;
360                  case UPLOAD_ERR_INI_SIZE:
361                  case UPLOAD_ERR_FORM_SIZE:
362                         $this->show_form(_('That file is too big.'));
363                         return;
364                  case UPLOAD_ERR_PARTIAL:
365                         @unlink($_FILES['avatarfile']['tmp_name']);
366                         $this->show_form(_('Partial upload.'));
367                         return;
368                  default:
369                         $this->show_form(_('System error uploading file.'));
370                         return;
371                 }
372
373                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
374
375                 if (!$info) {
376                         @unlink($_FILES['avatarfile']['tmp_name']);
377                         $this->show_form(_('Not an image or corrupt file.'));
378                         return;
379                 }
380
381                 switch ($info[2]) {
382                  case IMAGETYPE_GIF:
383                  case IMAGETYPE_JPEG:
384                  case IMAGETYPE_PNG:
385                         break;
386                  default:
387                         $this->show_form(_('Unsupported image file format.'));
388                         return;
389                 }
390
391                 $user = common_current_user();
392                 $profile = $user->getProfile();
393
394                 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
395                         $this->show_form(_('Avatar updated.'), true);
396                 } else {
397                         $this->show_form(_('Failed updating avatar.'));
398                 }
399
400                 @unlink($_FILES['avatarfile']['tmp_name']);
401         }
402
403         function nickname_exists($nickname) {
404                 $user = common_current_user();
405                 $other = User::staticGet('nickname', $nickname);
406                 if (!$other) {
407                         return false;
408                 } else {
409                         return $other->id != $user->id;
410                 }
411         }
412
413         function change_password() {
414
415                 $user = common_current_user();
416                 assert(!is_null($user)); # should already be checked
417
418                 # FIXME: scrub input
419
420                 $newpassword = $this->arg('newpassword');
421                 $confirm = $this->arg('confirm');
422                 $token = $this->arg('token');
423
424                 if (0 != strcmp($newpassword, $confirm)) {
425                         $this->show_form(_('Passwords don\'t match.'));
426                         return;
427                 }
428
429                 if ($user->password) {
430                         $oldpassword = $this->arg('oldpassword');
431
432                         if (!common_check_user($user->nickname, $oldpassword)) {
433                                 $this->show_form(_('Incorrect old password'));
434                                 return;
435                         }
436                 }
437
438                 $original = clone($user);
439
440                 $user->password = common_munge_password($newpassword, $user->id);
441
442                 $val = $user->validate();
443                 if ($val !== TRUE) {
444                         $this->show_form(_('Error saving user; invalid.'));
445                         return;
446                 }
447
448                 if (!$user->update($original)) {
449                         common_server_error(_('Can\'t save new password.'));
450                         return;
451                 }
452
453                 $this->show_form(_('Password saved.'), true);
454         }
455
456         function delete_account() {
457
458                 $user = common_current_user();
459                 assert(!is_null($user)); # should already be checked
460
461                 $this->show_form(_('You will be asked to confirmed, nothing was deleted.'), true);
462     }
463 }