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