]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/restoreaccount.php
Merge branch 'righttoleave' 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
52     /**
53      * Returns the title of the page
54      * 
55      * @return string page title
56      */
57
58     function title()
59     {
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
71     function prepare($argarray)
72     {
73         parent::prepare($argarray);
74
75         $cur = common_current_user();
76
77         if (empty($cur)) {
78             throw new ClientException(_('Only logged-in users can restore their account.'), 403);
79         }
80
81         if (!$cur->hasRight(Right::RESTOREACCOUNT)) {
82             throw new ClientException(_('You may not restore your account.'), 403);
83         }
84
85         return true;
86     }
87
88     /**
89      * Handler method
90      *
91      * @param array $argarray is ignored since it's now passed in in prepare()
92      *
93      * @return void
94      */
95
96     function handle($argarray=null)
97     {
98         parent::handle($args);
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
116     function restoreAccount()
117     {
118         $this->checkSessionToken();
119
120         if (!isset($_FILES['restorefile']['error'])) {
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             // No file; probably just a non-AJAX submission.
146             return;
147         case UPLOAD_ERR_NO_TMP_DIR:
148             // TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
149             throw new ClientException(_('Missing a temporary folder.'));
150             return;
151         case UPLOAD_ERR_CANT_WRITE:
152             // TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
153             throw new ClientException(_('Failed to write file to disk.'));
154             return;
155         case UPLOAD_ERR_EXTENSION:
156             // TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
157             throw new ClientException(_('File upload stopped by extension.'));
158             return;
159         default:
160             common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
161                 $_FILES['restorefile']['error']);
162             // TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
163             throw new ClientException(_('System error uploading file.'));
164             return;
165         }
166
167         $filename = $_FILES['restorefile']['tmp_name'];
168
169         try {
170             if (!file_exists($filename)) {
171                 throw new ServerException("No such file '$filename'.");
172             }
173         
174             if (!is_file($filename)) {
175                 throw new ServerException("Not a regular file: '$filename'.");
176             }
177         
178             if (!is_readable($filename)) {
179                 throw new ServerException("File '$filename' not readable.");
180             }
181         
182             common_debug(sprintf(_("Getting backup from file '%s'."), $filename));
183
184             $xml = file_get_contents($filename);
185
186             // This check is costly but we should probably give
187             // the user some info ahead of time.
188
189             $doc = DOMDocument::loadXML($xml);
190
191             $feed = $doc->documentElement;
192
193             if ($feed->namespaceURI != Activity::ATOM ||
194                 $feed->localName != 'feed') {
195                 throw new ClientException(_("Not an atom feed."));
196             }
197
198             // Enqueue for processing.
199
200             $qm = QueueManager::get();
201             $qm->enqueue(array(common_current_user(), $xml, false), 'feedimp');
202
203             $this->success = true;
204             
205             $this->showPage();
206
207         } catch (Exception $e) {
208             // Delete the file and re-throw
209             @unlink($_FILES['restorefile']['tmp_name']);
210             throw $e;
211         }
212     }
213
214     /**
215      * Show a little form so that the person can upload a file to restore
216      *
217      * @return void
218      */
219     
220     function showContent()
221     {
222         if ($this->success) {
223             $this->element('p', null,
224                            _('Feed will be restored. Please wait a few minutes for results.'));
225         } else {
226             $form = new RestoreAccountForm($this);
227             $form->show();
228         }
229     }
230  
231     /**
232      * Return true if read only.
233      *
234      * MAY override
235      *
236      * @param array $args other arguments
237      *
238      * @return boolean is read only action?
239      */
240
241     function isReadOnly($args)
242     {
243         return false;
244     }
245
246     /**
247      * Return last modified, if applicable.
248      *
249      * MAY override
250      *
251      * @return string last modified http header
252      */
253
254     function lastModified()
255     {
256         // For comparison with If-Last-Modified
257         // If not applicable, return null
258         return null;
259     }
260
261     /**
262      * Return etag, if applicable.
263      *
264      * MAY override
265      *
266      * @return string etag http header
267      */
268
269     function etag()
270     {
271         return null;
272     }
273 }
274
275 /**
276  * A form for backing up the account.
277  *
278  * @category  Account
279  * @package   StatusNet
280  * @author    Evan Prodromou <evan@status.net>
281  * @copyright 2010 StatusNet, Inc.
282  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
283  * @link      http://status.net/
284  */
285
286 class RestoreAccountForm extends Form
287 {
288     function __construct($out=null) {
289         parent::__construct($out);
290         $this->enctype = 'multipart/form-data';
291     }
292
293     /**
294      * Class of the form.
295      *
296      * @return string the form's class
297      */
298
299     function formClass()
300     {
301         return 'form_profile_restore';
302     }
303
304     /**
305      * URL the form posts to
306      *
307      * @return string the form's action URL
308      */
309
310     function action()
311     {
312         return common_local_url('restoreaccount');
313     }
314
315     /**
316      * Output form data
317      * 
318      * Really, just instructions for doing a backup.
319      *
320      * @return void
321      */
322
323     function formData()
324     {
325         $this->out->elementStart('p', 'instructions');
326
327         $this->out->raw(_('You can upload a backed-up stream in '.
328                           '<a href="http://activitystrea.ms/">Activity Streams</a> format.'));
329         
330         $this->out->elementEnd('p');
331
332         $this->out->elementStart('ul', 'form_data');
333
334         $this->out->elementStart('li', array ('id' => 'settings_attach'));
335         $this->out->element('input', array('name' => 'restorefile',
336                                            'type' => 'file',
337                                            'id' => 'restorefile'));
338         $this->out->elementEnd('li');
339
340         $this->out->elementEnd('ul');
341     }
342
343     /**
344      * Buttons for the form
345      * 
346      * In this case, a single submit button
347      *
348      * @return void
349      */
350
351     function formActions()
352     {
353         $this->out->submit('submit',
354                            _m('BUTTON', 'Upload'),
355                            'submit',
356                            null,
357                            _('Upload the file'));
358     }
359 }