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