]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowsubscription.php
AtomPub for single subscription
[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         switch ($_SERVER['REQUEST_METHOD']) {
109         case 'GET':
110             $this->showSubscription();
111             break;
112         case 'DELETE':
113             $this->deleteSubscription();
114             break;
115         default:
116             $this->clientError(_('HTTP method not supported.'), 405);
117             return;
118         }
119
120         return;
121     }
122
123     /**
124      * Show the subscription in ActivityStreams Atom format.
125      *
126      * @return void
127      */
128
129     function showSubscription()
130     {
131         $activity = $this->_subscription->asActivity();
132
133         header('Content-Type: application/atom+xml; charset=utf-8');
134
135         $this->startXML();
136         $this->raw($activity->asString(true, true, true));
137         $this->endXML();
138
139         return;
140     }
141
142     /**
143      * Delete the subscription
144      *
145      * @return void
146      */
147
148     function deleteSubscription()
149     {
150         if (empty($this->auth_user) ||
151             $this->auth_user->id != $this->_subscriber->id) {
152             throw new ClientException(_("Can't delete someone else's".
153                                         " subscription"), 403);
154         }
155
156         Subscription::cancel($this->_subscriber,
157                              $this->_subscribed);
158
159         return;
160     }
161
162     /**
163      * Is this action read only?
164      *
165      * @param array $args other arguments
166      *
167      * @return boolean true
168      */
169     
170     function isReadOnly($args)
171     {
172         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
173             return false;
174         } else {
175             return true;
176         }
177     }
178
179     /**
180      * Return last modified, if applicable.
181      *
182      * MAY override
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 }