basemirror.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * PHP version 5
  20. *
  21. * @category Action
  22. * @package StatusNet
  23. * @author Brion Vibber <brion@status.net>
  24. * @copyright 2010 StatusNet, Inc.
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  26. * @link http://status.net/
  27. */
  28. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  29. /**
  30. * Takes parameters:
  31. *
  32. * - feed: a profile ID
  33. * - token: session token to prevent CSRF attacks
  34. * - ajax: boolean; whether to return Ajax or full-browser results
  35. *
  36. * Only works if the current user is logged in.
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @copyright 2010 StatusNet, Inc.
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  42. * @link http://status.net/
  43. */
  44. abstract class BaseMirrorAction extends Action
  45. {
  46. var $user;
  47. var $profile;
  48. /**
  49. * Check pre-requisites and instantiate attributes
  50. *
  51. * @param Array $args array of arguments (URL, GET, POST)
  52. *
  53. * @return boolean success flag
  54. */
  55. protected function prepare(array $args=array())
  56. {
  57. parent::prepare($args);
  58. return $this->sharedBoilerplate();
  59. }
  60. protected function validateFeedUrl($url)
  61. {
  62. if (common_valid_http_url($url)) {
  63. return $url;
  64. } else {
  65. // TRANS: Client error displayed when entering an invalid URL for a feed.
  66. // TRANS: %s is the invalid feed URL.
  67. $this->clientError(sprintf(_m("Invalid feed URL: %s."), $url));
  68. }
  69. }
  70. protected function validateProfile($id)
  71. {
  72. $id = intval($id);
  73. $profile = Profile::getKV('id', $id);
  74. if ($profile && $profile->id != $this->user->id) {
  75. return $profile;
  76. }
  77. // TRANS: Error message returned to user when setting up feed mirroring,
  78. // TRANS: but we were unable to resolve the given URL to a working feed.
  79. $this->clientError(_m('Invalid profile for mirroring.'));
  80. }
  81. /**
  82. *
  83. * @param string $url
  84. * @return Profile
  85. */
  86. protected function profileForFeed($url)
  87. {
  88. try {
  89. // Maybe we got a web page?
  90. $oprofile = Ostatus_profile::ensureProfileURL($url);
  91. } catch (Exception $e) {
  92. // Direct feed URL?
  93. $oprofile = Ostatus_profile::ensureFeedURL($url);
  94. }
  95. if ($oprofile->isGroup()) {
  96. // TRANS: Client error displayed when trying to mirror a GNU social group feed.
  97. $this->clientError(_m('Cannot mirror a GNU social group at this time.'));
  98. }
  99. $this->oprofile = $oprofile; // @todo FIXME: ugly side effect :D
  100. return $oprofile->localProfile();
  101. }
  102. /**
  103. * @todo FIXME: none of this belongs in end classes
  104. * this stuff belongs in shared code!
  105. */
  106. function sharedBoilerplate()
  107. {
  108. // Only allow POST requests
  109. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  110. // TRANS: Client error displayed when trying to use another method than POST.
  111. $this->clientError(_m('This action only accepts POST requests.'));
  112. }
  113. // CSRF protection
  114. $token = $this->trimmed('token');
  115. if (!$token || $token != common_session_token()) {
  116. // TRANS: Client error displayed when the session token does not match or is not given.
  117. $this->clientError(_m('There was a problem with your session token.'.
  118. ' Try again, please.'));
  119. }
  120. // Only for logged-in users
  121. $this->user = common_current_user();
  122. if (empty($this->user)) {
  123. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  124. $this->clientError(_m('Not logged in.'));
  125. }
  126. return true;
  127. }
  128. /**
  129. * Handle request
  130. *
  131. * Does the subscription and returns results.
  132. *
  133. * @param Array $args unused.
  134. *
  135. * @return void
  136. */
  137. protected function handle()
  138. {
  139. // Throws exception on error
  140. $this->saveMirror();
  141. if ($this->boolean('ajax')) {
  142. $this->startHTML('text/xml;charset=utf-8');
  143. $this->elementStart('head');
  144. // TRANS: Page title for subscribed feed mirror.
  145. $this->element('title', null, _m('Subscribed'));
  146. $this->elementEnd('head');
  147. $this->elementStart('body');
  148. $unsubscribe = new EditMirrorForm($this, $this->profile);
  149. $unsubscribe->show();
  150. $this->elementEnd('body');
  151. $this->endHTML();
  152. } else {
  153. $url = common_local_url('mirrorsettings');
  154. common_redirect($url, 303);
  155. }
  156. }
  157. abstract protected function saveMirror();
  158. }