]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/subscribe.php
Using GNUSOCIAL_VERSION instead of STATUSNET_VERSION
[quix0rs-gnu-social.git] / actions / subscribe.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2011, StatusNet, Inc.
5  *
6  * Subscription action.
7  *
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.
12  *
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.
17  *
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/>.
20  *
21  * PHP version 5
22  *
23  * @category  Action
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Subscription action
37  *
38  * Subscribing to a profile. Likely to work for OStatus profiles.
39  *
40  * Takes parameters:
41  *
42  *    - subscribeto: a profile ID
43  *    - token: session token to prevent CSRF attacks
44  *    - ajax: boolean; whether to return Ajax or full-browser results
45  *
46  * Only works if the current user is logged in.
47  *
48  * @category  Action
49  * @package   StatusNet
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/
54  */
55 class SubscribeAction extends Action
56 {
57     var $user;
58     var $other;
59
60     /**
61      * Check pre-requisites and instantiate attributes
62      *
63      * @param Array $args array of arguments (URL, GET, POST)
64      *
65      * @return boolean success flag
66      */
67     function prepare($args)
68     {
69         parent::prepare($args);
70
71         // Only allow POST requests
72
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.'));
77             return false;
78         }
79
80         // CSRF protection
81
82         $token = $this->trimmed('token');
83
84         if (!$token || $token != common_session_token()) {
85             // TRANS: Client error displayed when the session token is not okay.
86             $this->clientError(_('There was a problem with your session token.'.
87                                  ' Try again, please.'));
88             return false;
89         }
90
91         // Only for logged-in users
92
93         $this->user = common_current_user();
94
95         if (empty($this->user)) {
96             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
97             $this->clientError(_('Not logged in.'));
98             return false;
99         }
100
101         // Profile to subscribe to
102
103         $other_id = $this->arg('subscribeto');
104
105         $this->other = Profile::getKV('id', $other_id);
106
107         if (empty($this->other)) {
108             // TRANS: Client error displayed trying to subscribe to a non-existing profile.
109             $this->clientError(_('No such profile.'));
110             return false;
111         }
112
113         return true;
114     }
115
116     /**
117      * Handle request
118      *
119      * Does the subscription and returns results.
120      *
121      * @param Array $args unused.
122      *
123      * @return void
124      */
125     function handle($args)
126     {
127         // Throws exception on error
128
129         $sub = Subscription::start($this->user->getProfile(),
130                                    $this->other);
131
132         if ($this->boolean('ajax')) {
133             $this->startHTML('text/xml;charset=utf-8');
134             $this->elementStart('head');
135             // TRANS: Page title when subscription succeeded.
136             $this->element('title', null, _('Subscribed'));
137             $this->elementEnd('head');
138             $this->elementStart('body');
139             if ($sub instanceof Subscription) {
140                 $form = new UnsubscribeForm($this, $this->other);
141             } else {
142                 $form = new CancelSubscriptionForm($this, $this->other);
143             }
144             $form->show();
145             $this->elementEnd('body');
146             $this->endHTML();
147         } else {
148             $url = common_local_url('subscriptions',
149                                     array('nickname' => $this->user->nickname));
150             common_redirect($url, 303);
151         }
152     }
153 }