]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowsubscription.php
Merge branch '1.0.x' into feedsub-wizard
[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 class AtompubshowsubscriptionAction extends ApiAuthAction
50 {
51     private $_subscriber   = null;
52     private $_subscribed   = null;
53     private $_subscription = null;
54
55     /**
56      * For initializing members of the class.
57      *
58      * @param array $argarray misc. arguments
59      *
60      * @return boolean true
61      */
62     function prepare($argarray)
63     {
64         parent::prepare($argarray);
65         $subscriberId = $this->trimmed('subscriber');
66
67         $this->_subscriber = Profile::staticGet('id', $subscriberId);
68
69         if (empty($this->_subscriber)) {
70             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
71             // TRANS: %d is the non-existing profile ID number.
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             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
82             // TRANS: %d is the non-existing profile ID number.
83             throw new ClientException(sprintf(_('No such profile id: %d.'),
84                                               $subscribedId), 404);
85         }
86
87         $this->_subscription =
88             Subscription::pkeyGet(array('subscriber' => $subscriberId,
89                                         'subscribed' => $subscribedId));
90
91         if (empty($this->_subscription)) {
92             // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
93             // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
94             $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
95                            $subscriberId, $subscribedId);
96             throw new ClientException($msg, 404);
97         }
98
99         return true;
100     }
101
102     /**
103      * Handler method
104      *
105      * @param array $argarray is ignored since it's now passed in in prepare()
106      *
107      * @return void
108      */
109     function handle($argarray=null)
110     {
111         parent::handle($argarray);
112         switch ($_SERVER['REQUEST_METHOD']) {
113         case 'HEAD':
114         case 'GET':
115             $this->showSubscription();
116             break;
117         case 'DELETE':
118             $this->deleteSubscription();
119             break;
120         default:
121             // TRANS: Client error shown when using a non-supported HTTP method.
122             $this->clientError(_('HTTP method not supported.'), 405);
123             return;
124         }
125
126         return;
127     }
128
129     /**
130      * Show the subscription in ActivityStreams Atom format.
131      *
132      * @return void
133      */
134     function showSubscription()
135     {
136         $activity = $this->_subscription->asActivity();
137
138         header('Content-Type: application/atom+xml; charset=utf-8');
139
140         $this->startXML();
141         $this->raw($activity->asString(true, true, true));
142         $this->endXML();
143
144         return;
145     }
146
147     /**
148      * Delete the subscription
149      *
150      * @return void
151      */
152     function deleteSubscription()
153     {
154         if (empty($this->auth_user) ||
155             $this->auth_user->id != $this->_subscriber->id) {
156             // TRANS: Client exception thrown when trying to delete a subscription of another user.
157             throw new ClientException(_("Cannot delete someone else's ".
158                                         "subscription."), 403);
159         }
160
161         Subscription::cancel($this->_subscriber,
162                              $this->_subscribed);
163
164         return;
165     }
166
167     /**
168      * Is this action read only?
169      *
170      * @param array $args other arguments
171      *
172      * @return boolean true
173      */
174     function isReadOnly($args)
175     {
176         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
177             return false;
178         } else {
179             return true;
180         }
181     }
182
183     /**
184      * Return last modified, if applicable.
185      *
186      * @return string last modified http header
187      */
188     function lastModified()
189     {
190         return max(strtotime($this->_subscriber->modified),
191                    strtotime($this->_subscribed->modified),
192                    strtotime($this->_subscription->modified));
193     }
194
195     /**
196      * Etag for this object
197      *
198      * @return string etag http header
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     function requiresAuth()
216     {
217         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
218             return true;
219         } else {
220             return false;
221         }
222     }
223 }