restoreaccount.php 12 KB

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