3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
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/
29 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34 * - feed: a profile ID
35 * - token: session token to prevent CSRF attacks
36 * - ajax: boolean; whether to return Ajax or full-browser results
38 * Only works if the current user is logged in.
42 * @copyright 2010 StatusNet, Inc.
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
44 * @link http://status.net/
46 abstract class BaseMirrorAction extends Action
52 * Check pre-requisites and instantiate attributes
54 * @param Array $args array of arguments (URL, GET, POST)
56 * @return boolean success flag
58 protected function prepare(array $args=array())
60 parent::prepare($args);
61 return $this->sharedBoilerplate();
64 protected function validateFeedUrl($url)
66 if (common_valid_http_url($url)) {
69 // TRANS: Client error displayed when entering an invalid URL for a feed.
70 // TRANS: %s is the invalid feed URL.
71 $this->clientError(sprintf(_m("Invalid feed URL: %s."), $url));
75 protected function validateProfile($id)
78 $profile = Profile::getKV('id', $id);
79 if ($profile && $profile->id != $this->user->id) {
82 // TRANS: Error message returned to user when setting up feed mirroring,
83 // TRANS: but we were unable to resolve the given URL to a working feed.
84 $this->clientError(_m('Invalid profile for mirroring.'));
92 protected function profileForFeed($url)
95 // Maybe we got a web page?
96 $oprofile = Ostatus_profile::ensureProfileURL($url);
97 } catch (Exception $e) {
99 $oprofile = Ostatus_profile::ensureFeedURL($url);
101 if ($oprofile->isGroup()) {
102 // TRANS: Client error displayed when trying to mirror a GNU social group feed.
103 $this->clientError(_m('Cannot mirror a GNU social group at this time.'));
105 $this->oprofile = $oprofile; // @todo FIXME: ugly side effect :D
106 return $oprofile->localProfile();
110 * @todo FIXME: none of this belongs in end classes
111 * this stuff belongs in shared code!
113 function sharedBoilerplate()
115 // Only allow POST requests
116 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
117 // TRANS: Client error displayed when trying to use another method than POST.
118 $this->clientError(_m('This action only accepts POST requests.'));
122 $token = $this->trimmed('token');
124 if (!$token || $token != common_session_token()) {
125 // TRANS: Client error displayed when the session token does not match or is not given.
126 $this->clientError(_m('There was a problem with your session token.'.
127 ' Try again, please.'));
130 // Only for logged-in users
132 $this->user = common_current_user();
134 if (empty($this->user)) {
135 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
136 $this->clientError(_m('Not logged in.'));
144 * Does the subscription and returns results.
146 * @param Array $args unused.
150 protected function handle()
152 // Throws exception on error
155 if ($this->boolean('ajax')) {
156 $this->startHTML('text/xml;charset=utf-8');
157 $this->elementStart('head');
158 // TRANS: Page title for subscribed feed mirror.
159 $this->element('title', null, _m('Subscribed'));
160 $this->elementEnd('head');
161 $this->elementStart('body');
162 $unsubscribe = new EditMirrorForm($this, $this->profile);
163 $unsubscribe->show();
164 $this->elementEnd('body');
167 $url = common_local_url('mirrorsettings');
168 common_redirect($url, 303);
172 abstract protected function saveMirror();