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;
51 private $inprogress = false;
54 * Returns the title of the page
56 * @return string page title
61 return _("Restore account");
65 * For initializing members of the class.
67 * @param array $argarray misc. arguments
69 * @return boolean true
72 function prepare($argarray)
74 parent::prepare($argarray);
76 $cur = common_current_user();
79 throw new ClientException(_('Only logged-in users can restore their account.'), 403);
82 if (!$cur->hasRight(Right::RESTOREACCOUNT)) {
83 throw new ClientException(_('You may not restore your account.'), 403);
92 * @param array $argarray is ignored since it's now passed in in prepare()
97 function handle($argarray=null)
99 parent::handle($argarray);
101 if ($this->isPost()) {
102 $this->restoreAccount();
110 * Queue a file for restoration
112 * Uses the UserActivityStream class; may take a long time!
117 function restoreAccount()
119 $this->checkSessionToken();
121 if (!isset($_FILES['restorefile']['error'])) {
122 throw new ClientException(_('No uploaded file.'));
125 switch ($_FILES['restorefile']['error']) {
126 case UPLOAD_ERR_OK: // success, jump out
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.'));
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.'));
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.'));
145 case UPLOAD_ERR_NO_FILE:
146 // No file; probably just a non-AJAX submission.
147 throw new ClientException(_('No uploaded file.'));
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.'));
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.'));
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.'));
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.'));
169 $filename = $_FILES['restorefile']['tmp_name'];
172 if (!file_exists($filename)) {
173 throw new ServerException("No such file '$filename'.");
176 if (!is_file($filename)) {
177 throw new ServerException("Not a regular file: '$filename'.");
180 if (!is_readable($filename)) {
181 throw new ServerException("File '$filename' not readable.");
184 common_debug(sprintf(_("Getting backup from file '%s'."), $filename));
186 $xml = file_get_contents($filename);
188 // This check is costly but we should probably give
189 // the user some info ahead of time.
190 $doc = new DOMDocument();
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);
197 error_reporting($old_err);
199 $feed = $doc->documentElement;
202 $feed->namespaceURI != Activity::ATOM ||
203 $feed->localName != 'feed') {
204 throw new ClientException(_("Not an atom feed."));
207 // Enqueue for processing.
209 $qm = QueueManager::get();
210 $qm->enqueue(array(common_current_user(), $xml, false), 'feedimp');
212 if ($qm instanceof UnQueueManager) {
213 // No active queuing means we've actually just completed the job!
214 $this->success = true;
216 // We've fed data into background queues, and it's probably still running.
217 $this->inprogress = true;
221 } catch (Exception $e) {
222 // Delete the file and re-throw
223 @unlink($_FILES['restorefile']['tmp_name']);
229 * Show a little form so that the person can upload a file to restore
234 function showContent()
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.'));
243 $form = new RestoreAccountForm($this);
249 * Return true if read only.
253 * @param array $args other arguments
255 * @return boolean is read only action?
258 function isReadOnly($args)
264 * Return last modified, if applicable.
268 * @return string last modified http header
271 function lastModified()
273 // For comparison with If-Last-Modified
274 // If not applicable, return null
279 * Return etag, if applicable.
283 * @return string etag http header
293 * A form for backing up the account.
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/
303 class RestoreAccountForm extends Form
305 function __construct($out=null) {
306 parent::__construct($out);
307 $this->enctype = 'multipart/form-data';
313 * @return string the form's class
318 return 'form_profile_restore';
322 * URL the form posts to
324 * @return string the form's action URL
329 return common_local_url('restoreaccount');
335 * Really, just instructions for doing a backup.
342 $this->out->elementStart('p', 'instructions');
344 $this->out->raw(_('You can upload a backed-up stream in '.
345 '<a href="http://activitystrea.ms/">Activity Streams</a> format.'));
347 $this->out->elementEnd('p');
349 $this->out->elementStart('ul', 'form_data');
351 $this->out->elementStart('li', array ('id' => 'settings_attach'));
352 $this->out->element('input', array('name' => 'restorefile',
354 'id' => 'restorefile'));
355 $this->out->elementEnd('li');
357 $this->out->elementEnd('ul');
361 * Buttons for the form
363 * In this case, a single submit button
368 function formActions()
370 $this->out->submit('submit',
371 _m('BUTTON', 'Upload'),
374 _('Upload the file'));