restoreaccount.php 12 KB

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