3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2011, 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. Likely to work for OStatus profiles.
42 * - subscribeto: a profile ID
43 * - token: session token to prevent CSRF attacks
44 * - ajax: boolean; whether to return Ajax or full-browser results
46 * Only works if the current user is logged in.
50 * @author Evan Prodromou <evan@status.net>
51 * @copyright 2008-2010 StatusNet, Inc.
52 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
53 * @link http://status.net/
55 class SubscribeAction extends Action
61 * Check pre-requisites and instantiate attributes
63 * @param Array $args array of arguments (URL, GET, POST)
65 * @return boolean success flag
67 function prepare(array $args = array())
69 parent::prepare($args);
71 // Only allow POST requests
73 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
74 // TRANS: Client error displayed trying to perform any request method other than POST.
75 // TRANS: Do not translate POST.
76 $this->clientError(_('This action only accepts POST requests.'));
81 $token = $this->trimmed('token');
83 if (!$token || $token != common_session_token()) {
84 // TRANS: Client error displayed when the session token is not okay.
85 $this->clientError(_('There was a problem with your session token.'.
86 ' Try again, please.'));
89 // Only for logged-in users
91 $this->user = common_current_user();
93 if (empty($this->user)) {
94 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
95 $this->clientError(_('Not logged in.'));
98 // Profile to subscribe to
100 $other_id = $this->arg('subscribeto');
102 $this->other = Profile::getKV('id', $other_id);
104 if (empty($this->other)) {
105 // TRANS: Client error displayed trying to subscribe to a non-existing profile.
106 $this->clientError(_('No such profile.'));
115 * Does the subscription and returns results.
117 * @param Array $args unused.
123 // Throws exception on error
125 $sub = Subscription::ensureStart($this->user->getProfile(),
128 if ($this->boolean('ajax')) {
129 $this->startHTML('text/xml;charset=utf-8');
130 $this->elementStart('head');
131 // TRANS: Page title when subscription succeeded.
132 $this->element('title', null, _('Subscribed'));
133 $this->elementEnd('head');
134 $this->elementStart('body');
135 if ($sub instanceof Subscription) {
136 $form = new UnsubscribeForm($this, $this->other);
138 $form = new CancelSubscriptionForm($this, $this->other);
141 $this->elementEnd('body');
144 $url = common_local_url('subscriptions',
145 array('nickname' => $this->user->nickname));
146 common_redirect($url, 303);