backupaccount.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Download a backup of your own account to 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. * Download a backup of your own account to the browser
  37. *
  38. * We go through some hoops to make this only respond to POST, since
  39. * it's kind of expensive and there's probably some downside to having
  40. * your account in all kinds of search engines.
  41. *
  42. * @category Account
  43. * @package StatusNet
  44. * @author Evan Prodromou <evan@status.net>
  45. * @copyright 2010 StatusNet, Inc.
  46. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  47. * @link http://status.net/
  48. */
  49. class BackupaccountAction extends Action
  50. {
  51. /**
  52. * Returns the title of the page
  53. *
  54. * @return string page title
  55. */
  56. function title()
  57. {
  58. // TRANS: Title for backup account page.
  59. return _('Backup account');
  60. }
  61. /**
  62. * For initializing members of the class.
  63. *
  64. * @param array $argarray misc. arguments
  65. *
  66. * @return boolean true
  67. */
  68. function prepare($argarray)
  69. {
  70. parent::prepare($argarray);
  71. $cur = common_current_user();
  72. if (empty($cur)) {
  73. // TRANS: Client exception thrown when trying to backup an account while not logged in.
  74. throw new ClientException(_('Only logged-in users can backup their account.'), 403);
  75. }
  76. if (!$cur->hasRight(Right::BACKUPACCOUNT)) {
  77. // TRANS: Client exception thrown when trying to backup an account without having backup rights.
  78. throw new ClientException(_('You may not backup your account.'), 403);
  79. }
  80. return true;
  81. }
  82. /**
  83. * Handler method
  84. *
  85. * @param array $argarray is ignored since it's now passed in in prepare()
  86. *
  87. * @return void
  88. */
  89. function handle($argarray=null)
  90. {
  91. parent::handle($argarray);
  92. if ($this->isPost()) {
  93. $this->sendFeed();
  94. } else {
  95. $this->showPage();
  96. }
  97. return;
  98. }
  99. /**
  100. * Send a feed of the user's activities to the browser
  101. *
  102. * Uses the UserActivityStream class; may take a long time!
  103. *
  104. * @return void
  105. */
  106. function sendFeed()
  107. {
  108. $cur = common_current_user();
  109. $stream = new UserActivityStream($cur, true, UserActivityStream::OUTPUT_RAW);
  110. header('Content-Disposition: attachment; filename='.$cur->nickname.'.atom');
  111. header('Content-Type: application/atom+xml; charset=utf-8');
  112. // @fixme atom feed logic is in getString...
  113. // but we just want it to output to the outputter.
  114. $this->raw($stream->getString());
  115. }
  116. /**
  117. * Show a little form so that the person can request a backup.
  118. *
  119. * @return void
  120. */
  121. function showContent()
  122. {
  123. $form = new BackupAccountForm($this);
  124. $form->show();
  125. }
  126. /**
  127. * Return true if read only.
  128. *
  129. * MAY override
  130. *
  131. * @param array $args other arguments
  132. *
  133. * @return boolean is read only action?
  134. */
  135. function isReadOnly($args)
  136. {
  137. return true;
  138. }
  139. /**
  140. * Return last modified, if applicable.
  141. *
  142. * MAY override
  143. *
  144. * @return string last modified http header
  145. */
  146. function lastModified()
  147. {
  148. // For comparison with If-Last-Modified
  149. // If not applicable, return null
  150. return null;
  151. }
  152. /**
  153. * Return etag, if applicable.
  154. *
  155. * MAY override
  156. *
  157. * @return string etag http header
  158. */
  159. function etag()
  160. {
  161. return null;
  162. }
  163. }
  164. /**
  165. * A form for backing up the account.
  166. *
  167. * @category Account
  168. * @package StatusNet
  169. * @author Evan Prodromou <evan@status.net>
  170. * @copyright 2010 StatusNet, Inc.
  171. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  172. * @link http://status.net/
  173. */
  174. class BackupAccountForm extends Form
  175. {
  176. /**
  177. * Class of the form.
  178. *
  179. * @return string the form's class
  180. */
  181. function formClass()
  182. {
  183. return 'form_profile_backup';
  184. }
  185. /**
  186. * URL the form posts to
  187. *
  188. * @return string the form's action URL
  189. */
  190. function action()
  191. {
  192. return common_local_url('backupaccount');
  193. }
  194. /**
  195. * Output form data
  196. *
  197. * Really, just instructions for doing a backup.
  198. *
  199. * @return void
  200. */
  201. function formData()
  202. {
  203. $msg =
  204. // TRANS: Information displayed on the backup account page.
  205. _('You can backup your account data in '.
  206. '<a href="http://activitystrea.ms/">Activity Streams</a> '.
  207. 'format. This is an experimental feature and provides an '.
  208. 'incomplete backup; private account '.
  209. 'information like email and IM addresses is not backed up. '.
  210. 'Additionally, uploaded files and direct messages are not '.
  211. 'backed up.');
  212. $this->out->elementStart('p');
  213. $this->out->raw($msg);
  214. $this->out->elementEnd('p');
  215. }
  216. /**
  217. * Buttons for the form
  218. *
  219. * In this case, a single submit button
  220. *
  221. * @return void
  222. */
  223. function formActions()
  224. {
  225. $this->out->submit('submit',
  226. // TRANS: Submit button to backup an account on the backup account page.
  227. _m('BUTTON', 'Backup'),
  228. 'submit',
  229. null,
  230. // TRANS: Title for submit button to backup an account on the backup account page.
  231. _('Backup your account.'));
  232. }
  233. }