]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deleteprofile.php
b1c346cb22204a8ebddd3839a19e3543abd9e531
[quix0rs-gnu-social.git] / actions / deleteprofile.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 class DeleteprofileAction extends Action {
23     function handle($args)
24     {
25         parent::handle($args);
26         $this->server_error(_('Code not yet ready.'));
27         return;
28         if ('POST' === $_SERVER['REQUEST_METHOD']) {
29             $this->handle_post();
30         }
31         else if ('GET' === $_SERVER['REQUEST_METHOD']) {
32             $this->show_form();
33         }
34     }
35
36     function get_instructions()
37     {
38         return _('Export and delete your user information.');
39     }
40
41     function form_header($title, $msg=null, $success=false)
42     {
43         common_show_header($title,
44                            null,
45                            array($msg, $success),
46                            array($this, 'show_top'));
47     }
48
49     function show_feeds_list($feeds)
50     {
51         common_element_start('div', array('class' => 'feedsdel'));
52         common_element('p', null, 'Feeds:');
53         common_element_start('ul', array('class' => 'xoxo'));
54
55         foreach ($feeds as $key => $value) {
56             $this->common_feed_item($feeds[$key]);
57         }
58         common_element_end('ul');
59         common_element_end('div');
60     }
61
62     //TODO move to common.php (and retrace its origin)
63     function common_feed_item($feed)
64     {
65         $user = common_current_user();
66         $nickname = $user->nickname;
67
68         switch($feed['item']) {
69             case 'notices': default:
70                 $feed_classname = $feed['type'];
71                 $feed_mimetype = "application/".$feed['type']."+xml";
72                 $feed_title = "$nickname's ".$feed['version']." notice feed";
73                 $feed['textContent'] = "RSS";
74                 break;
75
76             case 'foaf':
77                 $feed_classname = "foaf";
78                 $feed_mimetype = "application/".$feed['type']."+xml";
79                 $feed_title = "$nickname's FOAF file";
80                 $feed['textContent'] = "FOAF";
81                 break;
82         }
83         common_element_start('li');
84         common_element('a', array('href' => $feed['href'],
85                                   'class' => $feed_classname,
86                                   'type' => $feed_mimetype,
87                                   'title' => $feed_title),
88                             $feed['textContent']);
89         common_element_end('li');
90     }
91
92     function show_form($msg=null, $success=false)
93     {
94         $this->form_header(_('Delete my account'), $msg, $success);
95         common_element('h2', null, _('Delete my account confirmation'));
96         $this->show_confirm_delete_form();
97         common_show_footer();
98     }
99
100     function show_confirm_delete_form()
101     {
102         $user = common_current_user();
103         $notices = DB_DataObject::factory('notice');
104         $notices->profile_id = $user->id;
105         $notice_count = (int) $notices->count();
106
107         common_element_start('form', array('method' => 'POST',
108                                            'id' => 'delete',
109                                            'action' =>
110                                            common_local_url('deleteprofile')));
111
112         common_hidden('token', common_session_token());
113         common_element('p', null, "Last chance to copy your notices and contacts by saving the two links below before deleting your account. Be careful, this operation cannot be undone.");
114
115         $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('limit' => $notice_count, 'nickname' => $user->nickname)),
116                                               'type' => 'rss',
117                                               'version' => 'RSS 1.0',
118                                               'item' => 'notices'),
119                                      1=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
120                                               'type' => 'rdf',
121                                               'version' => 'FOAF',
122                                               'item' => 'foaf')));
123
124         common_checkbox('confirmation', _('Check if you are sure you want to delete your account.'));
125
126         common_submit('deleteaccount', _('Delete my account'));
127         common_element_end('form');
128     }
129
130     function handle_post()
131     {
132         # CSRF protection
133         $token = $this->trimmed('token');
134         if (!$token || $token != common_session_token()) {
135             $this->show_form(_('There was a problem with your session token. Try again, please.'));
136             return;
137         }
138
139         if ($this->arg('deleteaccount') && $this->arg('confirmation')) {
140             $this->delete_account();
141         }
142         $this->show_form();
143     }
144
145     function delete_account()
146     {
147         $user = common_current_user();
148         assert(!is_null($user)); # should already be checked
149
150         // deleted later through the profile
151         /*
152         $avatar = new Avatar;
153         $avatar->profile_id = $user->id;
154         $n_avatars_deleted = $avatar->delete();
155         */
156
157         $fave = new Fave;
158         $fave->user_id = $user->id;
159         $n_faves_deleted = $fave->delete();
160
161         $confirmation = new Confirm_address;
162         $confirmation->user_id = $user->id;
163         $n_confirmations_deleted = $confirmation->delete();
164
165         // TODO foreign stuff...
166
167         $invitation = new Invitation;
168         $invitation->user_id = $user->id;
169         $n_invitations_deleted = $invitation->delete();
170
171         $message_from = new Message;
172         $message_from->from_profile = $user->id;
173         $n_messages_from_deleted = $message_from->delete();
174
175         $message_to = new Message;
176         $message_to->to_profile = $user->id;
177         $n_messages_to_deleted = $message_to->delete();
178
179         $notice_inbox = new Notice_inbox;
180         $notice_inbox->user_id = $user->id;
181         $n_notices_inbox_deleted = $notice_inbox->delete();
182
183         $profile_tagger = new Profile_tag;
184         $profile_tagger->tagger = $user->id;
185         $n_profiles_tagger_deleted = $profile_tagger->delete();
186
187         $profile_tagged = new Profile_tag;
188         $profile_tagged->tagged = $user->id;
189         $n_profiles_tagged_deleted = $profile_tagged->delete();
190
191         $remember_me = new Remember_me;
192         $remember_me->user_id = $user->id;
193         $n_remember_mes_deleted = $remember_me->delete();
194
195         $reply= new Reply;
196         $reply->profile_id = $user->id;
197         $n_replies_deleted = $reply->delete();
198
199         // FIXME we're not removings replies to deleted notices.
200         //       notices should take care of that themselves.
201
202         $notice = new Notice;
203         $notice->profile_id = $user->id;
204         $n_notices_deleted = $notice->delete();
205
206         $subscriber = new Subscription;
207         $subscriber->subscriber = $user->id;
208         $n_subscribers_deleted = $subscriber->delete();
209
210         $subscribed = new Subscription;
211         $subscribed->subscribed = $user->id;
212         $n_subscribeds_deleted = $subscribed->delete();
213
214         $user_openid = new User_openid;
215         $user_openid->user_id = $user->id;
216         $n_user_openids_deleted = $user_openid->delete();
217
218         $profile = new Profile;
219         $profile->id = $user->id;
220         $profile->delete_avatars();
221         $n_profiles_deleted = $profile->delete();
222         $n_users_deleted = $user->delete();
223
224         // logout and redirect to public
225         common_set_user(null);
226         common_real_login(false); # not logged in
227         common_forgetme(); # don't log back in!
228         common_redirect(common_local_url('public'));
229     }
230
231     function show_top($arr)
232     {
233         $msg = $arr[0];
234         $success = $arr[1];
235         if ($msg) {
236             $this->message($msg, $success);
237         } else {
238             $inst = $this->get_instructions();
239             $output = common_markup_to_html($inst);
240             common_element_start('div', 'instructions');
241             common_raw($output);
242             common_element_end('div');
243         }
244         $this->settings_menu();
245     }
246
247     function settings_menu()
248     {
249         # action => array('prompt', 'title')
250         $menu =
251           array('profilesettings' =>
252                 array(_('Profile'),
253                       _('Change your profile settings')),
254                 'emailsettings' =>
255                 array(_('Email'),
256                       _('Change email handling')),
257                 'openidsettings' =>
258                 array(_('OpenID'),
259                       _('Add or remove OpenIDs')),
260                 'smssettings' =>
261                 array(_('SMS'),
262                       _('Updates by SMS')),
263                 'imsettings' =>
264                 array(_('IM'),
265                       _('Updates by instant messenger (IM)')),
266                 'twittersettings' =>
267                 array(_('Twitter'),
268                       _('Twitter integration options')),
269                 'othersettings' =>
270                 array(_('Other'),
271                       _('Other options')));
272
273         $action = $this->trimmed('action');
274         common_element_start('ul', array('id' => 'nav_views'));
275         foreach ($menu as $menuaction => $menudesc) {
276             if ($menuaction == 'imsettings' &&
277                 !common_config('xmpp', 'enabled')) {
278                 continue;
279             }
280             common_menu_item(common_local_url($menuaction),
281                     $menudesc[0],
282                     $menudesc[1],
283                     $action == $menuaction);
284         }
285         common_element_end('ul');
286     }
287 }
288