]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowsubscription.php
Merge branch 'master' of gitorious.org:social/mainline
[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 /**
38  * Show a single subscription
39  *
40  * @category  AtomPub
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class AtompubshowsubscriptionAction extends ApiAuthAction
48 {
49     private $_subscriber   = null;
50     private $_subscribed   = null;
51     private $_subscription = null;
52
53     /**
54      * For initializing members of the class.
55      *
56      * @param array $argarray misc. arguments
57      *
58      * @return boolean true
59      */
60     function prepare($argarray)
61     {
62         parent::prepare($argarray);
63         $subscriberId = $this->trimmed('subscriber');
64
65         $this->_subscriber = Profile::getKV('id', $subscriberId);
66
67         if (empty($this->_subscriber)) {
68             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
69             // TRANS: %d is the non-existing profile ID number.
70             throw new ClientException(sprintf(_('No such profile id: %d.'),
71                                               $subscriberId), 404);
72         }
73
74         $subscribedId = $this->trimmed('subscribed');
75
76         $this->_subscribed = Profile::getKV('id', $subscribedId);
77
78         if (empty($this->_subscribed)) {
79             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
80             // TRANS: %d is the non-existing profile ID number.
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             // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
91             // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
92             $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
93                            $subscriberId, $subscribedId);
94             throw new ClientException($msg, 404);
95         }
96
97         return true;
98     }
99
100     /**
101      * Handler method
102      *
103      * @param array $argarray is ignored since it's now passed in in prepare()
104      *
105      * @return void
106      */
107     function handle($argarray=null)
108     {
109         parent::handle($argarray);
110         switch ($_SERVER['REQUEST_METHOD']) {
111         case 'HEAD':
112         case 'GET':
113             $this->showSubscription();
114             break;
115         case 'DELETE':
116             $this->deleteSubscription();
117             break;
118         default:
119             // TRANS: Client error shown when using a non-supported HTTP method.
120             $this->clientError(_('HTTP method not supported.'), 405);
121         }
122
123         return;
124     }
125
126     /**
127      * Show the subscription in ActivityStreams Atom format.
128      *
129      * @return void
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     function deleteSubscription()
150     {
151         if (empty($this->auth_user) ||
152             $this->auth_user->id != $this->_subscriber->id) {
153             // TRANS: Client exception thrown when trying to delete a subscription of another user.
154             throw new ClientException(_("Cannot 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     function isReadOnly($args)
172     {
173         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
174             return false;
175         } else {
176             return true;
177         }
178     }
179
180     /**
181      * Return last modified, if applicable.
182      *
183      * @return string last modified http header
184      */
185     function lastModified()
186     {
187         return max(strtotime($this->_subscriber->modified),
188                    strtotime($this->_subscribed->modified),
189                    strtotime($this->_subscription->modified));
190     }
191
192     /**
193      * Etag for this object
194      *
195      * @return string etag http header
196      */
197     function etag()
198     {
199         $mtime = strtotime($this->_subscription->modified);
200
201         return 'W/"' . implode(':', array('AtomPubShowSubscription',
202                                           $this->_subscriber->id,
203                                           $this->_subscribed->id,
204                                           $mtime)) . '"';
205     }
206
207     /**
208      * Does this require authentication?
209      *
210      * @return boolean true if delete, else false
211      */
212     function requiresAuth()
213     {
214         if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
215             return true;
216         } else {
217             return false;
218         }
219     }
220 }