]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/subscribe.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / actions / subscribe.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2010, 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. Does not work for OMB 0.1 remote subscriptions,
39  * but may work for other remote subscription protocols, like OStatus.
40  *
41  * Takes parameters:
42  *
43  *    - subscribeto: a profile ID
44  *    - token: session token to prevent CSRF attacks
45  *    - ajax: boolean; whether to return Ajax or full-browser results
46  *
47  * Only works if the current user is logged in.
48  *
49  * @category  Action
50  * @package   StatusNet
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/
55  */
56
57 class SubscribeAction extends Action
58 {
59     var $user;
60     var $other;
61
62     /**
63      * Check pre-requisites and instantiate attributes
64      *
65      * @param Array $args array of arguments (URL, GET, POST)
66      *
67      * @return boolean success flag
68      */
69
70     function prepare($args)
71     {
72         parent::prepare($args);
73
74         // Only allow POST requests
75
76         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
77             $this->clientError(_('This action only accepts POST requests.'));
78             return false;
79         }
80
81         // CSRF protection
82
83         $token = $this->trimmed('token');
84
85         if (!$token || $token != common_session_token()) {
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             $this->clientError(_('Not logged in.'));
97             return false;
98         }
99
100         // Profile to subscribe to
101
102         $other_id = $this->arg('subscribeto');
103
104         $this->other = Profile::staticGet('id', $other_id);
105
106         if (empty($this->other)) {
107             $this->clientError(_('No such profile.'));
108             return false;
109         }
110
111         // OMB 0.1 doesn't have a mechanism for local-server-
112         // originated subscription.
113
114         $omb01 = Remote_profile::staticGet('id', $other_id);
115
116         if (!empty($omb01)) {
117             $this->clientError(_('You cannot subscribe to an OMB 0.1'.
118                                  ' remote profile with this action.'));
119             return false;
120         }
121
122         return true;
123     }
124
125     /**
126      * Handle request
127      *
128      * Does the subscription and returns results.
129      *
130      * @param Array $args unused.
131      *
132      * @return void
133      */
134
135     function handle($args)
136     {
137         // Throws exception on error
138
139         Subscription::start($this->user->getProfile(),
140                             $this->other);
141
142         if ($this->boolean('ajax')) {
143             $this->startHTML('text/xml;charset=utf-8');
144             $this->elementStart('head');
145             $this->element('title', null, _('Subscribed'));
146             $this->elementEnd('head');
147             $this->elementStart('body');
148             $unsubscribe = new UnsubscribeForm($this, $this->other);
149             $unsubscribe->show();
150             $this->elementEnd('body');
151             $this->elementEnd('html');
152         } else {
153             $url = common_local_url('subscriptions',
154                                     array('nickname' => $this->user->nickname));
155             common_redirect($url, 303);
156         }
157     }
158 }