]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deleteaccount.php
Extend Action with redirect after login logic, update some actions to use it
[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 class DeleteaccountAction extends Action
52 {
53     private $_complete = false;
54     private $_error = null;
55
56     /**
57      * For initializing members of the class.
58      *
59      * @param array $args misc. arguments
60      *
61      * @return boolean true
62      * @throws ClientException
63      */
64     function prepare(array $args = [])
65     {
66         parent::prepare($args);
67
68         $cur = common_current_user();
69
70         if (empty($cur)) {
71             // TRANS: Client exception displayed trying to delete a user account while not logged in.
72             throw new ClientException(_("Only logged-in users can delete their account."), 403);
73         }
74
75         if (!$cur->hasRight(Right::DELETEACCOUNT)) {
76             // TRANS: Client exception displayed trying to delete a user account without have the rights to do that.
77             throw new ClientException(_("You cannot delete your account."), 403);
78         }
79
80         return true;
81     }
82
83     /**
84      * Handler method
85      *
86      * @return void
87      * @throws AuthorizationException
88      * @throws ServerException
89      */
90     function handle()
91     {
92         parent::handle();
93
94         if ($this->isPost()) {
95             $this->deleteAccount();
96         } else {
97             $this->showPage();
98         }
99         return null;
100     }
101
102     /**
103      * Delete the current user's account
104      *
105      * Checks for the "I am sure." string to make sure the user really
106      * wants to delete their account.
107      *
108      * Then, marks the account as deleted and begins the deletion process
109      * (actually done by a back-end handler).
110      *
111      * If successful it logs the user out, and shows a brief completion message.
112      *
113      * @return void
114      * @throws AuthorizationException
115      * @throws ServerException
116      */
117     function deleteAccount()
118     {
119         $this->checkSessionToken();
120         // !!! If this string is changed, it also needs to be changed in DeleteAccountForm::formData()
121         // TRANS: Confirmation text for user deletion. The user has to type this exactly the same, including punctuation.
122         $iamsure = _('I am sure.');
123         if ($this->trimmed('iamsure') != $iamsure) {
124             // TRANS: Notification for user about the text that must be input to be able to delete a user account.
125             // TRANS: %s is the text that needs to be input.
126             $this->_error = sprintf(_('You must write "%s" exactly in the box.'), $iamsure);
127             $this->showPage();
128             return null;
129         }
130
131         $cur = common_current_user();
132
133         // Mark the account as deleted and shove low-level deletion tasks
134         // to background queues. Removing a lot of posts can take a while...
135
136         if (!$cur->hasRole(Profile_role::DELETED)) {
137             $cur->grantRole(Profile_role::DELETED);
138         }
139
140         $qm = QueueManager::get();
141         $qm->enqueue($cur, 'deluser');
142
143         // The user is really-truly logged out
144
145         common_set_user(null);
146         common_real_login(false); // not logged in
147         common_forgetme(); // don't log back in!
148
149         $this->_complete = true;
150         $this->showPage();
151     }
152
153     /**
154      * Return true if read only.
155      *
156      * MAY override
157      *
158      * @param array $args other arguments
159      *
160      * @return boolean is read only action?
161      */
162     function isReadOnly($args)
163     {
164         return false;
165     }
166
167     /**
168      * Return last modified, if applicable.
169      *
170      * MAY override
171      *
172      * @return string last modified http header
173      */
174     function lastModified()
175     {
176         // For comparison with If-Last-Modified
177         // If not applicable, return null
178         return null;
179     }
180
181     /**
182      * Return etag, if applicable.
183      *
184      * MAY override
185      *
186      * @return string etag http header
187      */
188     function etag()
189     {
190         return null;
191     }
192
193     /**
194      * Shows the page content.
195      *
196      * If the deletion is complete, just shows a completion message.
197      *
198      * Otherwise, shows the deletion form.
199      *
200      * @return void
201      *
202      */
203     function showContent()
204     {
205         if ($this->_complete) {
206             $this->element('p', 'confirmation',
207                 // TRANS: Confirmation that a user account has been deleted.
208                 _('Account deleted.'));
209             return null;
210         }
211
212         if (!empty($this->_error)) {
213             $this->element('p', 'error', $this->_error);
214             $this->_error = null;
215         }
216
217         $form = new DeleteAccountForm($this);
218         $form->show();
219     }
220
221     /**
222      * Show the title of the page
223      *
224      * @return string title
225      */
226
227     function title()
228     {
229         // TRANS: Page title for page on which a user account can be deleted.
230         return _('Delete account');
231     }
232 }
233
234 /**
235  * Form for deleting your account
236  *
237  * Note that this mostly is here to keep you from accidentally deleting your
238  * account.
239  *
240  * @category  Account
241  * @package   StatusNet
242  * @author    Evan Prodromou <evan@status.net>
243  * @copyright 2010 StatusNet, Inc.
244  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
245  * @link      http://status.net/
246  */
247 class DeleteAccountForm extends Form
248 {
249     /**
250      * Class of the form.
251      *
252      * @return string the form's class
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     function action()
265     {
266         return common_local_url('deleteaccount');
267     }
268
269     /**
270      * Output form data
271      *
272      * Instructions plus an 'i am sure' entry box.
273      *
274      * @return void
275      */
276     function formData()
277     {
278         $cur = common_current_user();
279
280         // TRANS: Form text for user deletion form.
281         $msg = '<p>' . _('This will <strong>permanently delete</strong> your account data from this server.') . '</p>';
282
283         if ($cur->hasRight(Right::BACKUPACCOUNT)) {
284             // TRANS: Additional form text for user deletion form shown if a user has account backup rights.
285             // TRANS: %s is a URL to the backup page.
286             $msg .= '<p>' . sprintf(_('You are strongly advised to <a href="%s">back up your data</a> before deletion.'),
287                     common_local_url('backupaccount')) . '</p>';
288         }
289
290         $this->out->elementStart('p');
291         $this->out->raw($msg);
292         $this->out->elementEnd('p');
293
294         // !!! If this string is changed, it also needs to be changed in class DeleteaccountAction.
295         // TRANS: Confirmation text for user deletion. The user has to type this exactly the same, including punctuation.
296         $iamsure = _("I am sure.");
297         $this->out->input('iamsure',
298             // TRANS: Field label for delete account confirmation entry.
299             _('Confirm'),
300             null,
301             // TRANS: Input title for the delete account field.
302             // TRANS: %s is the text that needs to be input.
303             sprintf(_('Enter "%s" to confirm that ' .
304                 'you want to delete your account.'), $iamsure));
305     }
306
307     /**
308      * Buttons for the form
309      *
310      * In this case, a single submit button
311      *
312      * @return void
313      */
314     function formActions()
315     {
316         $this->out->submit('submit',
317             // TRANS: Button text for user account deletion.
318             _m('BUTTON', 'Delete'),
319             'submit',
320             null,
321             // TRANS: Button title for user account deletion.
322             _('Permanently delete your account.'));
323     }
324 }