3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Restore a backup of your own account from the browser
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Restore a backup of your own account from the browser
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/
48 class RestoreaccountAction extends Action
50 private $success = false;
53 * Returns the title of the page
55 * @return string page title
60 return _("Restore account");
64 * For initializing members of the class.
66 * @param array $argarray misc. arguments
68 * @return boolean true
71 function prepare($argarray)
73 parent::prepare($argarray);
75 $cur = common_current_user();
78 throw new ClientException(_('Only logged-in users can restore their account.'), 403);
81 if (!$cur->hasRight(Right::RESTOREACCOUNT)) {
82 throw new ClientException(_('You may not restore your account.'), 403);
91 * @param array $argarray is ignored since it's now passed in in prepare()
96 function handle($argarray=null)
98 parent::handle($argarray);
100 if ($this->isPost()) {
101 $this->restoreAccount();
109 * Queue a file for restoration
111 * Uses the UserActivityStream class; may take a long time!
116 function restoreAccount()
118 $this->checkSessionToken();
120 if (!isset($_FILES['restorefile']['error'])) {
121 throw new ClientException(_('No uploaded file.'));
124 switch ($_FILES['restorefile']['error']) {
125 case UPLOAD_ERR_OK: // success, jump out
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.'));
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.'));
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.'));
144 case UPLOAD_ERR_NO_FILE:
145 // No file; probably just a non-AJAX submission.
146 throw new ClientException(_('No uploaded file.'));
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.'));
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.'));
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.'));
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.'));
168 $filename = $_FILES['restorefile']['tmp_name'];
171 if (!file_exists($filename)) {
172 throw new ServerException("No such file '$filename'.");
175 if (!is_file($filename)) {
176 throw new ServerException("Not a regular file: '$filename'.");
179 if (!is_readable($filename)) {
180 throw new ServerException("File '$filename' not readable.");
183 common_debug(sprintf(_("Getting backup from file '%s'."), $filename));
185 $xml = file_get_contents($filename);
187 // This check is costly but we should probably give
188 // the user some info ahead of time.
189 $doc = new DOMDocument();
191 // Disable PHP warnings so we don't spew low-level XML errors to output...
192 // would be nice if we can just get exceptions instead.
193 $old_err = error_reporting();
194 error_reporting($old_err & ~E_WARNING);
196 error_reporting($old_err);
198 $feed = $doc->documentElement;
201 $feed->namespaceURI != Activity::ATOM ||
202 $feed->localName != 'feed') {
203 throw new ClientException(_("Not an atom feed."));
206 // Enqueue for processing.
208 $qm = QueueManager::get();
209 $qm->enqueue(array(common_current_user(), $xml, false), 'feedimp');
211 $this->success = true;
215 } catch (Exception $e) {
216 // Delete the file and re-throw
217 @unlink($_FILES['restorefile']['tmp_name']);
223 * Show a little form so that the person can upload a file to restore
228 function showContent()
230 if ($this->success) {
231 $this->element('p', null,
232 _('Feed will be restored. Please wait a few minutes for results.'));
234 $form = new RestoreAccountForm($this);
240 * Return true if read only.
244 * @param array $args other arguments
246 * @return boolean is read only action?
249 function isReadOnly($args)
255 * Return last modified, if applicable.
259 * @return string last modified http header
262 function lastModified()
264 // For comparison with If-Last-Modified
265 // If not applicable, return null
270 * Return etag, if applicable.
274 * @return string etag http header
284 * A form for backing up the account.
288 * @author Evan Prodromou <evan@status.net>
289 * @copyright 2010 StatusNet, Inc.
290 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
291 * @link http://status.net/
294 class RestoreAccountForm extends Form
296 function __construct($out=null) {
297 parent::__construct($out);
298 $this->enctype = 'multipart/form-data';
304 * @return string the form's class
309 return 'form_profile_restore';
313 * URL the form posts to
315 * @return string the form's action URL
320 return common_local_url('restoreaccount');
326 * Really, just instructions for doing a backup.
333 $this->out->elementStart('p', 'instructions');
335 $this->out->raw(_('You can upload a backed-up stream in '.
336 '<a href="http://activitystrea.ms/">Activity Streams</a> format.'));
338 $this->out->elementEnd('p');
340 $this->out->elementStart('ul', 'form_data');
342 $this->out->elementStart('li', array ('id' => 'settings_attach'));
343 $this->out->element('input', array('name' => 'restorefile',
345 'id' => 'restorefile'));
346 $this->out->elementEnd('li');
348 $this->out->elementEnd('ul');
352 * Buttons for the form
354 * In this case, a single submit button
359 function formActions()
361 $this->out->submit('submit',
362 _m('BUTTON', 'Upload'),
365 _('Upload the file'));