]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deleteaccount.php
An action to delete your own account
[quix0rs-gnu-social.git] / actions / deleteaccount.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Delete your own account
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Account
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Action to delete your own account
39  * 
40  * Note that this is distinct from DeleteuserAction, which see. I thought
41  * that making that action do both things (delete another user and delete the
42  * current user) would open a lot of holes. I'm open to refactoring, however.
43  *
44  * @category  Account
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49  * @link      http://status.net/
50  */
51
52 class DeleteaccountAction extends Action
53 {
54     private $_complete = false;
55     private $_error    = null;
56
57     /**
58      * For initializing members of the class.
59      *
60      * @param array $argarray misc. arguments
61      *
62      * @return boolean true
63      */
64
65     function prepare($argarray)
66     {
67         parent::prepare($argarray);
68         
69         $cur = common_current_user();
70
71         if (empty($cur)) {
72             throw new ClientException(_("Only logged-in users ".
73                                         "can delete their account."), 403);
74         }
75
76         if (!$cur->hasRight(Right::DELETEACCOUNT)) {
77             throw new ClientException(_("You cannot delete your account."), 403);
78         }
79
80         return true;
81     }
82
83     /**
84      * Handler method
85      *
86      * @param array $argarray is ignored since it's now passed in in prepare()
87      *
88      * @return void
89      */
90
91     function handle($argarray=null)
92     {
93         parent::handle($argarray);
94
95         if ($this->isPost()) {
96             $this->deleteAccount();
97         } else {
98             $this->showPage();
99         }
100         return;
101     }
102
103     /**
104      * Return true if read only.
105      *
106      * MAY override
107      *
108      * @param array $args other arguments
109      *
110      * @return boolean is read only action?
111      */
112
113     function isReadOnly($args)
114     {
115         return false;
116     }
117
118     /**
119      * Return last modified, if applicable.
120      *
121      * MAY override
122      *
123      * @return string last modified http header
124      */
125
126     function lastModified()
127     {
128         // For comparison with If-Last-Modified
129         // If not applicable, return null
130         return null;
131     }
132
133     /**
134      * Return etag, if applicable.
135      *
136      * MAY override
137      *
138      * @return string etag http header
139      */
140
141     function etag()
142     {
143         return null;
144     }
145
146     /**
147      * Delete the current user's account
148      * 
149      * Checks for the "I am sure." string to make sure the user really
150      * wants to delete their account.
151      *
152      * Then, marks the account as deleted and begins the deletion process
153      * (actually done by a back-end handler).
154      *
155      * If successful it logs the user out, and shows a brief completion message.
156      *
157      * @return void
158      */
159
160     function deleteAccount()
161     {
162         $this->checkSessionToken();
163
164         if ($this->trimmed('iamsure') != _('I am sure.')) {
165             $this->_error = _('You must write  "I am sure." exactly in the box.');
166             $this->showPage();
167             return;
168         }
169
170         $cur = common_current_user();
171
172         // Mark the account as deleted and shove low-level deletion tasks
173         // to background queues. Removing a lot of posts can take a while...
174
175         if (!$cur->hasRole(Profile_role::DELETED)) {
176             $cur->grantRole(Profile_role::DELETED);
177         }
178
179         $qm = QueueManager::get();
180         $qm->enqueue($cur, 'deluser');
181
182         // The user is really-truly logged out
183
184         common_set_user(null);
185         common_real_login(false); // not logged in
186         common_forgetme(); // don't log back in!
187
188         $this->_complete = true;
189         $this->showPage();
190     }
191
192     /**
193      * Shows the page content.
194      * 
195      * If the deletion is complete, just shows a completion message.
196      *
197      * Otherwise, shows the deletion form.
198      *
199      * @return void
200      *
201      */
202
203     function showContent()
204     {
205         if ($this->_complete) {
206             $this->element('p', 'confirmation', 
207                            _('Account deleted.'));
208             return;
209         }
210
211         if (!empty($this->_error)) {
212             $this->element('p', 'error', $this->_error);
213             $this->_error = null;
214         }
215
216         $form = new DeleteAccountForm($this);
217         $form->show();
218     }
219     
220     /**
221      * Show the title of the page
222      *
223      * @return string title
224      */
225
226     function title()
227     {
228         return _('Delete account');
229     }
230 }
231
232 /**
233  * Form for deleting your account
234  * 
235  * Note that this mostly is here to keep you from accidentally deleting your
236  * account.
237  *
238  * @category  Account
239  * @package   StatusNet
240  * @author    Evan Prodromou <evan@status.net>
241  * @copyright 2010 StatusNet, Inc.
242  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
243  * @link      http://status.net/
244  */
245
246 class DeleteAccountForm extends Form
247 {
248     /**
249      * Class of the form.
250      *
251      * @return string the form's class
252      */
253
254     function formClass()
255     {
256         return 'form_profile_delete';
257     }
258
259     /**
260      * URL the form posts to
261      *
262      * @return string the form's action URL
263      */
264
265     function action()
266     {
267         return common_local_url('deleteaccount');
268     }
269
270     /**
271      * Output form data
272      * 
273      * Instructions plus an 'i am sure' entry box.
274      *
275      * @return void
276      */
277
278     function formData()
279     {
280         $cur = common_current_user();
281
282         $msg = _('<p>This will <strong>permanently delete</strong> '.
283                  'your account data from this server. </p>');
284
285         if ($cur->hasRight(Right::BACKUPACCOUNT)) {
286             $msg .= sprintf(_('<p>You are strongly advised to '.
287                               '<a href="%s">back up your data</a>'
288                               ' before deletion.</p>'),
289                            common_local_url('backupaccount'));
290         }
291
292         $this->out->elementStart('p');
293         $this->out->raw($msg);
294         $this->out->elementEnd('p');
295
296         $this->out->input('iamsure',
297                           _('Confirm'),
298                           null,
299                           _('Enter "I am sure." to confirm that '.
300                             'you want to delete your account.'));
301     }
302
303     /**
304      * Buttons for the form
305      * 
306      * In this case, a single submit button
307      *
308      * @return void
309      */
310
311     function formActions()
312     {
313         $this->out->submit('submit',
314                            _m('BUTTON', 'Delete'),
315                            'submit',
316                            null,
317                            _('Permanently your account'));
318     }
319 }