3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2010, StatusNet, Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2008-2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
38 * Subscribing to a profile. Does not work for OMB 0.1 remote subscriptions,
39 * but may work for other remote subscription protocols, like OStatus.
43 * - subscribeto: a profile ID
44 * - token: session token to prevent CSRF attacks
45 * - ajax: boolean; whether to return Ajax or full-browser results
47 * Only works if the current user is logged in.
51 * @author Evan Prodromou <evan@status.net>
52 * @copyright 2008-2010 StatusNet, Inc.
53 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
54 * @link http://status.net/
56 class SubscribeAction extends Action
62 * Check pre-requisites and instantiate attributes
64 * @param Array $args array of arguments (URL, GET, POST)
66 * @return boolean success flag
68 function prepare($args)
70 parent::prepare($args);
72 // Only allow POST requests
74 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
75 // TRANS: Client error displayed trying to perform any request method other than POST.
76 // TRANS: Do not translate POST.
77 $this->clientError(_('This action only accepts POST requests.'));
83 $token = $this->trimmed('token');
85 if (!$token || $token != common_session_token()) {
86 // TRANS: Client error displayed when the session token is not okay.
87 $this->clientError(_('There was a problem with your session token.'.
88 ' Try again, please.'));
92 // Only for logged-in users
94 $this->user = common_current_user();
96 if (empty($this->user)) {
97 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
98 $this->clientError(_('Not logged in.'));
102 // Profile to subscribe to
104 $other_id = $this->arg('subscribeto');
106 $this->other = Profile::staticGet('id', $other_id);
108 if (empty($this->other)) {
109 // TRANS: Client error displayed trying to subscribe to a non-existing profile.
110 $this->clientError(_('No such profile.'));
114 // OMB 0.1 doesn't have a mechanism for local-server-
115 // originated subscription.
117 $omb01 = Remote_profile::staticGet('id', $other_id);
119 if (!empty($omb01)) {
120 // TRANS: Client error displayed trying to subscribe to an OMB 0.1 remote profile.
121 $this->clientError(_('You cannot subscribe to an OMB 0.1'.
122 ' remote profile with this action.'));
132 * Does the subscription and returns results.
134 * @param Array $args unused.
138 function handle($args)
140 // Throws exception on error
142 $sub = Subscription::start($this->user->getProfile(),
145 if ($this->boolean('ajax')) {
146 $this->startHTML('text/xml;charset=utf-8');
147 $this->elementStart('head');
148 // TRANS: Page title when subscription succeeded.
149 $this->element('title', null, _('Subscribed'));
150 $this->elementEnd('head');
151 $this->elementStart('body');
152 if ($sub instanceof Subscription) {
153 $form = new UnsubscribeForm($this, $this->other);
155 $form = new CancelSubscriptionForm($this, $this->other);
158 $this->elementEnd('body');
159 $this->elementEnd('html');
161 $url = common_local_url('subscriptions',
162 array('nickname' => $this->user->nickname));
163 common_redirect($url, 303);