]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowsubscription.php
OAuth: Fix rare problem in which request tokens were sometimes being
[quix0rs-gnu-social.git] / actions / atompubshowsubscription.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Single subscription
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  AtomPub
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Show a single subscription
41  *
42  * @category  AtomPub
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2010 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49
50 class AtompubshowsubscriptionAction extends ApiAuthAction
51 {
52     private $_subscriber   = null;
53     private $_subscribed   = null;
54     private $_subscription = null;
55
56     /** 
57      * For initializing members of the class.
58      *
59      * @param array $argarray misc. arguments
60      *
61      * @return boolean true
62      */
63
64     function prepare($argarray)
65     {
66         parent::prepare($argarray);
67         $subscriberId = $this->trimmed('subscriber');
68
69         $this->_subscriber = Profile::staticGet('id', $subscriberId);
70
71         if (empty($this->_subscriber)) {
72             throw new ClientException(sprintf(_('No such profile id: %d'),
73                                               $subscriberId), 404);
74         }
75
76         $subscribedId = $this->trimmed('subscribed');
77
78         $this->_subscribed = Profile::staticGet('id', $subscribedId);
79
80         if (empty($this->_subscribed)) {
81             throw new ClientException(sprintf(_('No such profile id: %d'),
82                                               $subscribedId), 404);
83         }
84
85         $this->_subscription = 
86             Subscription::pkeyGet(array('subscriber' => $subscriberId,
87                                         'subscribed' => $subscribedId));
88
89         if (empty($this->_subscription)) {
90             $msg = sprintf(_('Profile %d not subscribed to profile %d'),
91                            $subscriberId, $subscribedId);
92             throw new ClientException($msg, 404);
93         }
94
95         return true;
96     }
97
98     /**
99      * Handler method
100      *
101      * @param array $argarray is ignored since it's now passed in in prepare()
102      *
103      * @return void
104      */
105
106     function handle($argarray=null)
107     {
108         parent::handle($argarray);
109         switch ($_SERVER['REQUEST_METHOD']) {
110         case 'HEAD':
111         case 'GET':
112             $this->showSubscription();
113             break;
114         case 'DELETE':
115             $this->deleteSubscription();
116             break;
117         default:
118             $this->clientError(_('HTTP method not supported.'), 405);
119             return;
120         }
121
122         return;
123     }
124
125     /**
126      * Show the subscription in ActivityStreams Atom format.
127      *
128      * @return void
129      */
130
131     function showSubscription()
132     {
133         $activity = $this->_subscription->asActivity();
134
135         header('Content-Type: application/atom+xml; charset=utf-8');
136
137         $this->startXML();
138         $this->raw($activity->asString(true, true, true));
139         $this->endXML();
140
141         return;
142     }
143
144     /**
145      * Delete the subscription
146      *
147      * @return void
148      */
149
150     function deleteSubscription()
151     {
152         if (empty($this->auth_user) ||
153             $this->auth_user->id != $this->_subscriber->id) {
154             throw new ClientException(_("Can't delete someone else's".
155                                         " subscription"), 403);
156         }
157
158         Subscription::cancel($this->_subscriber,
159                              $this->_subscribed);
160
161         return;
162     }
163
164     /**
165      * Is this action read only?
166      *
167      * @param array $args other arguments
168      *
169      * @return boolean true
170      */
171     
172     function isReadOnly($args)
173     {
174         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
175             return false;
176         } else {
177             return true;
178         }
179     }
180
181     /**
182      * Return last modified, if applicable.
183      *
184      * @return string last modified http header
185      */
186
187     function lastModified()
188     {
189         return max(strtotime($this->_subscriber->modified),
190                    strtotime($this->_subscribed->modified),
191                    strtotime($this->_subscription->modified));
192     }
193
194     /**
195      * Etag for this object
196      *
197      * @return string etag http header
198      */
199
200     function etag()
201     {
202         $mtime = strtotime($this->_subscription->modified);
203
204         return 'W/"' . implode(':', array('AtomPubShowSubscription',
205                                           $this->_subscriber->id,
206                                           $this->_subscribed->id,
207                                           $mtime)) . '"';
208     }
209
210     /**
211      * Does this require authentication?
212      *
213      * @return boolean true if delete, else false
214      */
215
216     function requiresAuth()
217     {
218         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
219             return true;
220         } else {
221             return false;
222         }
223     }
224 }