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