]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/restoreaccount.php
[CORE][StoreRemoteMedia] Fixed bug where sometimes images were written outside the...
[quix0rs-gnu-social.git] / actions / restoreaccount.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Restore a backup of your own account from the browser
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  * Restore a backup of your own account from the browser
39  *
40  * @category  Account
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class RestoreaccountAction extends Action
48 {
49     private $success = false;
50     private $inprogress = false;
51
52     /**
53      * Returns the title of the page
54      *
55      * @return string page title
56      */
57     function title()
58     {
59         // TRANS: Page title for page where a user account can be restored from backup.
60         return _('Restore account');
61     }
62
63     /**
64      * For initializing members of the class.
65      *
66      * @param array $args misc. arguments
67      *
68      * @return boolean true
69      * @throws ClientException
70      */
71     function prepare(array $args = [])
72     {
73         parent::prepare($args);
74
75         $cur = common_current_user();
76
77         if (empty($cur)) {
78             // TRANS: Client exception displayed when trying to restore an account while not logged in.
79             throw new ClientException(_('Only logged-in users can restore their account.'), 403);
80         }
81
82         if (!$cur->hasRight(Right::RESTOREACCOUNT)) {
83             // TRANS: Client exception displayed when trying to restore an account without having restore rights.
84             throw new ClientException(_('You may not restore your account.'), 403);
85         }
86
87         return true;
88     }
89
90     /**
91      * Handler method
92      *
93      * @return void
94      * @throws ClientException
95      */
96     function handle()
97     {
98         parent::handle();
99
100         if ($this->isPost()) {
101             $this->restoreAccount();
102         } else {
103             $this->showPage();
104         }
105         return null;
106     }
107
108     /**
109      * Queue a file for restoration
110      *
111      * Uses the UserActivityStream class; may take a long time!
112      *
113      * @return void
114      * @throws ClientException
115      * @throws Exception
116      */
117     function restoreAccount()
118     {
119         $this->checkSessionToken();
120
121         if (!isset($_FILES['restorefile']['error'])) {
122             // TRANS: Client exception displayed trying to restore an account while something went wrong uploading a file.
123             throw new ClientException(_('No uploaded file.'));
124         }
125
126         switch ($_FILES['restorefile']['error']) {
127         case UPLOAD_ERR_OK: // success, jump out
128             break;
129         case UPLOAD_ERR_INI_SIZE:
130             // TRANS: Client exception thrown when an uploaded file is larger than set in php.ini.
131             throw new ClientException(_('The uploaded file exceeds the ' .
132                 'upload_max_filesize directive in php.ini.'));
133         case UPLOAD_ERR_FORM_SIZE:
134             throw new ClientException(
135                 // TRANS: Client exception.
136                 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
137                 ' that was specified in the HTML form.'));
138         case UPLOAD_ERR_PARTIAL:
139             @unlink($_FILES['restorefile']['tmp_name']);
140             // TRANS: Client exception.
141             throw new ClientException(_('The uploaded file was only' .
142                 ' partially uploaded.'));
143         case UPLOAD_ERR_NO_FILE:
144             // TRANS: Client exception. No file; probably just a non-AJAX submission.
145             throw new ClientException(_('No uploaded file.'));
146         case UPLOAD_ERR_NO_TMP_DIR:
147             // TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
148             throw new ClientException(_('Missing a temporary folder.'));
149         case UPLOAD_ERR_CANT_WRITE:
150             // TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
151             throw new ClientException(_('Failed to write file to disk.'));
152         case UPLOAD_ERR_EXTENSION:
153             // TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
154             throw new ClientException(_('File upload stopped by extension.'));
155         default:
156             common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
157                 $_FILES['restorefile']['error']);
158             // TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
159             throw new ClientException(_('System error uploading file.'));
160         }
161
162         $filename = $_FILES['restorefile']['tmp_name'];
163
164         try {
165             if (!file_exists($filename)) {
166                 // TRANS: Server exception thrown when an expected file upload could not be found.
167                 throw new ServerException(_("No such file '$filename'."));
168             }
169
170             if (!is_file($filename)) {
171                 // TRANS: Server exception thrown when an expected file upload is not an actual file.
172                 throw new ServerException(_("Not a regular file: '$filename'."));
173             }
174
175             if (!is_readable($filename)) {
176                 // TRANS: Server exception thrown when an expected file upload could not be read.
177                 throw new ServerException(_("File '$filename' not readable."));
178             }
179
180             common_debug(sprintf("Getting backup from file '%s'.", $filename));
181
182             $xml = file_get_contents($filename);
183
184             // This check is costly but we should probably give
185             // the user some info ahead of time.
186             $doc = new DOMDocument();
187
188             // Disable PHP warnings so we don't spew low-level XML errors to output...
189             // would be nice if we can just get exceptions instead.
190             $old_err = error_reporting();
191             error_reporting($old_err & ~E_WARNING);
192             $doc->loadXML($xml);
193             error_reporting($old_err);
194
195             $feed = $doc->documentElement;
196
197             if (!$feed ||
198                 $feed->namespaceURI != Activity::ATOM ||
199                 $feed->localName != 'feed') {
200                 // TRANS: Client exception thrown when a feed is not an Atom feed.
201                 throw new ClientException(_("Not an Atom feed."));
202             }
203
204             // Enqueue for processing.
205
206             $qm = QueueManager::get();
207             $qm->enqueue([common_current_user(), $xml, false], 'feedimp');
208
209             if ($qm instanceof UnQueueManager) {
210                 // No active queuing means we've actually just completed the job!
211                 $this->success = true;
212             } else {
213                 // We've fed data into background queues, and it's probably still running.
214                 $this->inprogress = true;
215             }
216             $this->showPage();
217
218         } catch (Exception $e) {
219             // Delete the file and re-throw
220             @unlink($_FILES['restorefile']['tmp_name']);
221             throw $e;
222         }
223     }
224
225     /**
226      * Show a little form so that the person can upload a file to restore
227      *
228      * @return void
229      */
230     function showContent()
231     {
232         if ($this->success) {
233             $this->element('p', null,
234                            // TRANS: Success message when a feed has been restored.
235                            _('Feed has been restored. Your old posts should now appear in search and your profile page.'));
236         } else if ($this->inprogress) {
237             $this->element('p', null,
238                            // TRANS: Message when a feed restore is in progress.
239                            _('Feed will be restored. Please wait a few minutes for results.'));
240         } else {
241             $form = new RestoreAccountForm($this);
242             $form->show();
243         }
244     }
245
246     /**
247      * Return true if read only.
248      *
249      * MAY override
250      *
251      * @param array $args other arguments
252      *
253      * @return boolean is read only action?
254      */
255     function isReadOnly($args)
256     {
257         return false;
258     }
259
260     /**
261      * Return last modified, if applicable.
262      *
263      * MAY override
264      *
265      * @return string last modified http header
266      */
267     function lastModified()
268     {
269         // For comparison with If-Last-Modified
270         // If not applicable, return null
271         return null;
272     }
273
274     /**
275      * Return etag, if applicable.
276      *
277      * MAY override
278      *
279      * @return string etag http header
280      */
281     function etag()
282     {
283         return null;
284     }
285 }
286
287 /**
288  * A form for backing up the account.
289  *
290  * @category  Account
291  * @package   StatusNet
292  * @author    Evan Prodromou <evan@status.net>
293  * @copyright 2010 StatusNet, Inc.
294  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
295  * @link      http://status.net/
296  */
297 class RestoreAccountForm extends Form
298 {
299     function __construct($out=null) {
300         parent::__construct($out);
301         $this->enctype = 'multipart/form-data';
302     }
303
304     /**
305      * Class of the form.
306      *
307      * @return string the form's class
308      */
309     function formClass()
310     {
311         return 'form_profile_restore';
312     }
313
314     /**
315      * URL the form posts to
316      *
317      * @return string the form's action URL
318      */
319     function action()
320     {
321         return common_local_url('restoreaccount');
322     }
323
324     /**
325      * Output form data
326      *
327      * Really, just instructions for doing a backup.
328      *
329      * @return void
330      */
331     function formData()
332     {
333         $this->out->elementStart('p', 'instructions');
334
335         // TRANS: Form instructions for feed restore.
336         $this->out->raw(_('You can upload a backed-up timeline in '.
337                           '<a href="http://activitystrea.ms/">Activity Streams</a> format.'));
338
339         $this->out->elementEnd('p');
340
341         $this->out->elementStart('ul', 'form_data');
342
343         $this->out->elementStart('li', array ('id' => 'settings_attach'));
344         $this->out->element('input', array('name' => 'restorefile',
345                                            'type' => 'file',
346                                            'id' => 'restorefile'));
347         $this->out->elementEnd('li');
348
349         $this->out->elementEnd('ul');
350     }
351
352     /**
353      * Buttons for the form
354      *
355      * In this case, a single submit button
356      *
357      * @return void
358      */
359     function formActions()
360     {
361         $this->out->submit('submit',
362                            // TRANS: Submit button to confirm upload of a user backup file for account restore.
363                            _m('BUTTON', 'Upload'),
364                            'submit',
365                            null,
366                            // TRANS: Title for submit button to confirm upload of a user backup file for account restore.
367                            _('Upload the file'));
368     }
369 }