]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
delete user and all traces from the db.
[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
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'));
200
201                 foreach ($feeds as $key => $value) {
202                         $this->common_feed_item($feeds[$key]);
203                 }
204                 common_element_end('ul');
205                 common_element_end('div');
206         }
207
208         function common_feed_item($feed) {
209         $user = common_current_user();
210                 $nickname = $user->nickname;
211
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";
218                                 break;
219
220                         case 'foaf':
221                                 $feed_classname = "foaf";
222                                 $feed_mimetype = "application/".$feed['type']."+xml";
223                                 $feed_title = "$nickname's FOAF file";
224                                 $feed['textContent'] = "FOAF";
225                                 break;
226                 }
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');
234         }
235
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();
241
242                 common_element_start('form', array('method' => 'POST',
243                                                                                    'id' => 'delete',
244                                                                                    'action' =>
245                                                                                    common_local_url('profilesettings')));
246
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.");
249
250
251                 $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('limit' => $notice_count, 'nickname' => $user->nickname)), 
252                                                                                           'type' => 'rss',
253                                                                                           'version' => 'RSS 1.0',
254                                                                                           'item' => 'notices'),
255                                                                          1=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
256                                                                                           'type' => 'rdf',
257                                                                                           'version' => 'FOAF',
258                                                                                           'item' => 'foaf')));
259
260                 common_submit('deleteaccount', _('Delete my account'));
261                 common_element_end('form');
262         }
263
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');
274                 
275                 # Some validation
276
277                 if (!Validate::string($nickname, array('min_length' => 1,
278                                                                                            'max_length' => 64,
279                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
280                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
281                         return;
282                 } else if (!User::allowed_nickname($nickname)) {
283                         $this->show_form(_('Not a valid nickname.'));
284                         return;
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.'));
288                         return;
289                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
290                         $this->show_form(_('Full name is too long (max 255 chars).'));
291                         return;
292                 } else if (!is_null($bio) && strlen($bio) > 140) {
293                         $this->show_form(_('Bio is too long (max 140 chars).'));
294                         return;
295                 } else if (!is_null($location) && strlen($location) > 255) {
296                         $this->show_form(_('Location is too long (max 255 chars).'));
297                         return;
298                 }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
299                         $this->show_form(_('Timezone not selected.'));
300                         return;
301                 } else if ($this->nickname_exists($nickname)) {
302                         $this->show_form(_('Nickname already in use. Try another one.'));
303                         return;
304         } else if (!is_null($language) && strlen($language) > 50) {
305                 $this->show_form(_('Language is too long (max 50 chars).'));
306                         return;
307                 }
308
309                 if ($tagstring) {
310                         $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
311                 } else {
312                         $tags = array();
313                 }
314                         
315                 foreach ($tags as $tag) {
316                         if (!common_valid_profile_tag($tag)) {
317                                 $this->show_form(sprintf(_('Invalid tag: "%s"'), $tag));
318                                 return;
319                         }
320                 }
321                 
322                 $user = common_current_user();
323
324                 $user->query('BEGIN');
325
326                 if ($user->nickname != $nickname ||
327                         $user->language != $language ||
328                         $user->timezone != $timezone) {
329
330                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
331                                                  __FILE__);
332                         common_debug('Updating user language from ' . $user->language . ' to ' . $language,
333                                                  __FILE__);
334                         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
335                                                  __FILE__);
336
337                         $original = clone($user);
338
339                         $user->nickname = $nickname;
340                         $user->language = $language;
341                         $user->timezone = $timezone;
342
343                         $result = $user->updateKeys($original);
344
345                         if ($result === FALSE) {
346                                 common_log_db_error($user, 'UPDATE', __FILE__);
347                                 common_server_error(_('Couldn\'t update user.'));
348                                 return;
349                         } else {
350                                 # Re-initialize language environment if it changed
351                                 common_init_language();
352                         }
353                 }
354
355                 # XXX: XOR
356
357                 if ($user->autosubscribe ^ $autosubscribe) {
358
359                         $original = clone($user);
360
361                         $user->autosubscribe = $autosubscribe;
362
363                         $result = $user->update($original);
364
365                         if ($result === FALSE) {
366                                 common_log_db_error($user, 'UPDATE', __FILE__);
367                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
368                                 return;
369                         }
370                 }
371
372                 $profile = $user->getProfile();
373
374                 $orig_profile = clone($profile);
375
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);
382
383                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
384                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
385
386                 $result = $profile->update($orig_profile);
387
388                 if (!$result) {
389                         common_log_db_error($profile, 'UPDATE', __FILE__);
390                         common_server_error(_('Couldn\'t save profile.'));
391                         return;
392                 }
393
394                 # Set the user tags
395                 
396                 $result = $user->setSelfTags($tags);
397
398                 if (!$result) {
399                         common_server_error(_('Couldn\'t save tags.'));
400                         return;
401                 }
402                 
403                 $user->query('COMMIT');
404
405                 common_broadcast_profile($profile);
406
407                 $this->show_form(_('Settings saved.'), TRUE);
408         }
409
410
411         function upload_avatar() {
412                 switch ($_FILES['avatarfile']['error']) {
413                  case UPLOAD_ERR_OK: # success, jump out
414                         break;
415                  case UPLOAD_ERR_INI_SIZE:
416                  case UPLOAD_ERR_FORM_SIZE:
417                         $this->show_form(_('That file is too big.'));
418                         return;
419                  case UPLOAD_ERR_PARTIAL:
420                         @unlink($_FILES['avatarfile']['tmp_name']);
421                         $this->show_form(_('Partial upload.'));
422                         return;
423                  default:
424                         $this->show_form(_('System error uploading file.'));
425                         return;
426                 }
427
428                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
429
430                 if (!$info) {
431                         @unlink($_FILES['avatarfile']['tmp_name']);
432                         $this->show_form(_('Not an image or corrupt file.'));
433                         return;
434                 }
435
436                 switch ($info[2]) {
437                  case IMAGETYPE_GIF:
438                  case IMAGETYPE_JPEG:
439                  case IMAGETYPE_PNG:
440                         break;
441                  default:
442                         $this->show_form(_('Unsupported image file format.'));
443                         return;
444                 }
445
446                 $user = common_current_user();
447                 $profile = $user->getProfile();
448
449                 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
450                         $this->show_form(_('Avatar updated.'), true);
451                 } else {
452                         $this->show_form(_('Failed updating avatar.'));
453                 }
454
455                 @unlink($_FILES['avatarfile']['tmp_name']);
456         }
457
458         function nickname_exists($nickname) {
459                 $user = common_current_user();
460                 $other = User::staticGet('nickname', $nickname);
461                 if (!$other) {
462                         return false;
463                 } else {
464                         return $other->id != $user->id;
465                 }
466         }
467
468         function change_password() {
469
470                 $user = common_current_user();
471                 assert(!is_null($user)); # should already be checked
472
473                 # FIXME: scrub input
474
475                 $newpassword = $this->arg('newpassword');
476                 $confirm = $this->arg('confirm');
477                 $token = $this->arg('token');
478
479                 if (0 != strcmp($newpassword, $confirm)) {
480                         $this->show_form(_('Passwords don\'t match.'));
481                         return;
482                 }
483
484                 if ($user->password) {
485                         $oldpassword = $this->arg('oldpassword');
486
487                         if (!common_check_user($user->nickname, $oldpassword)) {
488                                 $this->show_form(_('Incorrect old password'));
489                                 return;
490                         }
491                 }
492
493                 $original = clone($user);
494
495                 $user->password = common_munge_password($newpassword, $user->id);
496
497                 $val = $user->validate();
498                 if ($val !== TRUE) {
499                         $this->show_form(_('Error saving user; invalid.'));
500                         return;
501                 }
502
503                 if (!$user->update($original)) {
504                         common_server_error(_('Can\'t save new password.'));
505                         return;
506                 }
507
508                 $this->show_form(_('Password saved.'), true);
509         }
510
511         function delete_account() {
512                 $user = common_current_user();
513                 assert(!is_null($user)); # should already be checked
514
515         // deleted later through the profile
516         /*
517         $avatar = new Avatar;
518         $avatar->profile_id = $user->id;
519         $n_avatars_deleted = $avatar->delete();
520         */
521
522         $fave = new Fave;
523         $fave->user_id = $user->id;
524         $n_faves_deleted = $fave->delete(); 
525
526         $confirmation = new Confirm_address;
527         $confirmation->user_id = $user->id;
528         $n_confirmations_deleted = $confirmation->delete();
529
530         // TODO foreign stuff...
531
532         $invitation = new Invitation;
533         $invitation->user_id = $user->id;
534         $n_invitations_deleted = $invitation->delete();
535
536         $message_from = new Message;
537         $message_from->from_profile = $user->id;
538         $n_messages_from_deleted = $message_from->delete();
539
540         $message_to = new Message;
541         $message_to->to_profile = $user->id;
542         $n_messages_to_deleted = $message_to->delete();
543
544         $notice = new Notice;
545         $notice->profile_id = $user->id;
546         $n_notices_deleted = $notice->delete();
547
548         $notice_inbox = new Notice_inbox;
549         $notice_inbox->user_id = $user->id;
550         $n_notices_inbox_deleted = $notice_inbox->delete();
551
552         $profile_tagger = new Profile_tag;
553         $profile_tagger->tagger = $user->id;
554         $n_profiles_tagger_deleted = $profile_tagger->delete();
555
556         $profile_tagged = new Profile_tag;
557         $profile_tagged->tagged = $user->id;
558         $n_profiles_tagged_deleted = $profile_tagged->delete();
559                
560         $remember_me = new Remember_me;
561         $remember_me->user_id = $user->id;
562         $n_remember_mes_deleted = $remember_me->delete();
563
564         $reply_from = new Reply;
565         $reply_from->profile_id = $user->id;
566         $n_replies_from_deleted = $reply_from->delete();
567
568         // not sure if this should be deleted...
569         //TODO: test
570         if (1) {
571             $reply_to = new Reply;
572             $reply_to->replied_id = $user->id;
573             $reply_to->find();
574             while ($reply_to->fetch()) {
575                 $str = print_r($reply_to, true);
576             }
577 //            $n_replies_to_deleted = $reply_to->delete();
578         }
579
580         $subscriber = new Subscription;
581         $subscriber->subscriber = $user->id;
582         $n_subscribers_deleted = $subscriber->delete();
583
584         $subscribed = new Subscription;
585         $subscribed->subscribed = $user->id;
586         $n_subscribeds_deleted = $subscribed->delete();
587
588         $user_openid = new User_openid;
589         $user_openid->user_id = $user->id;
590         $n_user_openids_deleted = $user_openid->delete();
591
592         // last steps
593         if (0) {
594             $profile = new Profile;
595             $profile->id = $user->id;
596             $profile->delete_avatars();
597             $n_profiles_deleted = $profile->delete();
598             $n_users_deleted = $user->delete();
599         }
600
601                 $this->show_form(_("Your account has been deleted. ($str)"), true);
602     }
603 }