]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TagSub/actions/tagsub.php
Filling in missing endHTML calls for Action AJAX
[quix0rs-gnu-social.git] / plugins / TagSub / actions / tagsub.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2011, StatusNet, Inc.
5  *
6  * Tag 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    Brion Vibber <brion@status.net>
26  * @author    Evan Prodromou <evan@status.net>
27  * @copyright 2008-2010 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * Tag subscription action
38  *
39  * Takes parameters:
40  *
41  *    - token: session token to prevent CSRF attacks
42  *    - ajax: boolean; whether to return Ajax or full-browser results
43  *
44  * Only works if the current user is logged in.
45  *
46  * @category  Action
47  * @package   StatusNet
48  * @author    Evan Prodromou <evan@status.net>
49  * @author    Brion Vibber <brion@status.net>
50  * @copyright 2008-2011 StatusNet, Inc.
51  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
52  * @link      http://status.net/
53  */
54 class TagsubAction extends Action
55 {
56     var $user;
57     var $tag;
58
59     /**
60      * Check pre-requisites and instantiate attributes
61      *
62      * @param Array $args array of arguments (URL, GET, POST)
63      *
64      * @return boolean success flag
65      */
66     function prepare($args)
67     {
68         parent::prepare($args);
69         if ($this->boolean('ajax')) {
70             StatusNet::setApi(true);
71         }
72
73         // Only allow POST requests
74
75         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
76             // TRANS: Client error displayed trying to perform any request method other than POST.
77             // TRANS: Do not translate POST.
78             $this->clientError(_m('This action only accepts POST requests.'));
79             return false;
80         }
81
82         // CSRF protection
83
84         $token = $this->trimmed('token');
85
86         if (!$token || $token != common_session_token()) {
87             // TRANS: Client error displayed when the session token is not okay.
88             $this->clientError(_m('There was a problem with your session token.'.
89                                  ' Try again, please.'));
90             return false;
91         }
92
93         // Only for logged-in users
94
95         $this->user = common_current_user();
96
97         if (empty($this->user)) {
98             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
99             $this->clientError(_m('Not logged in.'));
100             return false;
101         }
102
103         // Profile to subscribe to
104
105         $this->tag = $this->arg('tag');
106
107         if (empty($this->tag)) {
108             // TRANS: Client error displayed trying to subscribe to a non-existing profile.
109             $this->clientError(_m('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         TagSub::start($this->user->getProfile(),
130                       $this->tag);
131
132         if ($this->boolean('ajax')) {
133             $this->startHTML('text/xml;charset=utf-8');
134             $this->elementStart('head');
135             // TRANS: Page title when tag subscription succeeded.
136             $this->element('title', null, _m('Subscribed'));
137             $this->elementEnd('head');
138             $this->elementStart('body');
139             $unsubscribe = new TagUnsubForm($this, $this->tag);
140             $unsubscribe->show();
141             $this->elementEnd('body');
142             $this->endHTML();
143         } else {
144             $url = common_local_url('tag',
145                                     array('tag' => $this->tag));
146             common_redirect($url, 303);
147         }
148     }
149 }